blob: a3cd11ed47f46c5ac3a43f51af767ed499bf7c78 [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 */
Priyanka Bb977f562016-07-22 13:02:03 +053016package org.onosproject.pcerest;
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
Priyanka Bf97e13d2016-07-28 10:57:15 +053021import java.util.Collection;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053022import java.io.IOException;
23import java.io.InputStream;
24import java.util.List;
25import java.util.LinkedList;
26
27import javax.ws.rs.Consumes;
28import javax.ws.rs.DELETE;
29import javax.ws.rs.GET;
30import javax.ws.rs.POST;
31import javax.ws.rs.PUT;
32import javax.ws.rs.Path;
33import javax.ws.rs.PathParam;
34import javax.ws.rs.Produces;
35import javax.ws.rs.core.MediaType;
36import javax.ws.rs.core.Response;
37
38import org.onosproject.incubator.net.tunnel.Tunnel;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053039import org.onosproject.incubator.net.tunnel.TunnelId;
Priyanka Bf97e13d2016-07-28 10:57:15 +053040import org.onosproject.incubator.net.tunnel.TunnelService;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053041import org.onosproject.net.DeviceId;
42import org.onosproject.net.intent.Constraint;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053043import org.onosproject.pce.pceservice.api.PceService;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053044import org.onosproject.pce.pceservice.PcePath;
45import org.onosproject.pce.pceservice.DefaultPcePath;
46import org.onosproject.pce.pceservice.LspType;
47import org.onosproject.rest.AbstractWebResource;
48import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50
51import com.fasterxml.jackson.databind.JsonNode;
52import com.fasterxml.jackson.databind.node.ArrayNode;
53import com.fasterxml.jackson.databind.node.ObjectNode;
54
55/**
56 * Query and program pce path.
57 */
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053058@Path("path")
59public class PcePathWebResource extends AbstractWebResource {
60
61 private final Logger log = LoggerFactory.getLogger(PcePathWebResource.class);
62 public static final String PCE_PATH_NOT_FOUND = "Path not found";
63 public static final String PCE_PATH_ID_EXIST = "Path exists";
64 public static final String PCE_PATH_ID_NOT_EXIST = "Path does not exist for the identifier";
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053065 public static final String PCE_SETUP_PATH_FAILED = "PCE Setup path has failed.";
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053066
67 /**
68 * Retrieve details of all paths created.
69 *
70 * @return 200 OK
71 */
72 @GET
73 @Produces(MediaType.APPLICATION_JSON)
Priyanka Bb977f562016-07-22 13:02:03 +053074 @Consumes(MediaType.APPLICATION_JSON)
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053075 public Response queryAllPath() {
76 log.debug("Query all paths.");
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053077 Iterable<Tunnel> tunnels = get(PceService.class).queryAllPath();
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053078 ObjectNode result = mapper().createObjectNode();
79 ArrayNode pathEntry = result.putArray("paths");
80 if (tunnels != null) {
81 for (final Tunnel tunnel : tunnels) {
82 PcePath path = DefaultPcePath.builder().of(tunnel).build();
83 pathEntry.add(codec(PcePath.class).encode(path, this));
84 }
85 }
86 return ok(result.toString()).build();
87 }
88
89 /**
90 * Retrieve details of a specified path id.
91 *
92 * @param id path id
93 * @return 200 OK, 404 if given identifier does not exist
94 */
95 @GET
96 @Path("{path_id}")
97 @Produces(MediaType.APPLICATION_JSON)
Priyanka Bb977f562016-07-22 13:02:03 +053098 @Consumes(MediaType.APPLICATION_JSON)
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053099 public Response queryPath(@PathParam("path_id") String id) {
100 log.debug("Query path by identifier {}.", id);
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530101 Tunnel tunnel = nullIsNotFound(get(PceService.class).queryPath(TunnelId.valueOf(id)),
102 PCE_PATH_NOT_FOUND);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530103 PcePath path = DefaultPcePath.builder().of(tunnel).build();
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530104 if (path == null) {
105 return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
106 }
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530107 ObjectNode result = mapper().createObjectNode();
108 result.set("path", codec(PcePath.class).encode(path, this));
109 return ok(result.toString()).build();
110 }
111
112 /**
113 * Creates a new path.
114 *
115 * @param stream pce path from json
116 * @return status of the request
117 */
118 @POST
119 @Consumes(MediaType.APPLICATION_JSON)
120 @Produces(MediaType.APPLICATION_JSON)
121 public Response setupPath(InputStream stream) {
122 log.debug("Setup path.");
123 try {
124 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
125 JsonNode port = jsonTree.get("path");
Priyanka Bf97e13d2016-07-28 10:57:15 +0530126 TunnelService tunnelService = get(TunnelService.class);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530127 PcePath path = codec(PcePath.class).decode((ObjectNode) port, this);
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530128 if (path == null) {
129 return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
130 }
131
Priyanka Bf97e13d2016-07-28 10:57:15 +0530132 //Validating tunnel name, duplicated tunnel names not allowed
133 Collection<Tunnel> existingTunnels = tunnelService.queryTunnel(Tunnel.Type.MPLS);
134 if (existingTunnels != null) {
135 for (Tunnel t : existingTunnels) {
136 if (t.tunnelName().toString().equals(path.name())) {
137 return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
138 }
139 }
140 }
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530141
142 DeviceId srcDevice = DeviceId.deviceId(path.source());
143 DeviceId dstDevice = DeviceId.deviceId(path.destination());
144 LspType lspType = path.lspType();
145 List<Constraint> listConstrnt = new LinkedList<Constraint>();
146
Mahesh Poojary S33536202016-05-30 07:22:36 +0530147 // Add bandwidth
148 listConstrnt.add(path.bandwidthConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530149
Mahesh Poojary S33536202016-05-30 07:22:36 +0530150 // Add cost
151 listConstrnt.add(path.costConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530152
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530153 Boolean issuccess = nullIsNotFound(get(PceService.class)
154 .setupPath(srcDevice, dstDevice, path.name(), listConstrnt, lspType),
155 PCE_SETUP_PATH_FAILED);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530156 return Response.status(OK).entity(issuccess.toString()).build();
157 } catch (IOException e) {
158 log.error("Exception while creating path {}.", e.toString());
159 throw new IllegalArgumentException(e);
160 }
161 }
162
163 /**
164 * Update details of a specified path id.
165 *
166 * @param id path id
167 * @param stream pce path from json
168 * @return 200 OK, 404 if given identifier does not exist
169 */
170 @PUT
171 @Path("{path_id}")
172 @Produces(MediaType.APPLICATION_JSON)
173 @Consumes(MediaType.APPLICATION_JSON)
174 public Response updatePath(@PathParam("path_id") String id,
175 final InputStream stream) {
176 log.debug("Update path by identifier {}.", id);
177 try {
178 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
179 JsonNode pathNode = jsonTree.get("path");
180 PcePath path = codec(PcePath.class).decode((ObjectNode) pathNode, this);
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530181 if (path == null) {
182 return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
183 }
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530184 List<Constraint> constrntList = new LinkedList<Constraint>();
Mahesh Poojary S33536202016-05-30 07:22:36 +0530185 // Assign bandwidth
186 if (path.bandwidthConstraint() != null) {
187 constrntList.add(path.bandwidthConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530188 }
189
Mahesh Poojary S33536202016-05-30 07:22:36 +0530190 // Assign cost
191 if (path.costConstraint() != null) {
192 constrntList.add(path.costConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530193 }
194
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530195 Boolean result = nullIsNotFound(get(PceService.class).updatePath(TunnelId.valueOf(id), constrntList),
196 PCE_PATH_NOT_FOUND);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530197 return Response.status(OK).entity(result.toString()).build();
198 } catch (IOException e) {
199 log.error("Update path failed because of exception {}.", e.toString());
200 throw new IllegalArgumentException(e);
201 }
202 }
203
204 /**
205 * Release a specified path.
206 *
207 * @param id path id
208 * @return 200 OK, 404 if given identifier does not exist
209 */
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530210 @DELETE
Priyanka Bb977f562016-07-22 13:02:03 +0530211 @Path("{path_id}")
212 @Produces(MediaType.APPLICATION_JSON)
213 @Consumes(MediaType.APPLICATION_JSON)
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530214 public Response releasePath(@PathParam("path_id") String id) {
215 log.debug("Deletes path by identifier {}.", id);
216
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530217 Boolean isSuccess = nullIsNotFound(get(PceService.class).releasePath(TunnelId.valueOf(id)),
218 PCE_PATH_NOT_FOUND);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530219 if (!isSuccess) {
220 log.debug("Path identifier {} does not exist", id);
221 }
222
223 return Response.status(OK).entity(isSuccess.toString()).build();
224 }
225}