[ONOS-7553] Support injecting physical interfaces to openstack node

Change-Id: I5d746e9b4fa6015dbaec90d27ea7e1a7fa105e31
diff --git a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeCodecTest.java b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeCodecTest.java
index e009209..cea17c4 100644
--- a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeCodecTest.java
+++ b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeCodecTest.java
@@ -18,6 +18,7 @@
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableList;
 import org.hamcrest.MatcherAssert;
 import org.junit.Before;
 import org.junit.Test;
@@ -29,7 +30,9 @@
 import org.onosproject.net.DeviceId;
 import org.onosproject.openstacknode.api.NodeState;
 import org.onosproject.openstacknode.api.OpenstackNode;
+import org.onosproject.openstacknode.api.OpenstackPhyInterface;
 import org.onosproject.openstacknode.impl.DefaultOpenstackNode;
+import org.onosproject.openstacknode.impl.DefaultOpenstackPhyInterface;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -51,6 +54,7 @@
 public class OpenstackNodeCodecTest {
     MockCodecContext context;
     JsonCodec<OpenstackNode> openstackNodeCodec;
+    JsonCodec<OpenstackPhyInterface> openstackPhyIntfJsonCodec;
     final CoreService mockCoreService = createMock(CoreService.class);
     private static final String REST_APP_ID = "org.onosproject.rest";
 
@@ -58,7 +62,10 @@
     public void setUp() {
         context = new MockCodecContext();
         openstackNodeCodec = new OpenstackNodeCodec();
+        openstackPhyIntfJsonCodec = new OpenstackPhyInterfaceCodec();
+
         assertThat(openstackNodeCodec, notNullValue());
+        assertThat(openstackPhyIntfJsonCodec, notNullValue());
 
         expect(mockCoreService.registerApplication(REST_APP_ID))
                 .andReturn(APP_ID).anyTimes();
@@ -68,6 +75,16 @@
 
     @Test
     public void testOpenstackNodeEncode() {
+
+        OpenstackPhyInterface phyIntf1 = DefaultOpenstackPhyInterface.builder()
+                                .network("mgmtnetwork")
+                                .intf("eth3")
+                                .build();
+        OpenstackPhyInterface phyIntf2 = DefaultOpenstackPhyInterface.builder()
+                                .network("oamnetwork")
+                                .intf("eth4")
+                                .build();
+
         OpenstackNode node = DefaultOpenstackNode.builder()
                                 .hostname("compute")
                                 .type(OpenstackNode.NodeType.COMPUTE)
@@ -76,6 +93,7 @@
                                 .intgBridge(DeviceId.deviceId("br-int"))
                                 .vlanIntf("vxlan")
                                 .dataIp(IpAddress.valueOf("20.20.20.2"))
+                                .phyIntfs(ImmutableList.of(phyIntf1, phyIntf2))
                                 .build();
 
         ObjectNode nodeJson = openstackNodeCodec.encode(node, context);
@@ -92,6 +110,16 @@
         assertThat(node.dataIp().toString(), is("172.16.130.4"));
         assertThat(node.intgBridge().toString(), is("of:00000000000000a1"));
         assertThat(node.vlanIntf(), is("eth2"));
+        assertThat(node.phyIntfs().size(), is(2));
+
+        node.phyIntfs().forEach(intf -> {
+            if (intf.network().equals("mgmtnetwork")) {
+                assertThat(intf.intf(), is("eth3"));
+            }
+            if (intf.network().equals("oamnetwork")) {
+                assertThat(intf.intf(), is("eth4"));
+            }
+        });
     }
 
     /**
@@ -133,6 +161,9 @@
         @Override
         @SuppressWarnings("unchecked")
         public <T> JsonCodec<T> codec(Class<T> entityClass) {
+            if (entityClass == OpenstackPhyInterface.class) {
+                return (JsonCodec<T>) openstackPhyIntfJsonCodec;
+            }
             return manager.getCodec(entityClass);
         }
 
diff --git a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeJsonMatcher.java b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeJsonMatcher.java
index 74382b1..b9c4464 100644
--- a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeJsonMatcher.java
+++ b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeJsonMatcher.java
@@ -20,6 +20,7 @@
 import org.hamcrest.TypeSafeDiagnosingMatcher;
 import org.onosproject.openstacknode.api.Constants;
 import org.onosproject.openstacknode.api.OpenstackNode;
+import org.onosproject.openstacknode.api.OpenstackPhyInterface;
 
 import static org.onosproject.openstacknode.api.Constants.DATA_IP;
 import static org.onosproject.openstacknode.api.Constants.MANAGEMENT_IP;
@@ -33,6 +34,7 @@
     private final OpenstackNode node;
     private static final String INTEGRATION_BRIDGE = "integrationBridge";
     private static final String STATE = "state";
+    private static final String PHYSICAL_INTERFACES = "phyIntfs";
 
     private OpenstackNodeJsonMatcher(OpenstackNode node) {
         this.node = node;
@@ -100,6 +102,32 @@
             }
         }
 
+        // check physical interfaces
+        JsonNode jsonPhyIntfs = jsonNode.get(PHYSICAL_INTERFACES);
+        if (jsonPhyIntfs != null) {
+            if (jsonPhyIntfs.size() != node.phyIntfs().size()) {
+                description.appendText("physical interface size was " + jsonPhyIntfs.size());
+                return false;
+            }
+
+            for (OpenstackPhyInterface phyIntf : node.phyIntfs()) {
+                boolean intfFound = false;
+                for (int intfIndex = 0; intfIndex < jsonPhyIntfs.size(); intfIndex++) {
+                    OpenstackPhyInterfaceJsonMatcher intfMatcher =
+                            OpenstackPhyInterfaceJsonMatcher.matchesOpenstackPhyInterface(phyIntf);
+                    if (intfMatcher.matches(jsonPhyIntfs.get(intfIndex))) {
+                        intfFound = true;
+                        break;
+                    }
+                }
+
+                if (!intfFound) {
+                    description.appendText("PhyIntf not found " + phyIntf.toString());
+                    return false;
+                }
+            }
+        }
+
         return true;
     }
 
diff --git a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackPhyInterfaceJsonMatcher.java b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackPhyInterfaceJsonMatcher.java
new file mode 100644
index 0000000..09955bd
--- /dev/null
+++ b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackPhyInterfaceJsonMatcher.java
@@ -0,0 +1,70 @@
+/*
+ * 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.openstacknode.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.openstacknode.api.OpenstackPhyInterface;
+
+public final class OpenstackPhyInterfaceJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+    private final OpenstackPhyInterface phyIntf;
+    private static final String NETWORK = "network";
+    private static final String INTERFACE = "intf";
+
+    private OpenstackPhyInterfaceJsonMatcher(OpenstackPhyInterface phyIntf) {
+        this.phyIntf = phyIntf;
+    }
+
+    @Override
+    protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+
+        // check network name
+        String jsonNetwork = jsonNode.get(NETWORK).asText();
+        String network = phyIntf.network();
+        if (!jsonNetwork.equals(network)) {
+            description.appendText("network name was " + jsonNetwork);
+            return false;
+        }
+
+        // check interface name
+        String jsonIntf = jsonNode.get(INTERFACE).asText();
+        String intf = phyIntf.intf();
+        if (!jsonIntf.equals(intf)) {
+            description.appendText("interface name was " + jsonIntf);
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        description.appendText(phyIntf.toString());
+    }
+
+    /**
+     * Factory to allocate an openstack physical interface matcher.
+     *
+     * @param phyIntf openstack physical interface object we are looking for
+     * @return matcher
+     */
+    public static OpenstackPhyInterfaceJsonMatcher
+                    matchesOpenstackPhyInterface(OpenstackPhyInterface phyIntf) {
+        return new OpenstackPhyInterfaceJsonMatcher(phyIntf);
+    }
+}
diff --git a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackNodeHandlerTest.java b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackNodeHandlerTest.java
index 9c55df3..fccfd1b 100644
--- a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackNodeHandlerTest.java
+++ b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackNodeHandlerTest.java
@@ -18,6 +18,7 @@
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
 import com.google.common.util.concurrent.MoreExecutors;
 import org.junit.After;
 import org.junit.Before;
@@ -69,6 +70,7 @@
 import org.onosproject.net.provider.ProviderId;
 import org.onosproject.openstacknode.api.NodeState;
 import org.onosproject.openstacknode.api.OpenstackNode;
+import org.onosproject.openstacknode.api.OpenstackPhyInterface;
 import org.onosproject.ovsdb.controller.OvsdbClientService;
 import org.onosproject.ovsdb.controller.OvsdbController;
 
@@ -144,6 +146,10 @@
 
     private static final String GATEWAY_UPLINK_PORT = "eth0";
 
+    private static final Set<OpenstackPhyInterface> COMPUTE_1_PHY_INTFS = createPhyIntfs();
+    private static final Set<OpenstackPhyInterface> COMPUTE_2_PHY_INTFS = createPhyIntfs();
+    private static final Set<OpenstackPhyInterface> COMPUTE_3_PHY_INTFS = createPhyIntfs();
+
     private static final Device COMPUTE_1_INTG_DEVICE = createOpenFlowDevice(1, INTEGRATION_BRIDGE);
     private static final Device COMPUTE_2_INTG_DEVICE = createOpenFlowDevice(2, INTEGRATION_BRIDGE);
     private static final Device COMPUTE_3_INTG_DEVICE = createOpenFlowDevice(3, INTEGRATION_BRIDGE);
@@ -161,7 +167,8 @@
             COMPUTE,
             COMPUTE_1_INTG_DEVICE,
             COMPUTE_1_IP,
-            INIT
+            INIT,
+            COMPUTE_1_PHY_INTFS
     );
 
     private static final OpenstackNode COMPUTE_2 = createNode(
@@ -169,7 +176,8 @@
             COMPUTE,
             COMPUTE_2_INTG_DEVICE,
             COMPUTE_2_IP,
-            DEVICE_CREATED
+            DEVICE_CREATED,
+            COMPUTE_2_PHY_INTFS
     );
 
     private static final OpenstackNode COMPUTE_3 = createNode(
@@ -177,10 +185,11 @@
             COMPUTE,
             COMPUTE_3_INTG_DEVICE,
             COMPUTE_3_IP,
-            COMPLETE
+            COMPLETE,
+            COMPUTE_3_PHY_INTFS
     );
 
-    private static final OpenstackNode GATEWAY_1 = createNode(
+    private static final OpenstackNode GATEWAY_1 = createGatewayNode(
             GATEWAY_1_HOSTNAME,
             GATEWAY,
             GATEWAY_1_INTG_DEVICE,
@@ -189,7 +198,7 @@
             INIT
     );
 
-    private static final OpenstackNode GATEWAY_2 = createNode(
+    private static final OpenstackNode GATEWAY_2 = createGatewayNode(
             GATEWAY_2_HOSTNAME,
             GATEWAY,
             GATEWAY_2_INTG_DEVICE,
@@ -198,7 +207,7 @@
             DEVICE_CREATED
     );
 
-    private static final OpenstackNode GATEWAY_3 = createNode(
+    private static final OpenstackNode GATEWAY_3 = createGatewayNode(
             GATEWAY_3_HOSTNAME,
             GATEWAY,
             GATEWAY_3_INTG_DEVICE,
@@ -382,21 +391,26 @@
                 DefaultAnnotations.builder().set(PORT_NAME, portName).build());
     }
 
+    private static Set<OpenstackPhyInterface> createPhyIntfs() {
+        return Sets.newConcurrentHashSet();
+    }
+
     private static OpenstackNode createNode(String hostname,
                                             OpenstackNode.NodeType type,
                                             Device intgBridge,
                                             IpAddress ipAddr,
-                                            NodeState state) {
+                                            NodeState state,
+                                            Set<OpenstackPhyInterface> phyIntfs) {
         return new TestOpenstackNode(
                 hostname,
                 type,
                 intgBridge.id(),
                 ipAddr,
                 ipAddr,
-                null, null, state);
+                null, null, state, phyIntfs);
     }
 
-    private static OpenstackNode createNode(String hostname,
+    private static OpenstackNode createGatewayNode(String hostname,
                                             OpenstackNode.NodeType type,
                                             Device intgBridge,
                                             IpAddress ipAddr,
@@ -408,7 +422,7 @@
                 intgBridge.id(),
                 ipAddr,
                 ipAddr,
-                null, uplinkPort, state);
+                null, uplinkPort, state, null);
     }
 
     private static final class TestDevice extends DefaultDevice {
@@ -465,7 +479,8 @@
                                   IpAddress dataIp,
                                   String vlanIntf,
                                   String uplinkPort,
-                                  NodeState state) {
+                                  NodeState state,
+                                  Set<OpenstackPhyInterface> phyIntfs) {
             super(hostname,
                     type,
                     intgBridge,
@@ -473,7 +488,8 @@
                     dataIp,
                     vlanIntf,
                     uplinkPort,
-                    state);
+                    state,
+                    phyIntfs);
         }
 
         @Override
diff --git a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackPhyInterfaceTest.java b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackPhyInterfaceTest.java
new file mode 100644
index 0000000..55a7077
--- /dev/null
+++ b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackPhyInterfaceTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.openstacknode.impl;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+import org.onosproject.openstacknode.api.OpenstackPhyInterface;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+/**
+ * Unit tests for DefaultOpenstackPhyInterface.
+ */
+public class DefaultOpenstackPhyInterfaceTest {
+
+    private static final String NETWORK_1 = "mgmtnetwork";
+    private static final String NETWORK_2 = "oamnetwork";
+    private static final String INTERFACE_1 = "eth3";
+    private static final String INTERFACE_2 = "eth4";
+
+    private static final OpenstackPhyInterface OS_PHY_INTF_1 =
+            new DefaultOpenstackPhyInterface(NETWORK_1, INTERFACE_1);
+    private static final OpenstackPhyInterface OS_PHY_INTF_2 =
+            new DefaultOpenstackPhyInterface(NETWORK_1, INTERFACE_1);
+    private static final OpenstackPhyInterface OS_PHY_INTF_3 =
+            new DefaultOpenstackPhyInterface(NETWORK_2, INTERFACE_2);
+
+    @Test
+    public void testEquality() {
+        new EqualsTester().addEqualityGroup(OS_PHY_INTF_1, OS_PHY_INTF_2)
+                .addEqualityGroup(OS_PHY_INTF_3)
+                .testEquals();
+    }
+
+    @Test
+    public void testConstruction() {
+        DefaultOpenstackPhyInterface phyIntf = (DefaultOpenstackPhyInterface)
+                OS_PHY_INTF_1;
+
+        assertThat(phyIntf.network(), is(NETWORK_1));
+        assertThat(phyIntf.intf(), is(INTERFACE_1));
+    }
+}
\ No newline at end of file