87575ae1bab778808f2780dfa252f2582509a533
[bgpcep.git] / bgp / 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 com.google.common.base.Preconditions;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.util.ArrayList;
18 import java.util.List;
19 import javax.annotation.Nullable;
20 import org.opendaylight.protocol.bgp.evpn.spi.EvpnRegistry;
21 import org.opendaylight.protocol.bgp.evpn.spi.pojo.SimpleEvpnNlriRegistry;
22 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
23 import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
24 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.NlriType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.EvpnChoice;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.destination.EvpnDestination;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.destination.EvpnDestinationBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationEvpnCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationEvpnCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.evpn._case.DestinationEvpnBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes1;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes2;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpReachNlriBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpUnreachNlri;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpUnreachNlriBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.reach.nlri.AdvertizedRoutes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
45 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 public final class EvpnNlriParser implements NlriParser, NlriSerializer {
51     private static final Logger LOG = LoggerFactory.getLogger(EvpnNlriParser.class);
52     private static final NodeIdentifier EVPN_CHOICE_NID = new NodeIdentifier(EvpnChoice.QNAME);
53
54     @FunctionalInterface
55     private interface ExtractionInterface {
56         EvpnChoice check(EvpnRegistry reg, ChoiceNode cont);
57     }
58
59     public static EvpnDestination extractEvpnDestination(final DataContainerNode<? extends PathArgument> evpnChoice) {
60         return extractDestination(evpnChoice, EvpnRegistry::serializeEvpnModel);
61     }
62
63     private static EvpnDestination extractDestination(final DataContainerNode<? extends PathArgument> evpnChoice, final ExtractionInterface extract) {
64         final EvpnRegistry reg = SimpleEvpnNlriRegistry.getInstance();
65         final ChoiceNode cont = (ChoiceNode) evpnChoice.getChild(EVPN_CHOICE_NID).get();
66         final EvpnChoice evpnValue = extract.check(reg, cont);
67         if (evpnValue == null) {
68             LOG.warn("Unrecognized Nlri {}", cont);
69             return null;
70         }
71         return new EvpnDestinationBuilder().setRouteDistinguisher(extractRouteDistinguisher(evpnChoice)).setEvpnChoice(evpnValue).build();
72     }
73
74     public static EvpnDestination extractRouteKeyDestination(final DataContainerNode<? extends PathArgument> evpnChoice) {
75         return extractDestination(evpnChoice, EvpnRegistry::serializeEvpnRouteKey);
76     }
77
78     @Override
79     public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
80         if (!nlri.isReadable()) {
81             return;
82         }
83         final List<EvpnDestination> dst = parseNlri(nlri);
84
85         builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
86             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.update.attributes.mp.unreach.nlri.withdrawn.
87                 routes.destination.type.DestinationEvpnCaseBuilder().setDestinationEvpn(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.
88                 yang.bgp.evpn.rev171213.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.evpn._case.DestinationEvpnBuilder()
89                 .setEvpnDestination(dst).build()).build()).build());
90     }
91
92     @Override
93     public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder) throws BGPParsingException {
94         if (!nlri.isReadable()) {
95             return;
96         }
97         final List<EvpnDestination> dst = parseNlri(nlri);
98
99         builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
100             new DestinationEvpnCaseBuilder().setDestinationEvpn(new DestinationEvpnBuilder().setEvpnDestination(dst).build()).build()).build());
101     }
102
103     @Nullable
104     private static List<EvpnDestination> parseNlri(final ByteBuf nlri) {
105         if (!nlri.isReadable()) {
106             return null;
107         }
108         final List<EvpnDestination> dests = new ArrayList<>();
109
110         while (nlri.isReadable()) {
111             final EvpnDestinationBuilder builder = new EvpnDestinationBuilder();
112             final NlriType type = NlriType.forValue(nlri.readUnsignedByte());
113             final int length = nlri.readUnsignedByte();
114             final ByteBuf nlriBuf = nlri.readSlice(length);
115             builder.setRouteDistinguisher(parseRouteDistinguisher(nlriBuf));
116             builder.setEvpnChoice(SimpleEvpnNlriRegistry.getInstance().parseEvpn(type, nlriBuf));
117             dests.add(builder.build());
118         }
119         return dests;
120     }
121
122     @Override
123     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
124         Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a Attributes object");
125         final Attributes pathAttributes = (Attributes) attribute;
126         final Attributes1 pathAttributes1 = pathAttributes.getAugmentation(Attributes1.class);
127         final Attributes2 pathAttributes2 = pathAttributes.getAugmentation(Attributes2.class);
128         if (pathAttributes1 != null) {
129             final AdvertizedRoutes routes = pathAttributes1.getMpReachNlri().getAdvertizedRoutes();
130             if (routes != null && routes.getDestinationType() instanceof DestinationEvpnCase) {
131                 final DestinationEvpnCase evpnCase = (DestinationEvpnCase) routes.getDestinationType();
132                 serializeNlri(evpnCase.getDestinationEvpn().getEvpnDestination(), byteAggregator);
133             }
134         } else if (pathAttributes2 != null) {
135             final MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
136             final WithdrawnRoutes withdrawnRoutes = mpUnreachNlri.getWithdrawnRoutes();
137             if (withdrawnRoutes != null && withdrawnRoutes.getDestinationType() instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml
138                 .ns.yang.bgp.evpn.rev171213.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationEvpnCase) {
139                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.update.attributes.mp.unreach.nlri.withdrawn.
140                     routes.destination.type.DestinationEvpnCase evpnCase = (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml
141                     .ns.yang.bgp.evpn.rev171213.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationEvpnCase) mpUnreachNlri.getWithdrawnRoutes().getDestinationType();
142                 serializeNlri(evpnCase.getDestinationEvpn().getEvpnDestination(), byteAggregator);
143             }
144         }
145     }
146
147     public static void serializeNlri(final List<EvpnDestination> cEvpn, final ByteBuf output) {
148         ByteBuf nlriOutput = null;
149         for (final EvpnDestination dest : cEvpn) {
150             final ByteBuf nlriCommon = Unpooled.buffer();
151             serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriCommon);
152             nlriOutput = SimpleEvpnNlriRegistry.getInstance().serializeEvpn(dest.getEvpnChoice(), nlriCommon);
153         }
154         output.writeBytes(nlriOutput);
155     }
156 }