blob: 380e4ad05ab201e369289a03017cb54c71e83d75 [file] [log] [blame]
Madan Jampani38a88212015-09-15 11:21:27 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Madan Jampani38a88212015-09-15 11:21:27 -07003 *
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.vtnweb.resources;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19import static com.google.common.base.Preconditions.checkArgument;
20import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
21import static javax.ws.rs.core.Response.Status.OK;
lishuai0f47f342015-09-16 11:38:48 +080022import static javax.ws.rs.core.Response.Status.NOT_FOUND;
Madan Jampani38a88212015-09-15 11:21:27 -070023
24import java.io.InputStream;
25import java.util.Collections;
26import java.util.HashSet;
27import java.util.Iterator;
28import java.util.Set;
29import java.util.concurrent.ConcurrentMap;
30
31import javax.ws.rs.Consumes;
32import javax.ws.rs.DELETE;
33import javax.ws.rs.GET;
34import javax.ws.rs.POST;
35import javax.ws.rs.PUT;
36import javax.ws.rs.Path;
37import javax.ws.rs.PathParam;
38import javax.ws.rs.Produces;
39import javax.ws.rs.QueryParam;
40import javax.ws.rs.core.MediaType;
41import javax.ws.rs.core.Response;
42
43import org.onlab.util.ItemNotFoundException;
44import org.onosproject.rest.AbstractWebResource;
45import org.onosproject.vtnrsc.DefaultTenantNetwork;
46import org.onosproject.vtnrsc.PhysicalNetwork;
47import org.onosproject.vtnrsc.SegmentationId;
48import org.onosproject.vtnrsc.TenantId;
49import org.onosproject.vtnrsc.TenantNetwork;
50import org.onosproject.vtnrsc.TenantNetworkId;
51import org.onosproject.vtnrsc.TenantNetwork.State;
52import org.onosproject.vtnrsc.TenantNetwork.Type;
53import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
onosjcc36e04a82015-10-27 16:46:37 +080054import org.onosproject.vtnweb.web.TenantNetworkCodec;
Madan Jampani38a88212015-09-15 11:21:27 -070055import org.slf4j.Logger;
56import org.slf4j.LoggerFactory;
57
58import com.fasterxml.jackson.databind.JsonNode;
59import com.fasterxml.jackson.databind.ObjectMapper;
60import com.fasterxml.jackson.databind.node.ObjectNode;
61import com.google.common.collect.Maps;
62
63/**
64 * REST resource for interacting with the inventory of networks.
65 */
66@Path("networks")
67public class TenantNetworkWebResource extends AbstractWebResource {
68 public static final String NETWORK_NOT_FOUND = "Network is not found";
69 public static final String NETWORK_ID_EXIST = "Network id is existed";
70 public static final String NETWORK_ID_NOT_EXIST = "Network id is not existed";
71 public static final String CREATE_NETWORK = "create network";
72 public static final String UPDATE_NETWORK = "update network";
73 public static final String DELETE_NETWORK = "delete network";
74 public static final String JSON_NOT_NULL = "JsonNode can not be null";
75
76 protected static final Logger log = LoggerFactory
77 .getLogger(TenantNetworkWebResource.class);
78 private final ConcurrentMap<TenantNetworkId, TenantNetwork> networksMap = Maps
79 .newConcurrentMap();
80
81 @GET
82 @Produces({ MediaType.APPLICATION_JSON })
83 public Response getNetworks(@QueryParam("id") String queryId,
84 @QueryParam("name") String queryName,
85 @QueryParam("admin_state_up") String queryadminStateUp,
86 @QueryParam("status") String querystate,
87 @QueryParam("shared") String queryshared,
88 @QueryParam("tenant_id") String querytenantId,
89 @QueryParam("router:external") String routerExternal,
90 @QueryParam("provider:network_type") String type,
91 @QueryParam("provider:physical_network") String physicalNetwork,
92 @QueryParam("provider:segmentation_id") String segmentationId) {
93 Iterable<TenantNetwork> networks = get(TenantNetworkService.class)
94 .getNetworks();
95 Iterator<TenantNetwork> networkors = networks.iterator();
96 while (networkors.hasNext()) {
97 TenantNetwork network = networkors.next();
98 if ((queryId == null || queryId.equals(network.id().toString()))
99 && (queryName == null || queryName.equals(network.name()))
100 && (queryadminStateUp == null || queryadminStateUp
Satish K32035572015-11-22 14:49:14 +0530101 .equals(Boolean.toString(network.adminStateUp())))
Madan Jampani38a88212015-09-15 11:21:27 -0700102 && (querystate == null || querystate.equals(network.state()
103 .toString()))
Satish K32035572015-11-22 14:49:14 +0530104 && (queryshared == null || queryshared.equals(Boolean.toString(network
105 .shared())))
Madan Jampani38a88212015-09-15 11:21:27 -0700106 && (querytenantId == null || querytenantId.equals(network
107 .tenantId().toString()))
Satish K32035572015-11-22 14:49:14 +0530108 && (routerExternal == null || routerExternal.equals(Boolean.toString(network
109 .routerExternal())))
110 && (type == null || type.equals(network.type().toString()))
Madan Jampani38a88212015-09-15 11:21:27 -0700111 && (physicalNetwork == null || physicalNetwork
Satish K32035572015-11-22 14:49:14 +0530112 .equals(network.physicalNetwork().toString()))
Madan Jampani38a88212015-09-15 11:21:27 -0700113 && (segmentationId == null || segmentationId.equals(network
Satish K32035572015-11-22 14:49:14 +0530114 .segmentationId().toString()))) {
Madan Jampani38a88212015-09-15 11:21:27 -0700115 networksMap.putIfAbsent(network.id(), network);
116 }
117 }
118 networks = Collections.unmodifiableCollection(networksMap.values());
119 ObjectNode result = new ObjectMapper().createObjectNode();
120 result.set("networks", new TenantNetworkCodec().encode(networks, this));
121
122 return ok(result.toString()).build();
123 }
124
125 private State isState(String state) {
126 if (state.equals("ACTIVE")) {
127 return TenantNetwork.State.ACTIVE;
128 } else if (state.equals("BUILD")) {
129 return TenantNetwork.State.BUILD;
130 } else if (state.equals("DOWN")) {
131 return TenantNetwork.State.DOWN;
132 } else if (state.equals("ERROR")) {
133 return TenantNetwork.State.ERROR;
134 } else {
135 return null;
136 }
137 }
138
139 private Type isType(String type) {
140 if (type.equals("LOCAL")) {
141 return TenantNetwork.Type.LOCAL;
142 } else {
143 return null;
144 }
145 }
146
147 @GET
148 @Path("{id}")
149 @Produces({ MediaType.APPLICATION_JSON })
150 public Response getNetwork(@PathParam("id") String id) {
151
152 if (!get(TenantNetworkService.class).exists(TenantNetworkId
153 .networkId(id))) {
lishuai0f47f342015-09-16 11:38:48 +0800154 return Response.status(NOT_FOUND)
155 .entity(NETWORK_NOT_FOUND).build();
Madan Jampani38a88212015-09-15 11:21:27 -0700156 }
157 TenantNetwork network = nullIsNotFound(get(TenantNetworkService.class)
158 .getNetwork(TenantNetworkId.networkId(id)), NETWORK_NOT_FOUND);
159 ObjectNode result = new ObjectMapper().createObjectNode();
160 result.set("network", new TenantNetworkCodec().encode(network, this));
161
162 return ok(result.toString()).build();
163
164 }
165
166 @POST
167 @Produces(MediaType.APPLICATION_JSON)
168 @Consumes(MediaType.APPLICATION_JSON)
169 public Response createNetworks(InputStream input) {
170 try {
171 ObjectMapper mapper = new ObjectMapper();
172 JsonNode cfg = mapper.readTree(input);
173 JsonNode nodes = null;
174 Iterable<TenantNetwork> networks = null;
175 if (cfg.get("network") != null) {
176 nodes = cfg.get("network");
177 if (nodes.isArray()) {
178 networks = changeJson2objs(nodes);
179 } else {
180 networks = changeJson2obj(CREATE_NETWORK, null, nodes);
181 }
182 } else if (cfg.get("networks") != null) {
183 nodes = cfg.get("networks");
184 networks = changeJson2objs(nodes);
185 }
186 Boolean issuccess = nullIsNotFound((get(TenantNetworkService.class)
187 .createNetworks(networks)),
188 NETWORK_NOT_FOUND);
189
190 if (!issuccess) {
191 return Response.status(INTERNAL_SERVER_ERROR)
192 .entity(NETWORK_ID_EXIST).build();
193 }
194 return Response.status(OK).entity(issuccess.toString()).build();
195 } catch (Exception e) {
196 log.error("Creates tenantNetwork exception {}.", e.toString());
197 return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString())
198 .build();
199 }
200 }
201
202 @PUT
203 @Path("{id}")
204 @Produces(MediaType.APPLICATION_JSON)
205 @Consumes(MediaType.APPLICATION_JSON)
206 public Response updateNetworks(@PathParam("id") String id, InputStream input) {
207 try {
208 ObjectMapper mapper = new ObjectMapper();
209 JsonNode cfg = mapper.readTree(input);
210 JsonNode nodes = null;
211 Iterable<TenantNetwork> networks = null;
212 if (cfg.get("network") != null) {
213 nodes = cfg.get("network");
214 if (nodes.isArray()) {
215 networks = changeJson2objs(nodes);
216 } else {
217 networks = changeJson2obj(UPDATE_NETWORK,
218 TenantNetworkId.networkId(id),
219 nodes);
220 }
221 } else if (cfg.get("networks") != null) {
222 nodes = cfg.get("networks");
223 networks = changeJson2objs(nodes);
224 }
225 Boolean issuccess = nullIsNotFound((get(TenantNetworkService.class)
226 .updateNetworks(networks)),
227 NETWORK_NOT_FOUND);
228 if (!issuccess) {
229 return Response.status(INTERNAL_SERVER_ERROR)
230 .entity(NETWORK_ID_NOT_EXIST).build();
231 }
232 return Response.status(OK).entity(issuccess.toString()).build();
233 } catch (Exception e) {
234 log.error("Updates tenantNetwork failed because of exception {}.",
235 e.toString());
236 return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString())
237 .build();
238 }
239 }
240
241 @DELETE
242 @Path("{id}")
243 public Response deleteNetworks(@PathParam("id") String id) {
244 log.debug("Deletes network by identifier {}.", id);
245 Set<TenantNetworkId> networkSet = new HashSet<>();
246 networkSet.add(TenantNetworkId.networkId(id));
247 Boolean issuccess = nullIsNotFound(get(TenantNetworkService.class)
248 .removeNetworks(networkSet), NETWORK_NOT_FOUND);
249 if (!issuccess) {
250 log.debug("Network identifier {} is not existed", id);
251 return Response.status(INTERNAL_SERVER_ERROR)
252 .entity(NETWORK_ID_NOT_EXIST).build();
253 }
254 return Response.status(OK).entity(issuccess.toString()).build();
255 }
256
257 /**
258 * Returns a collection of tenantNetworks.
259 *
260 * @param flag the flag
261 * @param networkId network identifier
262 * @param node the network json node
263 * @return a collection of tenantNetworks
264 */
265 public Iterable<TenantNetwork> changeJson2obj(String flag,
266 TenantNetworkId networkId,
267 JsonNode node) {
268 checkNotNull(node, JSON_NOT_NULL);
269 TenantNetwork network = null;
270 ConcurrentMap<TenantNetworkId, TenantNetwork> networksMap = Maps
271 .newConcurrentMap();
Satish Kab54ffa2015-11-27 14:19:42 +0530272 checkArgument(node.get("admin_state_up").isBoolean(), "admin_state_up should be boolean");
273 checkArgument(node.get("shared").isBoolean(), "shared should be boolean");
274 checkArgument(node.get("router:external").isBoolean(), "router:external should be boolean");
275 String name = node.get("name").asText();
276 boolean adminStateUp = node.get("admin_state_up").asBoolean();
277 String state = node.get("status").asText();
278 boolean shared = node.get("shared").asBoolean();
279 String tenantId = node.get("tenant_id").asText();
280 boolean routerExternal = node.get("router:external").asBoolean();
281 String type = node.get("provider:network_type").asText();
282 String physicalNetwork = node.get("provider:physical_network").asText();
283 String segmentationId = node.get("provider:segmentation_id").asText();
284 TenantNetworkId id = null;
285 if (flag.equals(CREATE_NETWORK)) {
286 id = TenantNetworkId.networkId(node.get("id").asText());
287 } else if (flag.equals(UPDATE_NETWORK)) {
288 id = networkId;
Madan Jampani38a88212015-09-15 11:21:27 -0700289 }
Satish Kab54ffa2015-11-27 14:19:42 +0530290 network = new DefaultTenantNetwork(
291 id,
292 name,
293 adminStateUp,
294 isState(state),
295 shared,
296 TenantId.tenantId(tenantId),
297 routerExternal,
298 isType(type),
299 PhysicalNetwork
300 .physicalNetwork(physicalNetwork),
301 SegmentationId
302 .segmentationId(segmentationId));
303 networksMap.putIfAbsent(id, network);
304
Madan Jampani38a88212015-09-15 11:21:27 -0700305 return Collections.unmodifiableCollection(networksMap.values());
306 }
307
308 /**
309 * Returns a collection of tenantNetworks.
310 *
311 * @param nodes the network jsonnodes
312 * @return a collection of tenantNetworks
313 */
314 public Iterable<TenantNetwork> changeJson2objs(JsonNode nodes) {
315 checkNotNull(nodes, JSON_NOT_NULL);
316 TenantNetwork network = null;
317 ConcurrentMap<TenantNetworkId, TenantNetwork> networksMap = Maps
318 .newConcurrentMap();
Satish Kab54ffa2015-11-27 14:19:42 +0530319 for (JsonNode node : nodes) {
320 String id = node.get("id").asText();
321 String name = node.get("name").asText();
322 boolean adminStateUp = node.get("admin_state_up").asBoolean();
323 String state = node.get("status").asText();
324 boolean shared = node.get("shared").asBoolean();
325 String tenantId = node.get("tenant_id").asText();
326 boolean routerExternal = node.get("router:external")
327 .asBoolean();
328 String type = node.get("provider:network_type").asText();
329 String physicalNetwork = node.get("provider:physical_network").asText();
330 String segmentationId = node.get("provider:segmentation_id").asText();
331 network = new DefaultTenantNetwork(
332 TenantNetworkId.networkId(id),
333 name,
334 adminStateUp,
335 isState(state),
336 shared,
337 TenantId.tenantId(tenantId),
338 routerExternal,
339 isType(type),
340 PhysicalNetwork.physicalNetwork(physicalNetwork),
341 SegmentationId.segmentationId(segmentationId));
342 networksMap.putIfAbsent(TenantNetworkId.networkId(id), network);
Madan Jampani38a88212015-09-15 11:21:27 -0700343 }
Satish Kab54ffa2015-11-27 14:19:42 +0530344
Madan Jampani38a88212015-09-15 11:21:27 -0700345 return Collections.unmodifiableCollection(networksMap.values());
346 }
347
348 /**
349 * Returns the specified item if that items is null; otherwise throws not
350 * found exception.
351 *
352 * @param item item to check
353 * @param <T> item type
354 * @param message not found message
355 * @return item if not null
356 * @throws org.onlab.util.ItemNotFoundException if item is null
357 */
358 protected <T> T nullIsNotFound(T item, String message) {
359 if (item == null) {
360 throw new ItemNotFoundException(message);
361 }
362 return item;
363 }
364}