Create common parent for extensions families
[bgpcep.git] / bgp / extensions / evpn / src / main / java / org / opendaylight / protocol / bgp / evpn / impl / nlri / EvpnNlriParser.java
1 /*
2  * Copyright (c) 2016 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.evpn.impl.nlri;
9
10 import static org.opendaylight.bgp.concepts.RouteDistinguisherUtil.parseRouteDistinguisher;
11 import static org.opendaylight.bgp.concepts.RouteDistinguisherUtil.serializeRouteDistinquisher;
12 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.extractRouteDistinguisher;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
17 import java.util.List;
18 import javax.annotation.Nullable;
19 import org.opendaylight.protocol.bgp.evpn.spi.EvpnRegistry;
20 import org.opendaylight.protocol.bgp.evpn.spi.pojo.SimpleEvpnNlriRegistry;
21 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
22 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
23 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupportUtil;
24 import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
25 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
26 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
27 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.NlriType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.EvpnChoice;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.destination.EvpnDestination;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.destination.EvpnDestinationBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationEvpnCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationEvpnCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.evpn._case.DestinationEvpnBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutes;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
47 import org.opendaylight.yangtools.yang.common.QName;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
50 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 public final class EvpnNlriParser implements NlriParser, NlriSerializer {
56     private static final Logger LOG = LoggerFactory.getLogger(EvpnNlriParser.class);
57     private static final NodeIdentifier EVPN_CHOICE_NID = new NodeIdentifier(EvpnChoice.QNAME);
58     private static final NodeIdentifier  PATH_ID_NID =
59             new NodeIdentifier(QName.create(EvpnChoice.QNAME, "path-id").intern());
60
61     @FunctionalInterface
62     private interface ExtractionInterface {
63         EvpnChoice check(EvpnRegistry reg, ChoiceNode cont);
64     }
65
66     public static EvpnDestination extractEvpnDestination(final DataContainerNode<? extends PathArgument> route) {
67         return extractDestination(route, EvpnRegistry::serializeEvpnModel);
68     }
69
70     private static EvpnDestination extractDestination(final DataContainerNode<? extends PathArgument> route,
71             final ExtractionInterface extract) {
72         final EvpnRegistry reg = SimpleEvpnNlriRegistry.getInstance();
73         final ChoiceNode cont = (ChoiceNode) route.getChild(EVPN_CHOICE_NID).get();
74         final EvpnChoice evpnValue = extract.check(reg, cont);
75         if (evpnValue == null) {
76             LOG.warn("Unrecognized Nlri {}", cont);
77             return null;
78         }
79         return new EvpnDestinationBuilder()
80                 .setRouteDistinguisher(extractRouteDistinguisher(route))
81                 .setPathId(PathIdUtil.buildPathId(route, PATH_ID_NID))
82                 .setEvpnChoice(evpnValue).build();
83     }
84
85     public static EvpnDestination extractRouteKeyDestination(
86             final DataContainerNode<? extends PathArgument> evpnChoice) {
87         return extractDestination(evpnChoice, EvpnRegistry::serializeEvpnRouteKey);
88     }
89
90     @Override
91     public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder,
92             final PeerSpecificParserConstraint constraint) throws BGPParsingException {
93         if (!nlri.isReadable()) {
94             return;
95         }
96         final List<EvpnDestination> dst = parseNlri(nlri, constraint, builder.getAfi(), builder.getSafi());
97
98         builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
99             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update.attributes
100                     .mp.unreach.nlri.withdrawn.routes.destination.type.DestinationEvpnCaseBuilder()
101                     .setDestinationEvpn(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn
102                             .rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination
103                             .evpn._case.DestinationEvpnBuilder().setEvpnDestination(dst).build()).build()).build());
104     }
105
106     @Override
107     public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder,
108             final PeerSpecificParserConstraint constraint) throws BGPParsingException {
109         if (!nlri.isReadable()) {
110             return;
111         }
112         final List<EvpnDestination> dst = parseNlri(nlri, constraint, builder.getAfi(), builder.getSafi());
113
114         builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
115             new DestinationEvpnCaseBuilder().setDestinationEvpn(new DestinationEvpnBuilder()
116                     .setEvpnDestination(dst).build()).build()).build());
117     }
118
119     @Nullable
120     private static List<EvpnDestination> parseNlri(final ByteBuf nlri, final PeerSpecificParserConstraint constraints,
121             final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi) {
122         if (!nlri.isReadable()) {
123             return null;
124         }
125         final List<EvpnDestination> dests = new ArrayList<>();
126
127         while (nlri.isReadable()) {
128             final EvpnDestinationBuilder builder = new EvpnDestinationBuilder();
129             if (MultiPathSupportUtil.isTableTypeSupported(constraints, new BgpTableTypeImpl(afi, safi))) {
130                 builder.setPathId(PathIdUtil.readPathId(nlri));
131             }
132             final NlriType type = NlriType.forValue(nlri.readUnsignedByte());
133             final int length = nlri.readUnsignedByte();
134             final ByteBuf nlriBuf = nlri.readSlice(length);
135             builder.setRouteDistinguisher(parseRouteDistinguisher(nlriBuf));
136             builder.setEvpnChoice(SimpleEvpnNlriRegistry.getInstance().parseEvpn(type, nlriBuf));
137             dests.add(builder.build());
138         }
139         return dests;
140     }
141
142     @Override
143     public void serializeAttribute(final Attributes pathAttributes, final ByteBuf byteAggregator) {
144         final Attributes1 pathAttributes1 = pathAttributes.augmentation(Attributes1.class);
145         final Attributes2 pathAttributes2 = pathAttributes.augmentation(Attributes2.class);
146         if (pathAttributes1 != null) {
147             final AdvertizedRoutes routes = pathAttributes1.getMpReachNlri().getAdvertizedRoutes();
148             if (routes != null && routes.getDestinationType() instanceof DestinationEvpnCase) {
149                 final DestinationEvpnCase evpnCase = (DestinationEvpnCase) routes.getDestinationType();
150                 serializeNlri(evpnCase.getDestinationEvpn().getEvpnDestination(), byteAggregator);
151             }
152         } else if (pathAttributes2 != null) {
153             final MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
154             final WithdrawnRoutes withdrawnRoutes = mpUnreachNlri.getWithdrawnRoutes();
155             if (withdrawnRoutes != null && withdrawnRoutes.getDestinationType()
156                     instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329
157                     .update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationEvpnCase) {
158                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update
159                         .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationEvpnCase evpnCase =
160                         (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update
161                                 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type
162                                 .DestinationEvpnCase) mpUnreachNlri.getWithdrawnRoutes().getDestinationType();
163                 serializeNlri(evpnCase.getDestinationEvpn().getEvpnDestination(), byteAggregator);
164             }
165         }
166     }
167
168     public static void serializeNlri(final List<EvpnDestination> destinationList, final ByteBuf output) {
169         ByteBuf nlriOutput = null;
170         for (final EvpnDestination dest : destinationList) {
171             final ByteBuf nlriCommon = Unpooled.buffer();
172             serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriCommon);
173             nlriOutput = SimpleEvpnNlriRegistry.getInstance().serializeEvpn(dest.getEvpnChoice(), nlriCommon);
174         }
175         output.writeBytes(nlriOutput);
176     }
177 }