FPM Connections REST API
Corrected style check errors

Change-Id: I1fa61f613053f9f5a61bcf5db976adf761ec816d
diff --git a/apps/routing/fpm/BUCK b/apps/routing/fpm/BUCK
index c11818b..6a54db5 100644
--- a/apps/routing/fpm/BUCK
+++ b/apps/routing/fpm/BUCK
@@ -3,6 +3,7 @@
     '//apps/routing-api:onos-apps-routing-api',
     '//apps/routing/fpm/api:onos-apps-routing-fpm-api',
     '//apps/routing/fpm/app:onos-apps-routing-fpm-app',
+    '//apps/routing/fpm/web:onos-apps-routing-fpm-web',
 ]
 
 onos_app (
diff --git a/apps/routing/fpm/web/BUCK b/apps/routing/fpm/web/BUCK
new file mode 100644
index 0000000..6143bfb
--- /dev/null
+++ b/apps/routing/fpm/web/BUCK
@@ -0,0 +1,17 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//lib:JACKSON',
+    '//utils/rest:onlab-rest',
+    '//lib:javax.ws.rs-api',
+    '//apps/routing/fpm/app:onos-apps-routing-fpm-app',
+    '//apps/routing/fpm/api:onos-apps-routing-fpm-api',
+]
+
+osgi_jar (
+   deps = COMPILE_DEPS,
+   web_context = '/onos/v1/fpm',
+   api_title = 'FPM API',
+   api_version = '1.0',
+   api_description = 'REST API for FPM',
+   api_package = 'org.onosproject.fpm.web',
+)
diff --git a/apps/routing/fpm/web/pom.xml b/apps/routing/fpm/web/pom.xml
new file mode 100644
index 0000000..909b1f5
--- /dev/null
+++ b/apps/routing/fpm/web/pom.xml
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.onosproject</groupId>
+        <artifactId>onos-apps-fpm</artifactId>
+        <version>1.12.2-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>onos-apps-routing-fpm-web</artifactId>
+    <packaging>bundle</packaging>
+
+    <properties>
+        <web.context>/onos/v1/routing/fpm</web.context>
+        <api.version>1.0.0</api.version>
+        <api.title>ONOS FPM Connections Application REST API</api.title>
+        <api.description>
+            APIs for interacting with the FPM Peers.
+        </api.description>
+        <api.package>org.onosproject.routing.fpm.web</api.package>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-apps-routing-fpm-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-apps-routing-fpm-app</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+       <dependencies>
+</project>
diff --git a/apps/routing/fpm/web/src/main/java/org/onosproject/fpm/web/FpmCodec.java b/apps/routing/fpm/web/src/main/java/org/onosproject/fpm/web/FpmCodec.java
new file mode 100755
index 0000000..fda14db
--- /dev/null
+++ b/apps/routing/fpm/web/src/main/java/org/onosproject/fpm/web/FpmCodec.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2015-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.fpm.web;
+
+import org.onlab.util.Tools;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.routing.fpm.FpmPeerInfo;
+
+/**
+ * Codec of FpmPeerInfo class.
+ */
+public final class FpmCodec extends JsonCodec<FpmPeerInfo> {
+
+    // JSON field names
+    private static final String PEER_ADDRESS = "peerAddress";
+    private static final String PEER_PORT = "peerPort";
+    private static final String CONNECTED_TO = "connectedTo";
+    private static final String CONNECTION_TIME = "connectionTime";
+    private static final String LOCAL_ROUTES = "localRoutes";
+
+
+    @Override
+    public ObjectNode encode(FpmPeerInfo fpmPeerInfo, CodecContext context) {
+        final ObjectNode fpmConnectionArray = context.mapper().createObjectNode();
+
+        ArrayNode connectionArray = context.mapper().createArrayNode();
+        fpmPeerInfo.connections().forEach(connection -> {
+            ObjectNode fpmNode = context.mapper().createObjectNode();
+            fpmNode.put(PEER_ADDRESS, connection.peer().address().toString());
+            fpmNode.put(PEER_PORT, connection.peer().port());
+            fpmNode.put(CONNECTED_TO, connection.connectedTo().toString());
+            fpmNode.put(CONNECTION_TIME, Tools.timeAgo(connection.connectTime()));
+            fpmNode.put(LOCAL_ROUTES, fpmPeerInfo.routes());
+            connectionArray.add(fpmNode);
+        });
+
+        fpmConnectionArray.put("connection", connectionArray);
+        return fpmConnectionArray;
+    }
+
+
+
+}
diff --git a/apps/routing/fpm/web/src/main/java/org/onosproject/fpm/web/FpmWebApplication.java b/apps/routing/fpm/web/src/main/java/org/onosproject/fpm/web/FpmWebApplication.java
new file mode 100644
index 0000000..2d62de6
--- /dev/null
+++ b/apps/routing/fpm/web/src/main/java/org/onosproject/fpm/web/FpmWebApplication.java
@@ -0,0 +1,31 @@
+/*
+ * 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.fpm.web;
+
+import org.onlab.rest.AbstractWebApplication;
+
+import java.util.Set;
+
+/**
+ * FPM  Web application.
+ */
+public class FpmWebApplication extends AbstractWebApplication {
+    @Override
+    public Set<Class<?>> getClasses() {
+        return getClasses(FpmWebResource.class);
+    }
+}
diff --git a/apps/routing/fpm/web/src/main/java/org/onosproject/fpm/web/FpmWebResource.java b/apps/routing/fpm/web/src/main/java/org/onosproject/fpm/web/FpmWebResource.java
new file mode 100644
index 0000000..6b6ea92
--- /dev/null
+++ b/apps/routing/fpm/web/src/main/java/org/onosproject/fpm/web/FpmWebResource.java
@@ -0,0 +1,77 @@
+/*
+ * 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.fpm.web;
+
+import org.onlab.packet.IpAddress;
+
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.rest.AbstractWebResource;
+import org.onosproject.routing.fpm.FpmPeerInfo;
+import org.onosproject.routing.fpm.FpmInfoService;
+import org.onosproject.routing.fpm.FpmPeer;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import java.util.Comparator;
+import java.util.Map;
+
+/**
+ * FPM REST API.
+ */
+@Path("")
+public class FpmWebResource extends AbstractWebResource {
+
+    /**
+     * To get all fpm connections.
+     * @return 200 OK with component properties of given component and variable.
+     */
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    @Path("connections/")
+    public Response getFpmConnections() {
+        ObjectNode node = getFpmConnectionsJsonOutput();
+        return Response.status(200).entity(node).build();
+    }
+
+
+    private ObjectNode getFpmConnectionsJsonOutput() {
+
+        FpmInfoService fpmService = get(FpmInfoService.class);
+        ObjectNode node = mapper().createObjectNode();
+        ArrayNode connectionArray = mapper().createArrayNode();
+
+        Map<FpmPeer, FpmPeerInfo> fpmPeers = fpmService.peers();
+
+        fpmPeers.entrySet().stream()
+                .sorted(Comparator.<Map.Entry<FpmPeer, FpmPeerInfo>, IpAddress>comparing(e -> e.getKey().address())
+                                .thenComparing(e -> e.getKey().port()))
+                .map(Map.Entry::getValue)
+                .forEach(fpmPeerInfo -> connectionArray.add((new FpmCodec()).encode(fpmPeerInfo, this)));
+
+        node.put("fpm-connections", connectionArray);
+        return node;
+
+    }
+}
+
+
diff --git a/apps/routing/fpm/web/src/main/java/org/onosproject/fpm/web/package-info.java b/apps/routing/fpm/web/src/main/java/org/onosproject/fpm/web/package-info.java
new file mode 100644
index 0000000..ebeaed12
--- /dev/null
+++ b/apps/routing/fpm/web/src/main/java/org/onosproject/fpm/web/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017-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.
+ */
+
+/**
+ * FPM REST API.
+ */
+package org.onosproject.fpm.web;
diff --git a/apps/routing/fpm/web/src/main/webapp/WEB-INF/web.xml b/apps/routing/fpm/web/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 0000000..fba2540
--- /dev/null
+++ b/apps/routing/fpm/web/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xmlns="http://java.sun.com/xml/ns/javaee"
+         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+         id="ONOS" version="2.5">
+    <display-name>FPM REST API v1.0</display-name>
+
+    <security-constraint>
+        <web-resource-collection>
+            <web-resource-name>Secured</web-resource-name>
+            <url-pattern>/*</url-pattern>
+        </web-resource-collection>
+        <auth-constraint>
+            <role-name>admin</role-name>
+        </auth-constraint>
+    </security-constraint>
+
+    <security-role>
+        <role-name>admin</role-name>
+    </security-role>
+
+    <login-config>
+        <auth-method>BASIC</auth-method>
+        <realm-name>karaf</realm-name>
+    </login-config>
+
+    <servlet>
+        <servlet-name>JAX-RS Service</servlet-name>
+        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
+        <init-param>
+            <param-name>javax.ws.rs.Application</param-name>
+            <param-value>org.onosproject.fpm.web.FpmWebApplication</param-value>
+        </init-param>
+        <load-on-startup>1</load-on-startup>
+    </servlet>
+
+    <servlet-mapping>
+        <servlet-name>JAX-RS Service</servlet-name>
+        <url-pattern>/*</url-pattern>
+    </servlet-mapping>
+</web-app>