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