blob: 606ffd86cab00ada42a303f1440d111da2479410 [file] [log] [blame]
Phaneendra Manda8db7d092016-06-04 00:17:24 +05301/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Phaneendra Manda8db7d092016-06-04 00:17:24 +05303 *
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 org.onlab.util.Tools.nullIsNotFound;
20
21import java.util.Set;
22
23import javax.ws.rs.Consumes;
24import javax.ws.rs.GET;
25import javax.ws.rs.Path;
26import javax.ws.rs.PathParam;
27import javax.ws.rs.Produces;
28import javax.ws.rs.core.MediaType;
29import javax.ws.rs.core.Response;
30
31import org.onosproject.codec.CodecContext;
32import org.onosproject.rest.AbstractWebResource;
33import org.onosproject.vtnrsc.LoadBalanceId;
34import org.onosproject.vtnrsc.PortChain;
35import org.onosproject.vtnrsc.PortChainId;
36import org.onosproject.vtnrsc.portchain.PortChainService;
37
38import com.fasterxml.jackson.databind.node.ObjectNode;
39
40/**
41 * Query and program port chain.
42 */
43
44@Path("portChainDeviceMap")
45public class PortChainDeviceMapWebResource extends AbstractWebResource {
46
47 public static final String PORT_CHAIN_NOT_FOUND = "Port chain not found";
48 public static final String PORT_CHAIN_ID_EXIST = "Port chain exists";
49 public static final String PORT_CHAIN_ID_NOT_EXIST = "Port chain does not exist with identifier";
50
51 private static final String NAME = "name";
52 private static final String ID = "id";
53 private static final String CLASSIFIERS = "classifiers";
54 private static final String FORWARDERS = "forwarders";
55 private static final String LOADBALANCEID = "loadBalanceId";
56
57 /**
58 * Get details of a specified port chain id.
59 *
60 * @param id port chain id
61 * @return 200 OK, 404 if given identifier does not exist
62 */
63 @GET
64 @Path("{chain_id}")
65 @Produces(MediaType.APPLICATION_JSON)
66 @Consumes(MediaType.APPLICATION_JSON)
67 public Response getPortChainDeviceMap(@PathParam("chain_id") String id) {
68
69 PortChain portChain = nullIsNotFound(get(PortChainService.class).getPortChain(PortChainId.of(id)),
70 PORT_CHAIN_NOT_FOUND);
71 ObjectNode result = mapper().createObjectNode();
72 result.set("portChainDeviceMap", encode(portChain, this));
73
74 return ok(result.toString()).build();
75 }
76
77 private ObjectNode encode(PortChain portChain, CodecContext context) {
78 checkNotNull(portChain, "portChain cannot be null");
79 ObjectNode result = context.mapper().createObjectNode();
80 result.put(ID, portChain.portChainId().toString())
81 .put(NAME, portChain.name());
82
83 Set<LoadBalanceId> loadBalanceIds = portChain.getLoadBalancePathMapKeys();
84 for (LoadBalanceId id : loadBalanceIds) {
85 result.put(LOADBALANCEID, id.toString())
86 .put(CLASSIFIERS, portChain.getSfcClassifiers(id).toString())
87 .put(FORWARDERS, portChain.getSfcForwarders(id).toString());
88 }
89 return result;
90 }
91}