2 * Copyright (c) 2013 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.collect.HashMultimap;
11 import com.google.common.collect.Multimap;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
15 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
16 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
18 import org.opendaylight.protocol.rsvp.parser.spi.RSVPTeObjectRegistry;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Attributes1;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Attributes1Builder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.ProtocolId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.ObjectType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.destination.CLinkstateDestination;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.LinkCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.NodeCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.PrefixCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.TeLspCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.LinkStateAttribute;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.PrefixAttributesCase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.TeLspAttributesCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Parser for Link State Path Attribute.
44 * @see <a href="http://tools.ietf.org/html/draft-gredler-idr-ls-distribution-04">BGP-LS draft</a>
46 public final class LinkstateAttributeParser implements AttributeParser, AttributeSerializer {
48 private static final Logger LOG = LoggerFactory.getLogger(LinkstateAttributeParser.class);
50 private static final int TYPE = 29;
52 private static final int LEGACY_TYPE = 99;
54 private final int type;
56 private final RSVPTeObjectRegistry rsvpTeObjectRegistry;
58 public LinkstateAttributeParser(final boolean isIanaAssignedType, final RSVPTeObjectRegistry rsvpTeObjectRegistry) {
59 this.type = isIanaAssignedType ? TYPE : LEGACY_TYPE;
60 this.rsvpTeObjectRegistry = rsvpTeObjectRegistry;
63 private static Multimap<Integer, ByteBuf> getAttributesMap(final ByteBuf buffer) {
65 * e.g. IS-IS Area Identifier TLV can occur multiple times
67 final Multimap<Integer, ByteBuf> map = HashMultimap.create();
68 while (buffer.isReadable()) {
69 final int type = buffer.readUnsignedShort();
70 final int length = buffer.readUnsignedShort();
71 final ByteBuf value = buffer.readSlice(length);
77 public int getType() {
82 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPParsingException {
83 final CLinkstateDestination lsDestination = getNlriType(builder);
84 if (lsDestination == null) {
85 LOG.warn("No Linkstate NLRI found, not parsing Linkstate attribute");
88 final ObjectType nlriType = lsDestination.getObjectType();
89 final ProtocolId protocolId = lsDestination.getProtocolId();
90 final Attributes1 a = new Attributes1Builder().setLinkStateAttribute(parseLinkState(nlriType, protocolId, buffer)).build();
91 builder.addAugmentation(Attributes1.class, a);
94 private static CLinkstateDestination getNlriType(final AttributesBuilder pab) {
95 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1 mpr = pab.augmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1.class);
96 if (mpr != null && mpr.getMpReachNlri() != null) {
97 final DestinationType dt = mpr.getMpReachNlri().getAdvertizedRoutes().getDestinationType();
98 if (dt instanceof DestinationLinkstateCase) {
99 for (final CLinkstateDestination d : ((DestinationLinkstateCase) dt).getDestinationLinkstate().getCLinkstateDestination()) {
104 final Attributes2 mpu = pab.augmentation(Attributes2.class);
105 if (mpu != null && mpu.getMpUnreachNlri() != null) {
106 final DestinationType dt = mpu.getMpUnreachNlri().getWithdrawnRoutes().getDestinationType();
107 if (dt instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase) {
108 for(final CLinkstateDestination d :((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.unreach.nlri.withdrawn.
109 routes.destination.type.DestinationLinkstateCase) dt).getDestinationLinkstate().getCLinkstateDestination()) {
117 private LinkStateAttribute parseLinkState(final ObjectType nlri, final ProtocolId protocolId, final ByteBuf buffer) throws BGPParsingException {
119 if (nlri instanceof PrefixCase) {
120 return PrefixAttributesParser.parsePrefixAttributes(getAttributesMap(buffer), protocolId);
121 } else if (nlri instanceof LinkCase) {
122 return LinkAttributesParser.parseLinkAttributes(getAttributesMap(buffer), protocolId);
123 } else if (nlri instanceof NodeCase) {
124 return NodeAttributesParser.parseNodeAttributes(getAttributesMap(buffer), protocolId);
125 } else if (nlri instanceof TeLspCase) {
126 return TeLspAttributesParser.parseTeLspAttributes(this.rsvpTeObjectRegistry, getAttributesMap(buffer)
127 .entries().iterator().next().getValue());
129 throw new IllegalStateException("Unhandled NLRI type " + nlri);
134 * Serialize linkstate attributes.
136 * @param attribute DataObject representing LinkstatePathAttribute
137 * @param byteAggregator ByteBuf where all serialized data are aggregated
141 public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
142 final Attributes1 pathAttributes1 = attribute.augmentation(Attributes1.class);
143 if (pathAttributes1 == null) {
146 final LinkStateAttribute linkState = pathAttributes1.getLinkStateAttribute();
147 final ByteBuf lsBuffer = Unpooled.buffer();
148 if (linkState instanceof LinkAttributesCase) {
149 LinkAttributesParser.serializeLinkAttributes((LinkAttributesCase) linkState, lsBuffer);
150 } else if (linkState instanceof NodeAttributesCase) {
151 NodeAttributesParser.serializeNodeAttributes((NodeAttributesCase) linkState, lsBuffer);
152 } else if (linkState instanceof PrefixAttributesCase) {
153 PrefixAttributesParser.serializePrefixAttributes((PrefixAttributesCase) linkState, lsBuffer);
154 } else if (linkState instanceof TeLspAttributesCase) {
155 TeLspAttributesParser.serializeLspAttributes(this.rsvpTeObjectRegistry, (TeLspAttributesCase) linkState, lsBuffer);
156 byteAggregator.writeBytes(lsBuffer);
159 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, getType(), lsBuffer, byteAggregator);