blob: 385f399a65b3c086257dd43fc5d6e6cd8a653185 [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;
26import org.onosproject.core.DefaultGroupId;
27import org.onosproject.core.GroupId;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.group.DefaultGroup;
30import org.onosproject.net.group.DefaultGroupDescription;
31import org.onosproject.net.group.DefaultGroupKey;
32import org.onosproject.net.group.Group;
33import org.onosproject.net.group.GroupBucket;
34import org.onosproject.net.group.GroupBuckets;
35import org.onosproject.net.group.GroupDescription;
36import org.onosproject.net.group.GroupKey;
37import org.slf4j.Logger;
38
39import java.util.ArrayList;
40import java.util.List;
41import java.util.stream.IntStream;
Ray Milkey39616f32015-05-14 15:43:00 -070042
43import static com.google.common.base.Preconditions.checkNotNull;
Jian Liecb3c0f2015-12-15 10:07:49 -080044import static org.onlab.util.Tools.nullIsIllegal;
45import static org.slf4j.LoggerFactory.getLogger;
Ray Milkey39616f32015-05-14 15:43:00 -070046
47/**
48 * Group JSON codec.
49 */
50public final class GroupCodec extends JsonCodec<Group> {
Jian Liecb3c0f2015-12-15 10:07:49 -080051 private final Logger log = getLogger(getClass());
52
Ray Milkey39616f32015-05-14 15:43:00 -070053 // JSON field names
54 private static final String ID = "id";
55 private static final String STATE = "state";
56 private static final String LIFE = "life";
57 private static final String PACKETS = "packets";
58 private static final String BYTES = "bytes";
59 private static final String REFERENCE_COUNT = "referenceCount";
60 private static final String TYPE = "type";
Jian Liecb3c0f2015-12-15 10:07:49 -080061 private static final String GROUP_ID = "groupId";
Ray Milkey39616f32015-05-14 15:43:00 -070062 private static final String DEVICE_ID = "deviceId";
63 private static final String APP_ID = "appId";
Jian Liecb3c0f2015-12-15 10:07:49 -080064 private static final String APP_COOKIE = "appCookie";
65 private static final String GIVEN_GROUP_ID = "givenGroupId";
Ray Milkey39616f32015-05-14 15:43:00 -070066 private static final String BUCKETS = "buckets";
Jian Liecb3c0f2015-12-15 10:07:49 -080067 private static final String MISSING_MEMBER_MESSAGE =
68 " member is required in Group";
69 public static final String REST_APP_ID = "org.onosproject.rest";
Ray Milkey39616f32015-05-14 15:43:00 -070070
71 @Override
72 public ObjectNode encode(Group group, CodecContext context) {
73 checkNotNull(group, "Group cannot be null");
74 ObjectNode result = context.mapper().createObjectNode()
75 .put(ID, group.id().toString())
76 .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) {
85 result.put(APP_ID, group.appId().toString());
86 }
87
88 if (group.appCookie() != null) {
89 result.put(APP_COOKIE, group.appCookie().toString());
90 }
91
92 if (group.givenGroupId() != null) {
93 result.put(GIVEN_GROUP_ID, group.givenGroupId());
94 }
95
96 ArrayNode buckets = context.mapper().createArrayNode();
97 group.buckets().buckets().forEach(bucket -> {
Jian Liecb3c0f2015-12-15 10:07:49 -080098 ObjectNode bucketJson = context.codec(GroupBucket.class).encode(bucket, context);
99 buckets.add(bucketJson);
100 });
Ray Milkey39616f32015-05-14 15:43:00 -0700101 result.set(BUCKETS, buckets);
102 return result;
103 }
Jian Liecb3c0f2015-12-15 10:07:49 -0800104
105 @Override
106 public Group decode(ObjectNode json, CodecContext context) {
107 if (json == null || !json.isObject()) {
108 return null;
109 }
110
111 final JsonCodec<GroupBucket> groupBucketCodec = context.codec(GroupBucket.class);
112 CoreService coreService = context.getService(CoreService.class);
113
114 // parse group id
115 int groupIdInt = nullIsIllegal(json.get(GROUP_ID),
116 GROUP_ID + MISSING_MEMBER_MESSAGE).asInt();
Zsolt Haraszti265cd832016-02-25 08:40:34 -0800117 GroupId groupId = new DefaultGroupId(groupIdInt);
Jian Liecb3c0f2015-12-15 10:07:49 -0800118
119 // parse group key (appCookie)
120 String groupKeyStr = nullIsIllegal(json.get(APP_COOKIE),
121 APP_COOKIE + MISSING_MEMBER_MESSAGE).asText();
Charles Chanb3ef1fd2016-05-12 20:49:39 -0700122 if (!groupKeyStr.startsWith("0x")) {
123 throw new IllegalArgumentException("APP_COOKIE must be a hex string starts with 0x");
124 }
125 GroupKey groupKey = new DefaultGroupKey(HexString.fromHexString(
126 groupKeyStr.split("0x")[1], ""));
Jian Liecb3c0f2015-12-15 10:07:49 -0800127
128 // parse device id
129 DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID),
130 DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
131
132 // application id
133 ApplicationId appId = coreService.registerApplication(REST_APP_ID);
134
135 // parse group type
136 String type = nullIsIllegal(json.get(TYPE),
137 TYPE + MISSING_MEMBER_MESSAGE).asText();
138 GroupDescription.Type groupType = null;
139
140 switch (type) {
141 case "SELECT":
142 groupType = Group.Type.SELECT;
143 break;
144 case "INDIRECT":
145 groupType = Group.Type.INDIRECT;
146 break;
147 case "ALL":
148 groupType = Group.Type.ALL;
149 break;
150 case "FAILOVER":
151 groupType = Group.Type.FAILOVER;
152 break;
153 default:
Varun Sharmab711fbf42016-08-06 04:25:07 +0530154 nullIsIllegal(groupType, "The requested group type " + type + " is not valid");
Jian Liecb3c0f2015-12-15 10:07:49 -0800155 }
156
157 // parse group buckets
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530158
Jian Liecb3c0f2015-12-15 10:07:49 -0800159 GroupBuckets buckets = null;
160 List<GroupBucket> groupBucketList = new ArrayList<>();
161 JsonNode bucketsJson = json.get(BUCKETS);
162 checkNotNull(bucketsJson);
163 if (bucketsJson != null) {
164 IntStream.range(0, bucketsJson.size())
165 .forEach(i -> {
166 ObjectNode bucketJson = get(bucketsJson, i);
167 bucketJson.put("type", type);
168 groupBucketList.add(groupBucketCodec.decode(bucketJson, context));
169 });
170 buckets = new GroupBuckets(groupBucketList);
171 }
172
173 GroupDescription groupDescription = new DefaultGroupDescription(deviceId,
174 groupType, buckets, groupKey, groupIdInt, appId);
175
176 return new DefaultGroup(groupId, groupDescription);
177 }
Ray Milkey39616f32015-05-14 15:43:00 -0700178}