Create pcep topology parent aggregator
[bgpcep.git] / pcep / topology / topology-provider / src / main / java / org / opendaylight / controller / config / yang / pcep / topology / provider / PCEPTopologyProviderModule.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: config-pcep-topology-provider  yang module local name: pcep-topology-impl
12  * Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator
13  * Generated at: Mon Nov 18 21:08:29 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.pcep.topology.provider;
18
19 import static org.opendaylight.bgpcep.pcep.topology.provider.config.PCEPTopologyProviderUtil.contructKeys;
20
21 import com.google.common.base.Optional;
22 import com.google.common.base.Preconditions;
23 import com.google.common.net.InetAddresses;
24 import com.google.common.reflect.AbstractInvocationHandler;
25 import com.google.common.reflect.Reflection;
26 import io.netty.channel.epoll.Epoll;
27 import java.lang.reflect.Method;
28 import java.net.InetAddress;
29 import java.net.InetSocketAddress;
30 import org.opendaylight.bgpcep.pcep.topology.provider.PCEPTopologyProvider;
31 import org.opendaylight.bgpcep.pcep.topology.provider.config.PCEPTopologyDeployer;
32 import org.opendaylight.bgpcep.topology.DefaultTopologyReference;
33 import org.opendaylight.bgpcep.topology.TopologyReference;
34 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
35 import org.opendaylight.controller.config.api.osgi.WaitingServiceTracker;
36 import org.opendaylight.protocol.concepts.KeyMapping;
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.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
39 import org.osgi.framework.BundleContext;
40
41 /**
42  * @deprecated
43  */
44 public final class PCEPTopologyProviderModule extends
45     org.opendaylight.controller.config.yang.pcep.topology.provider.AbstractPCEPTopologyProviderModule {
46
47     private static final String IS_NOT_SET = "is not set.";
48     private static final String NATIVE_TRANSPORT_NOT_AVAILABLE = "Client is configured with password but native" +
49         " transport is not available";
50     private BundleContext bundleContext;
51
52     public PCEPTopologyProviderModule(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 PCEPTopologyProviderModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
58         final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
59         final PCEPTopologyProviderModule oldModule, final java.lang.AutoCloseable oldInstance) {
60         super(identifier, dependencyResolver, oldModule, oldInstance);
61     }
62
63     @Override
64     public void customValidation() {
65         JmxAttributeValidationException.checkNotNull(getTopologyId(), IS_NOT_SET, topologyIdJmxAttribute);
66         JmxAttributeValidationException.checkNotNull(getListenAddress(), IS_NOT_SET, listenAddressJmxAttribute);
67         JmxAttributeValidationException.checkNotNull(getListenPort(), IS_NOT_SET, listenPortJmxAttribute);
68         JmxAttributeValidationException.checkNotNull(getStatefulPlugin(), IS_NOT_SET, statefulPluginJmxAttribute);
69         JmxAttributeValidationException.checkNotNull(getRpcTimeout(), IS_NOT_SET, rpcTimeoutJmxAttribute);
70
71         final KeyMapping keys = contructKeys(getClient());
72         if (!keys.isEmpty()) {
73             JmxAttributeValidationException.checkCondition(Epoll.isAvailable(), NATIVE_TRANSPORT_NOT_AVAILABLE,
74                 clientJmxAttribute);
75         }
76     }
77
78     private InetAddress listenAddress() {
79         final IpAddress a = getListenAddress();
80         Preconditions.checkArgument(a.getIpv4Address() != null || a.getIpv6Address() != null,
81             "Address %s not supported", a);
82         if (a.getIpv4Address() != null) {
83             return InetAddresses.forString(a.getIpv4Address().getValue());
84         }
85         return InetAddresses.forString(a.getIpv6Address().getValue());
86     }
87
88     @Override
89     public java.lang.AutoCloseable createInstance() {
90         final WaitingServiceTracker<PCEPTopologyDeployer> pcepcTopologyDeployerTracker =
91             WaitingServiceTracker.create(PCEPTopologyDeployer.class, this.bundleContext);
92         final PCEPTopologyDeployer pcepcTopologyDeployer = pcepcTopologyDeployerTracker
93             .waitForService(WaitingServiceTracker.FIVE_MINUTES);
94
95         final TopologyId topologyID = getTopologyId();
96         final KeyMapping keys = contructKeys(getClient());
97
98         final InetSocketAddress inetSocketAddress = new InetSocketAddress(listenAddress(), getListenPort().getValue());
99
100         pcepcTopologyDeployer.createTopologyProvider(topologyID, inetSocketAddress, getRpcTimeout(), keys,
101             getSchedulerDependency(), Optional.fromNullable(getRootRuntimeBeanRegistratorWrapper()));
102
103         final WaitingServiceTracker<DefaultTopologyReference> defaultTopologyReferenceTracker =
104             WaitingServiceTracker.create(DefaultTopologyReference.class, this.bundleContext,
105                 "(" + PCEPTopologyProvider.class.getName() + "=" + topologyID.getValue() + ")");
106         final DefaultTopologyReference defaultTopologyReference = defaultTopologyReferenceTracker
107             .waitForService(WaitingServiceTracker.FIVE_MINUTES);
108
109         return Reflection.newProxy(PcepTopologyProviderCloseable.class, new AbstractInvocationHandler() {
110             @Override
111             protected Object handleInvocation(final Object proxy, final Method method, final Object[] args)
112                 throws Throwable {
113                 if (method.getName().equals("close")) {
114                     pcepcTopologyDeployer.removeTopologyProvider(topologyID);
115                     pcepcTopologyDeployerTracker.close();
116                     defaultTopologyReferenceTracker.close();
117                     return null;
118                 } else {
119                     return method.invoke(defaultTopologyReference, args);
120                 }
121             }
122         });
123     }
124
125     void setBundleContext(final BundleContext bundleContext) {
126         this.bundleContext = bundleContext;
127     }
128
129     private interface PcepTopologyProviderCloseable extends TopologyReference, AutoCloseable {
130
131     }
132 }