BUG-4692: Migrate TCP-MD5 support in bgp package to netty's native-epoll
[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.ProtocolId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.TopologyIdentifier;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.LinkStateAttribute;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.NodeAttributesCaseBuilder;
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.NodeAttributes;
35 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;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrAlgorithm;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.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
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     private static final int ROUTER_BIT = 4;
58     private static final int V6_BIT = 5;
59
60     /* Node Attribute TLVs */
61     private static final int NODE_FLAG_BITS = 1024;
62     private static final int NODE_OPAQUE = 1025;
63     private static final int DYNAMIC_HOSTNAME = 1026;
64     private static final int ISIS_AREA_IDENTIFIER = 1027;
65
66     /* Segment routing TLVs */
67     private static final int SR_CAPABILITIES = 1034;
68     private static final int SR_ALGORITHMS = 1035;
69
70     /**
71      * Parse Node Attributes.
72      *
73      * @param attributes key is the tlv type and value is the value of the tlv
74      * @param protocolId to differentiate parsing methods
75      * @return {@link LinkStateAttribute}
76      */
77     static LinkStateAttribute parseNodeAttributes(final Multimap<Integer, ByteBuf> attributes, final ProtocolId protocolId) {
78         final List<TopologyIdentifier> topologyMembership = new ArrayList<>();
79         final List<IsisAreaIdentifier> areaMembership = new ArrayList<>();
80         final NodeAttributesBuilder builder = new NodeAttributesBuilder();
81         for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
82             final int key = entry.getKey();
83             final ByteBuf value = entry.getValue();
84             LOG.trace("Node attribute TLV {}", key);
85             switch (key) {
86             case TlvUtil.MULTI_TOPOLOGY_ID:
87                 parseTopologyId(topologyMembership, value);
88                 break;
89             case NODE_FLAG_BITS:
90                 final BitArray flags = BitArray.valueOf(value, FLAGS_SIZE);
91                 builder.setNodeFlags(new NodeFlagBits(flags.get(OVERLOAD_BIT), flags.get(ATTACHED_BIT), flags.get
92                     (EXTERNAL_BIT), flags.get(ABBR_BIT), flags.get(ROUTER_BIT), flags.get(V6_BIT)));
93                 LOG.debug("Parsed Overload bit: {}, attached bit: {}, external bit: {}, area border router: {}.",
94                     flags.get(OVERLOAD_BIT), flags.get(ATTACHED_BIT), flags.get(EXTERNAL_BIT), flags.get(ABBR_BIT));
95                 break;
96             case NODE_OPAQUE:
97                 if (LOG.isDebugEnabled()) {
98                     LOG.debug("Ignoring opaque value: {}.", ByteBufUtil.hexDump(value));
99                 }
100                 break;
101             case DYNAMIC_HOSTNAME:
102                 builder.setDynamicHostname(new String(ByteArray.readAllBytes(value), Charsets.US_ASCII));
103                 LOG.debug("Parsed Node Name {}", builder.getDynamicHostname());
104                 break;
105             case ISIS_AREA_IDENTIFIER:
106                 final IsisAreaIdentifier ai = new IsisAreaIdentifier(ByteArray.readAllBytes(value));
107                 areaMembership.add(ai);
108                 LOG.debug("Parsed AreaIdentifier {}", ai);
109                 break;
110             case TlvUtil.LOCAL_IPV4_ROUTER_ID:
111                 final Ipv4RouterIdentifier ip4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
112                 builder.setIpv4RouterId(ip4);
113                 LOG.debug("Parsed IPv4 Router Identifier {}", ip4);
114                 break;
115             case TlvUtil.LOCAL_IPV6_ROUTER_ID:
116                 final Ipv6RouterIdentifier ip6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
117                 builder.setIpv6RouterId(ip6);
118                 LOG.debug("Parsed IPv6 Router Identifier {}", ip6);
119                 break;
120             case SR_CAPABILITIES:
121                 final SrCapabilities caps = SrNodeAttributesParser.parseSrCapabilities(value, protocolId);
122                 builder.setSrCapabilities(caps);
123                 LOG.debug("Parsed SR Capabilities {}", caps);
124                 break;
125             case SR_ALGORITHMS:
126                 final SrAlgorithm algs = SrNodeAttributesParser.parseSrAlgorithms(value);
127                 builder.setSrAlgorithm(algs);
128                 LOG.debug("Parsed SR Algorithms {}", algs);
129                 break;
130             default:
131                 LOG.warn("TLV {} is not a valid node attribute, ignoring it", key);
132             }
133         }
134         LOG.trace("Finished parsing Node Attributes.");
135         builder.setTopologyIdentifier(topologyMembership);
136         builder.setIsisAreaId(areaMembership);
137         return new NodeAttributesCaseBuilder().setNodeAttributes(builder.build()).build();
138     }
139
140     private static void parseTopologyId(final List<TopologyIdentifier> topologyMembership, final ByteBuf value) {
141         while (value.isReadable()) {
142             final TopologyIdentifier topId = new TopologyIdentifier(value.readUnsignedShort() & TlvUtil.TOPOLOGY_ID_OFFSET);
143             topologyMembership.add(topId);
144             LOG.debug("Parsed Topology Identifier: {}", topId);
145         }
146     }
147
148     static void serializeNodeAttributes(final NodeAttributesCase nodeAttributesCase, final ByteBuf byteAggregator) {
149         LOG.trace("Started serializing Node Attributes");
150         final NodeAttributes nodeAttributes = nodeAttributesCase.getNodeAttributes();
151         serializeTopologyId(nodeAttributes.getTopologyIdentifier(), byteAggregator);
152         serializeNodeFlagBits(nodeAttributes.getNodeFlags(), byteAggregator);
153         if (nodeAttributes.getDynamicHostname() != null) {
154             TlvUtil.writeTLV(DYNAMIC_HOSTNAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(nodeAttributes.getDynamicHostname())), byteAggregator);
155         }
156         final List<IsisAreaIdentifier> isisList = nodeAttributes.getIsisAreaId();
157         if (isisList != null) {
158             for (final IsisAreaIdentifier isisAreaIdentifier : isisList) {
159                 TlvUtil.writeTLV(ISIS_AREA_IDENTIFIER, Unpooled.wrappedBuffer(isisAreaIdentifier.getValue()), byteAggregator);
160             }
161         }
162         if (nodeAttributes.getIpv4RouterId() != null) {
163             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(nodeAttributes.getIpv4RouterId()), byteAggregator);
164         }
165         if (nodeAttributes.getIpv6RouterId() != null) {
166             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(nodeAttributes.getIpv6RouterId()), byteAggregator);
167         }
168         if (nodeAttributes.getSrCapabilities() != null) {
169             final ByteBuf capBuffer = Unpooled.buffer();
170             SrNodeAttributesParser.serializeSrCapabilities(nodeAttributes.getSrCapabilities(), capBuffer);
171             TlvUtil.writeTLV(SR_CAPABILITIES, capBuffer, byteAggregator);
172         }
173         if (nodeAttributes.getSrAlgorithm() != null) {
174             final ByteBuf capBuffer = Unpooled.buffer();
175             SrNodeAttributesParser.serializeSrAlgorithms(nodeAttributes.getSrAlgorithm(), capBuffer);
176             TlvUtil.writeTLV(SR_ALGORITHMS, capBuffer, byteAggregator);
177         }
178         LOG.trace("Finished serializing Node Attributes");
179     }
180
181     private static void serializeTopologyId(final List<TopologyIdentifier> topList, final ByteBuf byteAggregator) {
182         if (topList != null) {
183             final ByteBuf mpIdBuf = Unpooled.buffer();
184             for (final TopologyIdentifier topologyIdentifier : topList) {
185                 mpIdBuf.writeShort(topologyIdentifier.getValue());
186             }
187             TlvUtil.writeTLV(TlvUtil.MULTI_TOPOLOGY_ID, mpIdBuf, byteAggregator);
188         }
189     }
190
191     private static void serializeNodeFlagBits(final NodeFlagBits nodeFlagBits, final ByteBuf byteAggregator) {
192         if (nodeFlagBits != null) {
193             final ByteBuf nodeFlagBuf = Unpooled.buffer(1);
194             final BitArray flags = new BitArray(FLAGS_SIZE);
195             flags.set(OVERLOAD_BIT, nodeFlagBits.isOverload());
196             flags.set(ATTACHED_BIT, nodeFlagBits.isAttached());
197             flags.set(EXTERNAL_BIT, nodeFlagBits.isExternal());
198             flags.set(ABBR_BIT, nodeFlagBits.isAbr());
199             flags.set(ROUTER_BIT, nodeFlagBits.isRouter());
200             flags.set(V6_BIT, nodeFlagBits.isV6());
201             flags.toByteBuf(nodeFlagBuf);
202             TlvUtil.writeTLV(NODE_FLAG_BITS, nodeFlagBuf, byteAggregator);
203         }
204     }
205 }