blob: 029200827425f36d5fe29bb0261a38b860581545 [file] [log] [blame]
Ray Milkey39616f32015-05-14 15:43:00 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkey39616f32015-05-14 15:43:00 -07003 *
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
Jian Liecb3c0f2015-12-15 10:07:49 -080018import com.fasterxml.jackson.databind.JsonNode;
Ray Milkey39616f32015-05-14 15:43:00 -070019import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Charles Chanb3ef1fd2016-05-12 20:49:39 -070021import org.onlab.util.HexString;
Jian Liecb3c0f2015-12-15 10:07:49 -080022import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
Jian Liecb3c0f2015-12-15 10:07:49 -080026import org.onosproject.core.GroupId;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.group.DefaultGroup;
29import org.onosproject.net.group.DefaultGroupDescription;
30import org.onosproject.net.group.DefaultGroupKey;
31import org.onosproject.net.group.Group;
32import org.onosproject.net.group.GroupBucket;
33import org.onosproject.net.group.GroupBuckets;
34import org.onosproject.net.group.GroupDescription;
35import org.onosproject.net.group.GroupKey;
36import org.slf4j.Logger;
37
38import java.util.ArrayList;
39import java.util.List;
40import java.util.stream.IntStream;
Ray Milkey39616f32015-05-14 15:43:00 -070041
42import static com.google.common.base.Preconditions.checkNotNull;
Jian Liecb3c0f2015-12-15 10:07:49 -080043import static org.onlab.util.Tools.nullIsIllegal;
44import static org.slf4j.LoggerFactory.getLogger;
Ray Milkey39616f32015-05-14 15:43:00 -070045
46/**
47 * Group JSON codec.
48 */
49public final class GroupCodec extends JsonCodec<Group> {
Jian Liecb3c0f2015-12-15 10:07:49 -080050 private final Logger log = getLogger(getClass());
51
Ray Milkey39616f32015-05-14 15:43:00 -070052 // JSON field names
53 private static final String ID = "id";
54 private static final String STATE = "state";
55 private static final String LIFE = "life";
56 private static final String PACKETS = "packets";
57 private static final String BYTES = "bytes";
58 private static final String REFERENCE_COUNT = "referenceCount";
59 private static final String TYPE = "type";
Jian Liecb3c0f2015-12-15 10:07:49 -080060 private static final String GROUP_ID = "groupId";
Ray Milkey39616f32015-05-14 15:43:00 -070061 private static final String DEVICE_ID = "deviceId";
62 private static final String APP_ID = "appId";
Jian Liecb3c0f2015-12-15 10:07:49 -080063 private static final String APP_COOKIE = "appCookie";
64 private static final String GIVEN_GROUP_ID = "givenGroupId";
Ray Milkey39616f32015-05-14 15:43:00 -070065 private static final String BUCKETS = "buckets";
Jian Liecb3c0f2015-12-15 10:07:49 -080066 private static final String MISSING_MEMBER_MESSAGE =
67 " member is required in Group";
68 public static final String REST_APP_ID = "org.onosproject.rest";
Ray Milkey39616f32015-05-14 15:43:00 -070069
70 @Override
71 public ObjectNode encode(Group group, CodecContext context) {
72 checkNotNull(group, "Group cannot be null");
73 ObjectNode result = context.mapper().createObjectNode()
Prince Pereira3ff504c2016-08-30 14:23:43 +053074 .put(ID, group.id().id().toString())
Ray Milkey39616f32015-05-14 15:43:00 -070075 .put(STATE, group.state().toString())
76 .put(LIFE, group.life())
77 .put(PACKETS, group.packets())
78 .put(BYTES, group.bytes())
79 .put(REFERENCE_COUNT, group.referenceCount())
80 .put(TYPE, group.type().toString())
81 .put(DEVICE_ID, group.deviceId().toString());
82
83 if (group.appId() != null) {
varunsha34b30602016-08-18 10:31:18 -070084 result.put(APP_ID, group.appId().name());
Ray Milkey39616f32015-05-14 15:43:00 -070085 }
86
87 if (group.appCookie() != null) {
88 result.put(APP_COOKIE, group.appCookie().toString());
89 }
90
91 if (group.givenGroupId() != null) {
Prince Pereira3ff504c2016-08-30 14:23:43 +053092 result.put(GIVEN_GROUP_ID, group.givenGroupId().toString());
Ray Milkey39616f32015-05-14 15:43:00 -070093 }
94
95 ArrayNode buckets = context.mapper().createArrayNode();
96 group.buckets().buckets().forEach(bucket -> {
Jian Liecb3c0f2015-12-15 10:07:49 -080097 ObjectNode bucketJson = context.codec(GroupBucket.class).encode(bucket, context);
98 buckets.add(bucketJson);
99 });
Ray Milkey39616f32015-05-14 15:43:00 -0700100 result.set(BUCKETS, buckets);
101 return result;
102 }
Jian Liecb3c0f2015-12-15 10:07:49 -0800103
104 @Override
105 public Group decode(ObjectNode json, CodecContext context) {
106 if (json == null || !json.isObject()) {
107 return null;
108 }
109
110 final JsonCodec<GroupBucket> groupBucketCodec = context.codec(GroupBucket.class);
111 CoreService coreService = context.getService(CoreService.class);
112
113 // parse group id
114 int groupIdInt = nullIsIllegal(json.get(GROUP_ID),
115 GROUP_ID + MISSING_MEMBER_MESSAGE).asInt();
Prince Pereira3ff504c2016-08-30 14:23:43 +0530116 GroupId groupId = new GroupId(groupIdInt);
Jian Liecb3c0f2015-12-15 10:07:49 -0800117
118 // parse group key (appCookie)
119 String groupKeyStr = nullIsIllegal(json.get(APP_COOKIE),
120 APP_COOKIE + MISSING_MEMBER_MESSAGE).asText();
Charles Chanb3ef1fd2016-05-12 20:49:39 -0700121 if (!groupKeyStr.startsWith("0x")) {
122 throw new IllegalArgumentException("APP_COOKIE must be a hex string starts with 0x");
123 }
124 GroupKey groupKey = new DefaultGroupKey(HexString.fromHexString(
125 groupKeyStr.split("0x")[1], ""));
Jian Liecb3c0f2015-12-15 10:07:49 -0800126
127 // parse device id
128 DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID),
129 DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
130
131 // application id
132 ApplicationId appId = coreService.registerApplication(REST_APP_ID);
133
134 // parse group type
135 String type = nullIsIllegal(json.get(TYPE),
136 TYPE + MISSING_MEMBER_MESSAGE).asText();
137 GroupDescription.Type groupType = null;
138
139 switch (type) {
140 case "SELECT":
141 groupType = Group.Type.SELECT;
142 break;
143 case "INDIRECT":
144 groupType = Group.Type.INDIRECT;
145 break;
146 case "ALL":
147 groupType = Group.Type.ALL;
148 break;
149 case "FAILOVER":
150 groupType = Group.Type.FAILOVER;
151 break;
152 default:
Varun Sharmab711fbf42016-08-06 04:25:07 +0530153 nullIsIllegal(groupType, "The requested group type " + type + " is not valid");
Jian Liecb3c0f2015-12-15 10:07:49 -0800154 }
155
156 // parse group buckets
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530157
Jian Liecb3c0f2015-12-15 10:07:49 -0800158 GroupBuckets buckets = null;
159 List<GroupBucket> groupBucketList = new ArrayList<>();
160 JsonNode bucketsJson = json.get(BUCKETS);
161 checkNotNull(bucketsJson);
162 if (bucketsJson != null) {
163 IntStream.range(0, bucketsJson.size())
164 .forEach(i -> {
165 ObjectNode bucketJson = get(bucketsJson, i);
166 bucketJson.put("type", type);
167 groupBucketList.add(groupBucketCodec.decode(bucketJson, context));
168 });
169 buckets = new GroupBuckets(groupBucketList);
170 }
171
172 GroupDescription groupDescription = new DefaultGroupDescription(deviceId,
173 groupType, buckets, groupKey, groupIdInt, appId);
174
175 return new DefaultGroup(groupId, groupDescription);
176 }
Ray Milkey39616f32015-05-14 15:43:00 -0700177}