blob: cad67c0f67abf6c5a5d8c3475433e5132da433a5 [file] [log] [blame]
Matteo Scandolo198aef92020-01-08 00:43:17 +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;
yasin sapli2c93ddb2021-06-11 17:46:26 +030023import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
Matteo Scandolo198aef92020-01-08 00:43:17 +000025
26public class UniTagInformationCodec extends JsonCodec<UniTagInformation> {
27
yasin sapli2c93ddb2021-06-11 17:46:26 +030028 private final Logger log = LoggerFactory.getLogger(this.getClass());
29
30
Matteo Scandolo198aef92020-01-08 00:43:17 +000031 private static final String UNI_TAG_MATCH = "uniTagMatch";
32 private static final String PON_CTAG = "ponCTag";
33 private static final String PON_STAG = "ponSTag";
34 private static final String US_PON_CTAG_PCP = "usPonCTagPriority";
35 private static final String US_PON_STAG_PCP = "usPonSTagPriority";
36 private static final String DS_PON_CTAG_PCP = "dsPonCTagPriority";
37 private static final String DS_PON_STAG_PCP = "dsPonSTagPriority";
38 private static final String TP_ID = "technologyProfileId";
39 private static final String US_BP = "upstreamBandwidthProfile";
40 private static final String DS_BP = "downstreamBandwidthProfile";
yasin sapli2c93ddb2021-06-11 17:46:26 +030041 private static final String US_OLT_BP = "upstreamOltBandwidthProfile";
42 private static final String DS_OLT_BP = "downstreamOltBandwidthProfile";
Matteo Scandolo198aef92020-01-08 00:43:17 +000043 private static final String SN = "serviceName";
44 private static final String MAC_LEARN = "enableMacLearning";
45 private static final String MAC = "configuredMacAddress";
46 private static final String DHCP_REQ = "isDhcpRequired";
47 private static final String IGMP_REQ = "isIgmpRequired";
48 private static final int NO_PCP = -1;
49 private static final int NO_TP = -1;
50 private static final String EMPTY_BP = "";
51 private static final String EMPTY_SN = "";
52 private static final boolean DEFAULT_MAC_LEARN = false;
53 private static final String EMPTY_MAC = "";
54 private static final boolean DEFAULT_DHCP_REQ = false;
55 private static final boolean DEFAULT_IGMP_REQ = false;
56
57 @Override
58 public ObjectNode encode(UniTagInformation entry, CodecContext context) {
59 return context.mapper().createObjectNode()
60 .put(UNI_TAG_MATCH, entry.getUniTagMatch().toShort())
61 .put(PON_CTAG, entry.getPonCTag().toShort())
62 .put(PON_STAG, entry.getPonSTag().toShort())
63 .put(US_PON_CTAG_PCP, entry.getUsPonCTagPriority())
64 .put(US_PON_STAG_PCP, entry.getUsPonSTagPriority())
65 .put(DS_PON_CTAG_PCP, entry.getDsPonCTagPriority())
66 .put(DS_PON_STAG_PCP, entry.getDsPonSTagPriority())
67 .put(TP_ID, entry.getTechnologyProfileId())
68 .put(US_BP, entry.getUpstreamBandwidthProfile())
69 .put(DS_BP, entry.getDownstreamBandwidthProfile())
yasin sapli2c93ddb2021-06-11 17:46:26 +030070 .put(US_OLT_BP, entry.getUpstreamOltBandwidthProfile())
71 .put(DS_OLT_BP, entry.getDownstreamOltBandwidthProfile())
Matteo Scandolo198aef92020-01-08 00:43:17 +000072 .put(SN, entry.getServiceName())
73 .put(MAC_LEARN, entry.getEnableMacLearning())
74 .put(MAC, entry.getConfiguredMacAddress())
75 .put(DHCP_REQ, entry.getIsDhcpRequired())
76 .put(IGMP_REQ, entry.getIsIgmpRequired());
77 }
78
79 @Override
80 public UniTagInformation decode(ObjectNode json, CodecContext context) {
81 if (json == null || !json.isObject()) {
82 return null;
83 }
84
85 UniTagInformation.Builder tagInfoBuilder = new UniTagInformation.Builder();
yasin sapli2c93ddb2021-06-11 17:46:26 +030086 String usBp = json.get(US_BP) == null ? EMPTY_BP :
87 json.get(US_BP).asText();
88 String dsBp = json.get(DS_BP) == null ? EMPTY_BP :
89 json.get(DS_BP).asText();
90 String usOltBp = json.get(US_OLT_BP) == null ? usBp : json.get(US_OLT_BP).asText();
91 String dsOltBp = json.get(DS_OLT_BP) == null ? dsBp : json.get(DS_OLT_BP).asText();
Matteo Scandolo198aef92020-01-08 00:43:17 +000092 tagInfoBuilder.setUniTagMatch(json.get(UNI_TAG_MATCH) == null ? VlanId.vlanId(VlanId.NO_VID) :
93 VlanId.vlanId(json.get(UNI_TAG_MATCH).shortValue()))
94 .setPonCTag(json.get(PON_CTAG) == null ? VlanId.vlanId(VlanId.NO_VID) :
95 VlanId.vlanId(json.get(PON_CTAG).shortValue()))
96 .setPonCTag(json.get(PON_STAG) == null ? VlanId.vlanId(VlanId.NO_VID) :
97 VlanId.vlanId(json.get(PON_STAG).shortValue()))
98 .setUsPonCTagPriority(json.get(US_PON_CTAG_PCP) == null ? NO_PCP :
99 json.get(US_PON_CTAG_PCP).asInt())
100 .setUsPonSTagPriority(json.get(US_PON_STAG_PCP) == null ? NO_PCP :
101 json.get(US_PON_STAG_PCP).asInt())
102 .setDsPonCTagPriority(json.get(DS_PON_CTAG_PCP) == null ? NO_PCP :
103 json.get(DS_PON_CTAG_PCP).asInt())
104 .setDsPonSTagPriority(json.get(DS_PON_STAG_PCP) == null ? NO_PCP :
105 json.get(DS_PON_STAG_PCP).asInt())
106 .setTechnologyProfileId(json.get(TP_ID) == null ? NO_TP :
107 json.get(TP_ID).asInt())
yasin sapli2c93ddb2021-06-11 17:46:26 +0300108 .setUpstreamBandwidthProfile(usBp)
109 .setDownstreamBandwidthProfile(dsBp)
110 .setUpstreamOltBandwidthProfile(usOltBp)
111 .setDownstreamOltBandwidthProfile(dsOltBp)
Matteo Scandolo198aef92020-01-08 00:43:17 +0000112 .setServiceName(json.get(SN) == null ? EMPTY_SN :
113 json.get(SN).asText())
114 .setEnableMacLearning(json.get(MAC_LEARN) == null ? DEFAULT_MAC_LEARN :
115 json.get(MAC_LEARN).asBoolean())
116 .setConfiguredMacAddress(json.get(MAC) == null ? EMPTY_MAC :
117 json.get(MAC).asText())
118 .setIsDhcpRequired(json.get(DHCP_REQ) == null ? DEFAULT_DHCP_REQ :
119 json.get(DHCP_REQ).asBoolean())
120 .setIsIgmpRequired(json.get(IGMP_REQ) == null ? DEFAULT_IGMP_REQ :
121 json.get(IGMP_REQ).asBoolean());
yasin sapli2c93ddb2021-06-11 17:46:26 +0300122 log.info("Codec UniTagInformation Codec builder returning {}", tagInfoBuilder.build());
Matteo Scandolo198aef92020-01-08 00:43:17 +0000123 return tagInfoBuilder.build();
124 }
125}