ONOS-3503 Remove OchPort out of core.

- Implementation of a Behavior OpticalDevice has the knowledge of
  translating annotations into optical specific port.
- OpticalDeviceServiceView checks if the Device is a OpticalDevice
  and translate all the Ports to optical specific port before returning.

- This commit contains feedbacks, issues, and fixes by Michele Santuari.

- Note: 3 more Port types to go (OduClt, Oms, Otu)

Change-Id: I4cbda8bc1922fbdd4dac8de8d02294bad74b8058
diff --git a/core/net/src/main/java/org/onosproject/net/device/impl/OpticalPortOperator.java b/core/net/src/main/java/org/onosproject/net/device/impl/OpticalPortOperator.java
index 25bc848..fdd1f5d 100644
--- a/core/net/src/main/java/org/onosproject/net/device/impl/OpticalPortOperator.java
+++ b/core/net/src/main/java/org/onosproject/net/device/impl/OpticalPortOperator.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.net.device.impl;
 
+import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription;
 import static org.slf4j.LoggerFactory.getLogger;
 import static com.google.common.base.Preconditions.checkNotNull;
 
@@ -22,7 +23,6 @@
 import org.onosproject.net.config.basics.OpticalPortConfig;
 import org.onosproject.net.AnnotationKeys;
 import org.onosproject.net.DefaultAnnotations;
-import org.onosproject.net.OchPort;
 import org.onosproject.net.OtuPort;
 import org.onosproject.net.OduCltPort;
 import org.onosproject.net.OmsPort;
@@ -35,6 +35,8 @@
 import org.onosproject.net.device.OmsPortDescription;
 import org.onosproject.net.device.OtuPortDescription;
 import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.optical.OchPort;
+import org.onosproject.net.optical.OpticalDevice;
 import org.slf4j.Logger;
 
 /**
@@ -105,16 +107,21 @@
                 return new OmsPortDescription(port, oms.isEnabled(), oms.minFrequency(),
                         oms.maxFrequency(), oms.grid(), sa);
             case OCH:
-            // We might need to update lambda below with STATIC_LAMBDA.
-                OchPortDescription och = (OchPortDescription) descr;
-                return new OchPortDescription(port, och.isEnabled(), och.signalType(),
-                        och.isTunable(), och.lambda(), sa);
+                // We might need to update lambda below with STATIC_LAMBDA.
+                if (descr instanceof OchPortDescription) {
+                    // TODO This block can go away once deprecation is complete.
+                    OchPortDescription och = (OchPortDescription) descr;
+                    return ochPortDescription(port, och.isEnabled(), och.signalType(),
+                            och.isTunable(), och.lambda(), sa);
+                }
+                return descr;
             case ODUCLT:
                 OduCltPortDescription odu = (OduCltPortDescription) descr;
                 return new OduCltPortDescription(port, odu.isEnabled(), odu.signalType(), sa);
             case PACKET:
             case FIBER:
             case COPPER:
+                // TODO: it should be safe to just return descr. confirm and fix
                 return new DefaultPortDescription(port, descr.isEnabled(), descr.type(),
                         descr.portSpeed(), sa);
             case OTU:
@@ -182,9 +189,22 @@
                 return new OmsPortDescription(ptn, isup, oms.minFrequency(),
                         oms.maxFrequency(), oms.grid(), an);
             case OCH:
-                OchPort och = (OchPort) port;
-                return new OchPortDescription(ptn, isup, och.signalType(),
-                        och.isTunable(), och.lambda(), an);
+                if (port instanceof org.onosproject.net.OchPort) {
+                    // remove if-block once old OchPort deprecation is complete
+                    org.onosproject.net.OchPort och = (org.onosproject.net.OchPort) port;
+                    return ochPortDescription(ptn, isup, och.signalType(),
+                                              och.isTunable(), och.lambda(), an);
+                }
+                if (port.element().is(OpticalDevice.class)) {
+                    OpticalDevice optDevice = port.element().as(OpticalDevice.class);
+                    if (optDevice.portIs(port, OchPort.class)) {
+                        OchPort och = optDevice.portAs(port, OchPort.class).get();
+                        return ochPortDescription(ptn, isup, och.signalType(),
+                                                  och.isTunable(), och.lambda(), an);
+                    }
+                }
+                return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an);
+
             case ODUCLT:
                 OduCltPort odu = (OduCltPort) port;
                 return new OduCltPortDescription(ptn, isup, odu.signalType(), an);
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalCircuitIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalCircuitIntentCompiler.java
index 9e31234..bf8152e 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalCircuitIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalCircuitIntentCompiler.java
@@ -32,7 +32,6 @@
 import org.onosproject.net.AnnotationKeys;
 import org.onosproject.net.CltSignalType;
 import org.onosproject.net.ConnectPoint;
-import org.onosproject.net.OchPort;
 import org.onosproject.net.OduCltPort;
 import org.onosproject.net.OduSignalId;
 import org.onosproject.net.OduSignalType;
@@ -60,6 +59,7 @@
 import org.onosproject.net.intent.OpticalCircuitIntent;
 import org.onosproject.net.intent.OpticalConnectivityIntent;
 import org.onosproject.net.intent.impl.IntentCompilationException;
+import org.onosproject.net.optical.OchPort;
 import org.onosproject.net.intent.IntentSetMultimap;
 import org.onosproject.net.resource.ResourceAllocation;
 import org.onosproject.net.resource.Resource;
@@ -82,6 +82,7 @@
 import java.util.stream.Stream;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
 
 /**
  * An intent compiler for {@link org.onosproject.net.intent.OpticalCircuitIntent}.
@@ -156,6 +157,7 @@
 
     @Activate
     public void activate(ComponentContext context) {
+        deviceService = opticalView(deviceService);
         appId = coreService.registerApplication("org.onosproject.net.intent");
         intentManager.registerCompiler(OpticalCircuitIntent.class, this);
         cfgService.registerProperties(getClass());
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java
index d1b5925..b1ca006 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java
@@ -31,7 +31,6 @@
 import org.onosproject.net.DefaultOchSignalComparator;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.Link;
-import org.onosproject.net.OchPort;
 import org.onosproject.net.OchSignal;
 import org.onosproject.net.OchSignalType;
 import org.onosproject.net.Path;
@@ -43,6 +42,7 @@
 import org.onosproject.net.intent.OpticalConnectivityIntent;
 import org.onosproject.net.intent.OpticalPathIntent;
 import org.onosproject.net.intent.impl.IntentCompilationException;
+import org.onosproject.net.optical.OchPort;
 import org.onosproject.net.resource.ResourceAllocation;
 import org.onosproject.net.resource.Resource;
 import org.onosproject.net.resource.ResourceService;
@@ -63,6 +63,7 @@
 import java.util.stream.Stream;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
 
 /**
  * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}.
@@ -88,6 +89,7 @@
 
     @Activate
     public void activate() {
+        deviceService = opticalView(deviceService);
         intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
     }
 
diff --git a/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/OpticalCircuitIntentCompilerTest.java b/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/OpticalCircuitIntentCompilerTest.java
index 0cdadb5..9d72cd0 100644
--- a/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/OpticalCircuitIntentCompilerTest.java
+++ b/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/OpticalCircuitIntentCompilerTest.java
@@ -31,9 +31,9 @@
 import org.onosproject.net.CltSignalType;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DefaultDevice;
+import org.onosproject.net.DefaultPort;
 import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
-import org.onosproject.net.OchPort;
 import org.onosproject.net.OchSignal;
 import org.onosproject.net.OduCltPort;
 import org.onosproject.net.OduSignalId;
@@ -57,6 +57,8 @@
 import org.onosproject.net.intent.Key;
 import org.onosproject.net.intent.MockIdGenerator;
 import org.onosproject.net.intent.OpticalCircuitIntent;
+import org.onosproject.net.optical.OchPort;
+import org.onosproject.net.optical.impl.DefaultOchPort;
 import org.onosproject.net.provider.ProviderId;
 import org.onosproject.net.intent.IntentSetMultimap;
 import org.onosproject.net.behaviour.TributarySlotQuery;
@@ -128,11 +130,13 @@
 
     // Och ports with signalType=ODU2
     private static final OchPort D1P2 =
-            new OchPort(device1, PortNumber.portNumber(2), true, OduSignalType.ODU2,
-                    true, OchSignal.newDwdmSlot(ChannelSpacing.CHL_50GHZ, 1), annotations2);
+            new DefaultOchPort(new DefaultPort(device1, PortNumber.portNumber(2), true, annotations2),
+                               OduSignalType.ODU2,
+                    true, OchSignal.newDwdmSlot(ChannelSpacing.CHL_50GHZ, 1));
     private static final OchPort D2P2 =
-            new OchPort(device2, PortNumber.portNumber(2), true, OduSignalType.ODU2,
-                    true, OchSignal.newDwdmSlot(ChannelSpacing.CHL_50GHZ, 1), annotations2);
+            new DefaultOchPort(new DefaultPort(device2, PortNumber.portNumber(2), true, annotations2),
+                               OduSignalType.ODU2,
+                    true, OchSignal.newDwdmSlot(ChannelSpacing.CHL_50GHZ, 1));
 
     // OduClt ports with signalType=10GBE
     private static final OduCltPort D1P3 =
@@ -169,11 +173,11 @@
             if (deviceId.equals(deviceId(DEV1))) {
                 switch (portNumber.toString()) {
                     case "1":
-                        return (Port) D1P1;
+                        return D1P1;
                     case "2":
-                        return (Port) D1P2;
+                        return D1P2;
                     case "3":
-                        return (Port) D1P3;
+                        return D1P3;
                     default:
                         return null;
                 }
@@ -181,11 +185,11 @@
             if (deviceId.equals(deviceId(DEV2))) {
                 switch (portNumber.toString()) {
                     case "1":
-                        return (Port) D2P1;
+                        return D2P1;
                     case "2":
-                        return (Port) D2P2;
+                        return D2P2;
                     case "3":
-                        return (Port) D2P3;
+                        return D2P3;
                     default:
                         return null;
                 }