Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 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; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreService; |
| 25 | import org.onosproject.core.DefaultGroupId; |
| 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() |
| 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 Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 97 | ObjectNode bucketJson = context.codec(GroupBucket.class).encode(bucket, context); |
| 98 | buckets.add(bucketJson); |
| 99 | }); |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 100 | result.set(BUCKETS, buckets); |
| 101 | return result; |
| 102 | } |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 103 | |
| 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 Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 174 | } |