[ONOS-3114,3115] SFC Service and Manager Implementation

Change-Id: Ifaf8603e3558900f36fe2efec3975fcc5225d8b2
diff --git a/apps/vtn/sfcmgr/src/main/java/org/onosproject/sfc/manager/SfcService.java b/apps/vtn/sfcmgr/src/main/java/org/onosproject/sfc/manager/SfcService.java
index ef5fc52..4af2d47 100644
--- a/apps/vtn/sfcmgr/src/main/java/org/onosproject/sfc/manager/SfcService.java
+++ b/apps/vtn/sfcmgr/src/main/java/org/onosproject/sfc/manager/SfcService.java
@@ -15,6 +15,11 @@
  */
 package org.onosproject.sfc.manager;
 
+import org.onosproject.vtnrsc.PortPair;
+import org.onosproject.vtnrsc.PortPairGroup;
+import org.onosproject.vtnrsc.FlowClassifier;
+import org.onosproject.vtnrsc.PortChain;
+
 /**
  * SFC application that applies flows to the device.
  */
@@ -23,48 +28,64 @@
     /**
      * When port-pair is created, check whether Forwarding Rule needs to be
      * updated in OVS.
+     *
+     * @param portPair port-pair
      */
-    public void onPortPairCreated();
+    void onPortPairCreated(PortPair portPair);
 
     /**
      * When port-pair is deleted, check whether Forwarding Rule needs to be
      * updated in OVS.
+     *
+     * @param portPair port-pair
      */
-    public void onPortPairDeleted();
+    void onPortPairDeleted(PortPair portPair);
 
     /**
      * When port-pair-group is created, check whether Forwarding Rule needs to
      * be updated in OVS.
+     *
+     * @param portPairGroup port-pair-group
      */
-    public void onPortPairGroupCreated();
+    void onPortPairGroupCreated(PortPairGroup portPairGroup);
 
     /**
      * When port-pair-group is deleted, check whether Forwarding Rule needs to
      * be updated in OVS.
+     *
+     * @param portPairGroup port-pair-group
      */
-    public void onPortPairGroupDeleted();
+    void onPortPairGroupDeleted(PortPairGroup portPairGroup);
 
     /**
      * When flow-classifier is created, check whether Forwarding Rule needs to
      * be updated in OVS.
+     *
+     * @param flowClassifier flow-classifier
      */
-    public void onFlowClassifierCreated();
+    void onFlowClassifierCreated(FlowClassifier flowClassifier);
 
     /**
      * When flow-classifier is deleted, check whether Forwarding Rule needs to
      * be updated in OVS.
+     *
+     * @param flowClassifier flow-classifier
      */
-    public void onFlowClassifierDeleted();
+    void onFlowClassifierDeleted(FlowClassifier flowClassifier);
 
     /**
      * When port-chain is created, check whether Forwarding Rule needs to be
      * updated in OVS.
+     *
+     * @param portChain port-chain
      */
-    public void onPortChainCreated();
+    void onPortChainCreated(PortChain portChain);
 
     /**
      * When port-chain is deleted, check whether Forwarding Rule needs to be
      * updated in OVS.
+     *
+     * @param portChain port-chain
      */
-    public void onPortChainDeleted();
+    void onPortChainDeleted(PortChain portChain);
 }
diff --git a/apps/vtn/sfcmgr/src/main/java/org/onosproject/sfc/manager/impl/SfcManager.java b/apps/vtn/sfcmgr/src/main/java/org/onosproject/sfc/manager/impl/SfcManager.java
index 12d27c8..4df0792 100644
--- a/apps/vtn/sfcmgr/src/main/java/org/onosproject/sfc/manager/impl/SfcManager.java
+++ b/apps/vtn/sfcmgr/src/main/java/org/onosproject/sfc/manager/impl/SfcManager.java
@@ -20,9 +20,26 @@
 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.apache.felix.scr.annotations.Service;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
 import org.onosproject.sfc.manager.SfcService;
+import org.onosproject.vtnrsc.PortPair;
+import org.onosproject.vtnrsc.PortPairId;
+import org.onosproject.vtnrsc.PortPairGroup;
+import org.onosproject.vtnrsc.PortPairGroupId;
+import org.onosproject.vtnrsc.FlowClassifier;
+import org.onosproject.vtnrsc.FlowClassifierId;
 import org.onosproject.vtnrsc.PortChain;
+import org.onosproject.vtnrsc.PortChainId;
+import org.onosproject.vtnrsc.TenantId;
+import org.onosproject.vtnrsc.event.VtnRscEvent;
+import org.onosproject.vtnrsc.event.VtnRscListener;
+import org.onosproject.vtnrsc.service.VtnRscService;
+
 import org.slf4j.Logger;
 
 /**
@@ -33,93 +50,137 @@
 public class SfcManager implements SfcService {
 
     private final Logger log = getLogger(getClass());
+    private static final String APP_ID = "org.onosproject.app.vtn";
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected VtnRscService vtnRscService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    protected ApplicationId appId;
+
+    private final VtnRscListener vtnRscListener = new InnerVtnRscListener();
 
     @Activate
     public void activate() {
+        appId = coreService.registerApplication(APP_ID);
+
+        vtnRscService.addListener(vtnRscListener);
+
+        KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
+                .register(TenantId.class)
+                .register(PortPairId.class)
+                .register(PortPairGroupId.class)
+                .register(FlowClassifierId.class)
+                .register(PortChainId.class);
+
         log.info("Started");
     }
 
     @Deactivate
     public void deactivate() {
+        vtnRscService.removeListener(vtnRscListener);
+
         log.info("Stopped");
     }
 
+    /*
+     * Handle events.
+     */
+    private class InnerVtnRscListener implements VtnRscListener {
+        @Override
+        public void event(VtnRscEvent event) {
+
+            if (VtnRscEvent.Type.PORT_PAIR_PUT == event.type()) {
+                PortPair portPair = (PortPair) event.subject();
+                onPortPairCreated(portPair);
+            } else if (VtnRscEvent.Type.PORT_PAIR_DELETE == event.type()) {
+                PortPair portPair = (PortPair) event.subject();
+                onPortPairDeleted(portPair);
+            } else if (VtnRscEvent.Type.PORT_PAIR_UPDATE == event.type()) {
+                PortPair portPair = (PortPair) event.subject();
+                onPortPairDeleted(portPair);
+                onPortPairCreated(portPair);
+            } else if (VtnRscEvent.Type.PORT_PAIR_GROUP_PUT == event.type()) {
+                PortPairGroup portPairGroup = (PortPairGroup) event.subject();
+                onPortPairGroupCreated(portPairGroup);
+            } else if (VtnRscEvent.Type.PORT_PAIR_GROUP_DELETE == event.type()) {
+                PortPairGroup portPairGroup = (PortPairGroup) event.subject();
+                onPortPairGroupDeleted(portPairGroup);
+            } else if (VtnRscEvent.Type.PORT_PAIR_GROUP_UPDATE == event.type()) {
+                PortPairGroup portPairGroup = (PortPairGroup) event.subject();
+                onPortPairGroupDeleted(portPairGroup);
+                onPortPairGroupCreated(portPairGroup);
+            } else if (VtnRscEvent.Type.FLOW_CLASSIFIER_PUT == event.type()) {
+                FlowClassifier flowClassifier = (FlowClassifier) event.subject();
+                onFlowClassifierCreated(flowClassifier);
+            } else if (VtnRscEvent.Type.FLOW_CLASSIFIER_DELETE == event.type()) {
+                FlowClassifier flowClassifier = (FlowClassifier) event.subject();
+                onFlowClassifierDeleted(flowClassifier);
+            } else if (VtnRscEvent.Type.FLOW_CLASSIFIER_UPDATE == event.type()) {
+                FlowClassifier flowClassifier = (FlowClassifier) event.subject();
+                onFlowClassifierDeleted(flowClassifier);
+                onFlowClassifierCreated(flowClassifier);
+            } else if (VtnRscEvent.Type.PORT_CHAIN_PUT == event.type()) {
+                PortChain portChain = (PortChain) event.subject();
+                onPortChainCreated(portChain);
+            } else if (VtnRscEvent.Type.PORT_CHAIN_DELETE == event.type()) {
+                PortChain portChain = (PortChain) event.subject();
+                onPortChainDeleted(portChain);
+            } else if (VtnRscEvent.Type.PORT_CHAIN_UPDATE == event.type()) {
+                PortChain portChain = (PortChain) event.subject();
+                onPortChainDeleted(portChain);
+                onPortChainCreated(portChain);
+            }
+        }
+    }
+
     @Override
-    public void onPortPairCreated() {
+    public void onPortPairCreated(PortPair portPair) {
         log.debug("onPortPairCreated");
-        // TODO: Process port-pair on creation.
-        // TODO: Parameter also needs to be modified.
+        // TODO: Modify forwarding rule on port-pair creation.
     }
 
     @Override
-    public void onPortPairDeleted() {
+    public void onPortPairDeleted(PortPair portPair) {
         log.debug("onPortPairDeleted");
-        // TODO: Process port-pair on deletion.
-        // TODO: Parameter also needs to be modified.
+        // TODO: Modify forwarding rule on port-pair deletion.
     }
 
     @Override
-    public void onPortPairGroupCreated() {
+    public void onPortPairGroupCreated(PortPairGroup portPairGroup) {
         log.debug("onPortPairGroupCreated");
-        // TODO: Process port-pair-group on creation.
-        // TODO: Parameter also needs to be modified.
+        // TODO: Modify forwarding rule on port-pair-group creation.
     }
 
     @Override
-    public void onPortPairGroupDeleted() {
+    public void onPortPairGroupDeleted(PortPairGroup portPairGroup) {
         log.debug("onPortPairGroupDeleted");
-        // TODO: Process port-pair-group on deletion.
-        // TODO: Parameter also needs to be modified.
+        // TODO: Modify forwarding rule on port-pair-group deletion.
     }
 
     @Override
-    public void onFlowClassifierCreated() {
+    public void onFlowClassifierCreated(FlowClassifier flowClassifier) {
         log.debug("onFlowClassifierCreated");
-        // TODO: Process flow-classifier on creation.
-        // TODO: Parameter also needs to be modified.
+        // TODO: Modify forwarding rule on flow-classifier creation.
     }
 
     @Override
-    public void onFlowClassifierDeleted() {
+    public void onFlowClassifierDeleted(FlowClassifier flowClassifier) {
         log.debug("onFlowClassifierDeleted");
-        // TODO: Process flow-classifier on deletion.
-        // TODO: Parameter also needs to be modified.
+        // TODO: Modify forwarding rule on flow-classifier deletion.
     }
 
     @Override
-    public void onPortChainCreated() {
-        log.debug("onPortChainCreated");
-        // TODO: Process port-chain on creation.
-        // TODO: Parameter also needs to be modified.
-
+    public void onPortChainCreated(PortChain portChain) {
+         log.debug("onPortChainCreated");
+         //TODO: Apply forwarding rule on port-chain creation.
     }
 
     @Override
-    public void onPortChainDeleted() {
+    public void onPortChainDeleted(PortChain portChain) {
         log.debug("onPortChainDeleted");
-        // TODO: Process port-chain on deletion.
-        // TODO: Parameter also needs to be modified.
+        //TODO: Apply forwarding rule on port-chain deletion.
     }
-
-    /**
-     * Install SF Forwarding rule into OVS.
-     *
-     * @param portChain
-     *            port chain
-     */
-    public void installForwardingRule(PortChain portChain) {
-        log.debug("installForwardingRule");
-        // TODO: Installation of SF Forwarding rule into OVS.
-    }
-
-    /**
-     * Uninstall SF Forwarding rule from OVS.
-     *
-     * @param portChain
-     *            port chain
-     */
-    public void unInstallForwardingRule(PortChain portChain) {
-        log.debug("unInstallForwardingRule");
-        // TODO: Uninstallation of SF Forwarding rule from OVS.
-    }
-}
\ No newline at end of file
+}