2 * Copyright (c) 2013, 2016 Cisco Systems, Inc. and others. All rights reserved.
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
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
15 * Do not modify this file unless it is present under src/main directory
17 package org.opendaylight.controller.config.yang.bgp.rib.impl;
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;
48 public final class BGPPeerModule extends org.opendaylight.controller.config.yang.bgp.rib.impl.AbstractBGPPeerModule {
50 private BundleContext bundleContext;
52 public BGPPeerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
53 final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
54 super(identifier, dependencyResolver);
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) {
61 super(identifier, dependencyResolver, oldModule, oldInstance);
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);
71 JmxAttributeValidationException.checkNotNull(getPort(), "value is not set.", portJmxAttribute);
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);
79 if (isPasswordPresent(getPassword())) {
80 JmxAttributeValidationException.checkCondition(Epoll.isAvailable(),
81 "BGP Peer is configured with password but native transport is not available", passwordJmxAttribute);
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() {
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();
114 return method.invoke(peer, args);
120 private static boolean isPasswordPresent(final Rfc2385Key password) {
121 return password != null && ! password.getValue().isEmpty();
124 public void setBundleContext(final BundleContext bundleContext) {
125 this.bundleContext = bundleContext;
128 private static interface AutoCloseablePeer extends RIB, AutoCloseable {