[WIP] CORD-129 Support access agent in VTN

- Add ACCESS_AGENT type instance based on the access agent location
- Populate flow rules to provider L2 connectivity with OLTs
- Don't check "tap" port name prefix considering container instance
- Changed table name ACCESS_TYPE to ACCESS, DST_IP to DST

Change-Id: Ibcf7ea97cd5e16e15d6deff1c8579a83ac9e13f8
diff --git a/src/main/java/org/opencord/cordvtn/impl/handler/AccessAgentInstanceHandler.java b/src/main/java/org/opencord/cordvtn/impl/handler/AccessAgentInstanceHandler.java
new file mode 100644
index 0000000..b2de2c3
--- /dev/null
+++ b/src/main/java/org/opencord/cordvtn/impl/handler/AccessAgentInstanceHandler.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.opencord.cordvtn.impl.handler;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.net.flow.DefaultFlowRule;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.opencord.cordvtn.impl.AbstractInstanceHandler;
+import org.opencord.cordvtn.api.Instance;
+import org.opencord.cordvtn.api.InstanceHandler;
+import org.opencord.cordvtn.impl.CordVtnNodeManager;
+import org.opencord.cordvtn.impl.CordVtnPipeline;
+
+import java.util.Optional;
+
+import static org.onosproject.xosclient.api.VtnServiceApi.ServiceType.ACCESS_AGENT;
+
+/**
+ * Provides network connectivity for access agent instances.
+ */
+@Component(immediate = true)
+public class AccessAgentInstanceHandler extends AbstractInstanceHandler implements InstanceHandler {
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CordVtnPipeline pipeline;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CordVtnNodeManager nodeManager;
+
+    @Activate
+    protected void activate() {
+        serviceType = Optional.of(ACCESS_AGENT);
+        super.activate();
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        super.deactivate();
+    }
+
+    @Override
+    public void instanceDetected(Instance instance) {
+        log.info("Access agent instance detected {}", instance);
+        accessAgentRules(instance, true);
+    }
+
+    @Override
+    public void instanceRemoved(Instance instance) {
+        log.info("Access agent instance removed {}", instance);
+        accessAgentRules(instance, false);
+    }
+
+    private void accessAgentRules(Instance instance, boolean install) {
+        TrafficSelector selector = DefaultTrafficSelector.builder()
+                .matchEthDst(instance.mac())
+                .build();
+
+        TrafficTreatment treatment = DefaultTrafficTreatment.builder()
+                .setOutput(instance.portNumber())
+                .build();
+
+        FlowRule flowRule = DefaultFlowRule.builder()
+                .fromApp(appId)
+                .withSelector(selector)
+                .withTreatment(treatment)
+                .withPriority(CordVtnPipeline.PRIORITY_DEFAULT)
+                .forDevice(instance.deviceId())
+                .forTable(CordVtnPipeline.TABLE_DST)
+                .makePermanent()
+                .build();
+
+        pipeline.processFlowRule(install, flowRule);
+
+        selector = DefaultTrafficSelector.builder()
+                .matchInPort(instance.portNumber())
+                .build();
+
+        treatment = DefaultTrafficTreatment.builder()
+                .setOutput(nodeManager.dataPort(instance.deviceId()))
+                .build();
+
+        flowRule = DefaultFlowRule.builder()
+                .fromApp(appId)
+                .withSelector(selector)
+                .withTreatment(treatment)
+                .withPriority(CordVtnPipeline.PRIORITY_DEFAULT)
+                .forDevice(instance.deviceId())
+                .forTable(CordVtnPipeline.TABLE_IN_PORT)
+                .makePermanent()
+                .build();
+
+        pipeline.processFlowRule(install, flowRule);
+    }
+}
diff --git a/src/main/java/org/opencord/cordvtn/impl/handler/DefaultInstanceHandler.java b/src/main/java/org/opencord/cordvtn/impl/handler/DefaultInstanceHandler.java
index a4ca067..0036578 100644
--- a/src/main/java/org/opencord/cordvtn/impl/handler/DefaultInstanceHandler.java
+++ b/src/main/java/org/opencord/cordvtn/impl/handler/DefaultInstanceHandler.java
@@ -115,7 +115,7 @@
                 .build();
 
         TrafficTreatment treatment = DefaultTrafficTreatment.builder()
-                .transition(CordVtnPipeline.TABLE_ACCESS_TYPE)
+                .transition(CordVtnPipeline.TABLE_ACCESS)
                 .build();
 
         FlowRule flowRule = DefaultFlowRule.builder()
@@ -170,7 +170,7 @@
                 .withTreatment(treatment)
                 .withPriority(CordVtnPipeline.PRIORITY_DEFAULT)
                 .forDevice(instance.deviceId())
-                .forTable(CordVtnPipeline.TABLE_DST_IP)
+                .forTable(CordVtnPipeline.TABLE_DST)
                 .makePermanent()
                 .build();
 
@@ -200,7 +200,7 @@
                     .withTreatment(treatment)
                     .withPriority(CordVtnPipeline.PRIORITY_DEFAULT)
                     .forDevice(node.integrationBridgeId())
-                    .forTable(CordVtnPipeline.TABLE_DST_IP)
+                    .forTable(CordVtnPipeline.TABLE_DST)
                     .makePermanent()
                     .build();
 
@@ -239,7 +239,7 @@
                 .build();
 
         TrafficTreatment treatment = DefaultTrafficTreatment.builder()
-                .transition(CordVtnPipeline.TABLE_DST_IP)
+                .transition(CordVtnPipeline.TABLE_DST)
                 .build();
 
 
@@ -250,7 +250,7 @@
                     .withTreatment(treatment)
                     .withPriority(CordVtnPipeline.PRIORITY_DEFAULT)
                     .forDevice(node.integrationBridgeId())
-                    .forTable(CordVtnPipeline.TABLE_ACCESS_TYPE)
+                    .forTable(CordVtnPipeline.TABLE_ACCESS)
                     .makePermanent()
                     .build();
 
@@ -275,7 +275,7 @@
                     .withTreatment(treatment)
                     .withPriority(CordVtnPipeline.PRIORITY_LOW)
                     .forDevice(node.integrationBridgeId())
-                    .forTable(CordVtnPipeline.TABLE_ACCESS_TYPE)
+                    .forTable(CordVtnPipeline.TABLE_ACCESS)
                     .makePermanent()
                     .build();
 
diff --git a/src/main/java/org/opencord/cordvtn/impl/handler/ManagementInstanceHandler.java b/src/main/java/org/opencord/cordvtn/impl/handler/ManagementInstanceHandler.java
index 7a77b98..2bcbcab 100644
--- a/src/main/java/org/opencord/cordvtn/impl/handler/ManagementInstanceHandler.java
+++ b/src/main/java/org/opencord/cordvtn/impl/handler/ManagementInstanceHandler.java
@@ -123,7 +123,7 @@
                 .withTreatment(treatment)
                 .withPriority(CordVtnPipeline.PRIORITY_DEFAULT)
                 .forDevice(instance.deviceId())
-                .forTable(CordVtnPipeline.TABLE_DST_IP)
+                .forTable(CordVtnPipeline.TABLE_DST)
                 .makePermanent()
                 .build();
 
@@ -173,7 +173,7 @@
                 .withTreatment(treatment)
                 .withPriority(CordVtnPipeline.PRIORITY_DEFAULT)
                 .forDevice(instance.deviceId())
-                .forTable(CordVtnPipeline.TABLE_DST_IP)
+                .forTable(CordVtnPipeline.TABLE_DST)
                 .makePermanent()
                 .build();
 
diff --git a/src/main/java/org/opencord/cordvtn/impl/handler/OltAgentInstanceHandler.java b/src/main/java/org/opencord/cordvtn/impl/handler/OltAgentInstanceHandler.java
deleted file mode 100644
index 853963e..0000000
--- a/src/main/java/org/opencord/cordvtn/impl/handler/OltAgentInstanceHandler.java
+++ /dev/null
@@ -1,116 +0,0 @@
-/*
- * Copyright 2016-present Open Networking Laboratory
- *
- * 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.opencord.cordvtn.impl.handler;
-
-import com.google.common.collect.Maps;
-import org.apache.felix.scr.annotations.Activate;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Deactivate;
-
-import org.opencord.cordvtn.impl.AbstractInstanceHandler;
-import org.opencord.cordvtn.api.CordVtnConfig;
-import org.opencord.cordvtn.api.Instance;
-import org.opencord.cordvtn.api.InstanceHandler;
-import org.onosproject.net.DeviceId;
-
-import org.onosproject.net.config.ConfigFactory;
-import org.onosproject.net.config.NetworkConfigEvent;
-import org.onosproject.net.config.NetworkConfigListener;
-import org.onosproject.net.config.basics.SubjectFactories;
-import org.opencord.cordconfig.access.AccessAgentConfig;
-import org.opencord.cordconfig.access.AccessAgentData;
-
-import java.util.Map;
-import java.util.Optional;
-import java.util.Set;
-
-import static org.onosproject.xosclient.api.VtnServiceApi.ServiceType.OLT_AGENT;
-
-/**
- * Provides network connectivity for OLT agent instances.
- */
-@Component(immediate = true)
-public class OltAgentInstanceHandler extends AbstractInstanceHandler implements InstanceHandler {
-
-    private static final Class<AccessAgentConfig> CONFIG_CLASS = AccessAgentConfig.class;
-    private ConfigFactory<DeviceId, AccessAgentConfig> configFactory =
-            new ConfigFactory<DeviceId, AccessAgentConfig>(
-                    SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessAgent") {
-                @Override
-                public AccessAgentConfig createConfig() {
-                    return new AccessAgentConfig();
-                }
-            };
-
-    private Map<DeviceId, AccessAgentData> oltAgentData = Maps.newConcurrentMap();
-
-    @Activate
-    protected void activate() {
-        serviceType = Optional.of(OLT_AGENT);
-        configListener = new InternalConfigListener();
-        super.activate();
-
-        configRegistry.registerConfigFactory(configFactory);
-    }
-
-    @Deactivate
-    protected void deactivate() {
-        super.deactivate();
-    }
-
-    @Override
-    public void instanceDetected(Instance instance) {
-        log.info("OLT agent instance detected {}", instance);
-        // TODO implement
-    }
-
-    @Override
-    public void instanceRemoved(Instance instance) {
-        log.info("OLT agent instance removed {}", instance);
-        // TODO implement
-    }
-
-    private void readAccessAgentConfig() {
-
-        Set<DeviceId> deviceSubjects = configRegistry.getSubjects(DeviceId.class, CONFIG_CLASS);
-        deviceSubjects.stream().forEach(subject -> {
-            AccessAgentConfig config = configRegistry.getConfig(subject, CONFIG_CLASS);
-            if (config != null) {
-                oltAgentData.put(subject, config.getAgent());
-            }
-        });
-    }
-
-    private class InternalConfigListener implements NetworkConfigListener {
-
-        @Override
-        public void event(NetworkConfigEvent event) {
-
-            switch (event.type()) {
-                case CONFIG_UPDATED:
-                case CONFIG_ADDED:
-                    if (event.configClass().equals(CordVtnConfig.class)) {
-                        readConfiguration();
-                    } else if (event.configClass().equals(CONFIG_CLASS)) {
-                        readAccessAgentConfig();
-                    }
-                    break;
-                default:
-                    break;
-            }
-        }
-    }
-}
diff --git a/src/main/java/org/opencord/cordvtn/impl/handler/VsgInstanceHandler.java b/src/main/java/org/opencord/cordvtn/impl/handler/VsgInstanceHandler.java
index 648c341..298d60a 100644
--- a/src/main/java/org/opencord/cordvtn/impl/handler/VsgInstanceHandler.java
+++ b/src/main/java/org/opencord/cordvtn/impl/handler/VsgInstanceHandler.java
@@ -297,7 +297,7 @@
                     .withTreatment(downstreamTreatment)
                     .withPriority(CordVtnPipeline.PRIORITY_DEFAULT)
                     .forDevice(vsgVm.deviceId())
-                    .forTable(CordVtnPipeline.TABLE_DST_IP)
+                    .forTable(CordVtnPipeline.TABLE_DST)
                     .makePermanent()
                     .build();