blob: 69d10bc97221ed61a89be052a8eed53f4956743c [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
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;
nassima toumi95761312017-05-23 14:00:33 +030029import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070030import com.fasterxml.jackson.databind.JsonNode;
Ray Milkey2b217142014-12-15 09:24:24 -080031import com.fasterxml.jackson.databind.node.ArrayNode;
32import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkeyf7cb4012015-07-20 13:01:07 -070033import com.google.common.net.UrlEscapers;
Ray Milkey2b217142014-12-15 09:24:24 -080034
35import static com.google.common.base.Preconditions.checkNotNull;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070036import static org.onlab.util.Tools.nullIsIllegal;
Jayasree Ghosh44929b72016-10-18 03:10:21 +053037import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey2b217142014-12-15 09:24:24 -080038
39/**
40 * Intent JSON codec.
41 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080042public final class IntentCodec extends JsonCodec<Intent> {
Ray Milkey2b217142014-12-15 09:24:24 -080043
Ray Milkeyb82c42b2015-06-30 09:42:20 -070044 protected static final String TYPE = "type";
45 protected static final String ID = "id";
46 protected static final String APP_ID = "appId";
Ray Milkeyb82c42b2015-06-30 09:42:20 -070047 protected static final String STATE = "state";
48 protected static final String PRIORITY = "priority";
49 protected static final String RESOURCES = "resources";
Luca Prete670ac5d2017-02-03 15:55:43 -080050 protected static final String RESOURCE_GROUP = "resourceGroup";
Ray Milkeyb82c42b2015-06-30 09:42:20 -070051 protected static final String MISSING_MEMBER_MESSAGE =
52 " member is required in Intent";
Jayasree Ghosh44929b72016-10-18 03:10:21 +053053 private static final String E_APP_ID_NOT_FOUND =
54 "Application ID is not found";
Ray Milkeyb82c42b2015-06-30 09:42:20 -070055
Ray Milkey2b217142014-12-15 09:24:24 -080056 @Override
57 public ObjectNode encode(Intent intent, CodecContext context) {
58 checkNotNull(intent, "Intent cannot be null");
Ray Milkeyf7cb4012015-07-20 13:01:07 -070059
Ray Milkey2b217142014-12-15 09:24:24 -080060 final ObjectNode result = context.mapper().createObjectNode()
Ray Milkeyb82c42b2015-06-30 09:42:20 -070061 .put(TYPE, intent.getClass().getSimpleName())
62 .put(ID, intent.id().toString())
Ray Milkeyf7cb4012015-07-20 13:01:07 -070063 .put(APP_ID, UrlEscapers.urlPathSegmentEscaper()
64 .escape(intent.appId().name()));
Luca Prete670ac5d2017-02-03 15:55:43 -080065 if (intent.resourceGroup() != null) {
66 result.put(RESOURCE_GROUP, intent.resourceGroup().toString());
67 }
Ray Milkey2b217142014-12-15 09:24:24 -080068
Ray Milkeyb82c42b2015-06-30 09:42:20 -070069 final ArrayNode jsonResources = result.putArray(RESOURCES);
Ray Milkey2b217142014-12-15 09:24:24 -080070
Rafal Szaleckif97e0ed2017-06-01 13:07:18 +020071 intent.resources()
Yi Tseng10b69e82017-06-14 13:23:19 -070072 .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 Milkey1534f8d2015-05-13 15:42:50 -070079
80 IntentService service = context.getService(IntentService.class);
81 IntentState state = service.getIntentState(intent.key());
Ray Milkeyccab22d2015-05-18 13:24:50 -070082 if (state != null) {
Ray Milkeyb82c42b2015-06-30 09:42:20 -070083 result.put(STATE, state.toString());
Ray Milkeyccab22d2015-05-18 13:24:50 -070084 }
Ray Milkey1534f8d2015-05-13 15:42:50 -070085
Ray Milkey2b217142014-12-15 09:24:24 -080086 return result;
87 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -070088
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 Contolia8f69ff2016-07-28 01:06:07 +0900100 } else if (type.equals(SinglePointToMultiPointIntent.class.getSimpleName())) {
101 return context.codec(SinglePointToMultiPointIntent.class).decode(json, context);
nassima toumi95761312017-05-23 14:00:33 +0300102 } else if (type.equals(MultiPointToSinglePointIntent.class.getSimpleName())) {
103 return context.codec(MultiPointToSinglePointIntent.class).decode(json, context);
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700104 }
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 Milkeyf7cb4012015-07-20 13:01:07 -0700120 String appId = nullIsIllegal(json.get(IntentCodec.APP_ID),
121 IntentCodec.APP_ID + IntentCodec.MISSING_MEMBER_MESSAGE).asText();
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700122 CoreService service = context.getService(CoreService.class);
Jayasree Ghosh44929b72016-10-18 03:10:21 +0530123 builder.appId(nullIsNotFound(service.getAppId(appId), IntentCodec.E_APP_ID_NOT_FOUND));
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700124
125 JsonNode priorityJson = json.get(IntentCodec.PRIORITY);
126 if (priorityJson != null) {
127 builder.priority(priorityJson.asInt());
128 }
Luca Prete670ac5d2017-02-03 15:55:43 -0800129
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 Milkeyb82c42b2015-06-30 09:42:20 -0700137 }
Ray Milkey2b217142014-12-15 09:24:24 -0800138}