FPM Connections REST API
Corrected style check errors

Change-Id: I1fa61f613053f9f5a61bcf5db976adf761ec816d
diff --git a/apps/routing/fpm/BUILD b/apps/routing/fpm/BUILD
index bcf45df..977b589 100644
--- a/apps/routing/fpm/BUILD
+++ b/apps/routing/fpm/BUILD
@@ -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/BUILD b/apps/routing/fpm/web/BUILD
new file mode 100644
index 0000000..5be3e42
--- /dev/null
+++ b/apps/routing/fpm/web/BUILD
@@ -0,0 +1,14 @@
+COMPILE_DEPS = CORE_DEPS + JACKSON + REST + [
+    "@jersey_server//jar",
+    "//apps/routing/fpm/app:onos-apps-routing-fpm-app",
+    "//apps/routing/fpm/api:onos-apps-routing-fpm-api",
+]
+
+osgi_jar(
+    api_description = "REST API for FPM",
+    api_package = "org.onosproject.fpm.web",
+    api_title = "FPM API",
+    api_version = "1.0",
+    web_context = "/onos/v1/fpm",
+    deps = COMPILE_DEPS,
+)
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>