2 * Copyright 2015 Open Networking Laboratory
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onosproject.driver.handshaker;
18 import com.google.common.collect.ImmutableSet;
19 import org.onosproject.net.Device;
20 import org.onosproject.openflow.controller.OpenFlowOpticalSwitch;
21 import org.onosproject.openflow.controller.PortDescPropertyType;
22 import org.onosproject.openflow.controller.driver.AbstractOpenFlowSwitch;
23 import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeAlreadyStarted;
24 import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeCompleted;
25 import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeNotStarted;
26 import org.projectfloodlight.openflow.protocol.OFCircuitPortStatus;
27 import org.projectfloodlight.openflow.protocol.OFCircuitPortsReply;
28 import org.projectfloodlight.openflow.protocol.OFCircuitPortsRequest;
29 import org.projectfloodlight.openflow.protocol.OFFlowMod;
30 import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest;
31 import org.projectfloodlight.openflow.protocol.OFMessage;
32 import org.projectfloodlight.openflow.protocol.OFObject;
33 import org.projectfloodlight.openflow.protocol.OFPortDesc;
34 import org.projectfloodlight.openflow.protocol.OFPortDescPropOpticalTransport;
35 import org.projectfloodlight.openflow.protocol.OFPortDescStatsReply;
36 import org.projectfloodlight.openflow.protocol.OFPortOptical;
37 import org.projectfloodlight.openflow.protocol.OFStatsReply;
38 import org.projectfloodlight.openflow.protocol.OFStatsType;
39 import org.projectfloodlight.openflow.protocol.action.OFAction;
40 import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
41 import org.projectfloodlight.openflow.protocol.match.Match;
42 import org.projectfloodlight.openflow.protocol.match.MatchField;
43 import org.projectfloodlight.openflow.protocol.oxm.OFOxmExpOchSigId;
44 import org.projectfloodlight.openflow.types.CircuitSignalID;
45 import org.projectfloodlight.openflow.types.OFPort;
46 import org.projectfloodlight.openflow.types.U8;
48 import java.io.IOException;
49 import java.util.ArrayList;
50 import java.util.Collections;
51 import java.util.LinkedList;
52 import java.util.List;
54 import java.util.concurrent.atomic.AtomicBoolean;
57 * LINC-OE Optical Emulator switch class.
59 * The LINC ROADM emulator exposes two types of ports: OCh ports connect to ports in the packet layer,
60 * while OMS ports connect to an OMS port on a neighbouring ROADM.
62 * LINC sends the tap ports (OCh for our purposes) in the regular port desc stats reply,
63 * while it sends *all* ports (both tap and WDM ports, i.e., OCh and OMS) in the experimenter port desc stats reply.
65 * As LINC implements custom OF optical extensions (in contrast to the final standard as specified in
66 * ONF TS-022 (March 15, 2015), we need to rewrite flow stat requests and flow mods in {@link #sendMsg(OFMessage)}.
69 public class OfOpticalSwitchImplLinc13
70 extends AbstractOpenFlowSwitch implements OpenFlowOpticalSwitch {
72 private final AtomicBoolean driverHandshakeComplete = new AtomicBoolean(false);
73 private long barrierXidToWaitFor = -1;
75 private List<OFPortOptical> opticalPorts;
78 public void startDriverHandshake() {
79 log.warn("Starting driver handshake for sw {}", getStringId());
80 if (startDriverHandshakeCalled) {
81 throw new SwitchDriverSubHandshakeAlreadyStarted();
83 startDriverHandshakeCalled = true;
85 sendHandshakeOFExperimenterPortDescRequest();
86 } catch (IOException e) {
87 log.error("LINC-OE exception while sending experimenter port desc:",
94 public boolean isDriverHandshakeComplete() {
95 return driverHandshakeComplete.get();
99 public void processDriverHandshakeMessage(OFMessage m) {
100 if (!startDriverHandshakeCalled) {
101 throw new SwitchDriverSubHandshakeNotStarted();
103 if (driverHandshakeComplete.get()) {
104 throw new SwitchDriverSubHandshakeCompleted(m);
107 switch (m.getType()) {
109 if (m.getXid() == barrierXidToWaitFor) {
110 log.debug("LINC-OE Received barrier response");
114 log.error("Switch {} Error {}", getStringId(), m);
120 case GET_ASYNC_REPLY:
125 log.warn("****LINC-OE Port Status {} {}", getStringId(), m);
126 processOFPortStatus((OFCircuitPortStatus) m);
128 case QUEUE_GET_CONFIG_REPLY:
133 OFStatsReply stats = (OFStatsReply) m;
134 if (stats.getStatsType() == OFStatsType.EXPERIMENTER) {
135 log.warn("LINC-OE : Received stats reply message {}", m);
136 createOpticalPortList((OFCircuitPortsReply) m);
137 driverHandshakeComplete.set(true);
141 log.warn("Received message {} during switch-driver " +
142 "subhandshake " + "from switch {} ... " +
143 "Ignoring message", m,
149 public void processOFPortStatus(OFCircuitPortStatus ps) {
150 log.debug("LINC-OE ..OF Port Status :", ps);
153 private void sendHandshakeOFExperimenterPortDescRequest() throws
155 // send multi part message for port description for optical switches
156 OFCircuitPortsRequest circuitPortsRequest = factory()
157 .buildCircuitPortsRequest().setXid(getNextTransactionId())
159 log.warn("LINC-OE : Sending experimented circuit port stats " +
162 circuitPortsRequest.toString());
163 this.sendHandshakeMessage(circuitPortsRequest);
168 * Returns a list of standard (Ethernet) ports.
170 * @return List of ports
172 public List<OFPortDesc> getPorts() {
173 return Collections.EMPTY_LIST;
177 * Rewrite match object to use LINC OF optical extensions.
179 * @param match original match
180 * @return rewritten match
182 private Match rewriteMatch(Match match) {
183 Match.Builder mBuilder = factory().buildMatch();
184 for (MatchField mf : match.getMatchFields()) {
185 if (mf == MatchField.EXP_OCH_SIG_ID) {
186 mBuilder.setExact(MatchField.OCH_SIGID, (CircuitSignalID) match.get(mf));
189 if (mf == MatchField.EXP_OCH_SIGTYPE) {
190 mBuilder.setExact(MatchField.OCH_SIGTYPE, (U8) match.get(mf));
193 mBuilder.setExact(mf, match.get(mf));
196 return mBuilder.build();
200 * Rewrite actions to use LINC OF optical extensions.
202 * @param actions original actions
203 * @return rewritten actions
205 private List<OFAction> rewriteActions(List<OFAction> actions) {
206 List<OFAction> newActions = new LinkedList<>();
208 for (OFAction action : actions) {
209 if (!(action instanceof OFActionSetField)) {
210 newActions.add(action);
214 OFActionSetField sf = (OFActionSetField) action;
215 if (!(sf instanceof OFOxmExpOchSigId)) {
216 newActions.add(action);
219 OFOxmExpOchSigId oxm = (OFOxmExpOchSigId) sf.getField();
220 CircuitSignalID signalId = oxm.getValue();
223 factory().actions().circuit(factory().oxms().ochSigid(signalId)));
230 public void sendMsg(OFMessage msg) {
231 // Ignore everything but flow mods and stat requests
232 if (!(msg instanceof OFFlowMod || msg instanceof OFFlowStatsRequest)) {
238 OFMessage newMsg = null;
240 if (msg instanceof OFFlowStatsRequest) {
241 // Rewrite match only
242 OFFlowStatsRequest fsr = (OFFlowStatsRequest) msg;
243 newMatch = rewriteMatch(fsr.getMatch());
244 newMsg = fsr.createBuilder().setMatch(newMatch).build();
245 } else if (msg instanceof OFFlowMod) {
246 // Rewrite match and actions
247 OFFlowMod fm = (OFFlowMod) msg;
248 newMatch = rewriteMatch(fm.getMatch());
249 List<OFAction> actions = rewriteActions(fm.getActions());
251 newMsg = fm.createBuilder().setMatch(newMatch).setActions(actions).build();
254 super.sendMsg(newMsg);
258 public Boolean supportNxRole() {
263 public Device.Type deviceType() {
264 return Device.Type.ROADM;
268 * Checks if given port is also part of the regular port desc stats, i.e., is the port a tap port.
270 * @param port given OF port
271 * @return true if the port is a tap (OCh), false otherwise (OMS port)
273 private boolean hasPort(OFPort port) {
274 for (OFPortDescStatsReply reply : this.ports) {
275 for (OFPortDesc p : reply.getEntries()) {
276 if (p.getPortNo().equals(port)) {
286 * Creates an OpenFlow optical port based on the given port and transport type.
288 * @param port OpenFlow optical port
289 * @param type transport type
290 * @return OpenFlow optical port
292 private OFPortOptical createOpticalPort(OFPortOptical port, short type) {
293 List<OFPortDescPropOpticalTransport> descList = new ArrayList<>(port.getDesc().size());
295 for (OFPortDescPropOpticalTransport desc : port.getDesc()) {
296 OFPortDescPropOpticalTransport newDesc = desc.createBuilder()
297 .setType(desc.getType())
298 .setPortSignalType(type)
299 .setPortType(desc.getPortType())
300 .setReserved(desc.getReserved())
302 descList.add(newDesc);
305 OFPortOptical newPort = port.createBuilder()
306 .setConfig(port.getConfig())
308 .setHwAddr(port.getHwAddr())
309 .setName(port.getName())
310 .setPortNo(port.getPortNo())
311 .setState(port.getState())
318 * Builds list of OFPortOptical ports based on the multi-part circuit ports reply.
320 * Ensure the optical transport port's signal type is configured correctly.
322 * @param wPorts OF reply with circuit ports
324 private void createOpticalPortList(OFCircuitPortsReply wPorts) {
325 opticalPorts = new ArrayList<>(wPorts.getEntries().size());
327 for (OFPortOptical p : wPorts.getEntries()) {
330 // FIXME: use constants once loxi has full optical extensions
331 if (hasPort(p.getPortNo())) {
332 signalType = 5; // OCH port
334 signalType = 2; // OMS port
337 opticalPorts.add(createOpticalPort(p, signalType));
342 public List<? extends OFObject> getPortsOf(PortDescPropertyType type) {
343 if (!type.equals(PortDescPropertyType.OPTICAL_TRANSPORT)) {
344 return Collections.EMPTY_LIST;
351 public Set<PortDescPropertyType> getPortTypes() {
352 return ImmutableSet.of(PortDescPropertyType.OPTICAL_TRANSPORT);