Harshada Chaundkar | b78db19 | 2019-02-19 16:18:16 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package org.onosproject.fpm.web; |
| 19 | |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | |
| 22 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 23 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 24 | import org.onosproject.rest.AbstractWebResource; |
| 25 | import org.onosproject.routing.fpm.FpmPeerInfo; |
| 26 | import org.onosproject.routing.fpm.FpmInfoService; |
| 27 | import org.onosproject.routing.fpm.FpmPeer; |
| 28 | |
| 29 | import javax.ws.rs.GET; |
| 30 | import javax.ws.rs.Path; |
| 31 | import javax.ws.rs.Produces; |
| 32 | import javax.ws.rs.core.MediaType; |
| 33 | import javax.ws.rs.core.Response; |
| 34 | |
| 35 | import java.util.Comparator; |
| 36 | import java.util.Map; |
| 37 | |
| 38 | /** |
| 39 | * FPM REST API. |
| 40 | */ |
| 41 | @Path("") |
| 42 | public 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 | |