blob: 11429d48e09ed6174c324d4c9303347c447ab08c [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;
Ray Milkey2b217142014-12-15 09:24:24 -080021import org.onosproject.net.NetworkResource;
Chiara Contolia8f69ff2016-07-28 01:06:07 +090022import org.onosproject.net.intent.PointToPointIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080023import org.onosproject.net.intent.Intent;
Ray Milkey1534f8d2015-05-13 15:42:50 -070024import org.onosproject.net.intent.IntentService;
25import org.onosproject.net.intent.IntentState;
Chiara Contolia8f69ff2016-07-28 01:06:07 +090026import org.onosproject.net.intent.HostToHostIntent;
27import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070028import com.fasterxml.jackson.databind.JsonNode;
Ray Milkey2b217142014-12-15 09:24:24 -080029import com.fasterxml.jackson.databind.node.ArrayNode;
30import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkeyf7cb4012015-07-20 13:01:07 -070031import com.google.common.net.UrlEscapers;
Ray Milkey2b217142014-12-15 09:24:24 -080032
33import static com.google.common.base.Preconditions.checkNotNull;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070034import static org.onlab.util.Tools.nullIsIllegal;
Jayasree Ghosh44929b72016-10-18 03:10:21 +053035import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey2b217142014-12-15 09:24:24 -080036
37/**
38 * Intent JSON codec.
39 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080040public final class IntentCodec extends JsonCodec<Intent> {
Ray Milkey2b217142014-12-15 09:24:24 -080041
Ray Milkeyb82c42b2015-06-30 09:42:20 -070042 protected static final String TYPE = "type";
43 protected static final String ID = "id";
44 protected static final String APP_ID = "appId";
Ray Milkeyb82c42b2015-06-30 09:42:20 -070045 protected static final String STATE = "state";
46 protected static final String PRIORITY = "priority";
47 protected static final String RESOURCES = "resources";
48 protected static final String MISSING_MEMBER_MESSAGE =
49 " member is required in Intent";
Jayasree Ghosh44929b72016-10-18 03:10:21 +053050 private static final String E_APP_ID_NOT_FOUND =
51 "Application ID is not found";
Ray Milkeyb82c42b2015-06-30 09:42:20 -070052
Ray Milkey2b217142014-12-15 09:24:24 -080053 @Override
54 public ObjectNode encode(Intent intent, CodecContext context) {
55 checkNotNull(intent, "Intent cannot be null");
Ray Milkeyf7cb4012015-07-20 13:01:07 -070056
Ray Milkey2b217142014-12-15 09:24:24 -080057 final ObjectNode result = context.mapper().createObjectNode()
Ray Milkeyb82c42b2015-06-30 09:42:20 -070058 .put(TYPE, intent.getClass().getSimpleName())
59 .put(ID, intent.id().toString())
Ray Milkeyf7cb4012015-07-20 13:01:07 -070060 .put(APP_ID, UrlEscapers.urlPathSegmentEscaper()
61 .escape(intent.appId().name()));
Ray Milkey2b217142014-12-15 09:24:24 -080062
Ray Milkeyb82c42b2015-06-30 09:42:20 -070063 final ArrayNode jsonResources = result.putArray(RESOURCES);
Ray Milkey2b217142014-12-15 09:24:24 -080064
Sho SHIMIZUd7d18002015-01-21 14:37:14 -080065 for (final NetworkResource resource : intent.resources()) {
66 jsonResources.add(resource.toString());
Ray Milkey2b217142014-12-15 09:24:24 -080067 }
Ray Milkey1534f8d2015-05-13 15:42:50 -070068
69 IntentService service = context.getService(IntentService.class);
70 IntentState state = service.getIntentState(intent.key());
Ray Milkeyccab22d2015-05-18 13:24:50 -070071 if (state != null) {
Ray Milkeyb82c42b2015-06-30 09:42:20 -070072 result.put(STATE, state.toString());
Ray Milkeyccab22d2015-05-18 13:24:50 -070073 }
Ray Milkey1534f8d2015-05-13 15:42:50 -070074
Ray Milkey2b217142014-12-15 09:24:24 -080075 return result;
76 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -070077
78 @Override
79 public Intent decode(ObjectNode json, CodecContext context) {
80 checkNotNull(json, "JSON cannot be null");
81
82 String type = nullIsIllegal(json.get(TYPE),
83 TYPE + MISSING_MEMBER_MESSAGE).asText();
84
85 if (type.equals(PointToPointIntent.class.getSimpleName())) {
86 return context.codec(PointToPointIntent.class).decode(json, context);
87 } else if (type.equals(HostToHostIntent.class.getSimpleName())) {
88 return context.codec(HostToHostIntent.class).decode(json, context);
Chiara Contolia8f69ff2016-07-28 01:06:07 +090089 } else if (type.equals(SinglePointToMultiPointIntent.class.getSimpleName())) {
90 return context.codec(SinglePointToMultiPointIntent.class).decode(json, context);
Ray Milkeyb82c42b2015-06-30 09:42:20 -070091 }
92
93 throw new IllegalArgumentException("Intent type "
94 + type + " is not supported");
95 }
96
97 /**
98 * Extracts base intent specific attributes from a JSON object
99 * and adds them to a builder.
100 *
101 * @param json root JSON object
102 * @param context code context
103 * @param builder builder to use for storing the attributes
104 */
105 public static void intentAttributes(ObjectNode json, CodecContext context,
106 Intent.Builder builder) {
Ray Milkeyf7cb4012015-07-20 13:01:07 -0700107 String appId = nullIsIllegal(json.get(IntentCodec.APP_ID),
108 IntentCodec.APP_ID + IntentCodec.MISSING_MEMBER_MESSAGE).asText();
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700109 CoreService service = context.getService(CoreService.class);
Jayasree Ghosh44929b72016-10-18 03:10:21 +0530110 builder.appId(nullIsNotFound(service.getAppId(appId), IntentCodec.E_APP_ID_NOT_FOUND));
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700111
112 JsonNode priorityJson = json.get(IntentCodec.PRIORITY);
113 if (priorityJson != null) {
114 builder.priority(priorityJson.asInt());
115 }
116 }
Ray Milkey2b217142014-12-15 09:24:24 -0800117}