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/cli/src/main/java/org/onosproject/cli/net/AddOpticalIntentCommand.java b/cli/src/main/java/org/onosproject/cli/net/AddOpticalIntentCommand.java
index 3d3bca4..001c273 100644
--- a/cli/src/main/java/org/onosproject/cli/net/AddOpticalIntentCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/AddOpticalIntentCommand.java
@@ -21,7 +21,6 @@
 import org.onosproject.net.CltSignalType;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.Device;
-import org.onosproject.net.OchPort;
 import org.onosproject.net.OduCltPort;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.OduSignalType;
@@ -32,10 +31,12 @@
 import org.onosproject.net.intent.OpticalCircuitIntent;
 import org.onosproject.net.intent.OpticalConnectivityIntent;
 import org.onosproject.net.intent.OpticalOduIntent;
+import org.onosproject.net.optical.OchPort;
 
 import java.util.List;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
 
 /**
  * Installs optical connectivity or circuit intents, depending on given port types.
@@ -93,7 +94,8 @@
             return;
         }
 
-        DeviceService deviceService = get(DeviceService.class);
+        DeviceService deviceService = opticalView(get(DeviceService.class));
+
         Port srcPort = deviceService.getPort(ingress.deviceId(), ingress.port());
         Port dstPort = deviceService.getPort(egress.deviceId(), egress.port());
 
@@ -142,6 +144,19 @@
                     .signalType(signalType)
                     .bidirectional(bidirectional)
                     .build();
+        } else if (srcPort instanceof org.onosproject.net.OchPort &&
+                   dstPort instanceof org.onosproject.net.OchPort) {
+            print("WARN: encountered old OchPort model");
+            // old OchPort model can be removed when ready
+            OduSignalType signalType = ((org.onosproject.net.OchPort) srcPort).signalType();
+            intent = OpticalConnectivityIntent.builder()
+                    .appId(appId())
+                    .key(key())
+                    .src(ingress)
+                    .dst(egress)
+                    .signalType(signalType)
+                    .bidirectional(bidirectional)
+                    .build();
         } else {
             print("Unable to create optical intent between connect points %s and %s", ingress, egress);
             return;
diff --git a/cli/src/main/java/org/onosproject/cli/net/DevicePortsListCommand.java b/cli/src/main/java/org/onosproject/cli/net/DevicePortsListCommand.java
index 6549b07..e8413f2 100644
--- a/cli/src/main/java/org/onosproject/cli/net/DevicePortsListCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/DevicePortsListCommand.java
@@ -25,19 +25,20 @@
 import org.onlab.util.Frequency;
 import org.onosproject.utils.Comparators;
 import org.onosproject.net.Device;
-import org.onosproject.net.OchPort;
 import org.onosproject.net.OduCltPort;
 import org.onosproject.net.OmsPort;
 import org.onosproject.net.OtuPort;
 import org.onosproject.net.Port;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.device.DeviceService;
-
+import org.onosproject.net.optical.OchPort;
+import org.onosproject.net.optical.OpticalDevice;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
 import static org.onosproject.net.DeviceId.deviceId;
+import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
 
 /**
  * Lists all ports or all ports of a device.
@@ -65,7 +66,7 @@
 
     @Override
     protected void execute() {
-        DeviceService service = get(DeviceService.class);
+        DeviceService service = opticalView(get(DeviceService.class));
         if (uri == null) {
             if (outputJson()) {
                 print("%s", jsonPorts(service, getSortedDevices(service)));
@@ -154,10 +155,37 @@
             String annotations = annotations(port.annotations());
             switch (port.type()) {
                 case OCH:
-                     print(FMT_OCH, portName, portIsEnabled, portType,
-                             ((OchPort) port).signalType().toString(),
-                             ((OchPort) port).isTunable() ? "yes" : "no", annotations);
-                     break;
+                    if (port instanceof org.onosproject.net.OchPort) {
+                        // old OchPort model
+                        org.onosproject.net.OchPort oPort = (org.onosproject.net.OchPort) port;
+                        print("WARN: OchPort in old model");
+                        print(FMT_OCH, portName, portIsEnabled, portType,
+                              oPort.signalType().toString(),
+                              oPort.isTunable() ? "yes" : "no", annotations);
+                        break;
+                    }
+                    if (port instanceof OchPort) {
+                        OchPort och = (OchPort) port;
+                        print(FMT_OCH, portName, portIsEnabled, portType,
+                              och.signalType().toString(),
+                              och.isTunable() ? "yes" : "no", annotations);
+                       break;
+                    } else if (port.element().is(OpticalDevice.class)) {
+                        // Note: should never reach here, but
+                        // leaving it here as an example to
+                        // manually translate to specific port.
+                        OpticalDevice optDevice = port.element().as(OpticalDevice.class);
+                        if (optDevice.portIs(port, OchPort.class)) {
+                            OchPort och = optDevice.portAs(port, OchPort.class).get();
+                            print(FMT_OCH, portName, portIsEnabled, portType,
+                                  och.signalType().toString(),
+                                  och.isTunable() ? "yes" : "no", annotations);
+                            break;
+                        }
+                    }
+                    print("WARN: OchPort but not on OpticalDevice or ill-formed");
+                    print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations);
+                    break;
                 case ODUCLT:
                      print(FMT_ODUCLT_OTU, portName, portIsEnabled, portType,
                             ((OduCltPort) port).signalType().toString(), annotations);