LINC driver updated to always use LINC's non-standard OF optical extensions.

ONOS-3399

Change-Id: I7bbd39947facfbbd53a164ea34c7da50782bfd6a
diff --git a/drivers/src/main/java/org/onosproject/driver/handshaker/OfOpticalSwitchImplLinc13.java b/drivers/src/main/java/org/onosproject/driver/handshaker/OfOpticalSwitchImplLinc13.java
index ff65e0c..f91e2a7 100644
--- a/drivers/src/main/java/org/onosproject/driver/handshaker/OfOpticalSwitchImplLinc13.java
+++ b/drivers/src/main/java/org/onosproject/driver/handshaker/OfOpticalSwitchImplLinc13.java
@@ -15,8 +15,8 @@
  */
 package org.onosproject.driver.handshaker;
 
-import org.onosproject.net.Device;
 import com.google.common.collect.ImmutableSet;
+import org.onosproject.net.Device;
 import org.onosproject.openflow.controller.OpenFlowOpticalSwitch;
 import org.onosproject.openflow.controller.PortDescPropertyType;
 import org.onosproject.openflow.controller.driver.AbstractOpenFlowSwitch;
@@ -26,6 +26,8 @@
 import org.projectfloodlight.openflow.protocol.OFCircuitPortStatus;
 import org.projectfloodlight.openflow.protocol.OFCircuitPortsReply;
 import org.projectfloodlight.openflow.protocol.OFCircuitPortsRequest;
+import org.projectfloodlight.openflow.protocol.OFFlowMod;
+import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest;
 import org.projectfloodlight.openflow.protocol.OFMessage;
 import org.projectfloodlight.openflow.protocol.OFObject;
 import org.projectfloodlight.openflow.protocol.OFPortDesc;
@@ -34,11 +36,19 @@
 import org.projectfloodlight.openflow.protocol.OFPortOptical;
 import org.projectfloodlight.openflow.protocol.OFStatsReply;
 import org.projectfloodlight.openflow.protocol.OFStatsType;
+import org.projectfloodlight.openflow.protocol.action.OFAction;
+import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
+import org.projectfloodlight.openflow.protocol.match.Match;
+import org.projectfloodlight.openflow.protocol.match.MatchField;
+import org.projectfloodlight.openflow.protocol.oxm.OFOxmExpOchSigId;
+import org.projectfloodlight.openflow.types.CircuitSignalID;
 import org.projectfloodlight.openflow.types.OFPort;
+import org.projectfloodlight.openflow.types.U8;
 
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -52,6 +62,9 @@
  * LINC sends the tap ports (OCh for our purposes) in the regular port desc stats reply,
  * while it sends *all* ports (both tap and WDM ports, i.e., OCh and OMS) in the experimenter port desc stats reply.
  *
+ * As LINC implements custom OF optical extensions (in contrast to the final standard as specified in
+ * ONF TS-022 (March 15, 2015), we need to rewrite flow stat requests and flow mods in {@link #sendMsg(OFMessage)}.
+ *
  */
 public class OfOpticalSwitchImplLinc13
  extends AbstractOpenFlowSwitch implements OpenFlowOpticalSwitch {
@@ -160,6 +173,86 @@
         return Collections.EMPTY_LIST;
     }
 
+    /**
+     * Rewrite match object to use LINC OF optical extensions.
+     *
+     * @param match original match
+     * @return rewritten match
+     */
+    private Match rewriteMatch(Match match) {
+        Match.Builder mBuilder = factory().buildMatch();
+        for (MatchField mf : match.getMatchFields()) {
+            if (mf == MatchField.EXP_OCH_SIG_ID) {
+                mBuilder.setExact(MatchField.OCH_SIGID, (CircuitSignalID) match.get(mf));
+                continue;
+            }
+            if (mf == MatchField.EXP_OCH_SIGTYPE) {
+                mBuilder.setExact(MatchField.OCH_SIGTYPE, (U8) match.get(mf));
+                continue;
+            }
+            mBuilder.setExact(mf, match.get(mf));
+        }
+
+        return mBuilder.build();
+    }
+
+    /**
+     * Rewrite actions to use LINC OF optical extensions.
+     *
+     * @param actions original actions
+     * @return rewritten actions
+     */
+    private List<OFAction> rewriteActions(List<OFAction> actions) {
+        List<OFAction> newActions = new LinkedList<>();
+
+        for (OFAction action : actions) {
+            if (!(action instanceof OFActionSetField)) {
+                newActions.add(action);
+                continue;
+            }
+
+            OFActionSetField sf = (OFActionSetField) action;
+            if (!(sf instanceof OFOxmExpOchSigId)) {
+                newActions.add(action);
+            }
+
+            OFOxmExpOchSigId oxm = (OFOxmExpOchSigId) sf.getField();
+            CircuitSignalID signalId = oxm.getValue();
+
+            newActions.add(
+                    factory().actions().circuit(factory().oxms().ochSigid(signalId)));
+        }
+
+        return newActions;
+    }
+
+    @Override
+    public void sendMsg(OFMessage msg) {
+        // Ignore everything but flow mods and stat requests
+        if (!(msg instanceof OFFlowMod || msg instanceof OFFlowStatsRequest)) {
+            super.sendMsg(msg);
+            return;
+        }
+
+        Match newMatch;
+        OFMessage newMsg = null;
+
+        if (msg instanceof OFFlowStatsRequest) {
+            // Rewrite match only
+            OFFlowStatsRequest fsr = (OFFlowStatsRequest) msg;
+            newMatch = rewriteMatch(fsr.getMatch());
+            newMsg = fsr.createBuilder().setMatch(newMatch).build();
+        } else if (msg instanceof OFFlowMod) {
+            // Rewrite match and actions
+            OFFlowMod fm = (OFFlowMod) msg;
+            newMatch = rewriteMatch(fm.getMatch());
+            List<OFAction> actions = rewriteActions(fm.getActions());
+
+            newMsg = fm.createBuilder().setMatch(newMatch).setActions(actions).build();
+        }
+
+        super.sendMsg(newMsg);
+    }
 
     @Override
     public Boolean supportNxRole() {
diff --git a/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowModBuilderVer13.java b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowModBuilderVer13.java
index 135150d..90def43 100644
--- a/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowModBuilderVer13.java
+++ b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowModBuilderVer13.java
@@ -320,7 +320,7 @@
     }
 
     private OFAction buildModLambdaInstruction(ModLambdaInstruction instruction) {
-        return factory().actions().circuit(factory().oxms().ochSigidBasic(
+        return factory().actions().circuit(factory().oxms().expOchSigId(
                 new CircuitSignalID((byte) 1, (byte) 2, instruction.lambda(), (short) 1)));
     }
 
@@ -329,7 +329,7 @@
         byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
         byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
 
-        return factory().actions().circuit(factory().oxms().ochSigidBasic(
+        return factory().actions().circuit(factory().oxms().expOchSigId(
                 new CircuitSignalID(gridType, channelSpacing,
                         (short) signal.spacingMultiplier(), (short) signal.slotGranularity())
         ));