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