blob: 36ff8facf441b6842fe3ee862c1b0a53ec62e4ae [file] [log] [blame]
Ray Milkey2b217142014-12-15 09:24:24 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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;
31
32import static com.google.common.base.Preconditions.checkNotNull;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070033import static org.onlab.util.Tools.nullIsIllegal;
Ray Milkey2b217142014-12-15 09:24:24 -080034
35/**
36 * Intent JSON codec.
37 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080038public final class IntentCodec extends JsonCodec<Intent> {
Ray Milkey2b217142014-12-15 09:24:24 -080039
Ray Milkeyb82c42b2015-06-30 09:42:20 -070040 protected static final String TYPE = "type";
41 protected static final String ID = "id";
42 protected static final String APP_ID = "appId";
43 protected static final String DETAILS = "details";
44 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");
53 final ObjectNode result = context.mapper().createObjectNode()
Ray Milkeyb82c42b2015-06-30 09:42:20 -070054 .put(TYPE, intent.getClass().getSimpleName())
55 .put(ID, intent.id().toString())
56 .put(APP_ID, intent.appId().toString())
57 .put(DETAILS, intent.toString());
Ray Milkey2b217142014-12-15 09:24:24 -080058
Ray Milkeyb82c42b2015-06-30 09:42:20 -070059 final ArrayNode jsonResources = result.putArray(RESOURCES);
Ray Milkey2b217142014-12-15 09:24:24 -080060
Sho SHIMIZUd7d18002015-01-21 14:37:14 -080061 for (final NetworkResource resource : intent.resources()) {
62 jsonResources.add(resource.toString());
Ray Milkey2b217142014-12-15 09:24:24 -080063 }
Ray Milkey1534f8d2015-05-13 15:42:50 -070064
65 IntentService service = context.getService(IntentService.class);
66 IntentState state = service.getIntentState(intent.key());
Ray Milkeyccab22d2015-05-18 13:24:50 -070067 if (state != null) {
Ray Milkeyb82c42b2015-06-30 09:42:20 -070068 result.put(STATE, state.toString());
Ray Milkeyccab22d2015-05-18 13:24:50 -070069 }
Ray Milkey1534f8d2015-05-13 15:42:50 -070070
Ray Milkey2b217142014-12-15 09:24:24 -080071 return result;
72 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -070073
74 @Override
75 public Intent decode(ObjectNode json, CodecContext context) {
76 checkNotNull(json, "JSON cannot be null");
77
78 String type = nullIsIllegal(json.get(TYPE),
79 TYPE + MISSING_MEMBER_MESSAGE).asText();
80
81 if (type.equals(PointToPointIntent.class.getSimpleName())) {
82 return context.codec(PointToPointIntent.class).decode(json, context);
83 } else if (type.equals(HostToHostIntent.class.getSimpleName())) {
84 return context.codec(HostToHostIntent.class).decode(json, context);
85 }
86
87 throw new IllegalArgumentException("Intent type "
88 + type + " is not supported");
89 }
90
91 /**
92 * Extracts base intent specific attributes from a JSON object
93 * and adds them to a builder.
94 *
95 * @param json root JSON object
96 * @param context code context
97 * @param builder builder to use for storing the attributes
98 */
99 public static void intentAttributes(ObjectNode json, CodecContext context,
100 Intent.Builder builder) {
101 short appId = (short) nullIsIllegal(json.get(IntentCodec.APP_ID),
102 IntentCodec.TYPE + IntentCodec.MISSING_MEMBER_MESSAGE).asInt();
103 CoreService service = context.getService(CoreService.class);
104 builder.appId(service.getAppId(appId));
105
106 JsonNode priorityJson = json.get(IntentCodec.PRIORITY);
107 if (priorityJson != null) {
108 builder.priority(priorityJson.asInt());
109 }
110 }
Ray Milkey2b217142014-12-15 09:24:24 -0800111}