Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 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 | package org.onosproject.codec.impl; |
| 17 | |
| 18 | import org.onosproject.codec.CodecContext; |
| 19 | import org.onosproject.codec.JsonCodec; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 20 | import org.onosproject.core.CoreService; |
Rafal Szalecki | f97e0ed | 2017-06-01 13:07:18 +0200 | [diff] [blame] | 21 | import org.onosproject.net.Link; |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 22 | import org.onosproject.net.ResourceGroup; |
Chiara Contoli | a8f69ff | 2016-07-28 01:06:07 +0900 | [diff] [blame] | 23 | import org.onosproject.net.intent.PointToPointIntent; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 24 | import org.onosproject.net.intent.Intent; |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 25 | import org.onosproject.net.intent.IntentService; |
| 26 | import org.onosproject.net.intent.IntentState; |
Chiara Contoli | a8f69ff | 2016-07-28 01:06:07 +0900 | [diff] [blame] | 27 | import org.onosproject.net.intent.HostToHostIntent; |
| 28 | import org.onosproject.net.intent.SinglePointToMultiPointIntent; |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 29 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 30 | import com.fasterxml.jackson.databind.JsonNode; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 31 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 32 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 33 | import com.google.common.net.UrlEscapers; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 34 | |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 36 | import static org.onlab.util.Tools.nullIsIllegal; |
Jayasree Ghosh | 44929b7 | 2016-10-18 03:10:21 +0530 | [diff] [blame] | 37 | import static org.onlab.util.Tools.nullIsNotFound; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Intent JSON codec. |
| 41 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 42 | public final class IntentCodec extends JsonCodec<Intent> { |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 43 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 44 | protected static final String TYPE = "type"; |
| 45 | protected static final String ID = "id"; |
| 46 | protected static final String APP_ID = "appId"; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 47 | protected static final String STATE = "state"; |
| 48 | protected static final String PRIORITY = "priority"; |
| 49 | protected static final String RESOURCES = "resources"; |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 50 | protected static final String RESOURCE_GROUP = "resourceGroup"; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 51 | protected static final String MISSING_MEMBER_MESSAGE = |
| 52 | " member is required in Intent"; |
Jayasree Ghosh | 44929b7 | 2016-10-18 03:10:21 +0530 | [diff] [blame] | 53 | private static final String E_APP_ID_NOT_FOUND = |
| 54 | "Application ID is not found"; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 55 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 56 | @Override |
| 57 | public ObjectNode encode(Intent intent, CodecContext context) { |
| 58 | checkNotNull(intent, "Intent cannot be null"); |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 59 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 60 | final ObjectNode result = context.mapper().createObjectNode() |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 61 | .put(TYPE, intent.getClass().getSimpleName()) |
| 62 | .put(ID, intent.id().toString()) |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 63 | .put(APP_ID, UrlEscapers.urlPathSegmentEscaper() |
| 64 | .escape(intent.appId().name())); |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 65 | if (intent.resourceGroup() != null) { |
| 66 | result.put(RESOURCE_GROUP, intent.resourceGroup().toString()); |
| 67 | } |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 68 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 69 | final ArrayNode jsonResources = result.putArray(RESOURCES); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 70 | |
Rafal Szalecki | f97e0ed | 2017-06-01 13:07:18 +0200 | [diff] [blame] | 71 | intent.resources() |
Yi Tseng | 10b69e8 | 2017-06-14 13:23:19 -0700 | [diff] [blame] | 72 | .forEach(resource -> { |
| 73 | if (resource instanceof Link) { |
| 74 | jsonResources.add(context.codec(Link.class).encode((Link) resource, context)); |
| 75 | } else { |
| 76 | jsonResources.add(resource.toString()); |
| 77 | } |
| 78 | }); |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 79 | |
| 80 | IntentService service = context.getService(IntentService.class); |
| 81 | IntentState state = service.getIntentState(intent.key()); |
Ray Milkey | ccab22d | 2015-05-18 13:24:50 -0700 | [diff] [blame] | 82 | if (state != null) { |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 83 | result.put(STATE, state.toString()); |
Ray Milkey | ccab22d | 2015-05-18 13:24:50 -0700 | [diff] [blame] | 84 | } |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 85 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 86 | return result; |
| 87 | } |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 88 | |
| 89 | @Override |
| 90 | public Intent decode(ObjectNode json, CodecContext context) { |
| 91 | checkNotNull(json, "JSON cannot be null"); |
| 92 | |
| 93 | String type = nullIsIllegal(json.get(TYPE), |
| 94 | TYPE + MISSING_MEMBER_MESSAGE).asText(); |
| 95 | |
| 96 | if (type.equals(PointToPointIntent.class.getSimpleName())) { |
| 97 | return context.codec(PointToPointIntent.class).decode(json, context); |
| 98 | } else if (type.equals(HostToHostIntent.class.getSimpleName())) { |
| 99 | return context.codec(HostToHostIntent.class).decode(json, context); |
Chiara Contoli | a8f69ff | 2016-07-28 01:06:07 +0900 | [diff] [blame] | 100 | } else if (type.equals(SinglePointToMultiPointIntent.class.getSimpleName())) { |
| 101 | return context.codec(SinglePointToMultiPointIntent.class).decode(json, context); |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 102 | } else if (type.equals(MultiPointToSinglePointIntent.class.getSimpleName())) { |
| 103 | return context.codec(MultiPointToSinglePointIntent.class).decode(json, context); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | throw new IllegalArgumentException("Intent type " |
| 107 | + type + " is not supported"); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Extracts base intent specific attributes from a JSON object |
| 112 | * and adds them to a builder. |
| 113 | * |
| 114 | * @param json root JSON object |
| 115 | * @param context code context |
| 116 | * @param builder builder to use for storing the attributes |
| 117 | */ |
| 118 | public static void intentAttributes(ObjectNode json, CodecContext context, |
| 119 | Intent.Builder builder) { |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 120 | String appId = nullIsIllegal(json.get(IntentCodec.APP_ID), |
| 121 | IntentCodec.APP_ID + IntentCodec.MISSING_MEMBER_MESSAGE).asText(); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 122 | CoreService service = context.getService(CoreService.class); |
Jayasree Ghosh | 44929b7 | 2016-10-18 03:10:21 +0530 | [diff] [blame] | 123 | builder.appId(nullIsNotFound(service.getAppId(appId), IntentCodec.E_APP_ID_NOT_FOUND)); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 124 | |
| 125 | JsonNode priorityJson = json.get(IntentCodec.PRIORITY); |
| 126 | if (priorityJson != null) { |
| 127 | builder.priority(priorityJson.asInt()); |
| 128 | } |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 129 | |
| 130 | JsonNode resourceGroup = json.get(IntentCodec.RESOURCE_GROUP); |
| 131 | if (resourceGroup != null) { |
| 132 | String resourceGroupId = resourceGroup.asText(); |
| 133 | builder.resourceGroup(ResourceGroup.of( |
| 134 | Long.parseUnsignedLong(resourceGroupId.substring(2), 16) |
| 135 | )); |
| 136 | } |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 137 | } |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 138 | } |