SureshBR | 163b5db | 2015-10-28 19:15:45 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Foundation |
SureshBR | 163b5db | 2015-10-28 19:15:45 +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 | package org.onosproject.vtnweb.web; |
| 17 | |
| 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 | import java.util.UUID; |
| 24 | |
| 25 | import org.onosproject.codec.CodecContext; |
| 26 | import org.onosproject.codec.JsonCodec; |
| 27 | import org.onosproject.vtnrsc.TenantId; |
| 28 | import org.onosproject.vtnrsc.DefaultPortChain; |
| 29 | import org.onosproject.vtnrsc.FlowClassifierId; |
| 30 | import org.onosproject.vtnrsc.PortChain; |
| 31 | import org.onosproject.vtnrsc.PortChainId; |
| 32 | import org.onosproject.vtnrsc.PortPairGroupId; |
| 33 | |
| 34 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 35 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 36 | import com.google.common.collect.Lists; |
| 37 | |
| 38 | /** |
| 39 | * Port chain JSON codec. |
| 40 | */ |
| 41 | public final class PortChainCodec extends JsonCodec<PortChain> { |
| 42 | |
| 43 | private static final String ID = "id"; |
| 44 | private static final String TENANT_ID = "tenant_id"; |
| 45 | private static final String NAME = "name"; |
| 46 | private static final String DESCRIPTION = "description"; |
| 47 | private static final String PORT_PAIR_GROUPS = "port_pair_groups"; |
| 48 | private static final String FLOW_CLASSIFIERS = "flow_classifiers"; |
| 49 | private static final String MISSING_MEMBER_MESSAGE = |
| 50 | " member is required in PortChain"; |
| 51 | |
| 52 | @Override |
| 53 | public PortChain decode(ObjectNode json, CodecContext context) { |
| 54 | if (json == null || !json.isObject()) { |
| 55 | return null; |
| 56 | } |
| 57 | |
| 58 | PortChain.Builder resultBuilder = new DefaultPortChain.Builder(); |
| 59 | |
| 60 | String id = nullIsIllegal(json.get(ID), |
| 61 | ID + MISSING_MEMBER_MESSAGE).asText(); |
Mahesh Poojary Huawei | a9feb6d | 2015-11-05 13:24:16 +0530 | [diff] [blame] | 62 | resultBuilder.setId(PortChainId.of(id)); |
SureshBR | 163b5db | 2015-10-28 19:15:45 +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 | ArrayNode arrayNode = (ArrayNode) json.path(PORT_PAIR_GROUPS); |
| 77 | if (arrayNode != null) { |
| 78 | List<PortPairGroupId> list = Lists.newArrayList(); |
Mahesh Poojary Huawei | f5f1edd | 2015-11-05 11:28:01 +0530 | [diff] [blame] | 79 | arrayNode.forEach(i -> list.add(PortPairGroupId.of(i.asText()))); |
SureshBR | 163b5db | 2015-10-28 19:15:45 +0530 | [diff] [blame] | 80 | resultBuilder.setPortPairGroups(list); |
| 81 | } |
| 82 | |
| 83 | arrayNode = (ArrayNode) json.path(FLOW_CLASSIFIERS); |
| 84 | if (arrayNode != null) { |
| 85 | List<FlowClassifierId> list = Lists.newArrayList(); |
Mahesh Poojary Huawei | 321cc50 | 2015-11-03 13:23:32 +0530 | [diff] [blame] | 86 | arrayNode.forEach(i -> list.add(FlowClassifierId.of(UUID.fromString(i.asText())))); |
SureshBR | 163b5db | 2015-10-28 19:15:45 +0530 | [diff] [blame] | 87 | resultBuilder.setFlowClassifiers(list); |
| 88 | } |
| 89 | |
| 90 | return resultBuilder.build(); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public ObjectNode encode(PortChain portChain, CodecContext context) { |
| 95 | checkNotNull(portChain, "port pair cannot be null"); |
| 96 | ObjectNode result = context.mapper().createObjectNode() |
| 97 | .put(ID, portChain.portChainId().toString()) |
| 98 | .put(TENANT_ID, portChain.tenantId().toString()) |
| 99 | .put(NAME, portChain.name()) |
| 100 | .put(DESCRIPTION, portChain.description()) |
| 101 | .put(PORT_PAIR_GROUPS, portChain.portPairGroups().toString()) |
| 102 | .put(FLOW_CLASSIFIERS, portChain.flowClassifiers().toString()); |
| 103 | return result; |
| 104 | } |
| 105 | } |