BUG-3823 : fixed sonar issues in linkstate
[bgpcep.git] / bgp / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / attribute / NodeAttributesParser.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.linkstate.attribute;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Charsets;
12 import com.google.common.collect.Multimap;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufUtil;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Map.Entry;
19 import org.opendaylight.protocol.bgp.linkstate.attribute.sr.SrNodeAttributesParser;
20 import org.opendaylight.protocol.bgp.linkstate.spi.TlvUtil;
21 import org.opendaylight.protocol.util.BitArray;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.protocol.util.Ipv4Util;
24 import org.opendaylight.protocol.util.Ipv6Util;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv4RouterIdentifier;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv6RouterIdentifier;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.IsisAreaIdentifier;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeFlagBits;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.TopologyIdentifier;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.LinkStateAttribute;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.NodeAttributesCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrAlgorithm;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrCapabilities;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrSidLabel;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 @VisibleForTesting
42 public final class NodeAttributesParser {
43
44     private static final Logger LOG = LoggerFactory.getLogger(NodeAttributesParser.class);
45
46     private NodeAttributesParser() {
47         throw new UnsupportedOperationException();
48     }
49
50     private static final int FLAGS_SIZE = 8;
51
52     // node flag bits
53     private static final int OVERLOAD_BIT = 0;
54     private static final int ATTACHED_BIT = 1;
55     private static final int EXTERNAL_BIT = 2;
56     private static final int ABBR_BIT = 3;
57
58     /* Node Attribute TLVs */
59     private static final int NODE_FLAG_BITS = 1024;
60     private static final int NODE_OPAQUE = 1025;
61     private static final int DYNAMIC_HOSTNAME = 1026;
62     private static final int ISIS_AREA_IDENTIFIER = 1027;
63
64     /* Segment routing TLVs */
65     private static final int SID_LABEL_BINDING = 1033;
66     private static final int SR_CAPABILITIES = 1034;
67     private static final int SR_ALGORITHMS = 1035;
68
69     /**
70      * Parse Node Attributes.
71      *
72      * @param attributes key is the tlv type and value is the value of the tlv
73      * @return {@link LinkStateAttribute}
74      */
75     static LinkStateAttribute parseNodeAttributes(final Multimap<Integer, ByteBuf> attributes) {
76         final List<TopologyIdentifier> topologyMembership = new ArrayList<>();
77         final List<IsisAreaIdentifier> areaMembership = new ArrayList<>();
78         final NodeAttributesBuilder builder = new NodeAttributesBuilder();
79         for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
80             final int key = entry.getKey();
81             final ByteBuf value = entry.getValue();
82             LOG.trace("Node attribute TLV {}", key);
83             switch (key) {
84             case TlvUtil.MULTI_TOPOLOGY_ID:
85                 parseTopologyId(topologyMembership, value);
86                 break;
87             case NODE_FLAG_BITS:
88                 final BitArray flags = BitArray.valueOf(value, FLAGS_SIZE);
89                 builder.setNodeFlags(new NodeFlagBits(flags.get(OVERLOAD_BIT), flags.get(ATTACHED_BIT), flags.get(EXTERNAL_BIT), flags.get(ABBR_BIT)));
90                 LOG.debug("Parsed Overload bit: {}, attached bit: {}, external bit: {}, area border router: {}.",
91                     flags.get(OVERLOAD_BIT), flags.get(ATTACHED_BIT), flags.get(EXTERNAL_BIT), flags.get(ABBR_BIT));
92                 break;
93             case NODE_OPAQUE:
94                 if (LOG.isDebugEnabled()) {
95                     LOG.debug("Ignoring opaque value: {}.", ByteBufUtil.hexDump(value));
96                 }
97                 break;
98             case DYNAMIC_HOSTNAME:
99                 builder.setDynamicHostname(new String(ByteArray.readAllBytes(value), Charsets.US_ASCII));
100                 LOG.debug("Parsed Node Name {}", builder.getDynamicHostname());
101                 break;
102             case ISIS_AREA_IDENTIFIER:
103                 final IsisAreaIdentifier ai = new IsisAreaIdentifier(ByteArray.readAllBytes(value));
104                 areaMembership.add(ai);
105                 LOG.debug("Parsed AreaIdentifier {}", ai);
106                 break;
107             case TlvUtil.LOCAL_IPV4_ROUTER_ID:
108                 final Ipv4RouterIdentifier ip4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
109                 builder.setIpv4RouterId(ip4);
110                 LOG.debug("Parsed IPv4 Router Identifier {}", ip4);
111                 break;
112             case TlvUtil.LOCAL_IPV6_ROUTER_ID:
113                 final Ipv6RouterIdentifier ip6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
114                 builder.setIpv6RouterId(ip6);
115                 LOG.debug("Parsed IPv6 Router Identifier {}", ip6);
116                 break;
117             case SID_LABEL_BINDING:
118                 final SrSidLabel label = SrNodeAttributesParser.parseSidLabelBinding(value);
119                 builder.setSrSidLabel(label);
120                 LOG.debug("Parsed SID Label Binding {}", label);
121                 break;
122             case SR_CAPABILITIES:
123                 final SrCapabilities caps = SrNodeAttributesParser.parseSrCapabilities(value);
124                 builder.setSrCapabilities(caps);
125                 LOG.debug("Parsed SR Capabilities {}", caps);
126                 break;
127             case SR_ALGORITHMS:
128                 final SrAlgorithm algs = SrNodeAttributesParser.parseSrAlgorithms(value);
129                 builder.setSrAlgorithm(algs);
130                 LOG.debug("Parsed SR Algorithms {}", algs);
131                 break;
132             default:
133                 LOG.warn("TLV {} is not a valid node attribute, ignoring it", key);
134             }
135         }
136         LOG.trace("Finished parsing Node Attributes.");
137         builder.setTopologyIdentifier(topologyMembership);
138         builder.setIsisAreaId(areaMembership);
139         return new NodeAttributesCaseBuilder().setNodeAttributes(builder.build()).build();
140     }
141
142     private static void parseTopologyId(final List<TopologyIdentifier> topologyMembership, final ByteBuf value) {
143         while (value.isReadable()) {
144             final TopologyIdentifier topId = new TopologyIdentifier(value.readUnsignedShort() & TlvUtil.TOPOLOGY_ID_OFFSET);
145             topologyMembership.add(topId);
146             LOG.debug("Parsed Topology Identifier: {}", topId);
147         }
148     }
149
150     static void serializeNodeAttributes(final NodeAttributesCase nodeAttributesCase, final ByteBuf byteAggregator) {
151         LOG.trace("Started serializing Node Attributes");
152         final NodeAttributes nodeAttributes = nodeAttributesCase.getNodeAttributes();
153         serializeTopologyId(nodeAttributes.getTopologyIdentifier(), byteAggregator);
154         serializeNodeFlagBits(nodeAttributes.getNodeFlags(), byteAggregator);
155         if (nodeAttributes.getDynamicHostname() != null) {
156             TlvUtil.writeTLV(DYNAMIC_HOSTNAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(nodeAttributes.getDynamicHostname())), byteAggregator);
157         }
158         final List<IsisAreaIdentifier> isisList = nodeAttributes.getIsisAreaId();
159         if (isisList != null) {
160             for (final IsisAreaIdentifier isisAreaIdentifier : isisList) {
161                 TlvUtil.writeTLV(ISIS_AREA_IDENTIFIER, Unpooled.wrappedBuffer(isisAreaIdentifier.getValue()), byteAggregator);
162             }
163         }
164         if (nodeAttributes.getIpv4RouterId() != null) {
165             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(nodeAttributes.getIpv4RouterId()), byteAggregator);
166         }
167         if (nodeAttributes.getIpv6RouterId() != null) {
168             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(nodeAttributes.getIpv6RouterId()), byteAggregator);
169         }
170         if (nodeAttributes.getSrSidLabel() != null) {
171             final ByteBuf sidBuffer = Unpooled.buffer();
172             SrNodeAttributesParser.serializeSidLabelBinding(nodeAttributes.getSrSidLabel(), sidBuffer);
173             TlvUtil.writeTLV(SID_LABEL_BINDING, sidBuffer, byteAggregator);
174         }
175         if (nodeAttributes.getSrCapabilities() != null) {
176             final ByteBuf capBuffer = Unpooled.buffer();
177             SrNodeAttributesParser.serializeSrCapabilities(nodeAttributes.getSrCapabilities(), capBuffer);
178             TlvUtil.writeTLV(SR_CAPABILITIES, capBuffer, byteAggregator);
179         }
180         if (nodeAttributes.getSrAlgorithm() != null) {
181             final ByteBuf capBuffer = Unpooled.buffer();
182             SrNodeAttributesParser.serializeSrAlgorithms(nodeAttributes.getSrAlgorithm(), capBuffer);
183             TlvUtil.writeTLV(SR_ALGORITHMS, capBuffer, byteAggregator);
184         }
185         LOG.trace("Finished serializing Node Attributes");
186     }
187
188     private static void serializeTopologyId(final List<TopologyIdentifier> topList, final ByteBuf byteAggregator) {
189         if (topList != null) {
190             final ByteBuf mpIdBuf = Unpooled.buffer();
191             for (final TopologyIdentifier topologyIdentifier : topList) {
192                 mpIdBuf.writeShort(topologyIdentifier.getValue());
193             }
194             TlvUtil.writeTLV(TlvUtil.MULTI_TOPOLOGY_ID, mpIdBuf, byteAggregator);
195         }
196     }
197
198     private static void serializeNodeFlagBits(final NodeFlagBits nodeFlagBits, final ByteBuf byteAggregator) {
199         if (nodeFlagBits != null) {
200             final ByteBuf nodeFlagBuf = Unpooled.buffer(1);
201             final BitArray flags = new BitArray(FLAGS_SIZE);
202             flags.set(OVERLOAD_BIT, nodeFlagBits.isOverload());
203             flags.set(ATTACHED_BIT, nodeFlagBits.isAttached());
204             flags.set(EXTERNAL_BIT, nodeFlagBits.isExternal());
205             flags.set(ABBR_BIT, nodeFlagBits.isAbr());
206             flags.toByteBuf(nodeFlagBuf);
207             TlvUtil.writeTLV(NODE_FLAG_BITS, nodeFlagBuf, byteAggregator);
208         }
209     }
210 }