[ONOS-4226]Portchain to service function map interface

Change-Id: Ie49d2b6755bf5ee92b40e09755a3ae675c47e31d
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;