blob: 5e27e5249050804a8bfa7f5e3a9c0f67eab26941 [file] [log] [blame]
Daniele Moro82d9a362019-11-06 23:51:20 +00001/*
2 * Copyright 2017-present Open Networking Foundation
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 */
16package org.opencord.sadis.impl;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onlab.packet.VlanId;
20import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
22import org.opencord.sadis.UniTagInformation;
23
24public class UniTagInformationCodec extends JsonCodec<UniTagInformation> {
25
26 private static final String UNI_TAG_MATCH = "uniTagMatch";
27 private static final String PON_CTAG = "ponCTag";
28 private static final String PON_STAG = "ponSTag";
29 private static final String US_PON_CTAG_PCP = "usPonCTagPriority";
30 private static final String US_PON_STAG_PCP = "usPonSTagPriority";
31 private static final String DS_PON_CTAG_PCP = "dsPonCTagPriority";
32 private static final String DS_PON_STAG_PCP = "dsPonSTagPriority";
33 private static final String TP_ID = "technologyProfileId";
34 private static final String US_BP = "upstreamBandwidthProfile";
35 private static final String DS_BP = "downstreamBandwidthProfile";
36 private static final String SN = "serviceName";
37 private static final String MAC_LEARN = "enableMacLearning";
38 private static final String MAC = "configuredMacAddress";
39 private static final String DHCP_REQ = "isDhcpRequired";
40 private static final String IGMP_REQ = "isIgmpRequired";
41 private static final int NO_PCP = -1;
42 private static final int NO_TP = -1;
43 private static final String EMPTY_BP = "";
44 private static final String EMPTY_SN = "";
45 private static final boolean DEFAULT_MAC_LEARN = false;
46 private static final String EMPTY_MAC = "";
47 private static final boolean DEFAULT_DHCP_REQ = false;
48 private static final boolean DEFAULT_IGMP_REQ = false;
49
50 @Override
51 public ObjectNode encode(UniTagInformation entry, CodecContext context) {
52 return context.mapper().createObjectNode()
53 .put(UNI_TAG_MATCH, entry.getUniTagMatch().toShort())
54 .put(PON_CTAG, entry.getPonCTag().toShort())
55 .put(PON_STAG, entry.getPonSTag().toShort())
56 .put(US_PON_CTAG_PCP, entry.getUsPonCTagPriority())
57 .put(US_PON_STAG_PCP, entry.getUsPonSTagPriority())
58 .put(DS_PON_CTAG_PCP, entry.getDsPonCTagPriority())
59 .put(DS_PON_STAG_PCP, entry.getDsPonSTagPriority())
60 .put(TP_ID, entry.getTechnologyProfileId())
61 .put(US_BP, entry.getUpstreamBandwidthProfile())
62 .put(DS_BP, entry.getDownstreamBandwidthProfile())
63 .put(SN, entry.getServiceName())
64 .put(MAC_LEARN, entry.getEnableMacLearning())
65 .put(MAC, entry.getConfiguredMacAddress())
66 .put(DHCP_REQ, entry.getIsDhcpRequired())
67 .put(IGMP_REQ, entry.getIsIgmpRequired());
68 }
69
70 @Override
71 public UniTagInformation decode(ObjectNode json, CodecContext context) {
72 if (json == null || !json.isObject()) {
73 return null;
74 }
75
76 UniTagInformation.Builder tagInfoBuilder = new UniTagInformation.Builder();
77 tagInfoBuilder.setUniTagMatch(json.get(UNI_TAG_MATCH) == null ? VlanId.vlanId(VlanId.NO_VID) :
78 VlanId.vlanId(json.get(UNI_TAG_MATCH).shortValue()))
79 .setPonCTag(json.get(PON_CTAG) == null ? VlanId.vlanId(VlanId.NO_VID) :
80 VlanId.vlanId(json.get(PON_CTAG).shortValue()))
81 .setPonCTag(json.get(PON_STAG) == null ? VlanId.vlanId(VlanId.NO_VID) :
82 VlanId.vlanId(json.get(PON_STAG).shortValue()))
83 .setUsPonCTagPriority(json.get(US_PON_CTAG_PCP) == null ? NO_PCP :
84 json.get(US_PON_CTAG_PCP).asInt())
85 .setUsPonSTagPriority(json.get(US_PON_STAG_PCP) == null ? NO_PCP :
86 json.get(US_PON_STAG_PCP).asInt())
87 .setDsPonCTagPriority(json.get(DS_PON_CTAG_PCP) == null ? NO_PCP :
88 json.get(DS_PON_CTAG_PCP).asInt())
89 .setDsPonSTagPriority(json.get(DS_PON_STAG_PCP) == null ? NO_PCP :
90 json.get(DS_PON_STAG_PCP).asInt())
91 .setTechnologyProfileId(json.get(TP_ID) == null ? NO_TP :
92 json.get(TP_ID).asInt())
93 .setUpstreamBandwidthProfile(json.get(US_BP) == null ? EMPTY_BP :
94 json.get(US_BP).asText())
95 .setDownstreamBandwidthProfile(json.get(DS_BP) == null ? EMPTY_BP :
96 json.get(DS_BP).asText())
97 .setServiceName(json.get(SN) == null ? EMPTY_SN :
98 json.get(SN).asText())
99 .setEnableMacLearning(json.get(MAC_LEARN) == null ? DEFAULT_MAC_LEARN :
100 json.get(MAC_LEARN).asBoolean())
101 .setConfiguredMacAddress(json.get(MAC) == null ? EMPTY_MAC :
102 json.get(MAC).asText())
103 .setIsDhcpRequired(json.get(DHCP_REQ) == null ? DEFAULT_DHCP_REQ :
104 json.get(DHCP_REQ).asBoolean())
105 .setIsIgmpRequired(json.get(IGMP_REQ) == null ? DEFAULT_IGMP_REQ :
106 json.get(IGMP_REQ).asBoolean());
107
108 return tagInfoBuilder.build();
109 }
110}