Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Charles Chan | b3ef1fd | 2016-05-12 20:49:39 -0700 | [diff] [blame] | 21 | import org.onlab.util.HexString; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 22 | import org.onosproject.codec.CodecContext; |
| 23 | import org.onosproject.codec.JsonCodec; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 26 | import org.onosproject.core.GroupId; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.group.DefaultGroup; |
| 29 | import org.onosproject.net.group.DefaultGroupDescription; |
| 30 | import org.onosproject.net.group.DefaultGroupKey; |
| 31 | import org.onosproject.net.group.Group; |
| 32 | import org.onosproject.net.group.GroupBucket; |
| 33 | import org.onosproject.net.group.GroupBuckets; |
| 34 | import org.onosproject.net.group.GroupDescription; |
| 35 | import org.onosproject.net.group.GroupKey; |
| 36 | import org.slf4j.Logger; |
| 37 | |
| 38 | import java.util.ArrayList; |
| 39 | import java.util.List; |
| 40 | import java.util.stream.IntStream; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 41 | |
| 42 | import static com.google.common.base.Preconditions.checkNotNull; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 43 | import static org.onlab.util.Tools.nullIsIllegal; |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Group JSON codec. |
| 48 | */ |
| 49 | public final class GroupCodec extends JsonCodec<Group> { |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 50 | private final Logger log = getLogger(getClass()); |
| 51 | |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 52 | // 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 Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 60 | private static final String GROUP_ID = "groupId"; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 61 | private static final String DEVICE_ID = "deviceId"; |
| 62 | private static final String APP_ID = "appId"; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 63 | private static final String APP_COOKIE = "appCookie"; |
| 64 | private static final String GIVEN_GROUP_ID = "givenGroupId"; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 65 | private static final String BUCKETS = "buckets"; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 66 | 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 Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 69 | |
| 70 | @Override |
| 71 | public ObjectNode encode(Group group, CodecContext context) { |
| 72 | checkNotNull(group, "Group cannot be null"); |
| 73 | ObjectNode result = context.mapper().createObjectNode() |
Seyeon Jeong | 8188da1 | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 74 | // a Group id should be an unsigned integer |
| 75 | .put(ID, Integer.toUnsignedLong(group.id().id())) |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 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) { |
varunsha | 34b3060 | 2016-08-18 10:31:18 -0700 | [diff] [blame] | 85 | result.put(APP_ID, group.appId().name()); |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | if (group.appCookie() != null) { |
| 89 | result.put(APP_COOKIE, group.appCookie().toString()); |
| 90 | } |
| 91 | |
| 92 | if (group.givenGroupId() != null) { |
Seyeon Jeong | 8188da1 | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 93 | // a given Group id should be an unsigned integer |
| 94 | result.put(GIVEN_GROUP_ID, Integer.toUnsignedLong(group.givenGroupId())); |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | ArrayNode buckets = context.mapper().createArrayNode(); |
| 98 | group.buckets().buckets().forEach(bucket -> { |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 99 | ObjectNode bucketJson = context.codec(GroupBucket.class).encode(bucket, context); |
| 100 | buckets.add(bucketJson); |
| 101 | }); |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 102 | result.set(BUCKETS, buckets); |
| 103 | return result; |
| 104 | } |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 105 | |
| 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 Jeong | 8188da1 | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 115 | // 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 Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 133 | |
| 134 | // parse group key (appCookie) |
| 135 | String groupKeyStr = nullIsIllegal(json.get(APP_COOKIE), |
| 136 | APP_COOKIE + MISSING_MEMBER_MESSAGE).asText(); |
Charles Chan | b3ef1fd | 2016-05-12 20:49:39 -0700 | [diff] [blame] | 137 | 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 Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 142 | |
| 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 Cascone | 5079a7f | 2019-04-16 17:33:31 -0700 | [diff] [blame] | 165 | case "CLONE": |
| 166 | groupType = Group.Type.CLONE; |
| 167 | break; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 168 | case "FAILOVER": |
| 169 | groupType = Group.Type.FAILOVER; |
| 170 | break; |
| 171 | default: |
Varun Sharma | b711fbf4 | 2016-08-06 04:25:07 +0530 | [diff] [blame] | 172 | nullIsIllegal(groupType, "The requested group type " + type + " is not valid"); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // parse group buckets |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 176 | 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 Jeong | 8188da1 | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 191 | groupType, buckets, groupKey, givenGroupIdInt, appId); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 192 | |
| 193 | return new DefaultGroup(groupId, groupDescription); |
| 194 | } |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 195 | } |