blob: 1b7c48b66f485fa5aaea85865130cb98b644fc46 [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
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)
Priyanka Bb977f562016-07-22 13:02:03 +053072 @Consumes(MediaType.APPLICATION_JSON)
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053073 public Response queryAllPath() {
74 log.debug("Query all paths.");
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053075 Iterable<Tunnel> tunnels = get(PceService.class).queryAllPath();
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053076 ObjectNode result = mapper().createObjectNode();
77 ArrayNode pathEntry = result.putArray("paths");
78 if (tunnels != null) {
79 for (final Tunnel tunnel : tunnels) {
80 PcePath path = DefaultPcePath.builder().of(tunnel).build();
81 pathEntry.add(codec(PcePath.class).encode(path, this));
82 }
83 }
84 return ok(result.toString()).build();
85 }
86
87 /**
88 * Retrieve details of a specified path id.
89 *
90 * @param id path id
91 * @return 200 OK, 404 if given identifier does not exist
92 */
93 @GET
94 @Path("{path_id}")
95 @Produces(MediaType.APPLICATION_JSON)
Priyanka Bb977f562016-07-22 13:02:03 +053096 @Consumes(MediaType.APPLICATION_JSON)
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053097 public Response queryPath(@PathParam("path_id") String id) {
98 log.debug("Query path by identifier {}.", id);
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053099 Tunnel tunnel = nullIsNotFound(get(PceService.class).queryPath(TunnelId.valueOf(id)),
100 PCE_PATH_NOT_FOUND);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530101 PcePath path = DefaultPcePath.builder().of(tunnel).build();
102 ObjectNode result = mapper().createObjectNode();
103 result.set("path", codec(PcePath.class).encode(path, this));
104 return ok(result.toString()).build();
105 }
106
107 /**
108 * Creates a new path.
109 *
110 * @param stream pce path from json
111 * @return status of the request
112 */
113 @POST
114 @Consumes(MediaType.APPLICATION_JSON)
115 @Produces(MediaType.APPLICATION_JSON)
116 public Response setupPath(InputStream stream) {
117 log.debug("Setup path.");
118 try {
119 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
120 JsonNode port = jsonTree.get("path");
121 PcePath path = codec(PcePath.class).decode((ObjectNode) port, this);
122
123 DeviceId srcDevice = DeviceId.deviceId(path.source());
124 DeviceId dstDevice = DeviceId.deviceId(path.destination());
125 LspType lspType = path.lspType();
126 List<Constraint> listConstrnt = new LinkedList<Constraint>();
127
Mahesh Poojary S33536202016-05-30 07:22:36 +0530128 // Add bandwidth
129 listConstrnt.add(path.bandwidthConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530130
Mahesh Poojary S33536202016-05-30 07:22:36 +0530131 // Add cost
132 listConstrnt.add(path.costConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530133
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530134 Boolean issuccess = nullIsNotFound(get(PceService.class)
135 .setupPath(srcDevice, dstDevice, path.name(), listConstrnt, lspType),
136 PCE_SETUP_PATH_FAILED);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530137 return Response.status(OK).entity(issuccess.toString()).build();
138 } catch (IOException e) {
139 log.error("Exception while creating path {}.", e.toString());
140 throw new IllegalArgumentException(e);
141 }
142 }
143
144 /**
145 * Update details of a specified path id.
146 *
147 * @param id path id
148 * @param stream pce path from json
149 * @return 200 OK, 404 if given identifier does not exist
150 */
151 @PUT
152 @Path("{path_id}")
153 @Produces(MediaType.APPLICATION_JSON)
154 @Consumes(MediaType.APPLICATION_JSON)
155 public Response updatePath(@PathParam("path_id") String id,
156 final InputStream stream) {
157 log.debug("Update path by identifier {}.", id);
158 try {
159 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
160 JsonNode pathNode = jsonTree.get("path");
161 PcePath path = codec(PcePath.class).decode((ObjectNode) pathNode, this);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530162 List<Constraint> constrntList = new LinkedList<Constraint>();
Mahesh Poojary S33536202016-05-30 07:22:36 +0530163 // Assign bandwidth
164 if (path.bandwidthConstraint() != null) {
165 constrntList.add(path.bandwidthConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530166 }
167
Mahesh Poojary S33536202016-05-30 07:22:36 +0530168 // Assign cost
169 if (path.costConstraint() != null) {
170 constrntList.add(path.costConstraint());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530171 }
172
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530173 Boolean result = nullIsNotFound(get(PceService.class).updatePath(TunnelId.valueOf(id), constrntList),
174 PCE_PATH_NOT_FOUND);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530175 return Response.status(OK).entity(result.toString()).build();
176 } catch (IOException e) {
177 log.error("Update path failed because of exception {}.", e.toString());
178 throw new IllegalArgumentException(e);
179 }
180 }
181
182 /**
183 * Release a specified path.
184 *
185 * @param id path id
186 * @return 200 OK, 404 if given identifier does not exist
187 */
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530188 @DELETE
Priyanka Bb977f562016-07-22 13:02:03 +0530189 @Path("{path_id}")
190 @Produces(MediaType.APPLICATION_JSON)
191 @Consumes(MediaType.APPLICATION_JSON)
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530192 public Response releasePath(@PathParam("path_id") String id) {
193 log.debug("Deletes path by identifier {}.", id);
194
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530195 Boolean isSuccess = nullIsNotFound(get(PceService.class).releasePath(TunnelId.valueOf(id)),
196 PCE_PATH_NOT_FOUND);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530197 if (!isSuccess) {
198 log.debug("Path identifier {} does not exist", id);
199 }
200
201 return Response.status(OK).entity(isSuccess.toString()).build();
202 }
203}