blob: 4faced6449dab9f82d5f44a2fcac84b801c6d065 [file] [log] [blame]
Ray Milkey2b217142014-12-15 09:24:24 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkey2b217142014-12-15 09:24:24 -08003 *
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.codec.impl;
17
Antonio Marsico4f68ec92017-03-09 11:16:32 +010018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.net.UrlEscapers;
Ray Milkey2b217142014-12-15 09:24:24 -080022import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.JsonCodec;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070024import org.onosproject.core.CoreService;
Rafal Szaleckif97e0ed2017-06-01 13:07:18 +020025import org.onosproject.net.Link;
Luca Prete670ac5d2017-02-03 15:55:43 -080026import org.onosproject.net.ResourceGroup;
Antonio Marsico4f68ec92017-03-09 11:16:32 +010027import org.onosproject.net.intent.HostToHostIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080028import org.onosproject.net.intent.Intent;
Ray Milkey1534f8d2015-05-13 15:42:50 -070029import org.onosproject.net.intent.IntentService;
30import org.onosproject.net.intent.IntentState;
Antonio Marsico4f68ec92017-03-09 11:16:32 +010031import org.onosproject.net.intent.Key;
32import org.onosproject.net.intent.PointToPointIntent;
Chiara Contolia8f69ff2016-07-28 01:06:07 +090033import org.onosproject.net.intent.SinglePointToMultiPointIntent;
nassima toumi95761312017-05-23 14:00:33 +030034import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080035
36import static com.google.common.base.Preconditions.checkNotNull;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070037import static org.onlab.util.Tools.nullIsIllegal;
Jayasree Ghosh44929b72016-10-18 03:10:21 +053038import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey2b217142014-12-15 09:24:24 -080039
40/**
41 * Intent JSON codec.
42 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080043public final class IntentCodec extends JsonCodec<Intent> {
Ray Milkey2b217142014-12-15 09:24:24 -080044
Ray Milkey9c9cde42018-01-12 14:22:06 -080045 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 Milkeyb82c42b2015-06-30 09:42:20 -070054 " member is required in Intent";
Jayasree Ghosh44929b72016-10-18 03:10:21 +053055 private static final String E_APP_ID_NOT_FOUND =
56 "Application ID is not found";
Ray Milkeyb82c42b2015-06-30 09:42:20 -070057
Ray Milkey2b217142014-12-15 09:24:24 -080058 @Override
59 public ObjectNode encode(Intent intent, CodecContext context) {
60 checkNotNull(intent, "Intent cannot be null");
Ray Milkeyf7cb4012015-07-20 13:01:07 -070061
Ray Milkey2b217142014-12-15 09:24:24 -080062 final ObjectNode result = context.mapper().createObjectNode()
Ray Milkeyb82c42b2015-06-30 09:42:20 -070063 .put(TYPE, intent.getClass().getSimpleName())
64 .put(ID, intent.id().toString())
Antonio Marsico4f68ec92017-03-09 11:16:32 +010065 .put(KEY, intent.key().toString())
Ray Milkeyf7cb4012015-07-20 13:01:07 -070066 .put(APP_ID, UrlEscapers.urlPathSegmentEscaper()
67 .escape(intent.appId().name()));
Luca Prete670ac5d2017-02-03 15:55:43 -080068 if (intent.resourceGroup() != null) {
69 result.put(RESOURCE_GROUP, intent.resourceGroup().toString());
70 }
Ray Milkey2b217142014-12-15 09:24:24 -080071
Ray Milkeyb82c42b2015-06-30 09:42:20 -070072 final ArrayNode jsonResources = result.putArray(RESOURCES);
Ray Milkey2b217142014-12-15 09:24:24 -080073
Rafal Szaleckif97e0ed2017-06-01 13:07:18 +020074 intent.resources()
Yi Tseng10b69e82017-06-14 13:23:19 -070075 .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 Milkey1534f8d2015-05-13 15:42:50 -070082
83 IntentService service = context.getService(IntentService.class);
84 IntentState state = service.getIntentState(intent.key());
Ray Milkeyccab22d2015-05-18 13:24:50 -070085 if (state != null) {
Ray Milkeyb82c42b2015-06-30 09:42:20 -070086 result.put(STATE, state.toString());
Ray Milkeyccab22d2015-05-18 13:24:50 -070087 }
Ray Milkey1534f8d2015-05-13 15:42:50 -070088
Ray Milkey2b217142014-12-15 09:24:24 -080089 return result;
90 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -070091
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 Contolia8f69ff2016-07-28 01:06:07 +0900103 } else if (type.equals(SinglePointToMultiPointIntent.class.getSimpleName())) {
104 return context.codec(SinglePointToMultiPointIntent.class).decode(json, context);
nassima toumi95761312017-05-23 14:00:33 +0300105 } else if (type.equals(MultiPointToSinglePointIntent.class.getSimpleName())) {
106 return context.codec(MultiPointToSinglePointIntent.class).decode(json, context);
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700107 }
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 Milkeyf7cb4012015-07-20 13:01:07 -0700123 String appId = nullIsIllegal(json.get(IntentCodec.APP_ID),
124 IntentCodec.APP_ID + IntentCodec.MISSING_MEMBER_MESSAGE).asText();
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700125 CoreService service = context.getService(CoreService.class);
Jayasree Ghosh44929b72016-10-18 03:10:21 +0530126 builder.appId(nullIsNotFound(service.getAppId(appId), IntentCodec.E_APP_ID_NOT_FOUND));
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700127
128 JsonNode priorityJson = json.get(IntentCodec.PRIORITY);
129 if (priorityJson != null) {
130 builder.priority(priorityJson.asInt());
131 }
Luca Prete670ac5d2017-02-03 15:55:43 -0800132
Antonio Marsico4f68ec92017-03-09 11:16:32 +0100133 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 Prete670ac5d2017-02-03 15:55:43 -0800146 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 Milkeyb82c42b2015-06-30 09:42:20 -0700153 }
Ray Milkey2b217142014-12-15 09:24:24 -0800154}