BUG-2978 : Linkstate path attribute extracted to grouping
[bgpcep.git] / bgp / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / 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.attribute;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.HashMultimap;
12 import com.google.common.collect.Multimap;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
16 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.PathAttributes1;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.PathAttributes1Builder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.ObjectType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestination;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.LinkCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.LinkStateAttribute;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.PrefixAttributesCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.DestinationType;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Parser for Link State Path Attribute.
41  *
42  * @see <a href="http://tools.ietf.org/html/draft-gredler-idr-ls-distribution-04">BGP-LS draft</a>
43  */
44 public class LinkstateAttributeParser implements AttributeParser, AttributeSerializer {
45
46     private static final Logger LOG = LoggerFactory.getLogger(LinkstateAttributeParser.class);
47
48     private static final int TYPE = 29;
49
50     private static final int LEGACY_TYPE = 99;
51
52     private final int type;
53
54     public LinkstateAttributeParser(final boolean isIanaAssignedType) {
55         this.type = (isIanaAssignedType) ? TYPE : LEGACY_TYPE;
56     }
57
58     public int getType() {
59         return this.type;
60     }
61
62     @Override
63     public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPParsingException {
64         final ObjectType nlriType = getNlriType(builder);
65         if (nlriType == null) {
66             LOG.warn("No Linkstate NLRI found, not parsing Linkstate attribute");
67             return;
68         }
69         final PathAttributes1 a = new PathAttributes1Builder().setLinkStateAttribute(parseLinkState(nlriType, buffer)).build();
70         builder.addAugmentation(PathAttributes1.class, a);
71     }
72
73     private ObjectType getNlriType(final PathAttributesBuilder pab) {
74         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1 mpr = pab.getAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1.class);
75         if (mpr != null && mpr.getMpReachNlri() != null) {
76             final DestinationType dt = mpr.getMpReachNlri().getAdvertizedRoutes().getDestinationType();
77             if (dt instanceof DestinationLinkstateCase) {
78                 for (final CLinkstateDestination d : ((DestinationLinkstateCase) dt).getDestinationLinkstate().getCLinkstateDestination()) {
79                     return d.getObjectType();
80                 }
81             }
82         }
83         final PathAttributes2 mpu = pab.getAugmentation(PathAttributes2.class);
84         if (mpu != null && mpu.getMpUnreachNlri() != null) {
85             final DestinationType dt = mpu.getMpUnreachNlri().getWithdrawnRoutes().getDestinationType();
86             if (dt instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase) {
87                 for (final CLinkstateDestination d : ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase) dt).getDestinationLinkstate().getCLinkstateDestination()) {
88                     return d.getObjectType();
89                 }
90             }
91         }
92         return null;
93     }
94
95     private static LinkStateAttribute parseLinkState(final ObjectType nlri, final ByteBuf buffer) throws BGPParsingException {
96         /*
97          * e.g. IS-IS Area Identifier TLV can occur multiple times
98          */
99         final Multimap<Integer, ByteBuf> map = HashMultimap.create();
100         while (buffer.isReadable()) {
101             final int type = buffer.readUnsignedShort();
102             final int length = buffer.readUnsignedShort();
103             final ByteBuf value = buffer.readSlice(length);
104             map.put(type, value);
105         }
106         if (nlri instanceof PrefixCase) {
107             return PrefixAttributesParser.parsePrefixAttributes(map);
108         } else if (nlri instanceof LinkCase) {
109             return LinkAttributesParser.parseLinkAttributes(map);
110         } else if (nlri instanceof NodeCase) {
111             return NodeAttributesParser.parseNodeAttributes(map);
112         } else {
113             throw new IllegalStateException("Unhandled NLRI type " + nlri);
114         }
115     }
116
117     /**
118      * Serialize linkstate attributes.
119      *
120      * @param attribute DataObject representing LinkstatePathAttribute
121      * @param byteAggregator ByteBuf where all serialized data are aggregated
122      */
123
124     @Override
125     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
126         Preconditions.checkArgument(attribute instanceof PathAttributes, "Attribute parameter is not a PathAttribute object.");
127         final PathAttributes1 pathAttributes1 = ((PathAttributes) attribute).getAugmentation(PathAttributes1.class);
128         if (pathAttributes1 == null) {
129             return;
130         }
131         final LinkStateAttribute linkState = pathAttributes1.getLinkStateAttribute();
132         final ByteBuf lsBuffer = Unpooled.buffer();
133         if (linkState instanceof LinkAttributesCase) {
134             LinkAttributesParser.serializeLinkAttributes((LinkAttributesCase) linkState, lsBuffer);
135         } else if (linkState instanceof NodeAttributesCase) {
136             NodeAttributesParser.serializeNodeAttributes((NodeAttributesCase) linkState, lsBuffer);
137         } else if (linkState instanceof PrefixAttributesCase) {
138             PrefixAttributesParser.serializePrefixAttributes((PrefixAttributesCase) linkState, lsBuffer);
139         }
140         AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, getType(), lsBuffer, byteAggregator);
141     }
142 }