Fix optical port types in device store (ONOS-2046).
Fix CLI add-optical-intent cmd for TL1-based devices (ONOS-2045).

Change-Id: I4ba79476104d9906707cc7cc683fedd4ce4343bb
diff --git a/apps/optical/src/main/java/org/onosproject/optical/OpticalPathProvisioner.java b/apps/optical/src/main/java/org/onosproject/optical/OpticalPathProvisioner.java
index 96bacc5..efe64b1 100644
--- a/apps/optical/src/main/java/org/onosproject/optical/OpticalPathProvisioner.java
+++ b/apps/optical/src/main/java/org/onosproject/optical/OpticalPathProvisioner.java
@@ -206,7 +206,6 @@
                 src = p2pIntent.ingressPoint();
                 dst = p2pIntent.egressPoint();
             } else {
-                log.error("Unsupported intent type: {}", intent.getClass());
                 return;
             }
 
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 97341d7..c76bc3d 100644
--- a/cli/src/main/java/org/onosproject/cli/net/AddOpticalIntentCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/AddOpticalIntentCommand.java
@@ -20,6 +20,7 @@
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.OchPort;
 import org.onosproject.net.OduCltPort;
+import org.onosproject.net.DeviceId;
 import org.onosproject.net.OduSignalType;
 import org.onosproject.net.Port;
 import org.onosproject.net.device.DeviceService;
@@ -28,6 +29,10 @@
 import org.onosproject.net.intent.OpticalCircuitIntent;
 import org.onosproject.net.intent.OpticalConnectivityIntent;
 
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
 /**
  * Installs optical connectivity or circuit intents, depending on given port types.
  */
@@ -45,13 +50,37 @@
               required = true, multiValued = false)
     String egressDeviceString = null;
 
+    private ConnectPoint createConnectPoint(String devicePortString) {
+        String[] splitted = devicePortString.split("/");
+
+        checkArgument(splitted.length == 2,
+                "Connect point must be in \"deviceUri/portNumber\" format");
+
+        DeviceId deviceId = DeviceId.deviceId(splitted[0]);
+
+        DeviceService deviceService = get(DeviceService.class);
+
+        List<Port> ports = deviceService.getPorts(deviceId);
+
+        for (Port port : ports) {
+            if (splitted[1].equals(port.number().name())) {
+                return new ConnectPoint(deviceId, port.number());
+            }
+        }
+
+        return null;
+    }
+
     @Override
     protected void execute() {
         IntentService service = get(IntentService.class);
 
-        ConnectPoint ingress = ConnectPoint.deviceConnectPoint(ingressDeviceString);
+        ConnectPoint ingress = createConnectPoint(ingressDeviceString);
+        ConnectPoint egress = createConnectPoint(egressDeviceString);
 
-        ConnectPoint egress = ConnectPoint.deviceConnectPoint(egressDeviceString);
+        if (ingress == null || egress == null) {
+            print("Could not create optical intent");
+        }
 
         DeviceService deviceService = get(DeviceService.class);
         Port srcPort = deviceService.getPort(ingress.deviceId(), ingress.port());
diff --git a/core/api/src/main/java/org/onosproject/net/resource/device/DeviceResourceStore.java b/core/api/src/main/java/org/onosproject/net/resource/device/DeviceResourceStore.java
index 31cab02..a52a843 100644
--- a/core/api/src/main/java/org/onosproject/net/resource/device/DeviceResourceStore.java
+++ b/core/api/src/main/java/org/onosproject/net/resource/device/DeviceResourceStore.java
@@ -22,10 +22,17 @@
 import java.util.Set;
 
 public interface DeviceResourceStore {
+    /**
+     * Returns unallocated ports on the given device.
+     *
+     * @param deviceId device ID
+     * @return set of unallocated ports
+     */
     Set<Port> getFreePorts(DeviceId deviceId);
 
     /**
      * Allocates the given ports to the given intent.
+     *
      * @param ports set of ports to allocate
      * @param intentId intent ID
      * @return true if allocation was successful, false otherwise
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 4d8077e..144f2df 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
@@ -265,7 +265,8 @@
                 if (edge.link().type() != Link.Type.OPTICAL) {
                     return -1;
                 }
-                return edge.link().annotations().value("optical.type").equals("WDM") ? +1 : -1;
+                //return edge.link().annotations().value("optical.type").equals("WDM") ? +1 : -1;
+                return 1;
             }
         };
 
diff --git a/core/store/dist/src/main/java/org/onosproject/store/device/impl/GossipDeviceStore.java b/core/store/dist/src/main/java/org/onosproject/store/device/impl/GossipDeviceStore.java
index 8d6b5b6..ef836b5 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/device/impl/GossipDeviceStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/device/impl/GossipDeviceStore.java
@@ -37,6 +37,7 @@
 import org.onosproject.mastership.MastershipService;
 import org.onosproject.mastership.MastershipTerm;
 import org.onosproject.mastership.MastershipTermService;
+import org.onosproject.net.Annotations;
 import org.onosproject.net.AnnotationsUtil;
 import org.onosproject.net.DefaultAnnotations;
 import org.onosproject.net.DefaultDevice;
@@ -1007,6 +1008,26 @@
                                  chassisId, annotations);
     }
 
+    private Port buildTypedPort(Device device, PortNumber number, boolean isEnabled,
+                                 PortDescription description, Annotations annotations) {
+        switch (description.type()) {
+            case OMS:
+                OmsPortDescription omsDesc = (OmsPortDescription) description;
+                return new OmsPort(device, number, isEnabled, omsDesc.minFrequency(),
+                        omsDesc.maxFrequency(), omsDesc.grid(), annotations);
+            case OCH:
+                OchPortDescription ochDesc = (OchPortDescription) description;
+                return new OchPort(device, number, isEnabled, ochDesc.signalType(),
+                        ochDesc.isTunable(), ochDesc.lambda(), annotations);
+            case ODUCLT:
+                OduCltPortDescription oduDesc = (OduCltPortDescription) description;
+                return new OduCltPort(device, number, isEnabled, oduDesc.signalType(), annotations);
+            default:
+                return new DefaultPort(device, number, isEnabled, description.type(),
+                        description.portSpeed(), annotations);
+        }
+    }
+
     /**
      * Returns a Port, merging description given from multiple Providers.
      *
@@ -1048,25 +1069,7 @@
                 }
                 annotations = merge(annotations, otherPortDesc.value().annotations());
                 PortDescription other = otherPortDesc.value();
-                switch (other.type()) {
-                    case OMS:
-                        OmsPortDescription omsPortDesc = (OmsPortDescription) otherPortDesc.value();
-                        updated = new OmsPort(device, number, isEnabled, omsPortDesc.minFrequency(),
-                                              omsPortDesc.maxFrequency(), omsPortDesc.grid(), annotations);
-                        break;
-                    case OCH:
-                        OchPortDescription ochPortDesc = (OchPortDescription) otherPortDesc.value();
-                        updated = new OchPort(device, number, isEnabled, ochPortDesc.signalType(),
-                                              ochPortDesc.isTunable(), ochPortDesc.lambda(), annotations);
-                        break;
-                    case ODUCLT:
-                        OduCltPortDescription oduCltPortDesc = (OduCltPortDescription) otherPortDesc.value();
-                        updated = new OduCltPort(device, number, isEnabled, oduCltPortDesc.signalType(), annotations);
-                        break;
-                    default:
-                        updated = new DefaultPort(
-                                device, number, isEnabled, other.type(), other.portSpeed(), annotations);
-                }
+                updated = buildTypedPort(device, number, isEnabled, other, annotations);
                 newest = otherPortDesc.timestamp();
             }
         }
@@ -1075,7 +1078,7 @@
         }
         PortDescription current = portDesc.value();
         return updated == null
-                ? new DefaultPort(device, number, isEnabled, current.type(), current.portSpeed(), annotations)
+                ? buildTypedPort(device, number, isEnabled, current, annotations)
                 : updated;
     }