BUG-5785 Support for dissemination of L3VPN flow spec II
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / FlowspecActivator.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 java.util.ArrayList;
11 import java.util.List;
12 import org.opendaylight.protocol.bgp.flowspec.handlers.FSIpv4DestinationPrefixHandler;
13 import org.opendaylight.protocol.bgp.flowspec.handlers.FSIpv4FragmentHandler;
14 import org.opendaylight.protocol.bgp.flowspec.handlers.FSIpv4SourcePrefixHandler;
15 import org.opendaylight.protocol.bgp.flowspec.handlers.FSIpv6DestinationPrefixHandler;
16 import org.opendaylight.protocol.bgp.flowspec.handlers.FSIpv6FragmentHandler;
17 import org.opendaylight.protocol.bgp.flowspec.handlers.FSIpv6SourcePrefixHandler;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.DestinationPortCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.DscpCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.FragmentCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.IcmpCodeCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.IcmpTypeCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.PacketLengthCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.PortCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.SourcePortCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.TcpFlagsCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv4.flowspec.flowspec.type.ProtocolIpCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv4.flowspec.flowspec.type.SourcePrefixCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv6.flowspec.flowspec.type.DestinationIpv6PrefixCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv6.flowspec.flowspec.type.FlowLabelCase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv6.flowspec.flowspec.type.NextHeaderCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv6.flowspec.flowspec.type.SourceIpv6PrefixCase;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public final class FlowspecActivator implements AutoCloseable {
38     private static final Logger LOG = LoggerFactory.getLogger(FlowspecActivator.class);
39
40     private final List<AutoCloseable> registrations = new ArrayList<>();
41
42     public FlowspecActivator(final SimpleFlowspecExtensionProviderContext context) {
43         for (SimpleFlowspecExtensionProviderContext.SAFI safi : SimpleFlowspecExtensionProviderContext.SAFI.values()) {
44             registerCommonFlowspecTypeHandlers(
45                 SimpleFlowspecExtensionProviderContext.AFI.IPV4,
46                 safi,
47                 registrations,
48                 context
49             );
50             registerIpv4FlowspecTypeHandlers(
51                 SimpleFlowspecExtensionProviderContext.AFI.IPV4,
52                 safi,
53                 registrations,
54                 context
55             );
56
57             registerCommonFlowspecTypeHandlers(
58                 SimpleFlowspecExtensionProviderContext.AFI.IPV6,
59                 safi,
60                 registrations,
61                 context
62             );
63             registerIpv6FlowspecTypeHandlers(
64                 SimpleFlowspecExtensionProviderContext.AFI.IPV6,
65                 safi,
66                 registrations,
67                 context
68             );
69         }
70     }
71
72     /**
73      * Register the common flowspec type handlers
74      *
75      * @param afi
76      * @param safi
77      */
78     private void registerCommonFlowspecTypeHandlers(
79         final SimpleFlowspecExtensionProviderContext.AFI afi,
80         final SimpleFlowspecExtensionProviderContext.SAFI safi,
81         final List<AutoCloseable> regs,
82         final SimpleFlowspecExtensionProviderContext context
83     ) {
84         final SimpleFlowspecTypeRegistry flowspecTypeRegistry = context.getFlowspecTypeRegistry(afi, safi);
85
86         final FSPortHandler portHandler = new FSPortHandler();
87         regs.add(flowspecTypeRegistry.registerFlowspecTypeParser(FSPortHandler.PORT_VALUE, portHandler));
88         regs.add(flowspecTypeRegistry.registerFlowspecTypeSerializer(PortCase.class, portHandler));
89
90         final FSDestinationPortHandler destinationPortHandler = new FSDestinationPortHandler();
91         regs.add(flowspecTypeRegistry.registerFlowspecTypeParser(FSDestinationPortHandler.DESTINATION_PORT_VALUE, destinationPortHandler));
92         regs.add(flowspecTypeRegistry.registerFlowspecTypeSerializer(DestinationPortCase.class, destinationPortHandler));
93
94         final FSSourcePortHandler sourcePortHandler = new FSSourcePortHandler();
95         regs.add(flowspecTypeRegistry.registerFlowspecTypeParser(FSSourcePortHandler.SOURCE_PORT_VALUE, sourcePortHandler));
96         regs.add(flowspecTypeRegistry.registerFlowspecTypeSerializer(SourcePortCase.class, sourcePortHandler));
97
98         final FSIcmpTypeHandler icmpTypeHandler = new FSIcmpTypeHandler();
99         regs.add(flowspecTypeRegistry.registerFlowspecTypeParser(FSIcmpTypeHandler.ICMP_TYPE_VALUE, icmpTypeHandler));
100         regs.add(flowspecTypeRegistry.registerFlowspecTypeSerializer(IcmpTypeCase.class, icmpTypeHandler));
101
102         final FSIcmpCodeHandler icmpCodeHandler = new FSIcmpCodeHandler();
103         regs.add(flowspecTypeRegistry.registerFlowspecTypeParser(FSIcmpCodeHandler.ICMP_CODE_VALUE, icmpCodeHandler));
104         regs.add(flowspecTypeRegistry.registerFlowspecTypeSerializer(IcmpCodeCase.class, icmpCodeHandler));
105
106         final FSTcpFlagsHandler tcpFlagsHandler = new FSTcpFlagsHandler();
107         regs.add(flowspecTypeRegistry.registerFlowspecTypeParser(FSTcpFlagsHandler.TCP_FLAGS_VALUE, tcpFlagsHandler));
108         regs.add(flowspecTypeRegistry.registerFlowspecTypeSerializer(TcpFlagsCase.class, tcpFlagsHandler));
109
110         final FSPacketLengthHandler packetlengthHandler = new FSPacketLengthHandler();
111         regs.add(flowspecTypeRegistry.registerFlowspecTypeParser(FSPacketLengthHandler.PACKET_LENGTH_VALUE, packetlengthHandler));
112         regs.add(flowspecTypeRegistry.registerFlowspecTypeSerializer(PacketLengthCase.class, packetlengthHandler));
113
114         final FSDscpHandler dscpHandler = new FSDscpHandler();
115         regs.add(flowspecTypeRegistry.registerFlowspecTypeParser(FSDscpHandler.DSCP_VALUE, dscpHandler));
116         regs.add(flowspecTypeRegistry.registerFlowspecTypeSerializer(DscpCase.class, dscpHandler));
117     }
118
119     protected void registerIpv4FlowspecTypeHandlers(
120         final SimpleFlowspecExtensionProviderContext.AFI afi,
121         final SimpleFlowspecExtensionProviderContext.SAFI safi,
122         final List<AutoCloseable> regs,
123         final SimpleFlowspecExtensionProviderContext context
124     ) {
125         final SimpleFlowspecTypeRegistry flowspecTypeRegistry = context.getFlowspecTypeRegistry(afi, safi);
126
127         final FSIpv4DestinationPrefixHandler destinationPrefixHandler = new FSIpv4DestinationPrefixHandler();
128         regs.add(flowspecTypeRegistry.registerFlowspecTypeParser(FSIpv4DestinationPrefixHandler.DESTINATION_PREFIX_VALUE, destinationPrefixHandler));
129         regs.add(flowspecTypeRegistry.registerFlowspecTypeSerializer(DestinationPrefixCase.class, destinationPrefixHandler));
130
131         final FSIpv4SourcePrefixHandler sourcePrefixHandler = new FSIpv4SourcePrefixHandler();
132         regs.add(flowspecTypeRegistry.registerFlowspecTypeParser(FSIpv4SourcePrefixHandler.SOURCE_PREFIX_VALUE, sourcePrefixHandler));
133         regs.add(flowspecTypeRegistry.registerFlowspecTypeSerializer(SourcePrefixCase.class, sourcePrefixHandler));
134
135         final FSIpProtocolHandler ipProtocolHandler = new FSIpProtocolHandler();
136         regs.add(flowspecTypeRegistry.registerFlowspecTypeParser(FSIpProtocolHandler.IP_PROTOCOL_VALUE, ipProtocolHandler));
137         regs.add(flowspecTypeRegistry.registerFlowspecTypeSerializer(ProtocolIpCase.class, ipProtocolHandler));
138
139         final FSIpv4FragmentHandler fragmentHandler = new FSIpv4FragmentHandler();
140         regs.add(flowspecTypeRegistry.registerFlowspecTypeParser(FSIpv4FragmentHandler.FRAGMENT_VALUE, fragmentHandler));
141         regs.add(flowspecTypeRegistry.registerFlowspecTypeSerializer(FragmentCase.class, fragmentHandler));
142     }
143
144     protected void registerIpv6FlowspecTypeHandlers(
145         final SimpleFlowspecExtensionProviderContext.AFI afi,
146         final SimpleFlowspecExtensionProviderContext.SAFI safi,
147         final List<AutoCloseable> regs,
148         final SimpleFlowspecExtensionProviderContext context
149     ) {
150         final SimpleFlowspecTypeRegistry flowspecTypeRegistry = context.getFlowspecTypeRegistry(afi, safi);
151
152         final FSIpv6DestinationPrefixHandler destinationPrefixHandler = new FSIpv6DestinationPrefixHandler();
153         regs.add(flowspecTypeRegistry.registerFlowspecTypeParser(FSIpv6DestinationPrefixHandler.IPV6_DESTINATION_PREFIX_VALUE, destinationPrefixHandler));
154         regs.add(flowspecTypeRegistry.registerFlowspecTypeSerializer(DestinationIpv6PrefixCase.class, destinationPrefixHandler));
155
156         final FSIpv6SourcePrefixHandler sourcePrefixHandler = new FSIpv6SourcePrefixHandler();
157         regs.add(flowspecTypeRegistry.registerFlowspecTypeParser(FSIpv6SourcePrefixHandler.SOURCE_PREFIX_VALUE, sourcePrefixHandler));
158         regs.add(flowspecTypeRegistry.registerFlowspecTypeSerializer(SourceIpv6PrefixCase.class, sourcePrefixHandler));
159
160         final FSIpv6NextHeaderHandler nextHeaderHandler = new FSIpv6NextHeaderHandler();
161         regs.add(flowspecTypeRegistry.registerFlowspecTypeParser(FSIpv6NextHeaderHandler.NEXT_HEADER_VALUE, nextHeaderHandler));
162         regs.add(flowspecTypeRegistry.registerFlowspecTypeSerializer(NextHeaderCase.class, nextHeaderHandler));
163
164         final FSIpv6FragmentHandler fragmentHandler = new FSIpv6FragmentHandler();
165         regs.add(flowspecTypeRegistry.registerFlowspecTypeParser(FSIpv6FragmentHandler.FRAGMENT_VALUE, fragmentHandler));
166         regs.add(flowspecTypeRegistry.registerFlowspecTypeSerializer(FragmentCase.class, fragmentHandler));
167
168         final FSIpv6FlowLabelHandler flowlabelHandler = new FSIpv6FlowLabelHandler();
169         regs.add(flowspecTypeRegistry.registerFlowspecTypeParser(FSIpv6FlowLabelHandler.FLOW_LABEL_VALUE, flowlabelHandler));
170         regs.add(flowspecTypeRegistry.registerFlowspecTypeSerializer(FlowLabelCase.class, flowlabelHandler));
171     }
172
173     @Override
174     public void close() {
175         if (this.registrations == null) {
176             return;
177         }
178         for (final AutoCloseable r : this.registrations) {
179             try {
180                 r.close();
181             } catch (final Exception e) {
182                 LOG.warn("Failed to close registration", e);
183             }
184         }
185     }
186 }