Code clean up
[bgpcep.git] / bgp / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / impl / attribute / LinkstateAttributeParser.java
1 /*
2  * Copyright (c) 2013 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.impl.attribute;
9
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;
40
41 /**
42  * Parser for Link State Path Attribute.
43  *
44  * @see <a href="http://tools.ietf.org/html/draft-gredler-idr-ls-distribution-04">BGP-LS draft</a>
45  */
46 public final class LinkstateAttributeParser implements AttributeParser, AttributeSerializer {
47
48     private static final Logger LOG = LoggerFactory.getLogger(LinkstateAttributeParser.class);
49
50     private static final int TYPE = 29;
51
52     private static final int LEGACY_TYPE = 99;
53
54     private final int type;
55
56     private final RSVPTeObjectRegistry rsvpTeObjectRegistry;
57
58     public LinkstateAttributeParser(final boolean isIanaAssignedType, final RSVPTeObjectRegistry rsvpTeObjectRegistry) {
59         this.type = isIanaAssignedType ? TYPE : LEGACY_TYPE;
60         this.rsvpTeObjectRegistry = rsvpTeObjectRegistry;
61     }
62
63     private static Multimap<Integer, ByteBuf> getAttributesMap(final ByteBuf buffer) {
64         /*
65          * e.g. IS-IS Area Identifier TLV can occur multiple times
66          */
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);
72             map.put(type, value);
73         }
74         return map;
75     }
76
77     public int getType() {
78         return this.type;
79     }
80
81     @Override
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");
86             return;
87         }
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);
92     }
93
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()) {
100                     return d;
101                 }
102             }
103         }
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()) {
110                     return d;
111                 }
112             }
113         }
114         return null;
115     }
116
117     private LinkStateAttribute parseLinkState(final ObjectType nlri, final ProtocolId protocolId, final ByteBuf buffer) throws BGPParsingException {
118
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());
128         } else {
129             throw new IllegalStateException("Unhandled NLRI type " + nlri);
130         }
131     }
132
133     /**
134      * Serialize linkstate attributes.
135      *
136      * @param attribute DataObject representing LinkstatePathAttribute
137      * @param byteAggregator ByteBuf where all serialized data are aggregated
138      */
139
140     @Override
141     public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
142         final Attributes1 pathAttributes1 = attribute.augmentation(Attributes1.class);
143         if (pathAttributes1 == null) {
144             return;
145         }
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);
157             return;
158         }
159         AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, getType(), lsBuffer, byteAggregator);
160     }
161 }