blob: 9afbdcc528ed52919c80be2c4f278da84af6f173 [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 }
Matteo Scandolo198aef92020-01-08 00:43:17 +000084 UniTagInformation.Builder tagInfoBuilder = new UniTagInformation.Builder();
yasin sapli2c93ddb2021-06-11 17:46:26 +030085 String usBp = json.get(US_BP) == null ? EMPTY_BP :
86 json.get(US_BP).asText();
87 String dsBp = json.get(DS_BP) == null ? EMPTY_BP :
88 json.get(DS_BP).asText();
89 String usOltBp = json.get(US_OLT_BP) == null ? usBp : json.get(US_OLT_BP).asText();
90 String dsOltBp = json.get(DS_OLT_BP) == null ? dsBp : json.get(DS_OLT_BP).asText();
Matteo Scandolo198aef92020-01-08 00:43:17 +000091 tagInfoBuilder.setUniTagMatch(json.get(UNI_TAG_MATCH) == null ? VlanId.vlanId(VlanId.NO_VID) :
92 VlanId.vlanId(json.get(UNI_TAG_MATCH).shortValue()))
93 .setPonCTag(json.get(PON_CTAG) == null ? VlanId.vlanId(VlanId.NO_VID) :
94 VlanId.vlanId(json.get(PON_CTAG).shortValue()))
Andrea Campanella30041b92022-01-28 16:09:08 +010095 .setPonSTag(json.get(PON_STAG) == null ? VlanId.vlanId(VlanId.NO_VID) :
Matteo Scandolo198aef92020-01-08 00:43:17 +000096 VlanId.vlanId(json.get(PON_STAG).shortValue()))
97 .setUsPonCTagPriority(json.get(US_PON_CTAG_PCP) == null ? NO_PCP :
98 json.get(US_PON_CTAG_PCP).asInt())
99 .setUsPonSTagPriority(json.get(US_PON_STAG_PCP) == null ? NO_PCP :
100 json.get(US_PON_STAG_PCP).asInt())
101 .setDsPonCTagPriority(json.get(DS_PON_CTAG_PCP) == null ? NO_PCP :
102 json.get(DS_PON_CTAG_PCP).asInt())
103 .setDsPonSTagPriority(json.get(DS_PON_STAG_PCP) == null ? NO_PCP :
104 json.get(DS_PON_STAG_PCP).asInt())
105 .setTechnologyProfileId(json.get(TP_ID) == null ? NO_TP :
106 json.get(TP_ID).asInt())
yasin sapli2c93ddb2021-06-11 17:46:26 +0300107 .setUpstreamBandwidthProfile(usBp)
108 .setDownstreamBandwidthProfile(dsBp)
109 .setUpstreamOltBandwidthProfile(usOltBp)
110 .setDownstreamOltBandwidthProfile(dsOltBp)
Matteo Scandolo198aef92020-01-08 00:43:17 +0000111 .setServiceName(json.get(SN) == null ? EMPTY_SN :
112 json.get(SN).asText())
113 .setEnableMacLearning(json.get(MAC_LEARN) == null ? DEFAULT_MAC_LEARN :
114 json.get(MAC_LEARN).asBoolean())
115 .setConfiguredMacAddress(json.get(MAC) == null ? EMPTY_MAC :
116 json.get(MAC).asText())
117 .setIsDhcpRequired(json.get(DHCP_REQ) == null ? DEFAULT_DHCP_REQ :
118 json.get(DHCP_REQ).asBoolean())
119 .setIsIgmpRequired(json.get(IGMP_REQ) == null ? DEFAULT_IGMP_REQ :
120 json.get(IGMP_REQ).asBoolean());
yasin sapli2c93ddb2021-06-11 17:46:26 +0300121 log.info("Codec UniTagInformation Codec builder returning {}", tagInfoBuilder.build());
Matteo Scandolo198aef92020-01-08 00:43:17 +0000122 return tagInfoBuilder.build();
123 }
124}