blob: be8170253bc5bea72e4ae2a982c7d74bc637611e [file] [log] [blame]
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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
Priyanka Bbae0eeb12016-11-30 11:59:48 +053038import com.google.common.collect.ImmutableList;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053039import org.onosproject.incubator.net.tunnel.Tunnel;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053040import org.onosproject.incubator.net.tunnel.TunnelId;
Priyanka Bf97e13d2016-07-28 10:57:15 +053041import org.onosproject.incubator.net.tunnel.TunnelService;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053042import org.onosproject.net.DeviceId;
43import org.onosproject.net.intent.Constraint;
Priyanka Bbae0eeb12016-11-30 11:59:48 +053044import org.onosproject.pce.pceservice.ExplicitPathInfo;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053045import org.onosproject.pce.pceservice.api.PceService;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053046import org.onosproject.pce.pceservice.PcePath;
47import org.onosproject.pce.pceservice.DefaultPcePath;
48import org.onosproject.pce.pceservice.LspType;
49import org.onosproject.rest.AbstractWebResource;
50import org.slf4j.Logger;
51import org.slf4j.LoggerFactory;
52
53import com.fasterxml.jackson.databind.JsonNode;
54import com.fasterxml.jackson.databind.node.ArrayNode;
55import com.fasterxml.jackson.databind.node.ObjectNode;
56
57/**
58 * Query and program pce path.
59 */
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053060@Path("path")
61public class PcePathWebResource extends AbstractWebResource {
62
63 private final Logger log = LoggerFactory.getLogger(PcePathWebResource.class);
64 public static final String PCE_PATH_NOT_FOUND = "Path not found";
65 public static final String PCE_PATH_ID_EXIST = "Path exists";
66 public static final String PCE_PATH_ID_NOT_EXIST = "Path does not exist for the identifier";
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053067 public static final String PCE_SETUP_PATH_FAILED = "PCE Setup path has failed.";
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053068
69 /**
70 * Retrieve details of all paths created.
71 *
72 * @return 200 OK
73 */
74 @GET
75 @Produces(MediaType.APPLICATION_JSON)
Priyanka Bb977f562016-07-22 13:02:03 +053076 @Consumes(MediaType.APPLICATION_JSON)
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053077 public Response queryAllPath() {
78 log.debug("Query all paths.");
Priyanka Bbae0eeb12016-11-30 11:59:48 +053079 PceService pceService = get(PceService.class);
80 Iterable<Tunnel> tunnels = pceService.queryAllPath();
81
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053082 ObjectNode result = mapper().createObjectNode();
83 ArrayNode pathEntry = result.putArray("paths");
84 if (tunnels != null) {
85 for (final Tunnel tunnel : tunnels) {
Priyanka Bbae0eeb12016-11-30 11:59:48 +053086 PcePath path = DefaultPcePath.builder().of(tunnel, pceService.explicitPathInfoList(tunnel
87 .tunnelName().value()))
88 .build();
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053089 pathEntry.add(codec(PcePath.class).encode(path, this));
90 }
91 }
92 return ok(result.toString()).build();
93 }
94
95 /**
96 * Retrieve details of a specified path id.
97 *
98 * @param id path id
99 * @return 200 OK, 404 if given identifier does not exist
100 */
101 @GET
102 @Path("{path_id}")
103 @Produces(MediaType.APPLICATION_JSON)
Priyanka Bb977f562016-07-22 13:02:03 +0530104 @Consumes(MediaType.APPLICATION_JSON)
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530105 public Response queryPath(@PathParam("path_id") String id) {
106 log.debug("Query path by identifier {}.", id);
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530107 PceService pceService = get(PceService.class);
108
109 Tunnel tunnel = nullIsNotFound(pceService.queryPath(TunnelId.valueOf(id)),
110 PCE_PATH_NOT_FOUND);
111 PcePath path = DefaultPcePath.builder().of(tunnel, pceService.explicitPathInfoList(tunnel
112 .tunnelName().value())).build();
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530113 if (path == null) {
114 return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
115 }
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530116 ObjectNode result = mapper().createObjectNode();
117 result.set("path", codec(PcePath.class).encode(path, this));
118 return ok(result.toString()).build();
119 }
120
121 /**
122 * Creates a new path.
123 *
124 * @param stream pce path from json
125 * @return status of the request
126 */
127 @POST
128 @Consumes(MediaType.APPLICATION_JSON)
129 @Produces(MediaType.APPLICATION_JSON)
130 public Response setupPath(InputStream stream) {
131 log.debug("Setup path.");
132 try {
133 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
134 JsonNode port = jsonTree.get("path");
Priyanka Bf97e13d2016-07-28 10:57:15 +0530135 TunnelService tunnelService = get(TunnelService.class);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530136 PcePath path = codec(PcePath.class).decode((ObjectNode) port, this);
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530137 if (path == null) {
138 return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
139 }
140
Priyanka Bf97e13d2016-07-28 10:57:15 +0530141 //Validating tunnel name, duplicated tunnel names not allowed
142 Collection<Tunnel> existingTunnels = tunnelService.queryTunnel(Tunnel.Type.MPLS);
143 if (existingTunnels != null) {
144 for (Tunnel t : existingTunnels) {
145 if (t.tunnelName().toString().equals(path.name())) {
146 return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
147 }
148 }
149 }
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530150
151 DeviceId srcDevice = DeviceId.deviceId(path.source());
152 DeviceId dstDevice = DeviceId.deviceId(path.destination());
153 LspType lspType = path.lspType();
154 List<Constraint> listConstrnt = new LinkedList<Constraint>();
155
Mahesh Poojary S33536202016-05-30 07:22:36 +0530156 // Add bandwidth
157 listConstrnt.add(path.bandwidthConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530158
Mahesh Poojary S33536202016-05-30 07:22:36 +0530159 // Add cost
160 listConstrnt.add(path.costConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530161
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530162 List<ExplicitPathInfo> explicitPathInfoList = null;
163 if (path.explicitPathInfo() != null) {
164 explicitPathInfoList = ImmutableList.copyOf(path.explicitPathInfo());
165 }
166
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530167 Boolean issuccess = nullIsNotFound(get(PceService.class)
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530168 .setupPath(srcDevice, dstDevice, path.name(), listConstrnt,
169 lspType, explicitPathInfoList),
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530170 PCE_SETUP_PATH_FAILED);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530171 return Response.status(OK).entity(issuccess.toString()).build();
172 } catch (IOException e) {
173 log.error("Exception while creating path {}.", e.toString());
174 throw new IllegalArgumentException(e);
175 }
176 }
177
178 /**
179 * Update details of a specified path id.
180 *
181 * @param id path id
182 * @param stream pce path from json
183 * @return 200 OK, 404 if given identifier does not exist
184 */
185 @PUT
186 @Path("{path_id}")
187 @Produces(MediaType.APPLICATION_JSON)
188 @Consumes(MediaType.APPLICATION_JSON)
189 public Response updatePath(@PathParam("path_id") String id,
190 final InputStream stream) {
191 log.debug("Update path by identifier {}.", id);
192 try {
193 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
194 JsonNode pathNode = jsonTree.get("path");
195 PcePath path = codec(PcePath.class).decode((ObjectNode) pathNode, this);
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530196 if (path == null) {
197 return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
198 }
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530199 List<Constraint> constrntList = new LinkedList<Constraint>();
Mahesh Poojary S33536202016-05-30 07:22:36 +0530200 // Assign bandwidth
201 if (path.bandwidthConstraint() != null) {
202 constrntList.add(path.bandwidthConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530203 }
204
Mahesh Poojary S33536202016-05-30 07:22:36 +0530205 // Assign cost
206 if (path.costConstraint() != null) {
207 constrntList.add(path.costConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530208 }
209
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530210 Boolean result = nullIsNotFound(get(PceService.class).updatePath(TunnelId.valueOf(id), constrntList),
211 PCE_PATH_NOT_FOUND);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530212 return Response.status(OK).entity(result.toString()).build();
213 } catch (IOException e) {
214 log.error("Update path failed because of exception {}.", e.toString());
215 throw new IllegalArgumentException(e);
216 }
217 }
218
219 /**
220 * Release a specified path.
221 *
222 * @param id path id
223 * @return 200 OK, 404 if given identifier does not exist
224 */
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530225 @DELETE
Priyanka Bb977f562016-07-22 13:02:03 +0530226 @Path("{path_id}")
227 @Produces(MediaType.APPLICATION_JSON)
228 @Consumes(MediaType.APPLICATION_JSON)
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530229 public Response releasePath(@PathParam("path_id") String id) {
230 log.debug("Deletes path by identifier {}.", id);
231
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530232 Boolean isSuccess = nullIsNotFound(get(PceService.class).releasePath(TunnelId.valueOf(id)),
233 PCE_PATH_NOT_FOUND);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530234 if (!isSuccess) {
235 log.debug("Path identifier {} does not exist", id);
236 }
237
238 return Response.status(OK).entity(isSuccess.toString()).build();
239 }
240}