blob: 2b0ba86e88967416c3d53d54ecb22df0b1337311 [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();
104 ObjectNode result = mapper().createObjectNode();
105 result.set("path", codec(PcePath.class).encode(path, this));
106 return ok(result.toString()).build();
107 }
108
109 /**
110 * Creates a new path.
111 *
112 * @param stream pce path from json
113 * @return status of the request
114 */
115 @POST
116 @Consumes(MediaType.APPLICATION_JSON)
117 @Produces(MediaType.APPLICATION_JSON)
118 public Response setupPath(InputStream stream) {
119 log.debug("Setup path.");
120 try {
121 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
122 JsonNode port = jsonTree.get("path");
Priyanka Bf97e13d2016-07-28 10:57:15 +0530123 TunnelService tunnelService = get(TunnelService.class);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530124 PcePath path = codec(PcePath.class).decode((ObjectNode) port, this);
Priyanka Bf97e13d2016-07-28 10:57:15 +0530125 //Validating tunnel name, duplicated tunnel names not allowed
126 Collection<Tunnel> existingTunnels = tunnelService.queryTunnel(Tunnel.Type.MPLS);
127 if (existingTunnels != null) {
128 for (Tunnel t : existingTunnels) {
129 if (t.tunnelName().toString().equals(path.name())) {
130 return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
131 }
132 }
133 }
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530134
135 DeviceId srcDevice = DeviceId.deviceId(path.source());
136 DeviceId dstDevice = DeviceId.deviceId(path.destination());
137 LspType lspType = path.lspType();
138 List<Constraint> listConstrnt = new LinkedList<Constraint>();
139
Mahesh Poojary S33536202016-05-30 07:22:36 +0530140 // Add bandwidth
141 listConstrnt.add(path.bandwidthConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530142
Mahesh Poojary S33536202016-05-30 07:22:36 +0530143 // Add cost
144 listConstrnt.add(path.costConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530145
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530146 Boolean issuccess = nullIsNotFound(get(PceService.class)
147 .setupPath(srcDevice, dstDevice, path.name(), listConstrnt, lspType),
148 PCE_SETUP_PATH_FAILED);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530149 return Response.status(OK).entity(issuccess.toString()).build();
150 } catch (IOException e) {
151 log.error("Exception while creating path {}.", e.toString());
152 throw new IllegalArgumentException(e);
153 }
154 }
155
156 /**
157 * Update details of a specified path id.
158 *
159 * @param id path id
160 * @param stream pce path from json
161 * @return 200 OK, 404 if given identifier does not exist
162 */
163 @PUT
164 @Path("{path_id}")
165 @Produces(MediaType.APPLICATION_JSON)
166 @Consumes(MediaType.APPLICATION_JSON)
167 public Response updatePath(@PathParam("path_id") String id,
168 final InputStream stream) {
169 log.debug("Update path by identifier {}.", id);
170 try {
171 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
172 JsonNode pathNode = jsonTree.get("path");
173 PcePath path = codec(PcePath.class).decode((ObjectNode) pathNode, this);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530174 List<Constraint> constrntList = new LinkedList<Constraint>();
Mahesh Poojary S33536202016-05-30 07:22:36 +0530175 // Assign bandwidth
176 if (path.bandwidthConstraint() != null) {
177 constrntList.add(path.bandwidthConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530178 }
179
Mahesh Poojary S33536202016-05-30 07:22:36 +0530180 // Assign cost
181 if (path.costConstraint() != null) {
182 constrntList.add(path.costConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530183 }
184
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530185 Boolean result = nullIsNotFound(get(PceService.class).updatePath(TunnelId.valueOf(id), constrntList),
186 PCE_PATH_NOT_FOUND);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530187 return Response.status(OK).entity(result.toString()).build();
188 } catch (IOException e) {
189 log.error("Update path failed because of exception {}.", e.toString());
190 throw new IllegalArgumentException(e);
191 }
192 }
193
194 /**
195 * Release a specified path.
196 *
197 * @param id path id
198 * @return 200 OK, 404 if given identifier does not exist
199 */
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530200 @DELETE
Priyanka Bb977f562016-07-22 13:02:03 +0530201 @Path("{path_id}")
202 @Produces(MediaType.APPLICATION_JSON)
203 @Consumes(MediaType.APPLICATION_JSON)
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530204 public Response releasePath(@PathParam("path_id") String id) {
205 log.debug("Deletes path by identifier {}.", id);
206
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530207 Boolean isSuccess = nullIsNotFound(get(PceService.class).releasePath(TunnelId.valueOf(id)),
208 PCE_PATH_NOT_FOUND);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530209 if (!isSuccess) {
210 log.debug("Path identifier {} does not exist", id);
211 }
212
213 return Response.status(OK).entity(isSuccess.toString()).build();
214 }
215}