Merge "Bug-612: fix segment-routing serializers"
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / BGPActivator.java
1 /*
2  * Copyright (c) 2013 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.parser.impl;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 import org.opendaylight.protocol.bgp.parser.impl.message.BGPKeepAliveMessageParser;
14 import org.opendaylight.protocol.bgp.parser.impl.message.BGPNotificationMessageParser;
15 import org.opendaylight.protocol.bgp.parser.impl.message.BGPOpenMessageParser;
16 import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
17 import org.opendaylight.protocol.bgp.parser.impl.message.open.As4CapabilityHandler;
18 import org.opendaylight.protocol.bgp.parser.impl.message.open.CapabilityParameterParser;
19 import org.opendaylight.protocol.bgp.parser.impl.message.open.GracefulCapabilityHandler;
20 import org.opendaylight.protocol.bgp.parser.impl.message.open.MultiProtocolCapabilityHandler;
21 import org.opendaylight.protocol.bgp.parser.impl.message.update.AS4AggregatorAttributeParser;
22 import org.opendaylight.protocol.bgp.parser.impl.message.update.AS4PathAttributeParser;
23 import org.opendaylight.protocol.bgp.parser.impl.message.update.AdvertizedRoutesSerializer;
24 import org.opendaylight.protocol.bgp.parser.impl.message.update.AggregatorAttributeParser;
25 import org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathAttributeParser;
26 import org.opendaylight.protocol.bgp.parser.impl.message.update.AtomicAggregateAttributeParser;
27 import org.opendaylight.protocol.bgp.parser.impl.message.update.ClusterIdAttributeParser;
28 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunitiesAttributeParser;
29 import org.opendaylight.protocol.bgp.parser.impl.message.update.ExtendedCommunitiesAttributeParser;
30 import org.opendaylight.protocol.bgp.parser.impl.message.update.Ipv4NlriParser;
31 import org.opendaylight.protocol.bgp.parser.impl.message.update.Ipv6NlriParser;
32 import org.opendaylight.protocol.bgp.parser.impl.message.update.LocalPreferenceAttributeParser;
33 import org.opendaylight.protocol.bgp.parser.impl.message.update.MPReachAttributeParser;
34 import org.opendaylight.protocol.bgp.parser.impl.message.update.MPUnreachAttributeParser;
35 import org.opendaylight.protocol.bgp.parser.impl.message.update.MultiExitDiscriminatorAttributeParser;
36 import org.opendaylight.protocol.bgp.parser.impl.message.update.NextHopAttributeParser;
37 import org.opendaylight.protocol.bgp.parser.impl.message.update.OriginAttributeParser;
38 import org.opendaylight.protocol.bgp.parser.impl.message.update.OriginatorIdAttributeParser;
39 import org.opendaylight.protocol.bgp.parser.impl.message.update.WithdrawnRoutesSerializer;
40 import org.opendaylight.protocol.bgp.parser.spi.AbstractBGPExtensionProviderActivator;
41 import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
42 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
43 import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
44 import org.opendaylight.protocol.bgp.parser.spi.SubsequentAddressFamilyRegistry;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.c.parameters.As4BytesCase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Aggregator;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AsPath;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AtomicAggregate;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ClusterId;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Communities;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunities;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.LocalPref;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.MultiExitDisc;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Origin;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.OriginatorId;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.WithdrawnRoutes;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.c.parameters.GracefulRestartCase;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.c.parameters.MultiprotocolCase;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlri;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutes;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.MplsLabeledVpnSubsequentAddressFamily;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.NextHop;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
72
73 public final class BGPActivator extends AbstractBGPExtensionProviderActivator {
74
75     private static final int IPV4_AFI = 1;
76     private static final int IPV6_AFI = 2;
77
78     private static final int UNICAST_SAFI = 1;
79     private static final int VPN_SAFI = 128;
80
81     @Override
82     protected List<AutoCloseable> startImpl(final BGPExtensionProviderContext context) {
83         final List<AutoCloseable> regs = new ArrayList<>();
84
85         regs.add(context.registerNlriSerializer(AdvertizedRoutes.class, new AdvertizedRoutesSerializer()));
86         regs.add(context.registerNlriSerializer(WithdrawnRoutes.class, new WithdrawnRoutesSerializer()));
87
88         regs.add(context.registerAddressFamily(Ipv4AddressFamily.class, IPV4_AFI));
89         regs.add(context.registerAddressFamily(Ipv6AddressFamily.class, IPV6_AFI));
90
91         regs.add(context.registerSubsequentAddressFamily(UnicastSubsequentAddressFamily.class, UNICAST_SAFI));
92         regs.add(context.registerSubsequentAddressFamily(MplsLabeledVpnSubsequentAddressFamily.class, VPN_SAFI));
93
94         regs.add(context.registerNlriParser(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class, new Ipv4NlriParser()));
95         regs.add(context.registerNlriParser(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class, new Ipv6NlriParser()));
96
97         this.registerCapabilityParsers(regs, context);
98         this.registerAttributeParsers(regs, context);
99         this.registerMessageParsers(regs, context);
100         return regs;
101     }
102
103     private void registerCapabilityParsers(final List<AutoCloseable> regs, final BGPExtensionProviderContext context) {
104         final AddressFamilyRegistry afiReg = context.getAddressFamilyRegistry();
105         final SubsequentAddressFamilyRegistry safiReg = context.getSubsequentAddressFamilyRegistry();
106
107         final MultiProtocolCapabilityHandler multi = new MultiProtocolCapabilityHandler(afiReg, safiReg);
108         regs.add(context.registerCapabilityParser(MultiProtocolCapabilityHandler.CODE, multi));
109         regs.add(context.registerCapabilitySerializer(MultiprotocolCase.class, multi));
110
111         final As4CapabilityHandler as4 = new As4CapabilityHandler();
112         regs.add(context.registerCapabilityParser(As4CapabilityHandler.CODE, as4));
113         regs.add(context.registerCapabilitySerializer(As4BytesCase.class, as4));
114
115         final GracefulCapabilityHandler grace = new GracefulCapabilityHandler(afiReg, safiReg);
116         regs.add(context.registerCapabilitySerializer(GracefulRestartCase.class, grace));
117         regs.add(context.registerCapabilityParser(GracefulCapabilityHandler.CODE, grace));
118
119         final CapabilityParameterParser cpp = new CapabilityParameterParser(context.getCapabilityRegistry());
120         regs.add(context.registerParameterParser(CapabilityParameterParser.TYPE, cpp));
121         regs.add(context.registerParameterSerializer(BgpParameters.class, cpp));
122     }
123
124     private void registerAttributeParsers(final List<AutoCloseable> regs, final BGPExtensionProviderContext context) {
125         final OriginAttributeParser originAttributeParser = new OriginAttributeParser();
126         regs.add(context.registerAttributeSerializer(Origin.class, originAttributeParser));
127         regs.add(context.registerAttributeParser(OriginAttributeParser.TYPE, originAttributeParser));
128
129         final AsPathAttributeParser asPathAttributeParser = new AsPathAttributeParser(context.getReferenceCache());
130         regs.add(context.registerAttributeSerializer(AsPath.class, asPathAttributeParser));
131         regs.add(context.registerAttributeParser(AsPathAttributeParser.TYPE, asPathAttributeParser));
132
133         final NextHopAttributeParser nextHopAttributeParser = new NextHopAttributeParser();
134         regs.add(context.registerAttributeSerializer(NextHop.class, nextHopAttributeParser));
135         regs.add(context.registerAttributeParser(NextHopAttributeParser.TYPE, nextHopAttributeParser));
136
137         final MultiExitDiscriminatorAttributeParser multiExitDiscriminatorAttributeParser = new MultiExitDiscriminatorAttributeParser();
138         regs.add(context.registerAttributeSerializer(MultiExitDisc.class, multiExitDiscriminatorAttributeParser));
139         regs.add(context.registerAttributeParser(MultiExitDiscriminatorAttributeParser.TYPE, multiExitDiscriminatorAttributeParser));
140
141         final LocalPreferenceAttributeParser localPreferenceAttributeParser = new LocalPreferenceAttributeParser();
142         regs.add(context.registerAttributeSerializer(LocalPref.class, localPreferenceAttributeParser));
143         regs.add(context.registerAttributeParser(LocalPreferenceAttributeParser.TYPE, localPreferenceAttributeParser));
144
145         final AtomicAggregateAttributeParser atomicAggregateAttributeParser = new AtomicAggregateAttributeParser();
146         regs.add(context.registerAttributeSerializer(AtomicAggregate.class, atomicAggregateAttributeParser));
147         regs.add(context.registerAttributeParser(AtomicAggregateAttributeParser.TYPE, atomicAggregateAttributeParser));
148
149         final AggregatorAttributeParser as4AggregatorAttributeParser = new AggregatorAttributeParser(context.getReferenceCache());
150         regs.add(context.registerAttributeSerializer(Aggregator.class, as4AggregatorAttributeParser));
151         regs.add(context.registerAttributeParser(AggregatorAttributeParser.TYPE, as4AggregatorAttributeParser));
152
153         final CommunitiesAttributeParser communitiesAttributeParser = new CommunitiesAttributeParser(context.getReferenceCache());
154         regs.add(context.registerAttributeSerializer(Communities.class, communitiesAttributeParser));
155         regs.add(context.registerAttributeParser(CommunitiesAttributeParser.TYPE, communitiesAttributeParser));
156
157         final OriginatorIdAttributeParser originatorIdAttributeParser = new OriginatorIdAttributeParser();
158         regs.add(context.registerAttributeSerializer(OriginatorId.class, originatorIdAttributeParser));
159         regs.add(context.registerAttributeParser(OriginatorIdAttributeParser.TYPE, originatorIdAttributeParser));
160
161         final ClusterIdAttributeParser clusterIdAttributeParser = new ClusterIdAttributeParser();
162         regs.add(context.registerAttributeSerializer(ClusterId.class, clusterIdAttributeParser));
163         regs.add(context.registerAttributeParser(ClusterIdAttributeParser.TYPE, clusterIdAttributeParser));
164
165         final NlriRegistry nlriReg = context.getNlriRegistry();
166
167         final MPReachAttributeParser mpReachAttributeParser = new MPReachAttributeParser(nlriReg);
168         regs.add(context.registerAttributeSerializer(MpReachNlri.class, mpReachAttributeParser));
169         regs.add(context.registerAttributeParser(MPReachAttributeParser.TYPE, mpReachAttributeParser));
170
171         final MPUnreachAttributeParser mpUnreachAttributeParser = new MPUnreachAttributeParser(nlriReg);
172         regs.add(context.registerAttributeSerializer(MpUnreachNlri.class, mpUnreachAttributeParser));
173         regs.add(context.registerAttributeParser(MPUnreachAttributeParser.TYPE, mpUnreachAttributeParser));
174
175         final ExtendedCommunitiesAttributeParser extendedCommunitiesAttributeParser = new ExtendedCommunitiesAttributeParser(context.getReferenceCache());
176         regs.add(context.registerAttributeSerializer(ExtendedCommunities.class, extendedCommunitiesAttributeParser));
177         regs.add(context.registerAttributeParser(ExtendedCommunitiesAttributeParser.TYPE, extendedCommunitiesAttributeParser));
178
179         regs.add(context.registerAttributeParser(AS4AggregatorAttributeParser.TYPE, new AS4AggregatorAttributeParser()));
180         regs.add(context.registerAttributeParser(AS4PathAttributeParser.TYPE, new AS4PathAttributeParser()));
181     }
182
183     private void registerMessageParsers(final List<AutoCloseable> regs, final BGPExtensionProviderContext context) {
184         final BGPOpenMessageParser omp = new BGPOpenMessageParser(context.getParameterRegistry());
185         regs.add(context.registerMessageParser(BGPOpenMessageParser.TYPE, omp));
186         regs.add(context.registerMessageSerializer(Open.class, omp));
187
188         final BGPUpdateMessageParser ump = new BGPUpdateMessageParser(context.getAttributeRegistry());
189         regs.add(context.registerMessageParser(BGPUpdateMessageParser.TYPE, ump));
190         regs.add(context.registerMessageSerializer(Update.class, ump));
191
192         final BGPNotificationMessageParser nmp = new BGPNotificationMessageParser();
193         regs.add(context.registerMessageParser(BGPNotificationMessageParser.TYPE, nmp));
194         regs.add(context.registerMessageSerializer(Notify.class, nmp));
195
196         final BGPKeepAliveMessageParser kamp = new BGPKeepAliveMessageParser();
197         regs.add(context.registerMessageParser(BGPKeepAliveMessageParser.TYPE, kamp));
198         regs.add(context.registerMessageSerializer(Keepalive.class, kamp));
199     }
200 }