BUG-5685: Register BGP Peer Cluster Singleton Service
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / controller / config / yang / bgp / rib / impl / BGPPeerModule.java
1 /*
2  * Copyright (c) 2013, 2016 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 /**
9  * Generated file
10
11  * Generated from: yang module name: bgp-rib-impl  yang module local name: bgp-peer
12  * Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator
13  * Generated at: Sat Jan 25 11:00:14 CET 2014
14  *
15  * Do not modify this file unless it is present under src/main directory
16  */
17 package org.opendaylight.controller.config.yang.bgp.rib.impl;
18
19 import com.google.common.reflect.AbstractInvocationHandler;
20 import com.google.common.reflect.Reflection;
21 import io.netty.channel.epoll.Epoll;
22 import java.lang.reflect.Method;
23 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
24 import org.opendaylight.controller.config.api.osgi.WaitingServiceTracker;
25 import org.opendaylight.protocol.bgp.rib.impl.spi.BgpDeployer;
26 import org.opendaylight.protocol.bgp.rib.impl.spi.InstanceType;
27 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
28 import org.opendaylight.protocol.util.Ipv4Util;
29 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
30 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.NeighborKey;
31 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp;
32 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Neighbors;
33 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.Protocols;
34 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.Protocol;
35 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.ProtocolKey;
36 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.policy.types.rev151009.BGP;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev160614.Protocol1;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.rfc2385.cfg.rev160324.Rfc2385Key;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
43 import org.osgi.framework.BundleContext;
44
45 /**
46  *
47  */
48 @Deprecated
49 public final class BGPPeerModule extends org.opendaylight.controller.config.yang.bgp.rib.impl.AbstractBGPPeerModule {
50
51     private BundleContext bundleContext;
52
53     public BGPPeerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
54             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
55         super(identifier, dependencyResolver);
56     }
57
58     public BGPPeerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
59             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, final BGPPeerModule oldModule,
60             final java.lang.AutoCloseable oldInstance) {
61
62         super(identifier, dependencyResolver, oldModule, oldInstance);
63     }
64
65     @Override
66     protected void customValidation() {
67         final IpAddress host = getHost();
68         JmxAttributeValidationException.checkNotNull(host, "value is not set.", hostJmxAttribute);
69         JmxAttributeValidationException.checkCondition(host.getIpv4Address() != null || host.getIpv6Address() != null,
70                 "Unexpected host", hostJmxAttribute);
71
72         JmxAttributeValidationException.checkNotNull(getPort(), "value is not set.", portJmxAttribute);
73
74         if (getPeerRole() != null) {
75             final boolean isNotPeerRoleInternal= getPeerRole() != PeerRole.Internal;
76             JmxAttributeValidationException.checkCondition(isNotPeerRoleInternal,
77                     "Internal Peer Role is reserved for Application Peer use.", peerRoleJmxAttribute);
78         }
79
80         if (isPasswordPresent(getPassword())) {
81             JmxAttributeValidationException.checkCondition(Epoll.isAvailable(),
82                     "BGP Peer is configured with password but native transport is not available", passwordJmxAttribute);
83         }
84
85     }
86
87     @Override
88     public java.lang.AutoCloseable createInstance() {
89         final RIB rib = getRibDependency();
90         final WaitingServiceTracker<BgpDeployer> bgpDeployerTracker = WaitingServiceTracker.create(BgpDeployer.class, this.bundleContext);
91         final BgpDeployer bgpDeployer = bgpDeployerTracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);
92         //map configuration to OpenConfig BGP
93         final Neighbor neighbor = bgpDeployer.getMappingService().fromBgpPeer(getAddPathDependency(), getAdvertizedTableDependency(), getHoldtimer(),
94                 getHost(), getInitiateConnection(), getPassword(), getPort(), getRetrytimer(), getRemoteAs(), getPeerRole(), getSimpleRoutingPolicy());
95         //write to configuration DS
96         final KeyedInstanceIdentifier<Protocol, ProtocolKey> protocolIId = bgpDeployer.getInstanceIdentifier().child(Protocols.class)
97             .child(Protocol.class, new ProtocolKey(BGP.class, rib.getInstanceIdentifier().getKey().getId().getValue()));
98         final InstanceIdentifier<Bgp> bgpIID = protocolIId.augmentation(Protocol1.class).child(Bgp.class);
99         final KeyedInstanceIdentifier<Neighbor, NeighborKey> neighborIId = protocolIId.augmentation(Protocol1.class).child(Bgp.class)
100             .child(Neighbors.class).child(Neighbor.class, neighbor.getKey());
101         bgpDeployer.onNeighborModified(bgpIID, neighbor, () -> bgpDeployer.writeConfiguration(neighbor, neighborIId));
102         //get rib instance service, use filter
103         final WaitingServiceTracker<BGPPeerRuntimeMXBean> peerTracker = WaitingServiceTracker.create(BGPPeerRuntimeMXBean.class,
104                 this.bundleContext, "(" + InstanceType.PEER.getBeanName() + "=" + Ipv4Util.toStringIP(getHost()) + ")");
105         final BGPPeerRuntimeMXBean peer = peerTracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);
106         final BGPPeerRuntimeRegistration runtimeRegistration = getRootRuntimeBeanRegistratorWrapper().register(peer);
107         return Reflection.newProxy(AutoCloseablePeer.class, new AbstractInvocationHandler() {
108             @Override
109             protected Object handleInvocation(final Object proxy, final Method method, final Object[] args) throws Throwable {
110                 if (method.getName().equals("close")) {
111                     bgpDeployer.onNeighborRemoved(bgpIID, neighbor);
112                     runtimeRegistration.close();
113                     bgpDeployerTracker.close();
114                     peerTracker.close();
115                     return null;
116                 } else {
117                     return method.invoke(peer, args);
118                 }
119             }
120         });
121     }
122
123     private static boolean isPasswordPresent(final Rfc2385Key password) {
124         return password != null && ! password.getValue().isEmpty();
125     }
126
127     public void setBundleContext(final BundleContext bundleContext) {
128         this.bundleContext = bundleContext;
129     }
130
131     private static interface AutoCloseablePeer extends RIB, AutoCloseable {
132     }
133
134 }