91ceb1864b351d3c3e3a884fdc2335cc19fe3983
[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.KeyedInstanceIdentifier;
42 import org.osgi.framework.BundleContext;
43
44 /**
45  *
46  */
47 @Deprecated
48 public final class BGPPeerModule extends org.opendaylight.controller.config.yang.bgp.rib.impl.AbstractBGPPeerModule {
49
50     private BundleContext bundleContext;
51
52     public BGPPeerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
53             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
54         super(identifier, dependencyResolver);
55     }
56
57     public BGPPeerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
58             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, final BGPPeerModule oldModule,
59             final java.lang.AutoCloseable oldInstance) {
60
61         super(identifier, dependencyResolver, oldModule, oldInstance);
62     }
63
64     @Override
65     protected void customValidation() {
66         final IpAddress host = getHost();
67         JmxAttributeValidationException.checkNotNull(host, "value is not set.", hostJmxAttribute);
68         JmxAttributeValidationException.checkCondition(host.getIpv4Address() != null || host.getIpv6Address() != null,
69                 "Unexpected host", hostJmxAttribute);
70
71         JmxAttributeValidationException.checkNotNull(getPort(), "value is not set.", portJmxAttribute);
72
73         if (getPeerRole() != null) {
74             final boolean isNotPeerRoleInternal= getPeerRole() != PeerRole.Internal;
75             JmxAttributeValidationException.checkCondition(isNotPeerRoleInternal,
76                     "Internal Peer Role is reserved for Application Peer use.", peerRoleJmxAttribute);
77         }
78
79         if (isPasswordPresent(getPassword())) {
80             JmxAttributeValidationException.checkCondition(Epoll.isAvailable(),
81                     "BGP Peer is configured with password but native transport is not available", passwordJmxAttribute);
82         }
83
84     }
85
86     @Override
87     public java.lang.AutoCloseable createInstance() {
88         final RIB r = getRibDependency();
89         final WaitingServiceTracker<BgpDeployer> bgpDeployerTracker =
90                 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<Neighbor, NeighborKey> neighborIId = bgpDeployer.getInstanceIdentifier().child(Protocols.class).child(Protocol.class,
97                 new ProtocolKey(BGP.class, r.getInstanceIdentifier().getKey().getId().getValue()))
98                 .augmentation(Protocol1.class).child(Bgp.class).child(Neighbors.class).child(Neighbor.class, neighbor.getKey());
99         bgpDeployer.writeConfiguration(neighbor, neighborIId);
100         //get rib instance service, use filter
101         final WaitingServiceTracker<BGPPeerRuntimeMXBean> peerTracker = WaitingServiceTracker.create(BGPPeerRuntimeMXBean.class,
102                 this.bundleContext, "(" + InstanceType.PEER.getBeanName() + "=" + Ipv4Util.toStringIP(getHost()) + ")");
103         final BGPPeerRuntimeMXBean peer = peerTracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);
104         final BGPPeerRuntimeRegistration runtimeRegistration = getRootRuntimeBeanRegistratorWrapper().register(peer);
105         return Reflection.newProxy(AutoCloseablePeer.class, new AbstractInvocationHandler() {
106             @Override
107             protected Object handleInvocation(final Object proxy, final Method method, final Object[] args) throws Throwable {
108                 if (method.getName().equals("close")) {
109                     runtimeRegistration.close();
110                     bgpDeployerTracker.close();
111                     peerTracker.close();
112                     return null;
113                 } else {
114                     return method.invoke(peer, args);
115                 }
116             }
117         });
118     }
119
120     private static boolean isPasswordPresent(final Rfc2385Key password) {
121         return password != null && ! password.getValue().isEmpty();
122     }
123
124     public void setBundleContext(final BundleContext bundleContext) {
125         this.bundleContext = bundleContext;
126     }
127
128     private static interface AutoCloseablePeer extends RIB, AutoCloseable {
129     }
130
131 }