blob: 6a7e40496ae2ca7c771bace106018717d90060fb [file] [log] [blame]
Ray Milkey39616f32015-05-14 15:43:00 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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;
Jian Liecb3c0f2015-12-15 10:07:49 -080021import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
25import org.onosproject.core.DefaultGroupId;
26import 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()
74 .put(ID, group.id().toString())
75 .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) {
84 result.put(APP_ID, group.appId().toString());
85 }
86
87 if (group.appCookie() != null) {
88 result.put(APP_COOKIE, group.appCookie().toString());
89 }
90
91 if (group.givenGroupId() != null) {
92 result.put(GIVEN_GROUP_ID, group.givenGroupId());
93 }
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();
116 GroupId groupId = new DefaultGroupId((short) groupIdInt);
117
118 // parse group key (appCookie)
119 String groupKeyStr = nullIsIllegal(json.get(APP_COOKIE),
120 APP_COOKIE + MISSING_MEMBER_MESSAGE).asText();
121 GroupKey groupKey = new DefaultGroupKey(groupKeyStr.getBytes());
122
123 // parse device id
124 DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID),
125 DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
126
127 // application id
128 ApplicationId appId = coreService.registerApplication(REST_APP_ID);
129
130 // parse group type
131 String type = nullIsIllegal(json.get(TYPE),
132 TYPE + MISSING_MEMBER_MESSAGE).asText();
133 GroupDescription.Type groupType = null;
134
135 switch (type) {
136 case "SELECT":
137 groupType = Group.Type.SELECT;
138 break;
139 case "INDIRECT":
140 groupType = Group.Type.INDIRECT;
141 break;
142 case "ALL":
143 groupType = Group.Type.ALL;
144 break;
145 case "FAILOVER":
146 groupType = Group.Type.FAILOVER;
147 break;
148 default:
149 log.warn("The requested type {} is not defined for group.", type);
150 return null;
151 }
152
153 // parse group buckets
154 // TODO: make sure that INDIRECT group only has one bucket
155 GroupBuckets buckets = null;
156 List<GroupBucket> groupBucketList = new ArrayList<>();
157 JsonNode bucketsJson = json.get(BUCKETS);
158 checkNotNull(bucketsJson);
159 if (bucketsJson != null) {
160 IntStream.range(0, bucketsJson.size())
161 .forEach(i -> {
162 ObjectNode bucketJson = get(bucketsJson, i);
163 bucketJson.put("type", type);
164 groupBucketList.add(groupBucketCodec.decode(bucketJson, context));
165 });
166 buckets = new GroupBuckets(groupBucketList);
167 }
168
169 GroupDescription groupDescription = new DefaultGroupDescription(deviceId,
170 groupType, buckets, groupKey, groupIdInt, appId);
171
172 return new DefaultGroup(groupId, groupDescription);
173 }
Ray Milkey39616f32015-05-14 15:43:00 -0700174}