[ONOS-4226]Portchain to service function map interface

Change-Id: Ie49d2b6755bf5ee92b40e09755a3ae675c47e31d
diff --git a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/DefaultPortPairGroup.java b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/DefaultPortPairGroup.java
index 78f41a4..9817b0a 100644
--- a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/DefaultPortPairGroup.java
+++ b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/DefaultPortPairGroup.java
@@ -24,6 +24,7 @@
 import java.util.concurrent.ConcurrentHashMap;
 
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 
 /**
  * Implementation of port pair group.
@@ -99,6 +100,11 @@
     }
 
     @Override
+    public Map<PortPairId, Integer> portPairLoadMap() {
+        return ImmutableMap.copyOf(portPairLoadMap);
+    }
+
+    @Override
     public int hashCode() {
         return Objects.hash(portPairGroupId, tenantId, name, description,
                             portPairList);
diff --git a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/PortPairGroup.java b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/PortPairGroup.java
index 0b571b1..bdcfd1d 100644
--- a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/PortPairGroup.java
+++ b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/PortPairGroup.java
@@ -16,6 +16,7 @@
 package org.onosproject.vtnrsc;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * Abstraction of an entity providing Port Pair Group information.
@@ -63,7 +64,7 @@
      *
      * @param portPairId port pair id.
      */
-    public void addLoad(PortPairId portPairId);
+    void addLoad(PortPairId portPairId);
 
     /**
      * Get the load on the given port pair id.
@@ -71,7 +72,14 @@
      * @param portPairId port pair id
      * @return load on the given port pair id.
      */
-    public int getLoad(PortPairId portPairId);
+    int getLoad(PortPairId portPairId);
+
+    /**
+     * Get the map of port pair id and its load.
+     *
+     * @return port pair and load map
+     */
+    Map<PortPairId, Integer> portPairLoadMap();
 
     /**
      * Returns whether this port pair group is an exact match to the
diff --git a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/portchainsfmap/PortChainSfMapService.java b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/portchainsfmap/PortChainSfMapService.java
new file mode 100644
index 0000000..15cc24b
--- /dev/null
+++ b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/portchainsfmap/PortChainSfMapService.java
@@ -0,0 +1,44 @@
+/*
+ * 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.onosproject.vtnrsc.portchainsfmap;
+
+import java.util.List;
+
+import org.onosproject.vtnrsc.PortChainId;
+import org.onosproject.vtnrsc.ServiceFunctionGroup;
+
+/**
+ * Service for interacting with the inventory of service functions for a given port chain.
+ */
+public interface PortChainSfMapService {
+
+    /**
+     * Returns true if the port chain exists.
+     *
+     * @param portChainId port chain identifier
+     * @return true or false if one with the given identifier exists.
+     */
+    boolean exists(PortChainId portChainId);
+
+    /**
+     * Returns the list of service function groups available in the given port chain.
+     *
+     * @param portChainId port chain id
+     * @return list of service functions
+     */
+    List<ServiceFunctionGroup> getServiceFunctions(PortChainId portChainId);
+
+}
diff --git a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/portchainsfmap/impl/PortChainSfMapManager.java b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/portchainsfmap/impl/PortChainSfMapManager.java
new file mode 100644
index 0000000..2df0ee6
--- /dev/null
+++ b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/portchainsfmap/impl/PortChainSfMapManager.java
@@ -0,0 +1,98 @@
+/*
+ * 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.onosproject.vtnrsc.portchainsfmap.impl;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+import java.util.List;
+import java.util.ListIterator;
+
+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.onosproject.vtnrsc.PortChain;
+import org.onosproject.vtnrsc.PortChainId;
+import org.onosproject.vtnrsc.PortPairGroup;
+import org.onosproject.vtnrsc.PortPairGroupId;
+import org.onosproject.vtnrsc.ServiceFunctionGroup;
+import org.onosproject.vtnrsc.portchain.PortChainService;
+import org.onosproject.vtnrsc.portchainsfmap.PortChainSfMapService;
+import org.onosproject.vtnrsc.portpair.PortPairService;
+import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
+import org.slf4j.Logger;
+
+import com.google.common.collect.Lists;
+
+/**
+ * Provides implementation of the PortChainSfMapService.
+ * A port pair group is nothing but group of similar service functions.
+ * A port pair is nothing but a service function.
+ */
+@Component(immediate = true)
+@Service
+public class PortChainSfMapManager implements PortChainSfMapService {
+
+    private static final String PORT_CHAIN_ID_NULL = "PortChain ID cannot be null";
+
+    private final Logger log = getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected PortChainService portChainService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected PortPairGroupService portPairGroupService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected PortPairService portPairService;
+
+    @Activate
+    public void activate() {
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        log.info("Stopped");
+    }
+
+    @Override
+    public boolean exists(PortChainId portChainId) {
+        checkNotNull(portChainId, PORT_CHAIN_ID_NULL);
+        return portChainService.exists(portChainId);
+    }
+
+    @Override
+    public List<ServiceFunctionGroup> getServiceFunctions(PortChainId portChainId) {
+        List<ServiceFunctionGroup> serviceFunctionGroupList = Lists.newArrayList();
+        PortChain portChain = portChainService.getPortChain(portChainId);
+        // Go through the port pair group list
+        List<PortPairGroupId> portPairGrpList = portChain.portPairGroups();
+        ListIterator<PortPairGroupId> listGrpIterator = portPairGrpList.listIterator();
+
+        while (listGrpIterator.next() != null) {
+            PortPairGroupId portPairGroupId = listGrpIterator.next();
+            PortPairGroup portPairGroup = portPairGroupService.getPortPairGroup(portPairGroupId);
+            ServiceFunctionGroup sfg = new ServiceFunctionGroup(portPairGroup.name(), portPairGroup.description(),
+                                                                portPairGroup.portPairLoadMap());
+            serviceFunctionGroupList.add(sfg);
+        }
+        return serviceFunctionGroupList;
+    }
+}
diff --git a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/portchainsfmap/impl/package-info.java b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/portchainsfmap/impl/package-info.java
new file mode 100644
index 0000000..78874ee
--- /dev/null
+++ b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/portchainsfmap/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015 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.
+ */
+
+/**
+ * Service for interacting with the inventory of port chains to get details of service functions and stats.
+ */
+package org.onosproject.vtnrsc.portchainsfmap.impl;
diff --git a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/portchainsfmap/package-info.java b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/portchainsfmap/package-info.java
new file mode 100644
index 0000000..5cc5a321
--- /dev/null
+++ b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/portchainsfmap/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * Service for interacting with the inventory of port chains to get details of service functions and stats.
+ */
+package org.onosproject.vtnrsc.portchainsfmap;
diff --git a/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/PortPairGroupResourceTest.java b/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/PortPairGroupResourceTest.java
index 86f6bc6..c33d929 100644
--- a/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/PortPairGroupResourceTest.java
+++ b/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/PortPairGroupResourceTest.java
@@ -19,6 +19,7 @@
 import com.eclipsesource.json.JsonObject;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
+import java.util.Map;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -116,22 +117,25 @@
         }
 
         @Override
-        public boolean exactMatch(PortPairGroup portPairGroup) {
-            return this.equals(portPairGroup) &&
-                    Objects.equals(this.portPairGroupId, portPairGroup.portPairGroupId()) &&
-                    Objects.equals(this.tenantId, portPairGroup.tenantId());
-        }
-
-        @Override
         public void addLoad(PortPairId portPairId) {
-            // TODO Auto-generated method stub
         }
 
         @Override
         public int getLoad(PortPairId portPairId) {
-            // TODO Auto-generated method stub
             return 0;
         }
+
+        @Override
+        public Map<PortPairId, Integer> portPairLoadMap() {
+            return null;
+        }
+
+        @Override
+        public boolean exactMatch(PortPairGroup portPairGroup) {
+            return this.equals(portPairGroup) &&
+                    Objects.equals(this.portPairGroupId, portPairGroup.portPairGroupId()) &&
+                    Objects.equals(this.tenantId, portPairGroup.tenantId());
+        }
     }
 
     /**