blob: 2d16e025f4e36e451f5adebdf2c6d72749c6d37f [file] [log] [blame]
Laszlo Papp5bdd0e42017-10-27 10:18:21 +01001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
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 */
16
17package org.onosproject.net.optical.rest;
18
19import static org.onlab.util.Tools.nullIsIllegal;
20import static org.onlab.util.Tools.nullIsNotFound;
21
22import com.fasterxml.jackson.databind.JsonNode;
23import com.fasterxml.jackson.databind.node.ObjectNode;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.net.OchSignal;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.device.DeviceService;
29import org.onosproject.net.intent.Intent;
30import org.onosproject.net.intent.IntentService;
31import org.onosproject.net.intent.Key;
32import org.onosproject.net.optical.json.OchSignalCodec;
33import org.onosproject.rest.AbstractWebResource;
34import org.slf4j.Logger;
35
36import javax.ws.rs.Consumes;
37import javax.ws.rs.POST;
38import javax.ws.rs.Path;
39import javax.ws.rs.Produces;
40import javax.ws.rs.core.Context;
41import javax.ws.rs.core.MediaType;
42import javax.ws.rs.core.Response;
43import javax.ws.rs.core.UriBuilder;
44import javax.ws.rs.core.UriInfo;
45import java.io.IOException;
46import java.io.InputStream;
47
48import static org.slf4j.LoggerFactory.getLogger;
49
Ray Milkey86ee5e82018-04-02 15:33:07 -070050import static org.onlab.util.Tools.readTreeFromStream;
Laszlo Papp5bdd0e42017-10-27 10:18:21 +010051import static org.onosproject.net.optical.util.OpticalIntentUtility.createOpticalIntent;
52
Ray Milkey86ee5e82018-04-02 15:33:07 -070053
Laszlo Papp5bdd0e42017-10-27 10:18:21 +010054/**
55 * Query, submit and withdraw optical network intents.
56 */
57@Path("intents")
58public class OpticalIntentsWebResource extends AbstractWebResource {
59
60 private static final Logger log = getLogger(OpticalIntentsWebResource.class);
61
62 private static final String JSON_INVALID = "Invalid json input";
63
64 private static final String APP_ID = "appId";
65
66 private static final String INGRESS_POINT = "ingressPoint";
67 private static final String EGRESS_POINT = "egressPoint";
68
69 private static final String BIDIRECTIONAL = "bidirectional";
70
71 private static final String SIGNAL = "signal";
72
73 protected static final String MISSING_MEMBER_MESSAGE =
74 " member is required";
75 private static final String E_APP_ID_NOT_FOUND =
76 "Application ID is not found";
77
78 @Context
79 private UriInfo uriInfo;
80
81 /**
82 * Submits a new optical intent.
83 * Creates and submits optical intents from the JSON request.
84 *
85 * @param stream input JSON
86 * @return status of the request - CREATED if the JSON is correct,
87 * BAD_REQUEST if the JSON is invalid
88 * @onos.rsModel CreateIntent
89 */
90 @POST
91 @Consumes(MediaType.APPLICATION_JSON)
92 @Produces(MediaType.APPLICATION_JSON)
93 public Response createIntent(InputStream stream) {
94 try {
95 IntentService service = get(IntentService.class);
Ray Milkey86ee5e82018-04-02 15:33:07 -070096 ObjectNode root = readTreeFromStream(mapper(), stream);
Laszlo Papp5bdd0e42017-10-27 10:18:21 +010097 Intent intent = decode(root);
98 service.submit(intent);
99 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
100 .path("intents")
101 .path(intent.appId().name())
102 .path(Long.toString(intent.id().fingerprint()));
103 return Response
104 .created(locationBuilder.build())
105 .build();
106 } catch (IOException ioe) {
107 throw new IllegalArgumentException(ioe);
108 }
109 }
110
111 private Intent decode(ObjectNode json) {
112 JsonNode ingressJson = json.get(INGRESS_POINT);
113 if (!ingressJson.isObject()) {
114 throw new IllegalArgumentException(JSON_INVALID);
115 }
116
117 ConnectPoint ingress = codec(ConnectPoint.class).decode((ObjectNode) ingressJson, this);
118
119 JsonNode egressJson = json.get(EGRESS_POINT);
120 if (!egressJson.isObject()) {
121 throw new IllegalArgumentException(JSON_INVALID);
122 }
123
124 ConnectPoint egress = codec(ConnectPoint.class).decode((ObjectNode) egressJson, this);
125
126 JsonNode bidirectionalJson = json.get(BIDIRECTIONAL);
127 boolean bidirectional = bidirectionalJson != null ? bidirectionalJson.asBoolean() : false;
128
129 JsonNode signalJson = json.get(SIGNAL);
130 OchSignal signal = null;
131 if (signalJson != null) {
132 if (!signalJson.isObject()) {
133 throw new IllegalArgumentException(JSON_INVALID);
134 } else {
135 signal = OchSignalCodec.decode((ObjectNode) signalJson);
136 }
137 }
138
139 String appIdString = nullIsIllegal(json.get(APP_ID), APP_ID + MISSING_MEMBER_MESSAGE).asText();
140 CoreService service = getService(CoreService.class);
141 ApplicationId appId = nullIsNotFound(service.getAppId(appIdString), E_APP_ID_NOT_FOUND);
142 Key key = null;
143 DeviceService deviceService = get(DeviceService.class);
144
145 return createOpticalIntent(ingress, egress, deviceService, key, appId, bidirectional, signal);
146 }
147}