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

Change-Id: I5d746e9b4fa6015dbaec90d27ea7e1a7fa105e31
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackNodeCodec.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackNodeCodec.java
index 8bae1ea..3c5feba 100644
--- a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackNodeCodec.java
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackNodeCodec.java
@@ -15,6 +15,8 @@
  */
 package org.onosproject.openstacknode.codec;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.onlab.packet.IpAddress;
 import org.onosproject.codec.CodecContext;
@@ -22,9 +24,14 @@
 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.slf4j.Logger;
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.IntStream;
+
 import static com.google.common.base.Preconditions.checkNotNull;
 import static org.onlab.util.Tools.nullIsIllegal;
 import static org.onosproject.openstacknode.api.Constants.DATA_IP;
@@ -45,6 +52,7 @@
     private static final String TYPE = "type";
     private static final String INTEGRATION_BRIDGE = "integrationBridge";
     private static final String STATE = "state";
+    private static final String PHYSICAL_INTERFACES = "phyIntfs";
 
     private static final String MISSING_MESSAGE = " is required in OpenstackNode";
 
@@ -76,6 +84,13 @@
         // TODO: need to find a way to not refer to ServiceDirectory from
         // DefaultOpenstackNode
 
+        ArrayNode phyIntfs = context.mapper().createArrayNode();
+        node.phyIntfs().forEach(phyIntf -> {
+            ObjectNode phyIntfJson = context.codec(OpenstackPhyInterface.class).encode(phyIntf, context);
+            phyIntfs.add(phyIntfJson);
+        });
+        result.set(PHYSICAL_INTERFACES, phyIntfs);
+
         return result;
     }
 
@@ -85,6 +100,8 @@
             return null;
         }
 
+        final JsonCodec<OpenstackPhyInterface> phyIntfCodec = context.codec(OpenstackPhyInterface.class);
+
         String hostname = nullIsIllegal(json.get(HOST_NAME).asText(),
                 HOST_NAME + MISSING_MESSAGE);
         String type = nullIsIllegal(json.get(TYPE).asText(),
@@ -112,6 +129,17 @@
             nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
         }
 
+        // parse physical interfaces
+        List<OpenstackPhyInterface> phyIntfs = new ArrayList<>();
+        JsonNode phyIntfsJson = json.get(PHYSICAL_INTERFACES);
+        if (phyIntfsJson != null) {
+            IntStream.range(0, phyIntfsJson.size()).forEach(i -> {
+                ObjectNode intfJson = get(phyIntfsJson, i);
+                phyIntfs.add(phyIntfCodec.decode(intfJson, context));
+            });
+        }
+        nodeBuilder.phyIntfs(phyIntfs);
+
         log.trace("node is {}", nodeBuilder.build().toString());
 
         return nodeBuilder.build();
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackPhyInterfaceCodec.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackPhyInterfaceCodec.java
new file mode 100644
index 0000000..340a76d
--- /dev/null
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackPhyInterfaceCodec.java
@@ -0,0 +1,66 @@
+/*
+ * 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.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.openstacknode.api.OpenstackPhyInterface;
+import org.onosproject.openstacknode.impl.DefaultOpenstackPhyInterface;
+import org.slf4j.Logger;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Openstack physical interface codec used for serializing and de-serializing JSON string.
+ */
+public final class OpenstackPhyInterfaceCodec extends JsonCodec<OpenstackPhyInterface> {
+
+    private final Logger log = getLogger(getClass());
+
+    private static final String NETWORK = "network";
+    private static final String INTERFACE = "intf";
+
+    private static final String MISSING_MESSAGE = " is required in OpenstackPhyInterface";
+
+    @Override
+    public ObjectNode encode(OpenstackPhyInterface phyIntf, CodecContext context) {
+        checkNotNull(phyIntf, "Openstack physical interface cannot be null");
+
+        return context.mapper().createObjectNode()
+                .put(NETWORK, phyIntf.network())
+                .put(INTERFACE, phyIntf.intf());
+    }
+
+    @Override
+    public OpenstackPhyInterface decode(ObjectNode json, CodecContext context) {
+        if (json == null || !json.isObject()) {
+            return null;
+        }
+
+        String network = nullIsIllegal(json.get(NETWORK).asText(),
+                NETWORK + MISSING_MESSAGE);
+        String intf = nullIsIllegal(json.get(INTERFACE).asText(),
+                INTERFACE + MISSING_MESSAGE);
+
+        return DefaultOpenstackPhyInterface.builder()
+                        .network(network)
+                        .intf(intf)
+                        .build();
+    }
+}
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/impl/DefaultOpenstackNode.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/impl/DefaultOpenstackNode.java
index 8927880..50f5d53 100644
--- a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/impl/DefaultOpenstackNode.java
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/impl/DefaultOpenstackNode.java
@@ -29,7 +29,10 @@
 import org.onosproject.net.group.GroupKey;
 import org.onosproject.openstacknode.api.NodeState;
 import org.onosproject.openstacknode.api.OpenstackNode;
+import org.onosproject.openstacknode.api.OpenstackPhyInterface;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Objects;
 
 import static com.google.common.base.Preconditions.checkArgument;
@@ -51,6 +54,7 @@
     private final String vlanIntf;
     private final String uplinkPort;
     private final NodeState state;
+    private final Collection<OpenstackPhyInterface> phyIntfs;
 
     private static final String NOT_NULL_MSG = "Node % cannot be null";
 
@@ -67,6 +71,7 @@
      * @param vlanIntf      VLAN interface
      * @param uplinkPort    uplink port name
      * @param state         node state
+     * @param phyIntfs      physical interfaces
      */
     protected DefaultOpenstackNode(String hostname, NodeType type,
                                    DeviceId intgBridge,
@@ -74,7 +79,8 @@
                                    IpAddress dataIp,
                                    String vlanIntf,
                                    String uplinkPort,
-                                   NodeState state) {
+                                   NodeState state,
+                                   Collection<OpenstackPhyInterface> phyIntfs) {
         this.hostname = hostname;
         this.type = type;
         this.intgBridge = intgBridge;
@@ -83,6 +89,7 @@
         this.vlanIntf = vlanIntf;
         this.uplinkPort = uplinkPort;
         this.state = state;
+        this.phyIntfs = phyIntfs;
     }
 
     @Override
@@ -220,7 +227,8 @@
                     Objects.equals(managementIp, that.managementIp) &&
                     Objects.equals(dataIp, that.dataIp) &&
                     Objects.equals(uplinkPort, that.uplinkPort) &&
-                    Objects.equals(vlanIntf, that.vlanIntf);
+                    Objects.equals(vlanIntf, that.vlanIntf) &&
+                    Objects.equals(phyIntfs, that.phyIntfs);
         }
         return false;
     }
@@ -233,7 +241,8 @@
                 managementIp,
                 dataIp,
                 vlanIntf,
-                uplinkPort);
+                uplinkPort,
+                phyIntfs);
     }
 
     @Override
@@ -247,6 +256,7 @@
                 .add("vlanIntf", vlanIntf)
                 .add("uplinkPort", uplinkPort)
                 .add("state", state)
+                .add("phyIntfs", phyIntfs)
                 .toString();
     }
 
@@ -261,9 +271,20 @@
                 .vlanIntf(vlanIntf)
                 .uplinkPort(uplinkPort)
                 .state(newState)
+                .phyIntfs(phyIntfs)
                 .build();
     }
 
+    @Override
+    public Collection<OpenstackPhyInterface> phyIntfs() {
+
+        if (phyIntfs == null) {
+            return new ArrayList<>();
+        }
+
+        return phyIntfs;
+    }
+
     /**
      * Returns new builder instance.
      *
@@ -288,7 +309,8 @@
                 .dataIp(osNode.dataIp())
                 .vlanIntf(osNode.vlanIntf())
                 .uplinkPort(osNode.uplinkPort())
-                .state(osNode.state());
+                .state(osNode.state())
+                .phyIntfs(osNode.phyIntfs());
     }
 
     /**
@@ -304,6 +326,7 @@
         private String vlanIntf;
         private String uplinkPort;
         private NodeState state;
+        private Collection<OpenstackPhyInterface> phyIntfs;
 
         // private constructor not intended to use from external
         private Builder() {
@@ -331,7 +354,8 @@
                     dataIp,
                     vlanIntf,
                     uplinkPort,
-                    state);
+                    state,
+                    phyIntfs);
         }
 
         @Override
@@ -383,6 +407,12 @@
             this.state = state;
             return this;
         }
+
+        @Override
+        public Builder phyIntfs(Collection<OpenstackPhyInterface> phyIntfs) {
+            this.phyIntfs = phyIntfs;
+            return this;
+        }
     }
 }
 
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/impl/DefaultOpenstackNodeHandler.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/impl/DefaultOpenstackNodeHandler.java
index 939b628..025757e 100644
--- a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/impl/DefaultOpenstackNodeHandler.java
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/impl/DefaultOpenstackNodeHandler.java
@@ -59,6 +59,7 @@
 import org.onosproject.openstacknode.api.OpenstackNodeHandler;
 import org.onosproject.openstacknode.api.OpenstackNodeListener;
 import org.onosproject.openstacknode.api.OpenstackNodeService;
+import org.onosproject.openstacknode.api.OpenstackPhyInterface;
 import org.onosproject.ovsdb.controller.OvsdbClientService;
 import org.onosproject.ovsdb.controller.OvsdbController;
 import org.onosproject.ovsdb.controller.OvsdbNodeId;
@@ -210,6 +211,13 @@
                     !isIntfEnabled(osNode, osNode.vlanIntf())) {
                 addSystemInterface(osNode, INTEGRATION_BRIDGE, osNode.vlanIntf());
             }
+
+            osNode.phyIntfs().forEach(i -> {
+                if (!isIntfEnabled(osNode, i.intf())) {
+                    addSystemInterface(osNode, INTEGRATION_BRIDGE, i.intf());
+                }
+            });
+
         } catch (Exception e) {
             log.error("Exception occurred because of {}", e.toString());
         }
@@ -384,6 +392,13 @@
                         !isIntfEnabled(osNode, osNode.uplinkPort())) {
                     return false;
                 }
+
+                for (OpenstackPhyInterface intf : osNode.phyIntfs()) {
+                    if (intf != null && !isIntfEnabled(osNode, intf.intf())) {
+                        return false;
+                    }
+                }
+
                 return true;
             case COMPLETE:
             case INCOMPLETE:
@@ -508,7 +523,8 @@
                         if (osNode.state() == DEVICE_CREATED && (
                                 Objects.equals(portName, DEFAULT_TUNNEL) ||
                                 Objects.equals(portName, osNode.vlanIntf()) ||
-                                Objects.equals(portName, osNode.uplinkPort()))) {
+                                Objects.equals(portName, osNode.uplinkPort()) ||
+                                        containsPhyIntf(osNode, portName))) {
                             log.debug("Interface {} added to {}", portName, event.subject().id());
                             bootstrapNode(osNode);
                         }
@@ -521,7 +537,8 @@
                         if (osNode.state() == COMPLETE && (
                                 Objects.equals(portName, DEFAULT_TUNNEL) ||
                                 Objects.equals(portName, osNode.vlanIntf()) ||
-                                        Objects.equals(portName, osNode.uplinkPort()))) {
+                                        Objects.equals(portName, osNode.uplinkPort()) ||
+                                        containsPhyIntf(osNode, portName))) {
                             log.warn("Interface {} removed from {}", portName, event.subject().id());
                             setState(osNode, INCOMPLETE);
                         }
@@ -537,6 +554,24 @@
     }
 
     /**
+     * Checks whether the openstack node contains the given physical interface.
+     *
+     * @param osNode openstack node
+     * @param portName physical interface
+     * @return true if openstack node contains the given physical interface,
+     *          false otherwise
+     */
+    private boolean containsPhyIntf(OpenstackNode osNode, String portName) {
+        for (OpenstackPhyInterface phyIntf : osNode.phyIntfs()) {
+            if (Objects.equals(portName, phyIntf.intf())) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    /**
      * An internal openstack node listener.
      * The notification is triggered by OpenstackNodeStore.
      */
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/impl/DefaultOpenstackPhyInterface.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/impl/DefaultOpenstackPhyInterface.java
new file mode 100644
index 0000000..f4238a3
--- /dev/null
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/impl/DefaultOpenstackPhyInterface.java
@@ -0,0 +1,121 @@
+/*
+ * 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.base.MoreObjects;
+import org.onosproject.openstacknode.api.OpenstackPhyInterface;
+
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+public class DefaultOpenstackPhyInterface implements OpenstackPhyInterface {
+
+    private final String network;
+    private final String intf;
+
+    private static final String NOT_NULL_MSG = "% cannot be null";
+
+    /**
+     * A default constructor of Openstack physical interface.
+     *
+     * @param network   network that this physical interface connects with
+     * @param intf      name of physical interface
+     */
+    protected DefaultOpenstackPhyInterface(String network, String intf) {
+        this.network = network;
+        this.intf = intf;
+    }
+
+    @Override
+    public String network() {
+        return network;
+    }
+
+    @Override
+    public String intf() {
+        return intf;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+
+        if (obj instanceof DefaultOpenstackPhyInterface) {
+            DefaultOpenstackPhyInterface that = (DefaultOpenstackPhyInterface) obj;
+            return Objects.equals(network, that.network) &&
+                    Objects.equals(intf, that.intf);
+        }
+        return false;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(network, intf);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .add("network", network)
+                .add("intf", intf)
+                .toString();
+    }
+
+    /**
+     * Returns new builder instance.
+     *
+     * @return openstack physical interface builder
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    /**
+     * A builder class for openstack physical interface.
+     */
+    public static final class Builder implements OpenstackPhyInterface.Builder {
+
+        private String network;
+        private String intf;
+
+        // private constructor not intended to use from external
+        private Builder() {
+        }
+
+        @Override
+        public OpenstackPhyInterface build() {
+            checkArgument(network != null, NOT_NULL_MSG, "network");
+            checkArgument(intf != null, NOT_NULL_MSG, "intf");
+
+            return new DefaultOpenstackPhyInterface(network, intf);
+        }
+
+        @Override
+        public OpenstackPhyInterface.Builder network(String network) {
+            this.network = network;
+            return this;
+        }
+
+        @Override
+        public OpenstackPhyInterface.Builder intf(String intf) {
+            this.intf = intf;
+            return this;
+        }
+    }
+}
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/impl/DistributedOpenstackNodeStore.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/impl/DistributedOpenstackNodeStore.java
index 0b67d73..d3c2ce4 100644
--- a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/impl/DistributedOpenstackNodeStore.java
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/impl/DistributedOpenstackNodeStore.java
@@ -30,6 +30,7 @@
 import org.onosproject.openstacknode.api.OpenstackNodeEvent;
 import org.onosproject.openstacknode.api.OpenstackNodeStore;
 import org.onosproject.openstacknode.api.OpenstackNodeStoreDelegate;
+import org.onosproject.openstacknode.api.OpenstackPhyInterface;
 import org.onosproject.store.AbstractStore;
 import org.onosproject.store.serializers.KryoNamespaces;
 import org.onosproject.store.service.ConsistentMap;
@@ -40,6 +41,7 @@
 import org.onosproject.store.service.Versioned;
 import org.slf4j.Logger;
 
+import java.util.Collection;
 import java.util.Set;
 import java.util.concurrent.ExecutorService;
 
@@ -75,6 +77,9 @@
             .register(DefaultOpenstackNode.class)
             .register(OpenstackNode.NodeType.class)
             .register(NodeState.class)
+            .register(OpenstackPhyInterface.class)
+            .register(DefaultOpenstackPhyInterface.class)
+            .register(Collection.class)
             .build();
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeCodecRegister.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeCodecRegister.java
index 70df974..420e87a 100644
--- a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeCodecRegister.java
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeCodecRegister.java
@@ -22,7 +22,9 @@
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.onosproject.codec.CodecService;
 import org.onosproject.openstacknode.api.OpenstackNode;
+import org.onosproject.openstacknode.api.OpenstackPhyInterface;
 import org.onosproject.openstacknode.codec.OpenstackNodeCodec;
+import org.onosproject.openstacknode.codec.OpenstackPhyInterfaceCodec;
 
 import static org.slf4j.LoggerFactory.getLogger;
 
@@ -40,6 +42,7 @@
     @Activate
     protected void activate() {
         codecService.registerCodec(OpenstackNode.class, new OpenstackNodeCodec());
+        codecService.registerCodec(OpenstackPhyInterface.class, new OpenstackPhyInterfaceCodec());
 
         log.info("Started");
     }
@@ -47,6 +50,7 @@
     @Deactivate
     protected void deactivate() {
         codecService.unregisterCodec(OpenstackNode.class);
+        codecService.unregisterCodec(OpenstackPhyInterface.class);
 
         log.info("Stopped");
     }