Phaneendra Manda | 75f8be8 | 2015-10-29 16:57:00 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Phaneendra Manda | 75f8be8 | 2015-10-29 16:57:00 +0530 | [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 | |
| 17 | package org.onosproject.vtnweb.web; |
| 18 | |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | import static org.onlab.util.Tools.nullIsIllegal; |
| 21 | |
| 22 | import java.util.List; |
| 23 | |
| 24 | import org.onosproject.codec.CodecContext; |
| 25 | import org.onosproject.codec.JsonCodec; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.vtnrsc.DefaultPortPairGroup; |
| 28 | import org.onosproject.vtnrsc.PortPairGroup; |
| 29 | import org.onosproject.vtnrsc.PortPairGroupId; |
| 30 | import org.onosproject.vtnrsc.PortPairId; |
| 31 | import org.onosproject.vtnrsc.TenantId; |
| 32 | |
| 33 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 34 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 35 | import com.google.common.collect.Lists; |
| 36 | |
| 37 | /** |
| 38 | * Port Pair Group JSON codec. |
| 39 | */ |
| 40 | public final class PortPairGroupCodec extends JsonCodec<PortPairGroup> { |
| 41 | |
| 42 | private static final String ID = "id"; |
| 43 | private static final String TENANT_ID = "tenant_id"; |
| 44 | private static final String NAME = "name"; |
| 45 | private static final String DESCRIPTION = "description"; |
| 46 | private static final String PORT_PAIRS = "port_pairs"; |
| 47 | private static final String MISSING_MEMBER_MESSAGE = |
| 48 | " member is required in PortPairGroup"; |
| 49 | |
| 50 | @Override |
| 51 | public PortPairGroup decode(ObjectNode json, CodecContext context) { |
| 52 | if (json == null || !json.isObject()) { |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | PortPairGroup.Builder resultBuilder = new DefaultPortPairGroup.Builder(); |
| 57 | |
| 58 | CoreService coreService = context.getService(CoreService.class); |
| 59 | |
| 60 | String id = nullIsIllegal(json.get(ID), |
| 61 | ID + MISSING_MEMBER_MESSAGE).asText(); |
Mahesh Poojary Huawei | f5f1edd | 2015-11-05 11:28:01 +0530 | [diff] [blame] | 62 | resultBuilder.setId(PortPairGroupId.of(id)); |
Phaneendra Manda | 75f8be8 | 2015-10-29 16:57:00 +0530 | [diff] [blame] | 63 | |
| 64 | String tenantId = nullIsIllegal(json.get(TENANT_ID), |
| 65 | TENANT_ID + MISSING_MEMBER_MESSAGE).asText(); |
| 66 | resultBuilder.setTenantId(TenantId.tenantId(tenantId)); |
| 67 | |
| 68 | String name = nullIsIllegal(json.get(NAME), |
| 69 | NAME + MISSING_MEMBER_MESSAGE).asText(); |
| 70 | resultBuilder.setName(name); |
| 71 | |
| 72 | String description = nullIsIllegal(json.get(DESCRIPTION), |
| 73 | DESCRIPTION + MISSING_MEMBER_MESSAGE).asText(); |
| 74 | resultBuilder.setDescription(description); |
| 75 | |
| 76 | List<PortPairId> list = Lists.newArrayList(); |
| 77 | ArrayNode arrayNode = (ArrayNode) json.path(PORT_PAIRS); |
Mahesh Poojary Huawei | d0b1d13 | 2015-11-05 11:00:18 +0530 | [diff] [blame] | 78 | arrayNode.forEach(i -> list.add(PortPairId.of(i.asText()))); |
Phaneendra Manda | 75f8be8 | 2015-10-29 16:57:00 +0530 | [diff] [blame] | 79 | resultBuilder.setPortPairs(list); |
| 80 | |
| 81 | return resultBuilder.build(); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public ObjectNode encode(PortPairGroup portPairGroup, CodecContext context) { |
| 86 | checkNotNull(portPairGroup, "port pair group cannot be null"); |
| 87 | ObjectNode result = context.mapper().createObjectNode() |
| 88 | .put(ID, portPairGroup.portPairGroupId().toString()) |
| 89 | .put(TENANT_ID, portPairGroup.tenantId().toString()) |
| 90 | .put(NAME, portPairGroup.name()) |
| 91 | .put(DESCRIPTION, portPairGroup.description()) |
| 92 | .put(PORT_PAIRS, portPairGroup.portPairs().toString()); |
| 93 | return result; |
| 94 | } |
| 95 | } |