Update MRI projects for Aluminium
[bgpcep.git] / bgp / extensions / labeled-unicast / src / main / java / org / opendaylight / protocol / bgp / labeled / unicast / LabeledUnicastIpv4RIBSupport.java
1 /*
2  * Copyright (c) 2015 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.labeled.unicast;
9
10 import static com.google.common.base.Verify.verify;
11
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.Map;
15 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.bgp.rib.rib.loc.rib.tables.routes.LabeledUnicastRoutesCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.LabeledUnicastRoutes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.LabeledUnicastRoutesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.list.LabeledUnicastRoute;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.list.LabeledUnicastRouteKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLabeledUnicastCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicast;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
33
34 public final class LabeledUnicastIpv4RIBSupport
35         extends AbstractLabeledUnicastRIBSupport<LabeledUnicastRoutesCase, LabeledUnicastRoutes> {
36     private static final LabeledUnicastRoutes EMPTY_CONTAINER
37             = new LabeledUnicastRoutesBuilder().setLabeledUnicastRoute(Collections.emptyList()).build();
38     private static LabeledUnicastIpv4RIBSupport SINGLETON;
39
40     private LabeledUnicastIpv4RIBSupport(final BindingNormalizedNodeSerializer mappingService) {
41         super(mappingService,
42                 LabeledUnicastRoutesCase.class,
43                 LabeledUnicastRoutes.class,
44                 Ipv4AddressFamily.class,
45                 DestinationLabeledUnicast.QNAME);
46     }
47
48     static synchronized LabeledUnicastIpv4RIBSupport getInstance(final BindingNormalizedNodeSerializer mappingService) {
49         if (SINGLETON == null) {
50             SINGLETON = new LabeledUnicastIpv4RIBSupport(mappingService);
51         }
52         return SINGLETON;
53     }
54
55     @Override
56     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
57         return new DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
58                 new DestinationLabeledUnicastBuilder()
59                         .setCLabeledUnicastDestination(extractRoutes(routes)).build()).build();
60     }
61
62     @Override
63     protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
64         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329
65                 .update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
66                 .DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(new org.opendaylight.yang.gen.v1
67                 .urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.mp.unreach.nlri
68                 .withdrawn.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder()
69                 .setCLabeledUnicastDestination(extractRoutes(routes)).build()).build();
70     }
71
72     @Override
73     public IpPrefix extractPrefix(
74             final DataContainerNode<? extends PathArgument> route,
75             final NodeIdentifier prefixTypeNid) {
76         if (route.getChild(prefixTypeNid).isPresent()) {
77             final String prefixType = (String) route.getChild(prefixTypeNid).get().getValue();
78             return new IpPrefix(new Ipv4Prefix(prefixType));
79         }
80         return null;
81     }
82
83     @Override
84     public LabeledUnicastRoutes emptyRoutesContainer() {
85         return EMPTY_CONTAINER;
86     }
87
88     @Override
89     public Map<LabeledUnicastRouteKey, LabeledUnicastRoute> extractAdjRibInRoutes(final Routes routes) {
90         verify(routes instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast
91             .rev180329.bgp.rib.rib.peer.adj.rib.in.tables.routes.LabeledUnicastRoutesCase, "Unrecognized routes %s",
92             routes);
93         return ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329
94                 .bgp.rib.rib.peer.adj.rib.in.tables.routes.LabeledUnicastRoutesCase) routes).getLabeledUnicastRoutes()
95                 .nonnullLabeledUnicastRoute();
96     }
97 }