blob: a54e0b2a2ce785217b690885c4e84241dcb28505 [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 */
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053056@Path("path")
57public class PcePathWebResource extends AbstractWebResource {
58
59 private final Logger log = LoggerFactory.getLogger(PcePathWebResource.class);
60 public static final String PCE_PATH_NOT_FOUND = "Path not found";
61 public static final String PCE_PATH_ID_EXIST = "Path exists";
62 public static final String PCE_PATH_ID_NOT_EXIST = "Path does not exist for the identifier";
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053063 public static final String PCE_SETUP_PATH_FAILED = "PCE Setup path has failed.";
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053064
65 /**
66 * Retrieve details of all paths created.
67 *
68 * @return 200 OK
69 */
70 @GET
71 @Produces(MediaType.APPLICATION_JSON)
72 public Response queryAllPath() {
73 log.debug("Query all paths.");
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053074 Iterable<Tunnel> tunnels = get(PceService.class).queryAllPath();
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053075 ObjectNode result = mapper().createObjectNode();
76 ArrayNode pathEntry = result.putArray("paths");
77 if (tunnels != null) {
78 for (final Tunnel tunnel : tunnels) {
79 PcePath path = DefaultPcePath.builder().of(tunnel).build();
80 pathEntry.add(codec(PcePath.class).encode(path, this));
81 }
82 }
83 return ok(result.toString()).build();
84 }
85
86 /**
87 * Retrieve details of a specified path id.
88 *
89 * @param id path id
90 * @return 200 OK, 404 if given identifier does not exist
91 */
92 @GET
93 @Path("{path_id}")
94 @Produces(MediaType.APPLICATION_JSON)
95 public Response queryPath(@PathParam("path_id") String id) {
96 log.debug("Query path by identifier {}.", id);
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053097 Tunnel tunnel = nullIsNotFound(get(PceService.class).queryPath(TunnelId.valueOf(id)),
98 PCE_PATH_NOT_FOUND);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053099 PcePath path = DefaultPcePath.builder().of(tunnel).build();
100 ObjectNode result = mapper().createObjectNode();
101 result.set("path", codec(PcePath.class).encode(path, this));
102 return ok(result.toString()).build();
103 }
104
105 /**
106 * Creates a new path.
107 *
108 * @param stream pce path from json
109 * @return status of the request
110 */
111 @POST
112 @Consumes(MediaType.APPLICATION_JSON)
113 @Produces(MediaType.APPLICATION_JSON)
114 public Response setupPath(InputStream stream) {
115 log.debug("Setup path.");
116 try {
117 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
118 JsonNode port = jsonTree.get("path");
119 PcePath path = codec(PcePath.class).decode((ObjectNode) port, this);
120
121 DeviceId srcDevice = DeviceId.deviceId(path.source());
122 DeviceId dstDevice = DeviceId.deviceId(path.destination());
123 LspType lspType = path.lspType();
124 List<Constraint> listConstrnt = new LinkedList<Constraint>();
125
Mahesh Poojary S33536202016-05-30 07:22:36 +0530126 // Add bandwidth
127 listConstrnt.add(path.bandwidthConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530128
Mahesh Poojary S33536202016-05-30 07:22:36 +0530129 // Add cost
130 listConstrnt.add(path.costConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530131
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530132 Boolean issuccess = nullIsNotFound(get(PceService.class)
133 .setupPath(srcDevice, dstDevice, path.name(), listConstrnt, lspType),
134 PCE_SETUP_PATH_FAILED);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530135 return Response.status(OK).entity(issuccess.toString()).build();
136 } catch (IOException e) {
137 log.error("Exception while creating path {}.", e.toString());
138 throw new IllegalArgumentException(e);
139 }
140 }
141
142 /**
143 * Update details of a specified path id.
144 *
145 * @param id path id
146 * @param stream pce path from json
147 * @return 200 OK, 404 if given identifier does not exist
148 */
149 @PUT
150 @Path("{path_id}")
151 @Produces(MediaType.APPLICATION_JSON)
152 @Consumes(MediaType.APPLICATION_JSON)
153 public Response updatePath(@PathParam("path_id") String id,
154 final InputStream stream) {
155 log.debug("Update path by identifier {}.", id);
156 try {
157 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
158 JsonNode pathNode = jsonTree.get("path");
159 PcePath path = codec(PcePath.class).decode((ObjectNode) pathNode, this);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530160 List<Constraint> constrntList = new LinkedList<Constraint>();
Mahesh Poojary S33536202016-05-30 07:22:36 +0530161 // Assign bandwidth
162 if (path.bandwidthConstraint() != null) {
163 constrntList.add(path.bandwidthConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530164 }
165
Mahesh Poojary S33536202016-05-30 07:22:36 +0530166 // Assign cost
167 if (path.costConstraint() != null) {
168 constrntList.add(path.costConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530169 }
170
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530171 Boolean result = nullIsNotFound(get(PceService.class).updatePath(TunnelId.valueOf(id), constrntList),
172 PCE_PATH_NOT_FOUND);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530173 return Response.status(OK).entity(result.toString()).build();
174 } catch (IOException e) {
175 log.error("Update path failed because of exception {}.", e.toString());
176 throw new IllegalArgumentException(e);
177 }
178 }
179
180 /**
181 * Release a specified path.
182 *
183 * @param id path id
184 * @return 200 OK, 404 if given identifier does not exist
185 */
186 @Path("{path_id}")
187 @DELETE
188 public Response releasePath(@PathParam("path_id") String id) {
189 log.debug("Deletes path by identifier {}.", id);
190
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530191 Boolean isSuccess = nullIsNotFound(get(PceService.class).releasePath(TunnelId.valueOf(id)),
192 PCE_PATH_NOT_FOUND);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530193 if (!isSuccess) {
194 log.debug("Path identifier {} does not exist", id);
195 }
196
197 return Response.status(OK).entity(isSuccess.toString()).build();
198 }
199}