blob: ed209585343fd9ba08e411af6632f61dd6005c32 [file] [log] [blame]
Ray Milkey2b217142014-12-15 09:24:24 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
18import org.onosproject.codec.CodecContext;
19import org.onosproject.codec.JsonCodec;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070020import org.onosproject.core.CoreService;
Rafal Szaleckif97e0ed2017-06-01 13:07:18 +020021import org.onosproject.net.Link;
Luca Prete670ac5d2017-02-03 15:55:43 -080022import org.onosproject.net.ResourceGroup;
Chiara Contolia8f69ff2016-07-28 01:06:07 +090023import org.onosproject.net.intent.PointToPointIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080024import org.onosproject.net.intent.Intent;
Ray Milkey1534f8d2015-05-13 15:42:50 -070025import org.onosproject.net.intent.IntentService;
26import org.onosproject.net.intent.IntentState;
Chiara Contolia8f69ff2016-07-28 01:06:07 +090027import org.onosproject.net.intent.HostToHostIntent;
28import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070029import com.fasterxml.jackson.databind.JsonNode;
Ray Milkey2b217142014-12-15 09:24:24 -080030import com.fasterxml.jackson.databind.node.ArrayNode;
31import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkeyf7cb4012015-07-20 13:01:07 -070032import com.google.common.net.UrlEscapers;
Ray Milkey2b217142014-12-15 09:24:24 -080033
34import static com.google.common.base.Preconditions.checkNotNull;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070035import static org.onlab.util.Tools.nullIsIllegal;
Jayasree Ghosh44929b72016-10-18 03:10:21 +053036import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey2b217142014-12-15 09:24:24 -080037
38/**
39 * Intent JSON codec.
40 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080041public final class IntentCodec extends JsonCodec<Intent> {
Ray Milkey2b217142014-12-15 09:24:24 -080042
Ray Milkeyb82c42b2015-06-30 09:42:20 -070043 protected static final String TYPE = "type";
44 protected static final String ID = "id";
45 protected static final String APP_ID = "appId";
Ray Milkeyb82c42b2015-06-30 09:42:20 -070046 protected static final String STATE = "state";
47 protected static final String PRIORITY = "priority";
48 protected static final String RESOURCES = "resources";
Luca Prete670ac5d2017-02-03 15:55:43 -080049 protected static final String RESOURCE_GROUP = "resourceGroup";
Ray Milkeyb82c42b2015-06-30 09:42:20 -070050 protected static final String MISSING_MEMBER_MESSAGE =
51 " member is required in Intent";
Jayasree Ghosh44929b72016-10-18 03:10:21 +053052 private static final String E_APP_ID_NOT_FOUND =
53 "Application ID is not found";
Ray Milkeyb82c42b2015-06-30 09:42:20 -070054
Ray Milkey2b217142014-12-15 09:24:24 -080055 @Override
56 public ObjectNode encode(Intent intent, CodecContext context) {
57 checkNotNull(intent, "Intent cannot be null");
Ray Milkeyf7cb4012015-07-20 13:01:07 -070058
Ray Milkey2b217142014-12-15 09:24:24 -080059 final ObjectNode result = context.mapper().createObjectNode()
Ray Milkeyb82c42b2015-06-30 09:42:20 -070060 .put(TYPE, intent.getClass().getSimpleName())
61 .put(ID, intent.id().toString())
Ray Milkeyf7cb4012015-07-20 13:01:07 -070062 .put(APP_ID, UrlEscapers.urlPathSegmentEscaper()
63 .escape(intent.appId().name()));
Luca Prete670ac5d2017-02-03 15:55:43 -080064 if (intent.resourceGroup() != null) {
65 result.put(RESOURCE_GROUP, intent.resourceGroup().toString());
66 }
Ray Milkey2b217142014-12-15 09:24:24 -080067
Ray Milkeyb82c42b2015-06-30 09:42:20 -070068 final ArrayNode jsonResources = result.putArray(RESOURCES);
Ray Milkey2b217142014-12-15 09:24:24 -080069
Rafal Szaleckif97e0ed2017-06-01 13:07:18 +020070 intent.resources()
Yi Tseng10b69e82017-06-14 13:23:19 -070071 .forEach(resource -> {
72 if (resource instanceof Link) {
73 jsonResources.add(context.codec(Link.class).encode((Link) resource, context));
74 } else {
75 jsonResources.add(resource.toString());
76 }
77 });
Ray Milkey1534f8d2015-05-13 15:42:50 -070078
79 IntentService service = context.getService(IntentService.class);
80 IntentState state = service.getIntentState(intent.key());
Ray Milkeyccab22d2015-05-18 13:24:50 -070081 if (state != null) {
Ray Milkeyb82c42b2015-06-30 09:42:20 -070082 result.put(STATE, state.toString());
Ray Milkeyccab22d2015-05-18 13:24:50 -070083 }
Ray Milkey1534f8d2015-05-13 15:42:50 -070084
Ray Milkey2b217142014-12-15 09:24:24 -080085 return result;
86 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -070087
88 @Override
89 public Intent decode(ObjectNode json, CodecContext context) {
90 checkNotNull(json, "JSON cannot be null");
91
92 String type = nullIsIllegal(json.get(TYPE),
93 TYPE + MISSING_MEMBER_MESSAGE).asText();
94
95 if (type.equals(PointToPointIntent.class.getSimpleName())) {
96 return context.codec(PointToPointIntent.class).decode(json, context);
97 } else if (type.equals(HostToHostIntent.class.getSimpleName())) {
98 return context.codec(HostToHostIntent.class).decode(json, context);
Chiara Contolia8f69ff2016-07-28 01:06:07 +090099 } else if (type.equals(SinglePointToMultiPointIntent.class.getSimpleName())) {
100 return context.codec(SinglePointToMultiPointIntent.class).decode(json, context);
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700101 }
102
103 throw new IllegalArgumentException("Intent type "
104 + type + " is not supported");
105 }
106
107 /**
108 * Extracts base intent specific attributes from a JSON object
109 * and adds them to a builder.
110 *
111 * @param json root JSON object
112 * @param context code context
113 * @param builder builder to use for storing the attributes
114 */
115 public static void intentAttributes(ObjectNode json, CodecContext context,
116 Intent.Builder builder) {
Ray Milkeyf7cb4012015-07-20 13:01:07 -0700117 String appId = nullIsIllegal(json.get(IntentCodec.APP_ID),
118 IntentCodec.APP_ID + IntentCodec.MISSING_MEMBER_MESSAGE).asText();
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700119 CoreService service = context.getService(CoreService.class);
Jayasree Ghosh44929b72016-10-18 03:10:21 +0530120 builder.appId(nullIsNotFound(service.getAppId(appId), IntentCodec.E_APP_ID_NOT_FOUND));
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700121
122 JsonNode priorityJson = json.get(IntentCodec.PRIORITY);
123 if (priorityJson != null) {
124 builder.priority(priorityJson.asInt());
125 }
Luca Prete670ac5d2017-02-03 15:55:43 -0800126
127 JsonNode resourceGroup = json.get(IntentCodec.RESOURCE_GROUP);
128 if (resourceGroup != null) {
129 String resourceGroupId = resourceGroup.asText();
130 builder.resourceGroup(ResourceGroup.of(
131 Long.parseUnsignedLong(resourceGroupId.substring(2), 16)
132 ));
133 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700134 }
Ray Milkey2b217142014-12-15 09:24:24 -0800135}