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