blob: 8478a7646b01743c112de5495fbe3695fe7343fc [file] [log] [blame]
SureshBR163b5db2015-10-28 19:15:45 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
SureshBR163b5db2015-10-28 19:15:45 +05303 *
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.vtnweb.web;
17
18
19import static com.google.common.base.Preconditions.checkNotNull;
20import static org.onlab.util.Tools.nullIsIllegal;
21
22import java.util.List;
23import java.util.UUID;
24
25import org.onosproject.codec.CodecContext;
26import org.onosproject.codec.JsonCodec;
27import org.onosproject.vtnrsc.TenantId;
28import org.onosproject.vtnrsc.DefaultPortChain;
29import org.onosproject.vtnrsc.FlowClassifierId;
30import org.onosproject.vtnrsc.PortChain;
31import org.onosproject.vtnrsc.PortChainId;
32import org.onosproject.vtnrsc.PortPairGroupId;
33
34import com.fasterxml.jackson.databind.node.ArrayNode;
35import com.fasterxml.jackson.databind.node.ObjectNode;
36import com.google.common.collect.Lists;
37
38/**
39 * Port chain JSON codec.
40 */
41public 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 Huaweia9feb6d2015-11-05 13:24:16 +053062 resultBuilder.setId(PortChainId.of(id));
SureshBR163b5db2015-10-28 19:15:45 +053063
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 Huaweif5f1edd2015-11-05 11:28:01 +053079 arrayNode.forEach(i -> list.add(PortPairGroupId.of(i.asText())));
SureshBR163b5db2015-10-28 19:15:45 +053080 resultBuilder.setPortPairGroups(list);
81 }
82
83 arrayNode = (ArrayNode) json.path(FLOW_CLASSIFIERS);
84 if (arrayNode != null) {
85 List<FlowClassifierId> list = Lists.newArrayList();
Mahesh Poojary Huawei321cc502015-11-03 13:23:32 +053086 arrayNode.forEach(i -> list.add(FlowClassifierId.of(UUID.fromString(i.asText()))));
SureshBR163b5db2015-10-28 19:15:45 +053087 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}