blob: 8202eedca26739d22e4c6c5fd2cef7c278463c70 [file] [log] [blame]
Pier71c55772018-04-17 17:25:22 +02001/*
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 */
16package org.onosproject.segmentrouting.web;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableSet;
22import com.google.common.collect.Multimap;
23import org.onlab.packet.IpAddress;
24import org.onosproject.net.ConnectPoint;
25import org.onosproject.rest.AbstractWebResource;
26import org.onosproject.segmentrouting.SegmentRoutingService;
27
28import javax.ws.rs.GET;
29import javax.ws.rs.Path;
30import javax.ws.rs.PathParam;
31import javax.ws.rs.Produces;
32import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
34import java.util.List;
35import java.util.Set;
36import java.util.stream.Collectors;
37
38import static com.google.common.base.Strings.isNullOrEmpty;
39
40/**
41 * Query multicast trees.
42 */
43@Path("mcast")
44public class McastWebResource extends AbstractWebResource {
45
46 private ObjectNode encodeMcastTrees(String gAddr, String source) {
47 SegmentRoutingService srService = get(SegmentRoutingService.class);
48 Set<IpAddress> mcastGroups = ImmutableSet.copyOf(srService.getMcastLeaders(null)
49 .keySet());
50
51 if (!isNullOrEmpty(gAddr)) {
52 mcastGroups = mcastGroups.stream()
53 .filter(mcastIp -> mcastIp.equals(IpAddress.valueOf(gAddr)))
54 .collect(Collectors.toSet());
55 }
56
57 ObjectMapper mapper = new ObjectMapper();
58 ObjectNode root = mapper.createObjectNode();
59
60 // Print the trees for each group or build json objects
61 mcastGroups.forEach(group -> {
62 // We want to use source cp only for a specific group
63 ConnectPoint sourcecp = null;
64 if (!isNullOrEmpty(source) &&
65 !isNullOrEmpty(gAddr)) {
66 sourcecp = ConnectPoint.deviceConnectPoint(source);
67 }
68 Multimap<ConnectPoint, List<ConnectPoint>> mcastTree = srService.getMcastTrees(group, sourcecp);
69 // Build a json object for each group
70 root.putPOJO(group.toString(), json(mcastTree));
71
72 });
73 return root;
74 }
75
76 private ObjectNode json(Multimap<ConnectPoint, List<ConnectPoint>> mcastTree) {
77 ObjectMapper mapper = new ObjectMapper();
78 ObjectNode jsonSinks = mapper.createObjectNode();
79 mcastTree.asMap().forEach((sink, paths) -> {
80 ArrayNode jsonPaths = mapper.createArrayNode();
81 paths.forEach(path -> {
82 ArrayNode jsonPath = mapper.createArrayNode();
83 path.forEach(connectPoint -> jsonPath.add(connectPoint.toString()));
84 jsonPaths.addPOJO(jsonPath);
85 });
86 jsonSinks.putPOJO(sink.toString(), jsonPaths);
87 });
88 return jsonSinks;
89 }
90
91 /**
92 * Get all multicast trees.
93 * Returns an object of the multicast trees.
94 *
95 * @return status of OK
96 */
97 @GET
98 @Produces(MediaType.APPLICATION_JSON)
99 public Response getMcastTrees() {
100 ObjectNode root = encodeMcastTrees(null, null);
101 return ok(root).build();
102 }
103
104 /**
105 * Get the multicast trees of a group.
106 *
107 * @param group group IP address
108 * @return 200 OK with a multicast routes
109 */
110 @GET
111 @Produces(MediaType.APPLICATION_JSON)
112 @Path("{group}")
113 public Response getRoute(@PathParam("group") String group) {
114 ObjectNode root = encodeMcastTrees(group, null);
115 return ok(root).build();
116 }
117
118 /**
119 * Get the multicast tree of a group.
120 *
121 * @param group group IP address
122 * @param sourcecp source connect point
123 * @return 200 OK with a multicast routes
124 */
125 @GET
126 @Produces(MediaType.APPLICATION_JSON)
127 @Path("{group}/{sourcecp}")
128 public Response getRoute(@PathParam("group") String group,
129 @PathParam("sourcecp") String sourcecp) {
130 ObjectNode root = encodeMcastTrees(group, sourcecp);
131 return ok(root).build();
132 }
133
134}