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