Open Kilda Java Documentation
VerificationDispatchCommand.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.floodlight.command.flow;
17 
24 
25 import net.floodlightcontroller.core.IOFSwitch;
26 import net.floodlightcontroller.core.internal.IOFSwitchService;
27 import net.floodlightcontroller.core.module.FloodlightModuleContext;
28 import org.projectfloodlight.openflow.types.DatapathId;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Optional;
35 
36 public class VerificationDispatchCommand extends AbstractVerificationCommand {
37  private static final Logger log = LoggerFactory.getLogger(VerificationDispatchCommand.class);
38 
39  private final IOFSwitchService switchService;
40 
42  super(context, verificationRequest);
43 
44  FloodlightModuleContext moduleContext = getContext().getModuleContext();
45  switchService = moduleContext.getServiceImpl(IOFSwitchService.class);
46  }
47 
48  @Override
49  public void run() {
50  launchSubCommands(produceSubCommands());
51  }
52 
53  List<Optional<Command>> produceSubCommands() {
54  List<Optional<Command>> plan = new ArrayList<>();
55 
56  try {
57  // TODO(surabujin): will need to synchronise send/receive readiness in multi FL environment
58  plan.add(makeListenCommand());
59  plan.add(makeSendCommand());
60  } catch (InsufficientCapabilitiesException e) {
61  log.error(
62  "Unable to perform flow VERIFICATION due to {} (packetID: {})",
63  e.toString(), getVerificationRequest().getPacketId());
64  sendErrorResponse(FlowVerificationErrorCode.NOT_CAPABLE);
65  plan.clear();
66  }
67 
68  return plan;
69  }
70 
71  private void launchSubCommands(List<Optional<Command>> subCommands) {
72  for (Optional<Command> command : subCommands) {
73  command.ifPresent(this::startSubCommand);
74  }
75  }
76 
77  private Optional<Command> makeSendCommand() {
78  UniFlowVerificationRequest verificationRequest = getVerificationRequest();
79  if (!isOwnSwitch(verificationRequest.getSourceSwitchId())) {
80  log.debug("Switch {} is not under our control, do not produce flow verification send request");
81  return Optional.empty();
82  }
83 
84  log.debug("Initiate verification send command (request: {})", verificationRequest.getPacketId());
85  return Optional.of(new VerificationSendCommand(getContext(), verificationRequest));
86  }
87 
88  private Optional<Command> makeListenCommand() throws InsufficientCapabilitiesException {
89  UniFlowVerificationRequest verificationRequest = getVerificationRequest();
90  if (!isOwnSwitch(verificationRequest.getDestSwitchId())) {
91  log.debug("Switch {} is not under our control, do not produce flow verification receive handler");
92  return Optional.empty();
93  }
94 
95  log.debug("Initiate verification listen command (request: {})", verificationRequest.getPacketId());
96  return Optional.of(new VerificationListenCommand(getContext(), verificationRequest));
97  }
98 
99  private boolean isOwnSwitch(SwitchId switchId) {
100  DatapathId dpId = DatapathId.of(switchId.toLong());
101  IOFSwitch sw = switchService.getActiveSwitch(dpId);
102 
103  return sw != null;
104  }
105 }
def command(payload, fields)
Definition: share.py:102
VerificationDispatchCommand(CommandContext context, UniFlowVerificationRequest verificationRequest)
net
Definition: plan-b.py:46