blob: 6b6ea92d5e61a4d2445b9c13d9222e11134108a1 [file] [log] [blame]
Harshada Chaundkarb78db192019-02-19 16:18:16 -05001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.onosproject.fpm.web;
19
20import org.onlab.packet.IpAddress;
21
22import com.fasterxml.jackson.databind.node.ArrayNode;
23import com.fasterxml.jackson.databind.node.ObjectNode;
24import org.onosproject.rest.AbstractWebResource;
25import org.onosproject.routing.fpm.FpmPeerInfo;
26import org.onosproject.routing.fpm.FpmInfoService;
27import org.onosproject.routing.fpm.FpmPeer;
28
29import javax.ws.rs.GET;
30import javax.ws.rs.Path;
31import javax.ws.rs.Produces;
32import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
34
35import java.util.Comparator;
36import java.util.Map;
37
38/**
39 * FPM REST API.
40 */
41@Path("")
42public class FpmWebResource extends AbstractWebResource {
43
44 /**
45 * To get all fpm connections.
46 * @return 200 OK with component properties of given component and variable.
47 */
48 @GET
49 @Produces(MediaType.APPLICATION_JSON)
50 @Path("connections/")
51 public Response getFpmConnections() {
52 ObjectNode node = getFpmConnectionsJsonOutput();
53 return Response.status(200).entity(node).build();
54 }
55
56
57 private ObjectNode getFpmConnectionsJsonOutput() {
58
59 FpmInfoService fpmService = get(FpmInfoService.class);
60 ObjectNode node = mapper().createObjectNode();
61 ArrayNode connectionArray = mapper().createArrayNode();
62
63 Map<FpmPeer, FpmPeerInfo> fpmPeers = fpmService.peers();
64
65 fpmPeers.entrySet().stream()
66 .sorted(Comparator.<Map.Entry<FpmPeer, FpmPeerInfo>, IpAddress>comparing(e -> e.getKey().address())
67 .thenComparing(e -> e.getKey().port()))
68 .map(Map.Entry::getValue)
69 .forEach(fpmPeerInfo -> connectionArray.add((new FpmCodec()).encode(fpmPeerInfo, this)));
70
71 node.put("fpm-connections", connectionArray);
72 return node;
73
74 }
75}
76
77