blob: 8955226ca4353e3eebc38b457c1be47a598b09c2 [file] [log] [blame]
Ray Milkey39616f32015-05-14 15:43:00 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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()
Seyeon Jeong8188da12020-03-03 12:49:48 -080074 // a Group id should be an unsigned integer
75 .put(ID, Integer.toUnsignedLong(group.id().id()))
Ray Milkey39616f32015-05-14 15:43:00 -070076 .put(STATE, group.state().toString())
77 .put(LIFE, group.life())
78 .put(PACKETS, group.packets())
79 .put(BYTES, group.bytes())
80 .put(REFERENCE_COUNT, group.referenceCount())
81 .put(TYPE, group.type().toString())
82 .put(DEVICE_ID, group.deviceId().toString());
83
84 if (group.appId() != null) {
varunsha34b30602016-08-18 10:31:18 -070085 result.put(APP_ID, group.appId().name());
Ray Milkey39616f32015-05-14 15:43:00 -070086 }
87
88 if (group.appCookie() != null) {
89 result.put(APP_COOKIE, group.appCookie().toString());
90 }
91
92 if (group.givenGroupId() != null) {
Seyeon Jeong8188da12020-03-03 12:49:48 -080093 // a given Group id should be an unsigned integer
94 result.put(GIVEN_GROUP_ID, Integer.toUnsignedLong(group.givenGroupId()));
Ray Milkey39616f32015-05-14 15:43:00 -070095 }
96
97 ArrayNode buckets = context.mapper().createArrayNode();
98 group.buckets().buckets().forEach(bucket -> {
Jian Liecb3c0f2015-12-15 10:07:49 -080099 ObjectNode bucketJson = context.codec(GroupBucket.class).encode(bucket, context);
100 buckets.add(bucketJson);
101 });
Ray Milkey39616f32015-05-14 15:43:00 -0700102 result.set(BUCKETS, buckets);
103 return result;
104 }
Jian Liecb3c0f2015-12-15 10:07:49 -0800105
106 @Override
107 public Group decode(ObjectNode json, CodecContext context) {
108 if (json == null || !json.isObject()) {
109 return null;
110 }
111
112 final JsonCodec<GroupBucket> groupBucketCodec = context.codec(GroupBucket.class);
113 CoreService coreService = context.getService(CoreService.class);
114
Seyeon Jeong8188da12020-03-03 12:49:48 -0800115 // parse uInt Group id from the ID field
116 JsonNode idNode = json.get(ID);
117 Long id = (null == idNode) ?
118 // use GROUP_ID for the corresponding Group id if ID is not supplied
119 nullIsIllegal(json.get(GROUP_ID), ID + MISSING_MEMBER_MESSAGE).asLong() :
120 idNode.asLong();
121 GroupId groupId = GroupId.valueOf(id.intValue());
122
123 // parse uInt Group id given by caller. see the GroupDescription javadoc
124 JsonNode givenGroupIdNode = json.get(GIVEN_GROUP_ID);
125 // if GIVEN_GROUP_ID is not supplied, set null so the group subsystem
126 // to choose the Group id (which is the value of ID indeed).
127 // else, must be same with ID to show both originate from the same source
128 Integer givenGroupIdInt = (null == givenGroupIdNode) ?
129 null : new Long(json.get(GIVEN_GROUP_ID).asLong()).intValue();
130 if (givenGroupIdInt != null && !givenGroupIdInt.equals(groupId.id())) {
131 throw new IllegalArgumentException(GIVEN_GROUP_ID + " must be same with " + ID);
132 }
Jian Liecb3c0f2015-12-15 10:07:49 -0800133
134 // parse group key (appCookie)
135 String groupKeyStr = nullIsIllegal(json.get(APP_COOKIE),
136 APP_COOKIE + MISSING_MEMBER_MESSAGE).asText();
Charles Chanb3ef1fd2016-05-12 20:49:39 -0700137 if (!groupKeyStr.startsWith("0x")) {
138 throw new IllegalArgumentException("APP_COOKIE must be a hex string starts with 0x");
139 }
140 GroupKey groupKey = new DefaultGroupKey(HexString.fromHexString(
141 groupKeyStr.split("0x")[1], ""));
Jian Liecb3c0f2015-12-15 10:07:49 -0800142
143 // parse device id
144 DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID),
145 DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
146
147 // application id
148 ApplicationId appId = coreService.registerApplication(REST_APP_ID);
149
150 // parse group type
151 String type = nullIsIllegal(json.get(TYPE),
152 TYPE + MISSING_MEMBER_MESSAGE).asText();
153 GroupDescription.Type groupType = null;
154
155 switch (type) {
156 case "SELECT":
157 groupType = Group.Type.SELECT;
158 break;
159 case "INDIRECT":
160 groupType = Group.Type.INDIRECT;
161 break;
162 case "ALL":
163 groupType = Group.Type.ALL;
164 break;
Carmelo Cascone5079a7f2019-04-16 17:33:31 -0700165 case "CLONE":
166 groupType = Group.Type.CLONE;
167 break;
Jian Liecb3c0f2015-12-15 10:07:49 -0800168 case "FAILOVER":
169 groupType = Group.Type.FAILOVER;
170 break;
171 default:
Varun Sharmab711fbf42016-08-06 04:25:07 +0530172 nullIsIllegal(groupType, "The requested group type " + type + " is not valid");
Jian Liecb3c0f2015-12-15 10:07:49 -0800173 }
174
175 // parse group buckets
Jian Liecb3c0f2015-12-15 10:07:49 -0800176 GroupBuckets buckets = null;
177 List<GroupBucket> groupBucketList = new ArrayList<>();
178 JsonNode bucketsJson = json.get(BUCKETS);
179 checkNotNull(bucketsJson);
180 if (bucketsJson != null) {
181 IntStream.range(0, bucketsJson.size())
182 .forEach(i -> {
183 ObjectNode bucketJson = get(bucketsJson, i);
184 bucketJson.put("type", type);
185 groupBucketList.add(groupBucketCodec.decode(bucketJson, context));
186 });
187 buckets = new GroupBuckets(groupBucketList);
188 }
189
190 GroupDescription groupDescription = new DefaultGroupDescription(deviceId,
Seyeon Jeong8188da12020-03-03 12:49:48 -0800191 groupType, buckets, groupKey, givenGroupIdInt, appId);
Jian Liecb3c0f2015-12-15 10:07:49 -0800192
193 return new DefaultGroup(groupId, groupDescription);
194 }
Ray Milkey39616f32015-05-14 15:43:00 -0700195}