Bug 5061: Integrate RIBImplModule with blueprint
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / controller / config / yang / bgp / rib / impl / RIBImplModule.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 /**
9  * Generated file
10
11  * Generated from: yang module name: bgp-rib-impl  yang module local name: rib-impl
12  * Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator
13  * Generated at: Wed Nov 06 13:02:32 CET 2013
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 java.lang.reflect.Method;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.stream.Collectors;
26 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
27 import org.opendaylight.controller.config.api.osgi.WaitingServiceTracker;
28 import org.opendaylight.protocol.bgp.mode.api.PathSelectionMode;
29 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPBestPathSelection;
30 import org.opendaylight.protocol.bgp.rib.impl.spi.BgpDeployer;
31 import org.opendaylight.protocol.bgp.rib.impl.spi.InstanceType;
32 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
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.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
38 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
39 import org.osgi.framework.BundleContext;
40
41 /**
42  *
43  */
44 @Deprecated
45 public final class RIBImplModule extends org.opendaylight.controller.config.yang.bgp.rib.impl.AbstractRIBImplModule {
46
47     private static final String IS_NOT_SET = "is not set.";
48     private BundleContext bundleContext;
49
50     public RIBImplModule(final org.opendaylight.controller.config.api.ModuleIdentifier name,
51             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
52         super(name, dependencyResolver);
53     }
54
55     public RIBImplModule(final org.opendaylight.controller.config.api.ModuleIdentifier name,
56             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, final RIBImplModule oldModule,
57             final java.lang.AutoCloseable oldInstance) {
58         super(name, dependencyResolver, oldModule, oldInstance);
59     }
60
61     @Override
62     public void customValidation() {
63         JmxAttributeValidationException.checkNotNull(getExtensions(), IS_NOT_SET, extensionsJmxAttribute);
64         JmxAttributeValidationException.checkNotNull(getRibId(), IS_NOT_SET, ribIdJmxAttribute);
65         JmxAttributeValidationException.checkNotNull(getLocalAs(), IS_NOT_SET, localAsJmxAttribute);
66         JmxAttributeValidationException.checkNotNull(getBgpRibId(), IS_NOT_SET, bgpRibIdJmxAttribute);
67         JmxAttributeValidationException.checkNotNull(getLocalTable(), IS_NOT_SET, localTableJmxAttribute);
68     }
69
70     @Override
71     public java.lang.AutoCloseable createInstance() {
72         final WaitingServiceTracker<BgpDeployer> bgpDeployerTracker =
73                 WaitingServiceTracker.create(BgpDeployer.class, this.bundleContext);
74         final BgpDeployer bgpDeployer = bgpDeployerTracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);
75         //map configuration to OpenConfig BGP
76         final Protocol protocol = bgpDeployer.getMappingService().fromRib(getBgpRibId(), getClusterId(), getRibId(), new AsNumber(getLocalAs()), getLocalTableDependency(),
77                 mapBestPathSelectionStrategyByFamily(getRibPathSelectionModeDependency()));
78         //write to configuration DS
79         final KeyedInstanceIdentifier<Protocol, ProtocolKey> protocolIId = bgpDeployer.getInstanceIdentifier().child(Protocols.class).child(Protocol.class,
80                 protocol.getKey());
81         bgpDeployer.writeConfiguration(protocol, protocolIId);
82         //get rib instance service, use filter
83         final WaitingServiceTracker<RIB> ribTracker = WaitingServiceTracker.create(RIB.class,
84                 this.bundleContext, "(" + InstanceType.RIB.getBeanName() + "=" + getRibId().getValue() + ")");
85         final RIB rib = ribTracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);
86         final RIBImplRuntimeRegistration register = getRootRuntimeBeanRegistratorWrapper().register(rib.getRenderStats());
87         return Reflection.newProxy(AutoCloseableRIB.class, new AbstractInvocationHandler() {
88             @Override
89             protected Object handleInvocation(final Object proxy, final Method method, final Object[] args) throws Throwable {
90                 if (method.getName().equals("close")) {
91                     register.close();
92                     bgpDeployerTracker.close();
93                     ribTracker.close();
94                     return null;
95                 } else {
96                     return method.invoke(rib, args);
97                 }
98             }
99         });
100     }
101
102     public void setBundleContext(final BundleContext bundleContext) {
103         this.bundleContext = bundleContext;
104     }
105
106     private Map<TablesKey, PathSelectionMode> mapBestPathSelectionStrategyByFamily(final List<BGPBestPathSelection> bestPathSelectionDependency) {
107         return Collections.unmodifiableMap(bestPathSelectionDependency.stream().collect(
108                 Collectors.<BGPBestPathSelection, TablesKey, PathSelectionMode>toMap(st -> new TablesKey(st.getAfi(), st.getSafi()), st -> st.getStrategy())));
109     }
110
111     private static interface AutoCloseableRIB extends RIB, AutoCloseable {
112     }
113
114
115 }