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.attribute;
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.node.state.SrAlgorithm;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrCapabilities;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrSidLabel;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.LinkStateAttribute;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.NodeAttributesCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributesBuilder;
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);
46 private NodeAttributesParser() {
47 throw new UnsupportedOperationException();
50 private static final int FLAGS_SIZE = 8;
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;
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;
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;
70 * Parse Node Attributes.
72 * @param attributes key is the tlv type and value is the value of the tlv
73 * @return {@link LinkStateAttribute}
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);
84 case TlvUtil.MULTI_TOPOLOGY_ID:
85 while (value.isReadable()) {
86 final TopologyIdentifier topId = new TopologyIdentifier(value.readUnsignedShort() & TlvUtil.TOPOLOGY_ID_OFFSET);
87 topologyMembership.add(topId);
88 LOG.debug("Parsed Topology Identifier: {}", topId);
92 final BitArray flags = BitArray.valueOf(value, FLAGS_SIZE);
93 builder.setNodeFlags(new NodeFlagBits(flags.get(OVERLOAD_BIT), flags.get(ATTACHED_BIT), flags.get(EXTERNAL_BIT), flags.get(ABBR_BIT)));
94 LOG.debug("Parsed Overload bit: {}, attached bit: {}, external bit: {}, area border router: {}.",
95 flags.get(OVERLOAD_BIT), flags.get(ATTACHED_BIT), flags.get(EXTERNAL_BIT), flags.get(ABBR_BIT));
98 if (LOG.isDebugEnabled()) {
99 LOG.debug("Ignoring opaque value: {}.", ByteBufUtil.hexDump(value));
102 case DYNAMIC_HOSTNAME:
103 builder.setDynamicHostname(new String(ByteArray.readAllBytes(value), Charsets.US_ASCII));
104 LOG.debug("Parsed Node Name {}", builder.getDynamicHostname());
106 case ISIS_AREA_IDENTIFIER:
107 final IsisAreaIdentifier ai = new IsisAreaIdentifier(ByteArray.readAllBytes(value));
108 areaMembership.add(ai);
109 LOG.debug("Parsed AreaIdentifier {}", ai);
111 case TlvUtil.LOCAL_IPV4_ROUTER_ID:
112 final Ipv4RouterIdentifier ip4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
113 builder.setIpv4RouterId(ip4);
114 LOG.debug("Parsed IPv4 Router Identifier {}", ip4);
116 case TlvUtil.LOCAL_IPV6_ROUTER_ID:
117 final Ipv6RouterIdentifier ip6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
118 builder.setIpv6RouterId(ip6);
119 LOG.debug("Parsed IPv6 Router Identifier {}", ip6);
121 case SID_LABEL_BINDING:
122 final SrSidLabel label = SrNodeAttributesParser.parseSidLabelBinding(value);
123 builder.setSrSidLabel(label);
124 LOG.debug("Parsed SID Label Binding {}", label);
126 case SR_CAPABILITIES:
127 final SrCapabilities caps = SrNodeAttributesParser.parseSrCapabilities(value);
128 builder.setSrCapabilities(caps);
129 LOG.debug("Parsed SR Capabilities {}", caps);
132 final SrAlgorithm algs = SrNodeAttributesParser.parseSrAlgorithms(value);
133 builder.setSrAlgorithm(algs);
134 LOG.debug("Parsed SR Algorithms {}", algs);
137 LOG.warn("TLV {} is not a valid node attribute, ignoring it", key);
140 LOG.trace("Finished parsing Node Attributes.");
141 builder.setTopologyIdentifier(topologyMembership);
142 builder.setIsisAreaId(areaMembership);
143 return new NodeAttributesCaseBuilder().setNodeAttributes(builder.build()).build();
146 static void serializeNodeAttributes(final NodeAttributesCase nodeAttributesCase, final ByteBuf byteAggregator) {
147 LOG.trace("Started serializing Node Attributes");
148 final NodeAttributes nodeAttributes = nodeAttributesCase.getNodeAttributes();
149 final List<TopologyIdentifier> topList = nodeAttributes.getTopologyIdentifier();
150 if (topList != null) {
151 final ByteBuf mpIdBuf = Unpooled.buffer();
152 for (final TopologyIdentifier topologyIdentifier : topList) {
153 mpIdBuf.writeShort(topologyIdentifier.getValue());
155 TlvUtil.writeTLV(TlvUtil.MULTI_TOPOLOGY_ID, mpIdBuf, byteAggregator);
157 serializeNodeFlagBits(nodeAttributes.getNodeFlags(), byteAggregator);
158 if (nodeAttributes.getDynamicHostname() != null) {
159 TlvUtil.writeTLV(DYNAMIC_HOSTNAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(nodeAttributes.getDynamicHostname())), byteAggregator);
161 final List<IsisAreaIdentifier> isisList = nodeAttributes.getIsisAreaId();
162 if (isisList != null) {
163 for (final IsisAreaIdentifier isisAreaIdentifier : isisList) {
164 TlvUtil.writeTLV(ISIS_AREA_IDENTIFIER, Unpooled.wrappedBuffer(isisAreaIdentifier.getValue()), byteAggregator);
167 if (nodeAttributes.getIpv4RouterId() != null) {
168 TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(nodeAttributes.getIpv4RouterId()), byteAggregator);
170 if (nodeAttributes.getIpv6RouterId() != null) {
171 TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(nodeAttributes.getIpv6RouterId()), byteAggregator);
173 if (nodeAttributes.getSrSidLabel() != null) {
174 final ByteBuf sidBuffer = Unpooled.buffer();
175 SrNodeAttributesParser.serializeSidLabelBinding(nodeAttributes.getSrSidLabel(), sidBuffer);
176 TlvUtil.writeTLV(SID_LABEL_BINDING, sidBuffer, byteAggregator);
178 if (nodeAttributes.getSrCapabilities() != null) {
179 final ByteBuf capBuffer = Unpooled.buffer();
180 SrNodeAttributesParser.serializeSrCapabilities(nodeAttributes.getSrCapabilities(), capBuffer);
181 TlvUtil.writeTLV(SR_CAPABILITIES, capBuffer, byteAggregator);
183 if (nodeAttributes.getSrAlgorithm() != null) {
184 final ByteBuf capBuffer = Unpooled.buffer();
185 SrNodeAttributesParser.serializeSrAlgorithms(nodeAttributes.getSrAlgorithm(), capBuffer);
186 TlvUtil.writeTLV(SR_ALGORITHMS, capBuffer, byteAggregator);
188 LOG.trace("Finished serializing Node Attributes");
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.toByteBuf(nodeFlagBuf);
200 TlvUtil.writeTLV(NODE_FLAG_BITS, nodeFlagBuf, byteAggregator);