Implement CLI and REST API for Xconnect

Deprecate the old way of configuring Xconnect via network config

Change-Id: I5b9ac7852517c25805bcbfc0e7b3bec3a52eed9f
diff --git a/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/XconnectCodec.java b/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/XconnectCodec.java
new file mode 100644
index 0000000..77824e8
--- /dev/null
+++ b/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/XconnectCodec.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.segmentrouting.xconnect.api;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.Sets;
+import org.onlab.packet.VlanId;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Set;
+
+public class XconnectCodec extends JsonCodec<XconnectDesc> {
+    private static final String DEVICE_ID = "deviceId";
+    private static final String VLAN_ID = "vlanId";
+    private static final String PORTS = "ports";
+
+    private static Logger log = LoggerFactory.getLogger(XconnectCodec.class);
+
+    @Override
+    public ObjectNode encode(XconnectDesc desc, CodecContext context) {
+        final ObjectNode result = context.mapper().createObjectNode();
+        result.put(DEVICE_ID, desc.key().deviceId().toString());
+        result.put(VLAN_ID, desc.key().vlanId().toString());
+        final ArrayNode portNode = result.putArray(PORTS);
+        desc.ports().forEach(port -> portNode.add(port.toString()));
+
+        return result;
+    }
+
+    @Override
+    public XconnectDesc decode(ObjectNode json, CodecContext context) {
+        DeviceId deviceId = DeviceId.deviceId(json.path(DEVICE_ID).asText());
+        VlanId vlanId = VlanId.vlanId(json.path(VLAN_ID).asText());
+
+        Set<PortNumber> ports = Sets.newHashSet();
+        JsonNode portNodes = json.get(PORTS);
+        if (portNodes != null) {
+            portNodes.forEach(portNode -> ports.add(PortNumber.portNumber(portNode.asInt())));
+        }
+
+        XconnectKey key = new XconnectKey(deviceId, vlanId);
+        return new XconnectDesc(key, ports);
+    }
+}
diff --git a/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/XconnectDesc.java b/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/XconnectDesc.java
new file mode 100644
index 0000000..0eead31
--- /dev/null
+++ b/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/XconnectDesc.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.segmentrouting.xconnect.api;
+
+import com.google.common.base.MoreObjects;
+import org.onosproject.net.PortNumber;
+
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Xconnect description.
+ */
+public class XconnectDesc {
+    private XconnectKey key;
+    private Set<PortNumber> ports;
+
+    /**
+     * Constructs new Xconnect description with given device ID and VLAN ID.
+     *
+     * @param key Xconnect key
+     * @param ports set of ports
+     */
+    public XconnectDesc(XconnectKey key, Set<PortNumber> ports) {
+        this.key = key;
+        this.ports = ports;
+    }
+
+    /**
+     * Gets Xconnect key.
+     *
+     * @return Xconnect key
+     */
+    public XconnectKey key() {
+        return key;
+    }
+
+    /**
+     * Gets ports.
+     *
+     * @return set of ports
+     */
+    public Set<PortNumber> ports() {
+        return ports;
+    }
+
+    @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (!(obj instanceof XconnectDesc)) {
+            return false;
+        }
+        final XconnectDesc other = (XconnectDesc) obj;
+        return Objects.equals(this.key, other.key) &&
+                Objects.equals(this.ports, other.ports);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(key, ports);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .add("key", key)
+                .add("ports", ports)
+                .toString();
+    }
+}
diff --git a/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/XconnectKey.java b/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/XconnectKey.java
new file mode 100644
index 0000000..1f2d446
--- /dev/null
+++ b/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/XconnectKey.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.segmentrouting.xconnect.api;
+
+import com.google.common.base.MoreObjects;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.DeviceId;
+
+import java.util.Objects;
+
+/**
+ * Xconnect key.
+ */
+public class XconnectKey {
+    private DeviceId deviceId;
+    private VlanId vlanId;
+
+    /**
+     * Constructs new XconnectKey with given device ID and VLAN ID.
+     *
+     * @param deviceId device ID
+     * @param vlanId vlan ID
+     */
+    public XconnectKey(DeviceId deviceId, VlanId vlanId) {
+        this.deviceId = deviceId;
+        this.vlanId = vlanId;
+    }
+
+    /**
+     * Gets device ID.
+     *
+     * @return device ID of the Xconnect key
+     */
+    public DeviceId deviceId() {
+        return deviceId;
+    }
+
+    /**
+     * Gets VLAN ID.
+     *
+     * @return VLAN ID of the Xconnect key
+     */
+    public VlanId vlanId() {
+        return vlanId;
+    }
+
+    @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (!(obj instanceof XconnectKey)) {
+            return false;
+        }
+        final XconnectKey other = (XconnectKey) obj;
+        return Objects.equals(this.deviceId, other.deviceId) &&
+                Objects.equals(this.vlanId, other.vlanId);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(deviceId, vlanId);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .add("deviceId", deviceId)
+                .add("vlanId", vlanId)
+                .toString();
+    }
+}
diff --git a/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/XconnectService.java b/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/XconnectService.java
new file mode 100644
index 0000000..1fede71
--- /dev/null
+++ b/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/XconnectService.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.segmentrouting.xconnect.api;
+
+import org.apache.felix.scr.annotations.Service;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+
+import java.util.Set;
+
+/**
+ * VLAN cross connect between exactly two ports.
+ */
+@Service
+public interface XconnectService {
+
+    /**
+     * VLAN cross-connect ACL priority.
+     */
+    int XCONNECT_ACL_PRIORITY = 60000;
+
+    /**
+     * VLAN cross-connect Bridging priority.
+     */
+    int XCONNECT_PRIORITY = 1000;
+
+    /**
+     * Creates or updates Xconnect.
+     *
+     * @param deviceId device ID
+     * @param vlanId VLAN ID
+     * @param ports set of ports
+     */
+    void addOrUpdateXconnect(DeviceId deviceId, VlanId vlanId, Set<PortNumber> ports);
+
+    /**
+     * Deletes Xconnect.
+     *
+     * @param deviceId device ID
+     * @param vlanId VLAN ID
+     */
+    void removeXonnect(DeviceId deviceId, VlanId vlanId);
+
+    /**
+     * Gets Xconnects.
+     *
+     * @return set of Xconnect descriptions
+     */
+    Set<XconnectDesc> getXconnects();
+
+    /**
+     * Check if there is Xconnect configured on given connect point.
+     *
+     * @param cp connect point
+     * @return true if there is Xconnect configured on the connect point
+     */
+    boolean hasXconnect(ConnectPoint cp);
+
+}
diff --git a/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/package-info.java b/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/package-info.java
new file mode 100644
index 0000000..56df706
--- /dev/null
+++ b/app/src/main/java/org/onosproject/segmentrouting/xconnect/api/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.
+ */
+
+/**
+ * VLAN cross connect API.
+ */
+package org.onosproject.segmentrouting.xconnect.api;
\ No newline at end of file