8d16acd7cc2ada3a16e4444dbea4837135d878bb
[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.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 @VisibleForTesting
41 public final class NodeAttributesParser {
42
43     private static final Logger LOG = LoggerFactory.getLogger(NodeAttributesParser.class);
44
45     private NodeAttributesParser() {
46         throw new UnsupportedOperationException();
47     }
48
49     private static final int FLAGS_SIZE = 8;
50
51     // node flag bits
52     private static final int OVERLOAD_BIT = 0;
53     private static final int ATTACHED_BIT = 1;
54     private static final int EXTERNAL_BIT = 2;
55     private static final int ABBR_BIT = 3;
56     private static final int ROUTER_BIT = 4;
57     private static final int V6_BIT = 5;
58
59     /* Node Attribute TLVs */
60     private static final int NODE_FLAG_BITS = 1024;
61     private static final int NODE_OPAQUE = 1025;
62     private static final int DYNAMIC_HOSTNAME = 1026;
63     private static final int ISIS_AREA_IDENTIFIER = 1027;
64
65     /* Segment routing TLVs */
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
90                     (EXTERNAL_BIT), flags.get(ABBR_BIT), flags.get(ROUTER_BIT), flags.get(V6_BIT)));
91                 LOG.debug("Parsed Overload bit: {}, attached bit: {}, external bit: {}, area border router: {}.",
92                     flags.get(OVERLOAD_BIT), flags.get(ATTACHED_BIT), flags.get(EXTERNAL_BIT), flags.get(ABBR_BIT));
93                 break;
94             case NODE_OPAQUE:
95                 if (LOG.isDebugEnabled()) {
96                     LOG.debug("Ignoring opaque value: {}.", ByteBufUtil.hexDump(value));
97                 }
98                 break;
99             case DYNAMIC_HOSTNAME:
100                 builder.setDynamicHostname(new String(ByteArray.readAllBytes(value), Charsets.US_ASCII));
101                 LOG.debug("Parsed Node Name {}", builder.getDynamicHostname());
102                 break;
103             case ISIS_AREA_IDENTIFIER:
104                 final IsisAreaIdentifier ai = new IsisAreaIdentifier(ByteArray.readAllBytes(value));
105                 areaMembership.add(ai);
106                 LOG.debug("Parsed AreaIdentifier {}", ai);
107                 break;
108             case TlvUtil.LOCAL_IPV4_ROUTER_ID:
109                 final Ipv4RouterIdentifier ip4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
110                 builder.setIpv4RouterId(ip4);
111                 LOG.debug("Parsed IPv4 Router Identifier {}", ip4);
112                 break;
113             case TlvUtil.LOCAL_IPV6_ROUTER_ID:
114                 final Ipv6RouterIdentifier ip6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
115                 builder.setIpv6RouterId(ip6);
116                 LOG.debug("Parsed IPv6 Router Identifier {}", ip6);
117                 break;
118             case SR_CAPABILITIES:
119                 final SrCapabilities caps = SrNodeAttributesParser.parseSrCapabilities(value);
120                 builder.setSrCapabilities(caps);
121                 LOG.debug("Parsed SR Capabilities {}", caps);
122                 break;
123             case SR_ALGORITHMS:
124                 final SrAlgorithm algs = SrNodeAttributesParser.parseSrAlgorithms(value);
125                 builder.setSrAlgorithm(algs);
126                 LOG.debug("Parsed SR Algorithms {}", algs);
127                 break;
128             default:
129                 LOG.warn("TLV {} is not a valid node attribute, ignoring it", key);
130             }
131         }
132         LOG.trace("Finished parsing Node Attributes.");
133         builder.setTopologyIdentifier(topologyMembership);
134         builder.setIsisAreaId(areaMembership);
135         return new NodeAttributesCaseBuilder().setNodeAttributes(builder.build()).build();
136     }
137
138     private static void parseTopologyId(final List<TopologyIdentifier> topologyMembership, final ByteBuf value) {
139         while (value.isReadable()) {
140             final TopologyIdentifier topId = new TopologyIdentifier(value.readUnsignedShort() & TlvUtil.TOPOLOGY_ID_OFFSET);
141             topologyMembership.add(topId);
142             LOG.debug("Parsed Topology Identifier: {}", topId);
143         }
144     }
145
146     static void serializeNodeAttributes(final NodeAttributesCase nodeAttributesCase, final ByteBuf byteAggregator) {
147         LOG.trace("Started serializing Node Attributes");
148         final NodeAttributes nodeAttributes = nodeAttributesCase.getNodeAttributes();
149         serializeTopologyId(nodeAttributes.getTopologyIdentifier(), byteAggregator);
150         serializeNodeFlagBits(nodeAttributes.getNodeFlags(), byteAggregator);
151         if (nodeAttributes.getDynamicHostname() != null) {
152             TlvUtil.writeTLV(DYNAMIC_HOSTNAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(nodeAttributes.getDynamicHostname())), byteAggregator);
153         }
154         final List<IsisAreaIdentifier> isisList = nodeAttributes.getIsisAreaId();
155         if (isisList != null) {
156             for (final IsisAreaIdentifier isisAreaIdentifier : isisList) {
157                 TlvUtil.writeTLV(ISIS_AREA_IDENTIFIER, Unpooled.wrappedBuffer(isisAreaIdentifier.getValue()), byteAggregator);
158             }
159         }
160         if (nodeAttributes.getIpv4RouterId() != null) {
161             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(nodeAttributes.getIpv4RouterId()), byteAggregator);
162         }
163         if (nodeAttributes.getIpv6RouterId() != null) {
164             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(nodeAttributes.getIpv6RouterId()), byteAggregator);
165         }
166         if (nodeAttributes.getSrCapabilities() != null) {
167             final ByteBuf capBuffer = Unpooled.buffer();
168             SrNodeAttributesParser.serializeSrCapabilities(nodeAttributes.getSrCapabilities(), capBuffer);
169             TlvUtil.writeTLV(SR_CAPABILITIES, capBuffer, byteAggregator);
170         }
171         if (nodeAttributes.getSrAlgorithm() != null) {
172             final ByteBuf capBuffer = Unpooled.buffer();
173             SrNodeAttributesParser.serializeSrAlgorithms(nodeAttributes.getSrAlgorithm(), capBuffer);
174             TlvUtil.writeTLV(SR_ALGORITHMS, capBuffer, byteAggregator);
175         }
176         LOG.trace("Finished serializing Node Attributes");
177     }
178
179     private static void serializeTopologyId(final List<TopologyIdentifier> topList, final ByteBuf byteAggregator) {
180         if (topList != null) {
181             final ByteBuf mpIdBuf = Unpooled.buffer();
182             for (final TopologyIdentifier topologyIdentifier : topList) {
183                 mpIdBuf.writeShort(topologyIdentifier.getValue());
184             }
185             TlvUtil.writeTLV(TlvUtil.MULTI_TOPOLOGY_ID, mpIdBuf, byteAggregator);
186         }
187     }
188
189     private static void serializeNodeFlagBits(final NodeFlagBits nodeFlagBits, final ByteBuf byteAggregator) {
190         if (nodeFlagBits != null) {
191             final ByteBuf nodeFlagBuf = Unpooled.buffer(1);
192             final BitArray flags = new BitArray(FLAGS_SIZE);
193             flags.set(OVERLOAD_BIT, nodeFlagBits.isOverload());
194             flags.set(ATTACHED_BIT, nodeFlagBits.isAttached());
195             flags.set(EXTERNAL_BIT, nodeFlagBits.isExternal());
196             flags.set(ABBR_BIT, nodeFlagBits.isAbr());
197             flags.set(ROUTER_BIT, nodeFlagBits.isRouter());
198             flags.set(V6_BIT, nodeFlagBits.isV6());
199             flags.toByteBuf(nodeFlagBuf);
200             TlvUtil.writeTLV(NODE_FLAG_BITS, nodeFlagBuf, byteAggregator);
201         }
202     }
203 }