blob: 09060bbc26a263007fabfd68ceeff7f5a0e22fe4 [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";
yasin sapli9e3ebed2021-07-09 14:06:35 +000048 private static final String PPPOE_REQ = "isPppoeRequired";
Matteo Scandolo198aef92020-01-08 00:43:17 +000049 private static final int NO_PCP = -1;
50 private static final int NO_TP = -1;
51 private static final String EMPTY_BP = "";
52 private static final String EMPTY_SN = "";
53 private static final boolean DEFAULT_MAC_LEARN = false;
54 private static final String EMPTY_MAC = "";
55 private static final boolean DEFAULT_DHCP_REQ = false;
56 private static final boolean DEFAULT_IGMP_REQ = false;
yasin sapli9e3ebed2021-07-09 14:06:35 +000057 private static final boolean DEFAULT_PPPOE_REQ = false;
Matteo Scandolo198aef92020-01-08 00:43:17 +000058
59 @Override
60 public ObjectNode encode(UniTagInformation entry, CodecContext context) {
61 return context.mapper().createObjectNode()
62 .put(UNI_TAG_MATCH, entry.getUniTagMatch().toShort())
63 .put(PON_CTAG, entry.getPonCTag().toShort())
64 .put(PON_STAG, entry.getPonSTag().toShort())
65 .put(US_PON_CTAG_PCP, entry.getUsPonCTagPriority())
66 .put(US_PON_STAG_PCP, entry.getUsPonSTagPriority())
67 .put(DS_PON_CTAG_PCP, entry.getDsPonCTagPriority())
68 .put(DS_PON_STAG_PCP, entry.getDsPonSTagPriority())
69 .put(TP_ID, entry.getTechnologyProfileId())
70 .put(US_BP, entry.getUpstreamBandwidthProfile())
71 .put(DS_BP, entry.getDownstreamBandwidthProfile())
yasin sapli2c93ddb2021-06-11 17:46:26 +030072 .put(US_OLT_BP, entry.getUpstreamOltBandwidthProfile())
73 .put(DS_OLT_BP, entry.getDownstreamOltBandwidthProfile())
Matteo Scandolo198aef92020-01-08 00:43:17 +000074 .put(SN, entry.getServiceName())
75 .put(MAC_LEARN, entry.getEnableMacLearning())
76 .put(MAC, entry.getConfiguredMacAddress())
77 .put(DHCP_REQ, entry.getIsDhcpRequired())
yasin sapli9e3ebed2021-07-09 14:06:35 +000078 .put(IGMP_REQ, entry.getIsIgmpRequired())
79 .put(PPPOE_REQ, entry.getIsPppoeRequired());
Matteo Scandolo198aef92020-01-08 00:43:17 +000080 }
81
82 @Override
83 public UniTagInformation decode(ObjectNode json, CodecContext context) {
84 if (json == null || !json.isObject()) {
85 return null;
86 }
Matteo Scandolo198aef92020-01-08 00:43:17 +000087 UniTagInformation.Builder tagInfoBuilder = new UniTagInformation.Builder();
yasin sapli2c93ddb2021-06-11 17:46:26 +030088 String usBp = json.get(US_BP) == null ? EMPTY_BP :
89 json.get(US_BP).asText();
90 String dsBp = json.get(DS_BP) == null ? EMPTY_BP :
91 json.get(DS_BP).asText();
92 String usOltBp = json.get(US_OLT_BP) == null ? usBp : json.get(US_OLT_BP).asText();
93 String dsOltBp = json.get(DS_OLT_BP) == null ? dsBp : json.get(DS_OLT_BP).asText();
Matteo Scandolo198aef92020-01-08 00:43:17 +000094 tagInfoBuilder.setUniTagMatch(json.get(UNI_TAG_MATCH) == null ? VlanId.vlanId(VlanId.NO_VID) :
95 VlanId.vlanId(json.get(UNI_TAG_MATCH).shortValue()))
96 .setPonCTag(json.get(PON_CTAG) == null ? VlanId.vlanId(VlanId.NO_VID) :
97 VlanId.vlanId(json.get(PON_CTAG).shortValue()))
Andrea Campanella30041b92022-01-28 16:09:08 +010098 .setPonSTag(json.get(PON_STAG) == null ? VlanId.vlanId(VlanId.NO_VID) :
Matteo Scandolo198aef92020-01-08 00:43:17 +000099 VlanId.vlanId(json.get(PON_STAG).shortValue()))
100 .setUsPonCTagPriority(json.get(US_PON_CTAG_PCP) == null ? NO_PCP :
101 json.get(US_PON_CTAG_PCP).asInt())
102 .setUsPonSTagPriority(json.get(US_PON_STAG_PCP) == null ? NO_PCP :
103 json.get(US_PON_STAG_PCP).asInt())
104 .setDsPonCTagPriority(json.get(DS_PON_CTAG_PCP) == null ? NO_PCP :
105 json.get(DS_PON_CTAG_PCP).asInt())
106 .setDsPonSTagPriority(json.get(DS_PON_STAG_PCP) == null ? NO_PCP :
107 json.get(DS_PON_STAG_PCP).asInt())
108 .setTechnologyProfileId(json.get(TP_ID) == null ? NO_TP :
109 json.get(TP_ID).asInt())
yasin sapli2c93ddb2021-06-11 17:46:26 +0300110 .setUpstreamBandwidthProfile(usBp)
111 .setDownstreamBandwidthProfile(dsBp)
112 .setUpstreamOltBandwidthProfile(usOltBp)
113 .setDownstreamOltBandwidthProfile(dsOltBp)
Matteo Scandolo198aef92020-01-08 00:43:17 +0000114 .setServiceName(json.get(SN) == null ? EMPTY_SN :
115 json.get(SN).asText())
116 .setEnableMacLearning(json.get(MAC_LEARN) == null ? DEFAULT_MAC_LEARN :
117 json.get(MAC_LEARN).asBoolean())
118 .setConfiguredMacAddress(json.get(MAC) == null ? EMPTY_MAC :
119 json.get(MAC).asText())
120 .setIsDhcpRequired(json.get(DHCP_REQ) == null ? DEFAULT_DHCP_REQ :
121 json.get(DHCP_REQ).asBoolean())
122 .setIsIgmpRequired(json.get(IGMP_REQ) == null ? DEFAULT_IGMP_REQ :
yasin sapli9e3ebed2021-07-09 14:06:35 +0000123 json.get(IGMP_REQ).asBoolean())
124 .setIsPppoeRequired(json.get(PPPOE_REQ) == null ? DEFAULT_PPPOE_REQ :
125 json.get(PPPOE_REQ).asBoolean());
yasin sapli2c93ddb2021-06-11 17:46:26 +0300126 log.info("Codec UniTagInformation Codec builder returning {}", tagInfoBuilder.build());
Matteo Scandolo198aef92020-01-08 00:43:17 +0000127 return tagInfoBuilder.build();
128 }
129}