2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.linkstate.impl.attribute;
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.util.ArrayList;
16 import java.util.List;
17 import java.util.Map.Entry;
18 import java.nio.charset.StandardCharsets;
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;
42 public final class NodeAttributesParser {
44 private static final Logger LOG = LoggerFactory.getLogger(NodeAttributesParser.class);
45 private static final int FLAGS_SIZE = 8;
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;
62 private NodeAttributesParser() {
63 throw new UnsupportedOperationException();
67 * Parse Node Attributes.
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}
73 static LinkStateAttribute parseNodeAttributes(final Multimap<Integer, ByteBuf> attributes, final ProtocolId protocolId) {
74 final List<TopologyIdentifier> topologyMembership = new ArrayList<>();
75 final List<IsisAreaIdentifier> areaMembership = new ArrayList<>();
76 final NodeAttributesBuilder builder = new NodeAttributesBuilder();
77 for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
78 final int key = entry.getKey();
79 final ByteBuf value = entry.getValue();
80 LOG.trace("Node attribute TLV {}", key);
82 case TlvUtil.MULTI_TOPOLOGY_ID:
83 parseTopologyId(topologyMembership, value);
86 parseNodeFlags(value, builder);
89 if (LOG.isDebugEnabled()) {
90 LOG.debug("Ignoring opaque value: {}.", ByteBufUtil.hexDump(value));
93 case DYNAMIC_HOSTNAME:
94 builder.setDynamicHostname(new String(ByteArray.readAllBytes(value), StandardCharsets.US_ASCII));
95 LOG.debug("Parsed Node Name {}", builder.getDynamicHostname());
97 case ISIS_AREA_IDENTIFIER:
98 final IsisAreaIdentifier ai = new IsisAreaIdentifier(ByteArray.readAllBytes(value));
99 areaMembership.add(ai);
100 LOG.debug("Parsed AreaIdentifier {}", ai);
102 case TlvUtil.LOCAL_IPV4_ROUTER_ID:
103 final Ipv4RouterIdentifier ip4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
104 builder.setIpv4RouterId(ip4);
105 LOG.debug("Parsed IPv4 Router Identifier {}", ip4);
107 case TlvUtil.LOCAL_IPV6_ROUTER_ID:
108 final Ipv6RouterIdentifier ip6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
109 builder.setIpv6RouterId(ip6);
110 LOG.debug("Parsed IPv6 Router Identifier {}", ip6);
112 case SR_CAPABILITIES:
113 final SrCapabilities caps = SrNodeAttributesParser.parseSrCapabilities(value, protocolId);
114 builder.setSrCapabilities(caps);
115 LOG.debug("Parsed SR Capabilities {}", caps);
118 final SrAlgorithm algs = SrNodeAttributesParser.parseSrAlgorithms(value);
119 builder.setSrAlgorithm(algs);
120 LOG.debug("Parsed SR Algorithms {}", algs);
123 LOG.warn("TLV {} is not a valid node attribute, ignoring it", key);
126 LOG.trace("Finished parsing Node Attributes.");
127 builder.setTopologyIdentifier(topologyMembership);
128 builder.setIsisAreaId(areaMembership);
129 return new NodeAttributesCaseBuilder().setNodeAttributes(builder.build()).build();
132 private static void parseNodeFlags(final ByteBuf value, final NodeAttributesBuilder builder) {
133 final BitArray flags = BitArray.valueOf(value, FLAGS_SIZE);
134 builder.setNodeFlags(new NodeFlagBits(flags.get(OVERLOAD_BIT), flags.get(ATTACHED_BIT), flags.get
135 (EXTERNAL_BIT), flags.get(ABBR_BIT), flags.get(ROUTER_BIT), flags.get(V6_BIT)));
136 LOG.debug("Parsed Overload bit: {}, attached bit: {}, external bit: {}, area border router: {}.",
137 flags.get(OVERLOAD_BIT), flags.get(ATTACHED_BIT), flags.get(EXTERNAL_BIT), flags.get(ABBR_BIT));
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);
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(StandardCharsets.UTF_8.encode(nodeAttributes.getDynamicHostname())), byteAggregator);
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);
162 if (nodeAttributes.getIpv4RouterId() != null) {
163 TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(nodeAttributes.getIpv4RouterId()), byteAggregator);
165 if (nodeAttributes.getIpv6RouterId() != null) {
166 TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(nodeAttributes.getIpv6RouterId()), byteAggregator);
168 if (nodeAttributes.getSrCapabilities() != null) {
169 final ByteBuf capBuffer = Unpooled.buffer();
170 SrNodeAttributesParser.serializeSrCapabilities(nodeAttributes.getSrCapabilities(), capBuffer);
171 TlvUtil.writeTLV(SR_CAPABILITIES, capBuffer, byteAggregator);
173 if (nodeAttributes.getSrAlgorithm() != null) {
174 final ByteBuf capBuffer = Unpooled.buffer();
175 SrNodeAttributesParser.serializeSrAlgorithms(nodeAttributes.getSrAlgorithm(), capBuffer);
176 TlvUtil.writeTLV(SR_ALGORITHMS, capBuffer, byteAggregator);
178 LOG.trace("Finished serializing Node Attributes");
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());
187 TlvUtil.writeTLV(TlvUtil.MULTI_TOPOLOGY_ID, mpIdBuf, byteAggregator);
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);