blob: 9b5209bfddda77779e9f27869077e23d43c669f1 [file] [log] [blame]
Jian Li6e4da2f2018-05-21 18:11:31 +09001/*
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.openstacktelemetry.web;
17
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
Jian Li6e4da2f2018-05-21 18:11:31 +090020import com.fasterxml.jackson.databind.node.ObjectNode;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090021import com.google.common.collect.Sets;
22import org.apache.commons.lang3.exception.ExceptionUtils;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.openstacktelemetry.api.FlowInfo;
25import org.onosproject.openstacktelemetry.api.StatsFlowRule;
26import org.onosproject.openstacktelemetry.api.StatsFlowRuleAdminService;
27import org.onosproject.openstacktelemetry.codec.FlowInfoJsonCodec;
Jian Li6e4da2f2018-05-21 18:11:31 +090028import org.onosproject.rest.AbstractWebResource;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090029import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
Jian Li6e4da2f2018-05-21 18:11:31 +090031
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090032import javax.ws.rs.Consumes;
Jian Li6e4da2f2018-05-21 18:11:31 +090033import javax.ws.rs.GET;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090034import javax.ws.rs.POST;
Jian Li6e4da2f2018-05-21 18:11:31 +090035import javax.ws.rs.Path;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090036import javax.ws.rs.PathParam;
Jian Li6e4da2f2018-05-21 18:11:31 +090037import javax.ws.rs.Produces;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090038import javax.ws.rs.core.Context;
Jian Li6e4da2f2018-05-21 18:11:31 +090039import javax.ws.rs.core.MediaType;
40import javax.ws.rs.core.Response;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090041import javax.ws.rs.core.UriBuilder;
42import javax.ws.rs.core.UriInfo;
43import java.io.InputStream;
44import java.util.Set;
45
46import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
47import static javax.ws.rs.core.Response.created;
48import static org.onlab.util.Tools.readTreeFromStream;
Jian Li6e4da2f2018-05-21 18:11:31 +090049
50/**
51 * Handles REST API call of openstack telemetry.
52 */
53
54@Path("telemetry")
55public class OpenstackTelemetryWebResource extends AbstractWebResource {
56
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090057 private final Logger log = LoggerFactory.getLogger(getClass());
58
Jian Li6e4da2f2018-05-21 18:11:31 +090059 private final ObjectNode root = mapper().createObjectNode();
60
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090061 private static final String JSON_NODE_FLOW_RULE = "rules";
62 private static final String FLOW_RULE_ID = "STATS_FLOW_RULE_ID";
63
64 private final StatsFlowRuleAdminService statsFlowRuleService = get(StatsFlowRuleAdminService.class);
65
66 @Context
67 private UriInfo uriInfo;
68
69 /**
70 * Creates a flow rule for metric.
71 *
72 * @param input openstack flow rule JSON input stream
73 * @return 201 CREATED if the JSON is correct,
74 * 400 BAD_REQUEST if the JSON is malformed.
75 */
76 @POST
77 @Consumes(MediaType.APPLICATION_JSON)
78 @Produces(MediaType.APPLICATION_JSON)
79 public Response createBulkFlowRule(InputStream input) {
80 log.info("CREATE BULK FLOW RULE: {}", input.toString());
81
82 readNodeConfiguration(input).forEach(flowRule -> {
83 log.debug("FlowRule: {}", flowRule.toString());
84 statsFlowRuleService.createFlowRule(flowRule);
85 });
86
87 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
88 .path(JSON_NODE_FLOW_RULE)
89 .path(FLOW_RULE_ID);
90
91 return created(locationBuilder.build()).build();
92 }
93
94 /**
95 * Delete flow rules.
96 *
97 * @param input openstack flow rule JSON input stream
98 * @return 200 OK if processing is correct.
99 */
100 public Response deleteBulkFlowRule(InputStream input) {
101 log.info("DELETE BULK FLOW RULE: {}", input.toString());
102
103 readNodeConfiguration(input).forEach(flowRule -> {
104 log.debug("FlowRule: {}", flowRule.toString());
105 statsFlowRuleService.deleteFlowRule(flowRule);
106 });
107
108 return ok(root).build();
109 }
110
111
112 /**
113 * Get flow rules which is installed on ONOS.
114 *
115 * @return 200 OK
116 */
117 public Response readBulkFlowRule() {
118 log.info("READ BULK FLOW RULE");
119
120 return ok(root).build();
121 }
122
123
124 /**
125 * Get flow information list.
126 *
127 * @return Flow information list
128 */
129 @GET
130 @Path("list")
131 @Produces(MediaType.APPLICATION_JSON)
132 public Response getFlowInfoBulk() {
133 log.info("GET BULK FLOW RULE");
134
135 Set<FlowInfo> flowInfoSet;
136 flowInfoSet = statsFlowRuleService.getFlowRule();
137
138 log.info("\n\n======================================================\n" +
139 "FlowInfo Set: \n{}" +
140 "\n\n======================================================\n",
141 flowInfoSet);
142
143 JsonCodec<FlowInfo> flowInfoCodec = new FlowInfoJsonCodec();
144
145 ObjectNode nodeJson;
146 int idx = 0;
147 for (FlowInfo flowInfo: flowInfoSet) {
148 nodeJson = flowInfoCodec.encode(flowInfo, this);
149 root.put("FlowInfo" + String.valueOf(idx++), nodeJson.toString());
150 }
151 return ok(root).build();
152 }
153
154 @GET
155 @Path("list/{src_ip_prefix}/{dst_ip_prefix}")
156 @Produces(MediaType.APPLICATION_JSON)
157 public Response getFlowRule(
158 @PathParam("src_ip_prefix") String srcIpPrefix,
159 @PathParam("dst_ip_prefix") String dstIpPrefix) {
160 return ok(root).build();
161 }
162
163
164 private Set<StatsFlowRule> readNodeConfiguration(InputStream input) {
165 log.info("Input JSON Data: \n\t\t{}", input.toString());
166 Set<StatsFlowRule> flowRuleSet = Sets.newHashSet();
167 try {
168 JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
169 ArrayNode nodes = (ArrayNode) jsonTree.path(JSON_NODE_FLOW_RULE);
170 nodes.forEach(node -> {
171 try {
172 ObjectNode objectNode = node.deepCopy();
173 log.debug("ObjectNode: {}", objectNode.toString());
174 StatsFlowRule statsFlowRule = codec(StatsFlowRule.class)
175 .decode(objectNode, this);
176 log.debug("StatsFlowRule: {}", statsFlowRule.toString());
177 flowRuleSet.add(statsFlowRule);
178 } catch (Exception ex) {
179 log.error("Exception Stack:\n{}", ExceptionUtils.getStackTrace(ex));
180 throw new IllegalArgumentException();
181 }
182 });
183 } catch (Exception ex) {
184 log.error("Exception Stack:\n{}", ExceptionUtils.getStackTrace(ex));
185 throw new IllegalArgumentException(ex);
186 }
187
188 return flowRuleSet;
189 }
190
Jian Li6e4da2f2018-05-21 18:11:31 +0900191 /**
192 * OpenstackTelemetryImpl method.
193 *
194 * @return 200 OK
195 *
196 * @onos.rsModel dummy
197 */
198 @GET
199 @Produces(MediaType.APPLICATION_JSON)
200 public Response dummy() {
201 return ok(root).build();
202 }
203}