Fix a cast error when creating OpticalConnectivityIntent

A cast error occurred in FlowModBuilder when the type of
Criterion was OCH_SIGID.

Move two convertion methods from FlowModBuilderVer13 to
FlowModBuilderHelper for refactoring.

Change-Id: I4634d57fc70cfb144de64d72140cbca81e060248
diff --git a/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowModBuilder.java b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowModBuilder.java
index 5ec5d7b..9d949c6 100644
--- a/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowModBuilder.java
+++ b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowModBuilder.java
@@ -20,6 +20,7 @@
 import org.onlab.packet.Ip6Address;
 import org.onlab.packet.Ip6Prefix;
 import org.onlab.packet.VlanId;
+import org.onosproject.net.OchSignal;
 import org.onosproject.net.flow.FlowRule;
 import org.onosproject.net.flow.TrafficSelector;
 import org.onosproject.net.flow.criteria.EthCriterion;
@@ -36,9 +37,9 @@
 import org.onosproject.net.flow.criteria.IcmpTypeCriterion;
 import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
 import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
-import org.onosproject.net.flow.criteria.LambdaCriterion;
 import org.onosproject.net.flow.criteria.MetadataCriterion;
 import org.onosproject.net.flow.criteria.MplsCriterion;
+import org.onosproject.net.flow.criteria.OchSignalCriterion;
 import org.onosproject.net.flow.criteria.OpticalSignalTypeCriterion;
 import org.onosproject.net.flow.criteria.PortCriterion;
 import org.onosproject.net.flow.criteria.SctpPortCriterion;
@@ -374,10 +375,17 @@
                                   U16.of(exthdrFlagsCriterion.exthdrFlags()));
                 break;
             case OCH_SIGID:
-                LambdaCriterion lc = (LambdaCriterion) c;
-                mBuilder.setExact(MatchField.OCH_SIGID,
-                        new CircuitSignalID((byte) 1, (byte) 2,
-                                            (short) lc.lambda(), (short) 1));
+                try {
+                    OchSignalCriterion ochSignalCriterion = (OchSignalCriterion) c;
+                    OchSignal signal = ochSignalCriterion.lambda();
+                    byte gridType = FlowModBuilderHelper.convertGridType(signal.gridType());
+                    byte channelSpacing = FlowModBuilderHelper.convertChannelSpacing(signal.channelSpacing());
+                    mBuilder.setExact(MatchField.OCH_SIGID,
+                            new CircuitSignalID(gridType, channelSpacing,
+                                    (short) signal.spacingMultiplier(), (short) signal.slotGranularity()));
+                } catch (UnsupportedGridTypeException | UnsupportedChannelSpacingException e) {
+                    log.warn(e.getMessage());
+                }
                 break;
             case OCH_SIGTYPE:
                 OpticalSignalTypeCriterion sc =
diff --git a/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowModBuilderHelper.java b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowModBuilderHelper.java
new file mode 100644
index 0000000..fd9cac4
--- /dev/null
+++ b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowModBuilderHelper.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.provider.of.flow.impl;
+
+import org.onosproject.net.ChannelSpacing;
+import org.onosproject.net.GridType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Collection of helper methods to convert protocol agnostic models to values used in OpenFlow spec.
+ */
+final class FlowModBuilderHelper {
+
+    private static final Logger log = LoggerFactory.getLogger(FlowModBuilderHelper.class);
+
+    // prohibit instantiation
+    private FlowModBuilderHelper() {}
+
+    /**
+     * Converts a {@link GridType} to the corresponding byte value defined in
+     * ONF "Optical Transport Protocol Extensions Version 1.0".
+     *
+     * @param type grid type
+     * @return the byte value corresponding to the specified grid type
+     * @throws UnsupportedGridTypeException if the specified grid type is not supported
+     */
+    static byte convertGridType(GridType type) {
+        // See ONF "Optical Transport Protocol Extensions Version 1.0"
+        // for the following values
+        switch (type) {
+            case DWDM:
+                // OFPGRIDT_DWDM of enum ofp_grid_type
+                return 1;
+            case CWDM:
+                // OFPGRIDT_CWDM of enum ofp_grid_type
+                return 2;
+            case FLEX:
+                // OFPGRIDT_FLEX of enum ofp_grid_type
+                return 3;
+            default:
+                throw new UnsupportedGridTypeException(type);
+        }
+    }
+
+    /**
+     * Converts a {@link ChannelSpacing} to the corresponding byte value defined in
+     * ONF "Optical Transport Protocol Extensions Version 1.0".
+     *
+     * @param spacing channel spacing
+     * @return byte value corresponding to the specified channel spacing
+     * @throws UnsupportedChannelSpacingException if the specified channel spacing is not supported
+     */
+    static byte convertChannelSpacing(ChannelSpacing spacing) {
+        // See ONF "Optical Transport Protocol Extensions Version 1.0"
+        // for the following values
+        switch (spacing) {
+            case CHL_100GHZ:
+                // OFPCS_100GHZ of enum ofp_chl_spacing
+                return 1;
+            case CHL_50GHZ:
+                // OFPCS_50GHZ of enum ofp_chl_spacing
+                return 2;
+            case CHL_25GHZ:
+                // OFPCS_25GHZ of enum ofp_chl_spacing
+                return 3;
+            case CHL_12P5GHZ:
+                // OFPCS_12P5GHZ of enum ofp_chl_spacing
+                return 4;
+            case CHL_6P25GHZ:
+                // OFPCS_6P25GHZ of enum ofp_chl_spacing
+                return 5;
+            default:
+                throw new UnsupportedChannelSpacingException(spacing);
+        }
+    }
+}
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 18f43bf..a636a76 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
@@ -18,8 +18,6 @@
 import com.google.common.collect.Lists;
 import org.onlab.packet.Ip4Address;
 import org.onlab.packet.Ip6Address;
-import org.onosproject.net.ChannelSpacing;
-import org.onosproject.net.GridType;
 import org.onosproject.net.OchSignal;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.flow.FlowRule;
@@ -73,6 +71,9 @@
 import java.util.List;
 import java.util.Optional;
 
+import static org.onosproject.provider.of.flow.impl.FlowModBuilderHelper.convertChannelSpacing;
+import static org.onosproject.provider.of.flow.impl.FlowModBuilderHelper.convertGridType;
+
 /**
  * Flow mod builder for OpenFlow 1.3+.
  */
@@ -281,48 +282,6 @@
         ));
     }
 
-    private byte convertGridType(GridType type) {
-        // See ONF "Optical Transport Protocol Extensions Version 1.0"
-        // for the following values
-        switch (type) {
-            case DWDM:
-                // OFPGRIDT_DWDM of enum ofp_grid_type
-                return 1;
-            case CWDM:
-                // OFPGRIDT_CWDM of enum ofp_grid_type
-                return 2;
-            case FLEX:
-                // OFPGRIDT_FLEX of enum ofp_grid_type
-                return 3;
-            default:
-                throw new UnsupportedGridTypeException(type);
-        }
-    }
-
-    private byte convertChannelSpacing(ChannelSpacing spacing) {
-        // See ONF "Optical Transport Protocol Extensions Version 1.0"
-        // for the following values
-        switch (spacing) {
-            case CHL_100GHZ:
-                // OFPCS_100GHZ of enum ofp_chl_spacing
-                return 1;
-            case CHL_50GHZ:
-                // OFPCS_50GHZ of enum ofp_chl_spacing
-                return 2;
-            case CHL_25GHZ:
-                // OFPCS_25GHZ of enum ofp_chl_spacing
-                return 3;
-            case CHL_12P5GHZ:
-                // OFPCS_12P5GHZ of enum ofp_chl_spacing
-                return 4;
-            case CHL_6P25GHZ:
-                // OFPCS_6P25GHZ of enum ofp_chl_spacing
-                return 5;
-            default:
-                throw new UnsupportedChannelSpacingException(spacing);
-        }
-    }
-
     private OFAction buildL2Modification(Instruction i) {
         L2ModificationInstruction l2m = (L2ModificationInstruction) i;
         ModEtherInstruction eth;