blob: 448a2e0397794ef49fb7dc0ed78714dc6ec1b90c [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;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070022import org.onosproject.net.intent.HostToHostIntent;
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;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070026import org.onosproject.net.intent.PointToPointIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080027
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;
Ray Milkey2b217142014-12-15 09:24:24 -080035
36/**
37 * Intent JSON codec.
38 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080039public final class IntentCodec extends JsonCodec<Intent> {
Ray Milkey2b217142014-12-15 09:24:24 -080040
Ray Milkeyb82c42b2015-06-30 09:42:20 -070041 protected static final String TYPE = "type";
42 protected static final String ID = "id";
43 protected static final String APP_ID = "appId";
Ray Milkeyb82c42b2015-06-30 09:42:20 -070044 protected static final String STATE = "state";
45 protected static final String PRIORITY = "priority";
46 protected static final String RESOURCES = "resources";
47 protected static final String MISSING_MEMBER_MESSAGE =
48 " member is required in Intent";
49
Ray Milkey2b217142014-12-15 09:24:24 -080050 @Override
51 public ObjectNode encode(Intent intent, CodecContext context) {
52 checkNotNull(intent, "Intent cannot be null");
Ray Milkeyf7cb4012015-07-20 13:01:07 -070053
Ray Milkey2b217142014-12-15 09:24:24 -080054 final ObjectNode result = context.mapper().createObjectNode()
Ray Milkeyb82c42b2015-06-30 09:42:20 -070055 .put(TYPE, intent.getClass().getSimpleName())
56 .put(ID, intent.id().toString())
Ray Milkeyf7cb4012015-07-20 13:01:07 -070057 .put(APP_ID, UrlEscapers.urlPathSegmentEscaper()
58 .escape(intent.appId().name()));
Ray Milkey2b217142014-12-15 09:24:24 -080059
Ray Milkeyb82c42b2015-06-30 09:42:20 -070060 final ArrayNode jsonResources = result.putArray(RESOURCES);
Ray Milkey2b217142014-12-15 09:24:24 -080061
Sho SHIMIZUd7d18002015-01-21 14:37:14 -080062 for (final NetworkResource resource : intent.resources()) {
63 jsonResources.add(resource.toString());
Ray Milkey2b217142014-12-15 09:24:24 -080064 }
Ray Milkey1534f8d2015-05-13 15:42:50 -070065
66 IntentService service = context.getService(IntentService.class);
67 IntentState state = service.getIntentState(intent.key());
Ray Milkeyccab22d2015-05-18 13:24:50 -070068 if (state != null) {
Ray Milkeyb82c42b2015-06-30 09:42:20 -070069 result.put(STATE, state.toString());
Ray Milkeyccab22d2015-05-18 13:24:50 -070070 }
Ray Milkey1534f8d2015-05-13 15:42:50 -070071
Ray Milkey2b217142014-12-15 09:24:24 -080072 return result;
73 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -070074
75 @Override
76 public Intent decode(ObjectNode json, CodecContext context) {
77 checkNotNull(json, "JSON cannot be null");
78
79 String type = nullIsIllegal(json.get(TYPE),
80 TYPE + MISSING_MEMBER_MESSAGE).asText();
81
82 if (type.equals(PointToPointIntent.class.getSimpleName())) {
83 return context.codec(PointToPointIntent.class).decode(json, context);
84 } else if (type.equals(HostToHostIntent.class.getSimpleName())) {
85 return context.codec(HostToHostIntent.class).decode(json, context);
86 }
87
88 throw new IllegalArgumentException("Intent type "
89 + type + " is not supported");
90 }
91
92 /**
93 * Extracts base intent specific attributes from a JSON object
94 * and adds them to a builder.
95 *
96 * @param json root JSON object
97 * @param context code context
98 * @param builder builder to use for storing the attributes
99 */
100 public static void intentAttributes(ObjectNode json, CodecContext context,
101 Intent.Builder builder) {
Ray Milkeyf7cb4012015-07-20 13:01:07 -0700102 String appId = nullIsIllegal(json.get(IntentCodec.APP_ID),
103 IntentCodec.APP_ID + IntentCodec.MISSING_MEMBER_MESSAGE).asText();
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700104 CoreService service = context.getService(CoreService.class);
105 builder.appId(service.getAppId(appId));
106
107 JsonNode priorityJson = json.get(IntentCodec.PRIORITY);
108 if (priorityJson != null) {
109 builder.priority(priorityJson.asInt());
110 }
111 }
Ray Milkey2b217142014-12-15 09:24:24 -0800112}