Enforce checkstyle in bgp-linkstate
[bgpcep.git] / bgp / extensions / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / impl / 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.impl.attribute;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.collect.Multimap;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.ByteBufUtil;
14 import io.netty.buffer.Unpooled;
15 import java.nio.charset.StandardCharsets;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Map.Entry;
19 import org.opendaylight.protocol.bgp.linkstate.impl.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.rev180329.Ipv4RouterIdentifier;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Ipv6RouterIdentifier;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.IsisAreaIdentifier;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.NodeFlagBits;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.ProtocolId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.TopologyIdentifier;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.LinkStateAttribute;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.NodeAttributesCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.state.SrAlgorithm;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.state.SrCapabilities;
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     private static final int FLAGS_SIZE = 8;
46     // node flag bits
47     private static final int OVERLOAD_BIT = 0;
48     private static final int ATTACHED_BIT = 1;
49     private static final int EXTERNAL_BIT = 2;
50     private static final int ABBR_BIT = 3;
51     private static final int ROUTER_BIT = 4;
52     private static final int V6_BIT = 5;
53     /* Node Attribute TLVs */
54     private static final int NODE_FLAG_BITS = 1024;
55     private static final int NODE_OPAQUE = 1025;
56     private static final int DYNAMIC_HOSTNAME = 1026;
57     private static final int ISIS_AREA_IDENTIFIER = 1027;
58     /* Segment routing TLVs */
59     private static final int SR_CAPABILITIES = 1034;
60     private static final int SR_ALGORITHMS = 1035;
61
62     private NodeAttributesParser() {
63
64     }
65
66     /**
67      * Parse Node Attributes.
68      *
69      * @param attributes key is the tlv type and value is the value of the tlv
70      * @param protocolId to differentiate parsing methods
71      * @return {@link LinkStateAttribute}
72      */
73     static LinkStateAttribute parseNodeAttributes(final Multimap<Integer, ByteBuf> attributes,
74             final ProtocolId protocolId) {
75         final List<TopologyIdentifier> topologyMembership = new ArrayList<>();
76         final List<IsisAreaIdentifier> areaMembership = new ArrayList<>();
77         final NodeAttributesBuilder builder = new NodeAttributesBuilder();
78         for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
79             final int key = entry.getKey();
80             final ByteBuf value = entry.getValue();
81             LOG.trace("Node attribute TLV {}", key);
82             switch (key) {
83                 case TlvUtil.MULTI_TOPOLOGY_ID:
84                     parseTopologyId(topologyMembership, value);
85                     break;
86                 case NODE_FLAG_BITS:
87                     parseNodeFlags(value, builder);
88                     break;
89                 case NODE_OPAQUE:
90                     if (LOG.isDebugEnabled()) {
91                         LOG.debug("Ignoring opaque value: {}.", ByteBufUtil.hexDump(value));
92                     }
93                     break;
94                 case DYNAMIC_HOSTNAME:
95                     builder.setDynamicHostname(new String(ByteArray.readAllBytes(value), StandardCharsets.US_ASCII));
96                     LOG.debug("Parsed Node Name {}", builder.getDynamicHostname());
97                     break;
98                 case ISIS_AREA_IDENTIFIER:
99                     final IsisAreaIdentifier ai = new IsisAreaIdentifier(ByteArray.readAllBytes(value));
100                     areaMembership.add(ai);
101                     LOG.debug("Parsed AreaIdentifier {}", ai);
102                     break;
103                 case TlvUtil.LOCAL_IPV4_ROUTER_ID:
104                     final Ipv4RouterIdentifier ip4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
105                     builder.setIpv4RouterId(ip4);
106                     LOG.debug("Parsed IPv4 Router Identifier {}", ip4);
107                     break;
108                 case TlvUtil.LOCAL_IPV6_ROUTER_ID:
109                     final Ipv6RouterIdentifier ip6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
110                     builder.setIpv6RouterId(ip6);
111                     LOG.debug("Parsed IPv6 Router Identifier {}", ip6);
112                     break;
113                 case SR_CAPABILITIES:
114                     final SrCapabilities caps = SrNodeAttributesParser.parseSrCapabilities(value, protocolId);
115                     builder.setSrCapabilities(caps);
116                     LOG.debug("Parsed SR Capabilities {}", caps);
117                     break;
118                 case SR_ALGORITHMS:
119                     final SrAlgorithm algs = SrNodeAttributesParser.parseSrAlgorithms(value);
120                     builder.setSrAlgorithm(algs);
121                     LOG.debug("Parsed SR Algorithms {}", algs);
122                     break;
123                 default:
124                     LOG.warn("TLV {} is not a valid node attribute, ignoring it", key);
125             }
126         }
127         LOG.trace("Finished parsing Node Attributes.");
128         builder.setTopologyIdentifier(topologyMembership);
129         builder.setIsisAreaId(areaMembership);
130         return new NodeAttributesCaseBuilder().setNodeAttributes(builder.build()).build();
131     }
132
133     private static void parseNodeFlags(final ByteBuf value, final NodeAttributesBuilder builder) {
134         final BitArray flags = BitArray.valueOf(value, FLAGS_SIZE);
135         builder.setNodeFlags(new NodeFlagBits(flags.get(OVERLOAD_BIT), flags.get(ATTACHED_BIT), flags.get(EXTERNAL_BIT),
136             flags.get(ABBR_BIT), flags.get(ROUTER_BIT), flags.get(V6_BIT)));
137         LOG.debug("Parsed Overload bit: {}, attached bit: {}, external bit: {}, area border router: {}.",
138             flags.get(OVERLOAD_BIT), flags.get(ATTACHED_BIT), flags.get(EXTERNAL_BIT), flags.get(ABBR_BIT));
139     }
140
141     private static void parseTopologyId(final List<TopologyIdentifier> topologyMembership, final ByteBuf value) {
142         while (value.isReadable()) {
143             final TopologyIdentifier topId = new TopologyIdentifier(
144                 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(StandardCharsets.UTF_8.encode(
157                 nodeAttributes.getDynamicHostname())), byteAggregator);
158         }
159         final List<IsisAreaIdentifier> isisList = nodeAttributes.getIsisAreaId();
160         if (isisList != null) {
161             for (final IsisAreaIdentifier isisAreaIdentifier : isisList) {
162                 TlvUtil.writeTLV(ISIS_AREA_IDENTIFIER, Unpooled.wrappedBuffer(isisAreaIdentifier.getValue()),
163                     byteAggregator);
164             }
165         }
166         if (nodeAttributes.getIpv4RouterId() != null) {
167             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(nodeAttributes.getIpv4RouterId()),
168                 byteAggregator);
169         }
170         if (nodeAttributes.getIpv6RouterId() != null) {
171             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(nodeAttributes.getIpv6RouterId()),
172                 byteAggregator);
173         }
174         if (nodeAttributes.getSrCapabilities() != null) {
175             final ByteBuf capBuffer = Unpooled.buffer();
176             SrNodeAttributesParser.serializeSrCapabilities(nodeAttributes.getSrCapabilities(), capBuffer);
177             TlvUtil.writeTLV(SR_CAPABILITIES, capBuffer, byteAggregator);
178         }
179         if (nodeAttributes.getSrAlgorithm() != null) {
180             final ByteBuf capBuffer = Unpooled.buffer();
181             SrNodeAttributesParser.serializeSrAlgorithms(nodeAttributes.getSrAlgorithm(), capBuffer);
182             TlvUtil.writeTLV(SR_ALGORITHMS, capBuffer, byteAggregator);
183         }
184         LOG.trace("Finished serializing Node Attributes");
185     }
186
187     private static void serializeTopologyId(final List<TopologyIdentifier> topList, final ByteBuf byteAggregator) {
188         if (topList != null) {
189             final ByteBuf mpIdBuf = Unpooled.buffer();
190             for (final TopologyIdentifier topologyIdentifier : topList) {
191                 mpIdBuf.writeShort(topologyIdentifier.getValue());
192             }
193             TlvUtil.writeTLV(TlvUtil.MULTI_TOPOLOGY_ID, mpIdBuf, byteAggregator);
194         }
195     }
196
197     private static void serializeNodeFlagBits(final NodeFlagBits nodeFlagBits, final ByteBuf byteAggregator) {
198         if (nodeFlagBits != null) {
199             final ByteBuf nodeFlagBuf = Unpooled.buffer(1);
200             final BitArray flags = new BitArray(FLAGS_SIZE);
201             flags.set(OVERLOAD_BIT, nodeFlagBits.isOverload());
202             flags.set(ATTACHED_BIT, nodeFlagBits.isAttached());
203             flags.set(EXTERNAL_BIT, nodeFlagBits.isExternal());
204             flags.set(ABBR_BIT, nodeFlagBits.isAbr());
205             flags.set(ROUTER_BIT, nodeFlagBits.isRouter());
206             flags.set(V6_BIT, nodeFlagBits.isV6());
207             flags.toByteBuf(nodeFlagBuf);
208             TlvUtil.writeTLV(NODE_FLAG_BITS, nodeFlagBuf, byteAggregator);
209         }
210     }
211 }