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