Open Kilda Java Documentation
VerificationListenCommand.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 
27 
28 import net.floodlightcontroller.core.IOFSwitch;
29 import net.floodlightcontroller.core.internal.IOFSwitchService;
30 import net.floodlightcontroller.core.module.FloodlightModuleContext;
31 import net.floodlightcontroller.threadpool.IThreadPoolService;
32 import org.projectfloodlight.openflow.protocol.OFVersion;
33 import org.projectfloodlight.openflow.types.DatapathId;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 
37 import java.util.concurrent.TimeUnit;
38 
39 public class VerificationListenCommand extends AbstractVerificationCommand {
40  private static final Logger log = LoggerFactory.getLogger(VerificationListenCommand.class);
41 
42  private final VerificationData verificationData;
43 
44  private final FlowVerificationService flowVerificationService;
45  private final IThreadPoolService scheduler;
46 
49  super(context, verificationRequest);
50 
51  this.verificationData = VerificationData.of(verificationRequest);
52 
53  FloodlightModuleContext moduleContext = getContext().getModuleContext();
54  flowVerificationService = moduleContext.getServiceImpl(FlowVerificationService.class);
55  scheduler = moduleContext.getServiceImpl(IThreadPoolService.class);
56 
57  checkCapabilities(new SwitchUtils(moduleContext.getServiceImpl(IOFSwitchService.class)));
58  }
59 
60  @Override
61  public void run() {
62  flowVerificationService.subscribe(this);
63 
64  TimeoutNotification notification = new TimeoutNotification(this);
65  scheduler.getScheduledExecutor().schedule(
66  notification, getVerificationRequest().getTimeout(), TimeUnit.MILLISECONDS);
67  }
68 
72  public boolean packetIn(IOFSwitch sw, VerificationData payload) {
73  if (! verificationData.equals(payload)) {
74  return false;
75  }
76 
77  VerificationMeasures measures = payload.produceMeasurements(sw.getLatency().getValue());
78  log.debug(
79  "Receive flow VERIFICATION package - packetId: {}, latency: {}",
80  payload.getPacketId(), measures.getNetworkLatency());
81  UniFlowVerificationResponse response = new UniFlowVerificationResponse(getVerificationRequest(), measures);
82  sendResponse(response);
83 
84  return true;
85  }
86 
87  private void timeout() {
88  log.error("Give up waiting for flow VERIFICATION packet (packetId: {})", verificationData.getPacketId());
89 
90  flowVerificationService.unsubscribe(this);
91  sendErrorResponse(FlowVerificationErrorCode.TIMEOUT);
92  }
93 
94  private void checkCapabilities(SwitchUtils switchUtils) throws InsufficientCapabilitiesException {
95  DatapathId switchId = DatapathId.of(getVerificationRequest().getDestSwitchId().toLong());
96  IOFSwitch sw = switchUtils.lookupSwitch(switchId);
97 
98  if (0 < OFVersion.OF_13.compareTo(sw.getOFFactory().getVersion())) {
99  throw new InsufficientCapabilitiesException("Destination switch is unable to catch PING package");
100  }
101  }
102 
103  static class TimeoutNotification implements Runnable {
104  private final VerificationListenCommand operation;
105 
106  TimeoutNotification(VerificationListenCommand command) {
107  this.operation = command;
108  }
109 
110  @Override
111  public void run() {
112  operation.timeout();
113  }
114  }
115 }
static VerificationData of(DecodedJWT token)
def command(payload, fields)
Definition: share.py:102
VerificationMeasures produceMeasurements(long recipientLatency)
net
Definition: plan-b.py:46
VerificationListenCommand(CommandContext context, UniFlowVerificationRequest verificationRequest)