blob: 26ca7931a84dd634189c2e367efb6ed743752e21 [file] [log] [blame]
Jovana Vuletafe32db7d2017-05-01 12:18:00 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jovana Vuletafe32db7d2017-05-01 12:18:00 +02003 *
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 */
16package org.onosproject.ofagent.rest;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.onosproject.incubator.net.virtual.NetworkId;
23import org.onosproject.ofagent.api.OFAgent;
24import org.onosproject.ofagent.api.OFAgentAdminService;
25import org.onosproject.ofagent.api.OFAgentService;
26import org.onosproject.rest.AbstractWebResource;
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.core.MediaType;
36import javax.ws.rs.core.Response;
37
38import java.io.IOException;
39import java.io.InputStream;
40
41import static javax.ws.rs.core.Response.Status.*;
42
43
44/**
45 * Manage virtual switch and controller mapping.
46 */
47@Path("service")
48public class OFAgentWebResource extends AbstractWebResource {
49
50 private static final String OFAGENT_NOT_FOUND = "OFAgent not found";
51 private static final String OFAGENTS_NOT_FOUND = "OFAgent set not found";
52 private static final String OFAGENT_CREATED = "OFAgent created";
53 private static final String OFAGENT_NOT_CREATED = "OFAgent not created";
54 private static final String OFAGENT_STARTED = "OFAgent started";
55 private static final String OFAGENT_NOT_STARTED = "OFAgent not started";
56 private static final String OFAGENT_UPDATED = "OFAgent updated";
57 private static final String OFAGENT_NOT_UPDATED = "OFAgent not updated";
58
59 /**
60 * Lists OpenFlow agents.
61 * Shows OpenFlow agents for all virtual networks.
62 *
63 * @return 200 OK if set exists, 500 INTERNAL SERVER ERROR otherwise
64 */
65 @GET
66 @Path("ofagents")
67 public Response listOFAgents() {
68 OFAgentService service = get(OFAgentService.class);
69 ObjectMapper mapper = new ObjectMapper();
70 ObjectNode root = mapper.createObjectNode();
71 ArrayNode ofAgentsArray = mapper.createArrayNode();
72 if (service.agents() == null) {
73 return Response.status(INTERNAL_SERVER_ERROR)
74 .entity(OFAGENTS_NOT_FOUND).build();
75 } else {
76 service.agents().forEach(ofAgent -> ofAgentsArray.add((new OFAgentCodec()).encode(ofAgent, this)));
77
78 root.set("ofAgents", ofAgentsArray);
79 return Response.ok(root, MediaType.APPLICATION_JSON_TYPE).build();
80 }
81
82 }
83
84 /**
85 * Lists OpenFlow agent.
86 * Shows OpenFlow agent for given network.
87 *
88 * @param networkId OFAgent networkId
89 * @return 200 OK if OFAgent exists, 404 NOT FOUND otherwise
90 */
91 @GET
92 @Path("ofagent/{networkId}")
93 public Response listOFAgent(@PathParam("networkId") long networkId) {
94 OFAgentService service = get(OFAgentService.class);
95 OFAgent ofAgent = service.agent(NetworkId.networkId(networkId));
96 if (ofAgent == null) {
97 return Response.status(NOT_FOUND)
98 .entity(OFAGENT_NOT_FOUND).build();
99 } else {
100 return Response.ok((new OFAgentCodec()).encode(ofAgent, this), MediaType
101 .APPLICATION_JSON_TYPE)
102 .build();
103 }
104 }
105
106 /**
107 * Adds a new OpenFlow agent.
108 * Creates a new OpenFlow agent and adds it to OpenFlow agent store.
109 *
110 * @param stream JSON stream
111 * @return 201 CREATED , 400 BAD REQUEST if stream cannot be decoded to OFAgent
112 * @throws IOException if request cannot be parsed
113 */
114 @POST
115 @Path("ofagent-create")
116 @Consumes(MediaType.APPLICATION_JSON)
117 public Response createOFAgent(InputStream stream) throws IOException {
118 OFAgentAdminService adminService = get(OFAgentAdminService.class);
119
120 OFAgent ofAgent = (new OFAgentCodec()).decode((ObjectNode) mapper().readTree(stream), this);
121 if (ofAgent == null) {
122 return Response.status(BAD_REQUEST)
123 .entity(OFAGENT_NOT_CREATED).build();
124 } else {
125 adminService.createAgent(ofAgent);
126 return Response.status(CREATED).entity(OFAGENT_CREATED).build();
127 }
128 }
129
130 /**
131 * Starts OpenFlow agent.
132 * Starts OpenFlow agent for the given network.
133 *
134 * @param stream JSON stream
135 * @return 200 OK if OFAgent was started, 404 NOT FOUND when OF agent does not exist, 400 BAD REQUEST otherwise
136 * @throws IOException if request cannot be parsed
137 */
138 @POST
139 @Path("ofagent-start")
140 @Consumes(MediaType.APPLICATION_JSON)
141 public Response startOFAgent(InputStream stream) throws IOException {
142 OFAgentAdminService adminService = get(OFAgentAdminService.class);
143
144 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
145 JsonNode networkId = jsonTree.get("networkId");
146
147 if (networkId == null) {
148 return Response.status(BAD_REQUEST)
149 .entity(OFAGENT_NOT_STARTED).build();
150 } else if (get(OFAgentService.class).agent(NetworkId.networkId(networkId.asLong())) == null) {
151 return Response.status(NOT_FOUND)
152 .entity(OFAGENT_NOT_STARTED).build();
153 } else {
154 adminService.startAgent(NetworkId.networkId(networkId.asLong()));
155 return Response.status(OK).entity(OFAGENT_STARTED).build();
156 }
157 }
158
159 /**
160 * Updates OpenFlow agent.
161 * Updates existing OpenFlow agent for the given network.
162 *
163 * @param stream JSON stream
164 * @return 200 OK if OFAgent was updated, 404 NOT FOUND when OF agent does not exist, 400 BAD REQUEST otherwise
165 * @throws IOException if request cannot be parsed
166 */
167 @PUT
168 @Path("ofagent-update")
169 @Consumes(MediaType.APPLICATION_JSON)
170 public Response updateOFAgent(InputStream stream) throws IOException {
171 OFAgentAdminService adminService = get(OFAgentAdminService.class);
172
173 OFAgent ofAgent = (new OFAgentCodec()).decode((ObjectNode) mapper().readTree(stream), this);
174
175 if (ofAgent == null) {
176 return Response.status(NOT_FOUND)
177 .entity(OFAGENT_NOT_UPDATED).build();
178 } else if (get(OFAgentService.class).agent(ofAgent.networkId()) == null) {
179 return Response.status(NOT_FOUND)
180 .entity(OFAGENT_NOT_UPDATED).build();
181 }
182
183 adminService.updateAgent(ofAgent);
184 return Response.status(OK).entity(OFAGENT_UPDATED).build();
185 }
186
187
188 /**
189 * Stops OFAgent.
190 * Stops OFAgent for the given virtual network.
191 *
192 * @param stream JSON stream
193 * @return 204 NO CONTENT if OpenFlow agent was stopped, 404 NOT FOUND otherwise
194 * @throws IOException if request cannot be parsed
195 */
196 @POST
197 @Path("ofagent-stop")
198 @Consumes(MediaType.APPLICATION_JSON)
199 public Response stopOFAgent(InputStream stream) throws IOException {
200
201 OFAgentAdminService adminService = get(OFAgentAdminService.class);
202 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
203 JsonNode networkId = jsonTree.get("networkId");
204
205 if (get(OFAgentService.class).agent(NetworkId.networkId(networkId.asLong())) == null) {
206 return Response.status(NOT_FOUND)
207 .entity(OFAGENT_NOT_FOUND).build();
208 }
209
210 adminService.stopAgent(NetworkId.networkId(networkId.asLong()));
211 return Response.noContent().build();
212 }
213
214
215 /**
216 * Deletes OFAgent.
217 * Removes OFAgent for the given virtual network from repository.
218 *
219 * @param networkId OFAgent networkId
220 * @return 200 OK if OFAgent was removed, 404 NOT FOUND when OF agent does not exist, 400 BAD REQUEST otherwise
221 */
222 @DELETE
223 @Path("ofagent-remove/{networkId}")
224 public Response removeOFAgent(@PathParam("networkId") long networkId) {
225 if (get(OFAgentService.class).agent(NetworkId.networkId(networkId)) == null) {
226 return Response.status(BAD_REQUEST)
227 .entity(OFAGENT_NOT_FOUND).build();
228 }
229
230 OFAgentAdminService adminService = get(OFAgentAdminService.class);
231 OFAgent removed = adminService.removeAgent(NetworkId.networkId(networkId));
232 if (removed != null) {
233 return Response.ok((new OFAgentCodec()).encode(removed, this), MediaType
234 .APPLICATION_JSON_TYPE)
235 .build();
236 } else {
237 return Response.status(NOT_FOUND)
238 .entity(OFAGENT_NOT_FOUND).build();
239 }
240 }
241}