Fix EffectiveRibInWriter.writeTable()
[bgpcep.git] / bgp / extensions / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / FlowspecIpv6RIBSupport.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.Collections;
13 import java.util.List;
14 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.FlowspecSubsequentAddressFamily;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.bgp.rib.rib.loc.rib.tables.routes.FlowspecIpv6RoutesCase;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.bgp.rib.rib.loc.rib.tables.routes.FlowspecIpv6RoutesCaseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.ipv6.DestinationFlowspecIpv6;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.ipv6.route.FlowspecRoute;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.ipv6.route.FlowspecRouteBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.ipv6.route.FlowspecRouteKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.ipv6.routes.FlowspecIpv6Routes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.ipv6.routes.FlowspecIpv6RoutesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv6AddressFamily;
28
29 public final class FlowspecIpv6RIBSupport
30         extends AbstractFlowspecRIBSupport<SimpleFlowspecIpv6NlriParser,
31         FlowspecIpv6RoutesCase,
32         FlowspecIpv6Routes,
33         FlowspecRoute,
34         FlowspecRouteKey> {
35     private static final FlowspecIpv6Routes EMPTY_CONTAINER = new FlowspecIpv6RoutesBuilder()
36             .setFlowspecRoute(Collections.emptyList()).build();
37     private static final FlowspecIpv6RoutesCase EMPTY_CASE = new FlowspecIpv6RoutesCaseBuilder()
38             .setFlowspecIpv6Routes(EMPTY_CONTAINER).build();
39     private static FlowspecIpv6RIBSupport SINGLETON;
40
41     private FlowspecIpv6RIBSupport(final SimpleFlowspecExtensionProviderContext context,
42             final BindingNormalizedNodeSerializer mappingService) {
43         super(
44                 mappingService,
45                 FlowspecIpv6RoutesCase.class,
46                 FlowspecIpv6Routes.class,
47                 FlowspecRoute.class,
48                 Ipv6AddressFamily.class,
49                 FlowspecSubsequentAddressFamily.class,
50                 DestinationFlowspecIpv6.QNAME,
51                 new SimpleFlowspecIpv6NlriParser(context
52                         .getFlowspecTypeRegistry(SimpleFlowspecExtensionProviderContext.AFI.IPV6,
53                                 SimpleFlowspecExtensionProviderContext.SAFI.FLOWSPEC))
54         );
55     }
56
57     static synchronized FlowspecIpv6RIBSupport getInstance(
58             final SimpleFlowspecExtensionProviderContext context,
59             final BindingNormalizedNodeSerializer mappingService) {
60         if (SINGLETON == null) {
61             SINGLETON = new FlowspecIpv6RIBSupport(context, mappingService);
62         }
63         return SINGLETON;
64     }
65
66     @Override
67     public FlowspecRoute createRoute(final FlowspecRoute route, final FlowspecRouteKey key,
68             final Attributes attributes) {
69         final FlowspecRouteBuilder builder;
70         if (route != null) {
71             builder = new FlowspecRouteBuilder(route);
72         } else {
73             builder = new FlowspecRouteBuilder();
74         }
75         return builder.withKey(key).setAttributes(attributes).build();
76     }
77
78     @Override
79     public FlowspecIpv6RoutesCase emptyRoutesCase() {
80         return EMPTY_CASE;
81     }
82
83     @Override
84     public FlowspecIpv6Routes emptyRoutesContainer() {
85         return EMPTY_CONTAINER;
86     }
87
88     @Override
89     public FlowspecRouteKey createRouteListKey(final PathId pathId, final String routeKey) {
90         return new FlowspecRouteKey(pathId, routeKey);
91     }
92
93     @Override
94     public PathId extractPathId(final FlowspecRouteKey routeListKey) {
95         return routeListKey.getPathId();
96     }
97
98     @Override
99     public String extractRouteKey(final FlowspecRouteKey routeListKey) {
100         return routeListKey.getRouteKey();
101     }
102
103     @Override
104     public List<FlowspecRoute> extractAdjRibInRoutes(Routes routes) {
105         verify(routes instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329
106             .bgp.rib.rib.peer.adj.rib.in.tables.routes.FlowspecIpv6RoutesCase, "Unrecognized routes %s", routes);
107         return ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329
108                 .bgp.rib.rib.peer.adj.rib.in.tables.routes.FlowspecIpv6RoutesCase) routes).getFlowspecIpv6Routes()
109                 .nonnullFlowspecRoute();
110     }
111 }