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