Convert bgp-flowspec to OSGi DS
[bgpcep.git] / bgp / extensions / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / FlowspecIpv4RIBSupport.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.flowspec;
9
10 import static com.google.common.base.Verify.verify;
11
12 import java.util.Map;
13 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
14 import org.opendaylight.protocol.bgp.flowspec.FlowspecTypeRegistries.SAFI;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.FlowspecSubsequentAddressFamily;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.bgp.rib.rib.loc.rib.tables.routes.FlowspecRoutesCase;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.ipv4.DestinationFlowspecIpv4;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv4.route.FlowspecRoute;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv4.route.FlowspecRouteBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv4.route.FlowspecRouteKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.routes.FlowspecRoutes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.routes.FlowspecRoutesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
27
28 public final class FlowspecIpv4RIBSupport
29         extends AbstractFlowspecRIBSupport<SimpleFlowspecIpv4NlriParser,
30         FlowspecRoutesCase,
31         FlowspecRoutes,
32         FlowspecRoute,
33         FlowspecRouteKey> {
34     private static final FlowspecRoutes EMPTY_CONTAINER = new FlowspecRoutesBuilder().build();
35
36     public FlowspecIpv4RIBSupport(final BindingNormalizedNodeSerializer mappingService) {
37         super(
38                 mappingService,
39                 FlowspecRoutesCase.class,
40                 FlowspecRoutes.class,
41                 FlowspecRoute.class,
42                 Ipv4AddressFamily.class,
43                 FlowspecSubsequentAddressFamily.class,
44                 DestinationFlowspecIpv4.QNAME,
45                 new SimpleFlowspecIpv4NlriParser(SAFI.FLOWSPEC)
46         );
47     }
48
49     @Override
50     public FlowspecRoute createRoute(final FlowspecRoute route, final FlowspecRouteKey key,
51             final Attributes attributes) {
52         final FlowspecRouteBuilder builder;
53         if (route != null) {
54             builder = new FlowspecRouteBuilder(route);
55         } else {
56             builder = new FlowspecRouteBuilder();
57         }
58         return builder.withKey(key).setAttributes(attributes).build();
59     }
60
61     @Override
62     public FlowspecRoutes emptyRoutesContainer() {
63         return EMPTY_CONTAINER;
64     }
65
66     @Override
67     public FlowspecRouteKey createRouteListKey(final PathId pathId, final String routeKey) {
68         return new FlowspecRouteKey(pathId, routeKey);
69     }
70
71     @Override
72     public PathId extractPathId(final FlowspecRouteKey routeListKey) {
73         return routeListKey.getPathId();
74     }
75
76     @Override
77     public String extractRouteKey(final FlowspecRouteKey routeListKey) {
78         return routeListKey.getRouteKey();
79     }
80
81     @Override
82     public Map<FlowspecRouteKey, FlowspecRoute> extractAdjRibInRoutes(final Routes routes) {
83         verify(routes instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120
84             .bgp.rib.rib.peer.adj.rib.in.tables.routes.FlowspecRoutesCase, "Unrecognized routes %s", routes);
85         return ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120
86                 .bgp.rib.rib.peer.adj.rib.in.tables.routes.FlowspecRoutesCase) routes).getFlowspecRoutes()
87                 .nonnullFlowspecRoute();
88     }
89 }