blob: de2e84aef21e84bb9a33c1bee08ff545364078e8 [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;
Ray Milkey86ee5e82018-04-02 15:33:07 -070020import static org.onlab.util.Tools.readTreeFromStream;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053021
Priyanka Bf97e13d2016-07-28 10:57:15 +053022import java.util.Collection;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053023import java.io.IOException;
24import java.io.InputStream;
25import java.util.List;
26import java.util.LinkedList;
27
28import javax.ws.rs.Consumes;
29import javax.ws.rs.DELETE;
30import javax.ws.rs.GET;
31import javax.ws.rs.POST;
32import javax.ws.rs.PUT;
33import javax.ws.rs.Path;
34import javax.ws.rs.PathParam;
35import javax.ws.rs.Produces;
36import javax.ws.rs.core.MediaType;
37import javax.ws.rs.core.Response;
38
Priyanka Bbae0eeb12016-11-30 11:59:48 +053039import com.google.common.collect.ImmutableList;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053040import org.onosproject.incubator.net.tunnel.Tunnel;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053041import org.onosproject.incubator.net.tunnel.TunnelId;
Priyanka Bf97e13d2016-07-28 10:57:15 +053042import org.onosproject.incubator.net.tunnel.TunnelService;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053043import org.onosproject.net.DeviceId;
44import org.onosproject.net.intent.Constraint;
Priyanka Bbae0eeb12016-11-30 11:59:48 +053045import org.onosproject.pce.pceservice.ExplicitPathInfo;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053046import org.onosproject.pce.pceservice.api.PceService;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053047import org.onosproject.pce.pceservice.PcePath;
48import org.onosproject.pce.pceservice.DefaultPcePath;
49import org.onosproject.pce.pceservice.LspType;
50import org.onosproject.rest.AbstractWebResource;
51import org.slf4j.Logger;
52import org.slf4j.LoggerFactory;
53
54import com.fasterxml.jackson.databind.JsonNode;
55import com.fasterxml.jackson.databind.node.ArrayNode;
56import com.fasterxml.jackson.databind.node.ObjectNode;
57
58/**
59 * Query and program pce path.
60 */
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053061@Path("path")
62public class PcePathWebResource extends AbstractWebResource {
63
64 private final Logger log = LoggerFactory.getLogger(PcePathWebResource.class);
65 public static final String PCE_PATH_NOT_FOUND = "Path not found";
66 public static final String PCE_PATH_ID_EXIST = "Path exists";
67 public static final String PCE_PATH_ID_NOT_EXIST = "Path does not exist for the identifier";
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053068 public static final String PCE_SETUP_PATH_FAILED = "PCE Setup path has failed.";
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053069
70 /**
71 * Retrieve details of all paths created.
72 *
73 * @return 200 OK
74 */
75 @GET
76 @Produces(MediaType.APPLICATION_JSON)
Priyanka Bb977f562016-07-22 13:02:03 +053077 @Consumes(MediaType.APPLICATION_JSON)
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053078 public Response queryAllPath() {
79 log.debug("Query all paths.");
Priyanka Bbae0eeb12016-11-30 11:59:48 +053080 PceService pceService = get(PceService.class);
81 Iterable<Tunnel> tunnels = pceService.queryAllPath();
82
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053083 ObjectNode result = mapper().createObjectNode();
84 ArrayNode pathEntry = result.putArray("paths");
85 if (tunnels != null) {
86 for (final Tunnel tunnel : tunnels) {
Priyanka Bbae0eeb12016-11-30 11:59:48 +053087 PcePath path = DefaultPcePath.builder().of(tunnel, pceService.explicitPathInfoList(tunnel
88 .tunnelName().value()))
89 .build();
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053090 pathEntry.add(codec(PcePath.class).encode(path, this));
91 }
92 }
93 return ok(result.toString()).build();
94 }
95
96 /**
97 * Retrieve details of a specified path id.
98 *
99 * @param id path id
100 * @return 200 OK, 404 if given identifier does not exist
101 */
102 @GET
103 @Path("{path_id}")
104 @Produces(MediaType.APPLICATION_JSON)
Priyanka Bb977f562016-07-22 13:02:03 +0530105 @Consumes(MediaType.APPLICATION_JSON)
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530106 public Response queryPath(@PathParam("path_id") String id) {
107 log.debug("Query path by identifier {}.", id);
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530108 PceService pceService = get(PceService.class);
109
110 Tunnel tunnel = nullIsNotFound(pceService.queryPath(TunnelId.valueOf(id)),
111 PCE_PATH_NOT_FOUND);
112 PcePath path = DefaultPcePath.builder().of(tunnel, pceService.explicitPathInfoList(tunnel
113 .tunnelName().value())).build();
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530114 if (path == null) {
115 return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
116 }
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530117 ObjectNode result = mapper().createObjectNode();
118 result.set("path", codec(PcePath.class).encode(path, this));
119 return ok(result.toString()).build();
120 }
121
122 /**
123 * Creates a new path.
124 *
125 * @param stream pce path from json
126 * @return status of the request
127 */
128 @POST
129 @Consumes(MediaType.APPLICATION_JSON)
130 @Produces(MediaType.APPLICATION_JSON)
131 public Response setupPath(InputStream stream) {
132 log.debug("Setup path.");
133 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700134 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530135 JsonNode port = jsonTree.get("path");
Priyanka Bf97e13d2016-07-28 10:57:15 +0530136 TunnelService tunnelService = get(TunnelService.class);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530137 PcePath path = codec(PcePath.class).decode((ObjectNode) port, this);
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530138 if (path == null) {
139 return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
140 }
141
Priyanka Bf97e13d2016-07-28 10:57:15 +0530142 //Validating tunnel name, duplicated tunnel names not allowed
143 Collection<Tunnel> existingTunnels = tunnelService.queryTunnel(Tunnel.Type.MPLS);
144 if (existingTunnels != null) {
145 for (Tunnel t : existingTunnels) {
146 if (t.tunnelName().toString().equals(path.name())) {
147 return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
148 }
149 }
150 }
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530151
152 DeviceId srcDevice = DeviceId.deviceId(path.source());
153 DeviceId dstDevice = DeviceId.deviceId(path.destination());
154 LspType lspType = path.lspType();
155 List<Constraint> listConstrnt = new LinkedList<Constraint>();
156
Mahesh Poojary S33536202016-05-30 07:22:36 +0530157 // Add bandwidth
158 listConstrnt.add(path.bandwidthConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530159
Mahesh Poojary S33536202016-05-30 07:22:36 +0530160 // Add cost
161 listConstrnt.add(path.costConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530162
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530163 List<ExplicitPathInfo> explicitPathInfoList = null;
164 if (path.explicitPathInfo() != null) {
165 explicitPathInfoList = ImmutableList.copyOf(path.explicitPathInfo());
166 }
167
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530168 Boolean issuccess = nullIsNotFound(get(PceService.class)
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530169 .setupPath(srcDevice, dstDevice, path.name(), listConstrnt,
170 lspType, explicitPathInfoList),
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530171 PCE_SETUP_PATH_FAILED);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530172 return Response.status(OK).entity(issuccess.toString()).build();
173 } catch (IOException e) {
174 log.error("Exception while creating path {}.", e.toString());
175 throw new IllegalArgumentException(e);
176 }
177 }
178
179 /**
180 * Update details of a specified path id.
181 *
182 * @param id path id
183 * @param stream pce path from json
184 * @return 200 OK, 404 if given identifier does not exist
185 */
186 @PUT
187 @Path("{path_id}")
188 @Produces(MediaType.APPLICATION_JSON)
189 @Consumes(MediaType.APPLICATION_JSON)
190 public Response updatePath(@PathParam("path_id") String id,
191 final InputStream stream) {
192 log.debug("Update path by identifier {}.", id);
193 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700194 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530195 JsonNode pathNode = jsonTree.get("path");
196 PcePath path = codec(PcePath.class).decode((ObjectNode) pathNode, this);
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530197 if (path == null) {
198 return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
199 }
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530200 List<Constraint> constrntList = new LinkedList<Constraint>();
Mahesh Poojary S33536202016-05-30 07:22:36 +0530201 // Assign bandwidth
202 if (path.bandwidthConstraint() != null) {
203 constrntList.add(path.bandwidthConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530204 }
205
Mahesh Poojary S33536202016-05-30 07:22:36 +0530206 // Assign cost
207 if (path.costConstraint() != null) {
208 constrntList.add(path.costConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530209 }
210
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530211 Boolean result = nullIsNotFound(get(PceService.class).updatePath(TunnelId.valueOf(id), constrntList),
212 PCE_PATH_NOT_FOUND);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530213 return Response.status(OK).entity(result.toString()).build();
214 } catch (IOException e) {
215 log.error("Update path failed because of exception {}.", e.toString());
216 throw new IllegalArgumentException(e);
217 }
218 }
219
220 /**
221 * Release a specified path.
222 *
223 * @param id path id
224 * @return 200 OK, 404 if given identifier does not exist
225 */
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530226 @DELETE
Priyanka Bb977f562016-07-22 13:02:03 +0530227 @Path("{path_id}")
228 @Produces(MediaType.APPLICATION_JSON)
229 @Consumes(MediaType.APPLICATION_JSON)
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530230 public Response releasePath(@PathParam("path_id") String id) {
231 log.debug("Deletes path by identifier {}.", id);
232
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530233 Boolean isSuccess = nullIsNotFound(get(PceService.class).releasePath(TunnelId.valueOf(id)),
234 PCE_PATH_NOT_FOUND);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530235 if (!isSuccess) {
236 log.debug("Path identifier {} does not exist", id);
237 }
238
239 return Response.status(OK).entity(isSuccess.toString()).build();
240 }
241}