blob: 19244f7fe002d48690ed50eb6d7e5cf4abdac8b8 [file] [log] [blame]
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +05301/*
2 * Copyright 2016 Open Networking Laboratory
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.pce.web;
17
18import static javax.ws.rs.core.Response.Status.OK;
19
20import java.io.IOException;
21import java.io.InputStream;
22import java.util.List;
23import java.util.LinkedList;
24
25import javax.ws.rs.Consumes;
26import javax.ws.rs.DELETE;
27import javax.ws.rs.GET;
28import javax.ws.rs.POST;
29import javax.ws.rs.PUT;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
32import javax.ws.rs.Produces;
33import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
35
36import org.onosproject.incubator.net.tunnel.Tunnel;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.intent.Constraint;
39import org.onosproject.pce.pceservice.PcePath;
40import org.onosproject.pce.pceservice.DefaultPcePath;
41import org.onosproject.pce.pceservice.LspType;
42import org.onosproject.rest.AbstractWebResource;
43import org.slf4j.Logger;
44import org.slf4j.LoggerFactory;
45
46import com.fasterxml.jackson.databind.JsonNode;
47import com.fasterxml.jackson.databind.node.ArrayNode;
48import com.fasterxml.jackson.databind.node.ObjectNode;
49
50/**
51 * Query and program pce path.
52 */
53
54@Path("path")
55public class PcePathWebResource extends AbstractWebResource {
56
57 private final Logger log = LoggerFactory.getLogger(PcePathWebResource.class);
58 public static final String PCE_PATH_NOT_FOUND = "Path not found";
59 public static final String PCE_PATH_ID_EXIST = "Path exists";
60 public static final String PCE_PATH_ID_NOT_EXIST = "Path does not exist for the identifier";
61
62 /**
63 * Retrieve details of all paths created.
64 *
65 * @return 200 OK
66 */
67 @GET
68 @Produces(MediaType.APPLICATION_JSON)
69 public Response queryAllPath() {
70 log.debug("Query all paths.");
71 //TODO: need to uncomment below line once queryAllPath method is added to PceService
72 Iterable<Tunnel> tunnels = null; // = get(PceService.class).queryAllPath();
73 ObjectNode result = mapper().createObjectNode();
74 ArrayNode pathEntry = result.putArray("paths");
75 if (tunnels != null) {
76 for (final Tunnel tunnel : tunnels) {
77 PcePath path = DefaultPcePath.builder().of(tunnel).build();
78 pathEntry.add(codec(PcePath.class).encode(path, this));
79 }
80 }
81 return ok(result.toString()).build();
82 }
83
84 /**
85 * Retrieve details of a specified path id.
86 *
87 * @param id path id
88 * @return 200 OK, 404 if given identifier does not exist
89 */
90 @GET
91 @Path("{path_id}")
92 @Produces(MediaType.APPLICATION_JSON)
93 public Response queryPath(@PathParam("path_id") String id) {
94 log.debug("Query path by identifier {}.", id);
95 //TODO: need to uncomment below lines once queryPath method is added to PceService
96 Tunnel tunnel = null; // = nullIsNotFound(get(PceService.class).queryPath(PcePathId.of(id)),
97 //PCE_PATH_NOT_FOUND);
98 PcePath path = DefaultPcePath.builder().of(tunnel).build();
99 ObjectNode result = mapper().createObjectNode();
100 result.set("path", codec(PcePath.class).encode(path, this));
101 return ok(result.toString()).build();
102 }
103
104 /**
105 * Creates a new path.
106 *
107 * @param stream pce path from json
108 * @return status of the request
109 */
110 @POST
111 @Consumes(MediaType.APPLICATION_JSON)
112 @Produces(MediaType.APPLICATION_JSON)
113 public Response setupPath(InputStream stream) {
114 log.debug("Setup path.");
115 try {
116 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
117 JsonNode port = jsonTree.get("path");
118 PcePath path = codec(PcePath.class).decode((ObjectNode) port, this);
119
120 DeviceId srcDevice = DeviceId.deviceId(path.source());
121 DeviceId dstDevice = DeviceId.deviceId(path.destination());
122 LspType lspType = path.lspType();
123 List<Constraint> listConstrnt = new LinkedList<Constraint>();
124
125 // add cost
126 //TODO: need to uncomment below lines once Bandwidth and Cost constraint classes are ready
127 //CostConstraint.Type costType = CostConstraint.Type.values()[Integer.valueOf(path.constraint().cost())];
128 //listConstrnt.add(CostConstraint.of(costType));
129
130 // add bandwidth. Data rate unit is in BPS.
131 //listConstrnt.add(LocalBandwidthConstraint.of(Double.valueOf(path.constraint().bandwidth()), DataRateUnit
132 // .valueOf("BPS")));
133
134 //TODO: need to uncomment below lines once setupPath method is modified in PceService
135 Boolean issuccess = true; // = (null != get(PceService.class)
136 //.setupPath(srcDevice, dstDevice, path.name(), listConstrnt, lspType)) ? true : false;
137 return Response.status(OK).entity(issuccess.toString()).build();
138 } catch (IOException e) {
139 log.error("Exception while creating path {}.", e.toString());
140 throw new IllegalArgumentException(e);
141 }
142 }
143
144 /**
145 * Update details of a specified path id.
146 *
147 * @param id path id
148 * @param stream pce path from json
149 * @return 200 OK, 404 if given identifier does not exist
150 */
151 @PUT
152 @Path("{path_id}")
153 @Produces(MediaType.APPLICATION_JSON)
154 @Consumes(MediaType.APPLICATION_JSON)
155 public Response updatePath(@PathParam("path_id") String id,
156 final InputStream stream) {
157 log.debug("Update path by identifier {}.", id);
158 try {
159 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
160 JsonNode pathNode = jsonTree.get("path");
161 PcePath path = codec(PcePath.class).decode((ObjectNode) pathNode, this);
162 // Assign cost
163 List<Constraint> constrntList = new LinkedList<Constraint>();
164 //TODO: need to uncomment below lines once CostConstraint class is ready
165 if (path.costConstraint() != null) {
166 //CostConstraint.Type costType = CostConstraint.Type.values()[path.constraint().cost()];
167 //constrntList.add(CostConstraint.of(costType));
168 }
169
170 // Assign bandwidth. Data rate unit is in BPS.
171 if (path.bandwidthConstraint() != null) {
172 //TODO: need to uncomment below lines once BandwidthConstraint class is ready
173 //constrntList.add(LocalBandwidthConstraint
174 // .of(path.constraint().bandwidth(), DataRateUnit.valueOf("BPS")));
175 }
176
177 //TODO: need to uncomment below line once updatePath is added to PceService
178 Boolean result = true; // = (null != (get(PceService.class).updatePath(PcePathId.of(id), constrntList)))
179 //? true : false;
180 return Response.status(OK).entity(result.toString()).build();
181 } catch (IOException e) {
182 log.error("Update path failed because of exception {}.", e.toString());
183 throw new IllegalArgumentException(e);
184 }
185 }
186
187 /**
188 * Release a specified path.
189 *
190 * @param id path id
191 * @return 200 OK, 404 if given identifier does not exist
192 */
193 @Path("{path_id}")
194 @DELETE
195 public Response releasePath(@PathParam("path_id") String id) {
196 log.debug("Deletes path by identifier {}.", id);
197
198 //TODO: need to uncomment below lines once releasePath method is added to PceService
199 Boolean isSuccess = true; // = nullIsNotFound(get(PceService.class).releasePath(PcePathId.of(id)),
200 //PCE_PATH_NOT_FOUND);
201 if (!isSuccess) {
202 log.debug("Path identifier {} does not exist", id);
203 }
204
205 return Response.status(OK).entity(isSuccess.toString()).build();
206 }
207}