f9d59b709e801e51e6d81688142d613ad92cf491
[lispflowmapping.git] / mappingservice / netconf / src / main / java / org / opendaylight / lispflowmapping / netconf / impl / LispNetconfConnector.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.lispflowmapping.netconf.impl;
10
11 import java.lang.management.ManagementFactory;
12 import java.util.Set;
13
14 import javax.management.InstanceAlreadyExistsException;
15 import javax.management.InstanceNotFoundException;
16 import javax.management.MBeanServer;
17 import javax.management.ObjectName;
18
19 import org.opendaylight.controller.config.api.ConflictingVersionException;
20 import org.opendaylight.controller.config.api.ValidationException;
21 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
22 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
23 import org.opendaylight.controller.config.yang.md.sal.binding.impl.BindingBrokerImplModuleFactory;
24 import org.opendaylight.controller.config.yang.md.sal.connector.netconf.NetconfConnectorModuleFactory;
25 import org.opendaylight.controller.config.yang.md.sal.connector.netconf.NetconfConnectorModuleMXBean;
26 import org.opendaylight.controller.config.yang.md.sal.dom.impl.DomBrokerImplModuleFactory;
27 import org.opendaylight.controller.config.yang.netty.eventexecutor.GlobalEventExecutorModuleFactory;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
30 import org.opendaylight.controller.config.yang.threadpool.impl.flexible.FlexibleThreadPoolModuleFactory;
31 import org.opendaylight.controller.config.yang.config.netconf.client.dispatcher.NetconfClientDispatcherModuleFactory;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class LispNetconfConnector {
36     private ConfigRegistryJMXClient configRegistryClient;
37
38     private static final Logger LOG = LoggerFactory.getLogger(LispNetconfConnector.class);
39
40     private MBeanServer platformMBeanServer;
41
42     public LispNetconfConnector() {
43
44         // Obtain the platform's MBeanServer (should've been previously created)
45         // and create a ConfigRegistry JMX client via which modules can be created
46         // and destroyed
47         platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
48         configRegistryClient = new ConfigRegistryJMXClient(platformMBeanServer);
49
50     }
51
52     /**
53      * Build a sal-netconf-connector to device using given credentials. Module instantiation and dependency resolution
54      * are done via a JMX ConfigTransactionClient
55      * @param instanceName
56      * @param host
57      * @param port
58      * @param username
59      * @param password
60      * @throws InstanceAlreadyExistsException
61      * @throws ConflictingVersionException
62      * @throws ValidationException
63      */
64     public void createNetconfConnector(String instanceName, Host host, Integer port, String username, String password) throws InstanceAlreadyExistsException, ConflictingVersionException, ValidationException {
65
66         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
67
68         if (transaction == null) {
69             LOG.error("Could not create transaction with ConfigRegistry! Cannot build NETCONF connector!");
70             return;
71         }
72
73         // create sal-netconf-connector module and via an mxBean configure all
74         // yang defined parameters
75         ObjectName connName = transaction.createModule(NetconfConnectorModuleFactory.NAME, instanceName);
76         NetconfConnectorModuleMXBean mxBean = transaction.newMXBeanProxy(connName, NetconfConnectorModuleMXBean.class);
77
78         mxBean.setAddress(host);
79         mxBean.setPassword(password);
80         mxBean.setPort(new PortNumber(port));
81         mxBean.setUsername(username);
82         mxBean.setTcpOnly(false);
83
84         if (solveDependencies(transaction, mxBean) != true) {
85             LOG.error("Failed to solve dependencies! Aborting!");
86             return;
87         }
88
89         transaction.commit();
90
91     }
92
93     public void removeNetconfConnector(String instanceName) throws InstanceNotFoundException, ValidationException, ConflictingVersionException {
94         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
95         transaction.destroyModule(NetconfConnectorModuleFactory.NAME, instanceName);
96         transaction.commit();
97     }
98
99     /**
100      * Lookup sal-netconf-connector dependencies using a ConfigTransactionJMXClient and configure them for the module
101      * we are about to instantiate. As long as the netconf module in configuration/initial is loaded, all of
102      * dependencies should be solvable.
103      * @param transaction
104      * @param mxBean
105      * @return
106      */
107     private boolean solveDependencies(ConfigTransactionJMXClient transaction, NetconfConnectorModuleMXBean mxBean) {
108
109         ObjectName bindingBrokerRegistry = findConfigBean(BindingBrokerImplModuleFactory.NAME, transaction);
110         if (bindingBrokerRegistry != null ) {
111             mxBean.setBindingRegistry(bindingBrokerRegistry);
112         } else {
113             LOG.debug("No BindingBroker instance found");
114             return false;
115         }
116
117         ObjectName domRegistry = findConfigBean(DomBrokerImplModuleFactory.NAME, transaction);
118         if (domRegistry != null) {
119             mxBean.setDomRegistry(domRegistry);
120         } else {
121             LOG.debug("No DomRegistryBroker instance found");
122             return false;
123         }
124
125         ObjectName eventExecutor = findConfigBean(GlobalEventExecutorModuleFactory.NAME, transaction);
126         if (eventExecutor != null) {
127             mxBean.setEventExecutor(eventExecutor);
128         } else {
129             LOG.debug("No EventExecutor instance found");
130             return false;
131         }
132
133         ObjectName threadpool = findConfigBean(FlexibleThreadPoolModuleFactory.NAME, transaction);
134         if (threadpool != null) {
135             mxBean.setProcessingExecutor(threadpool);
136         } else {
137             LOG.debug("No ThreadPool instance found");
138             return false;
139         }
140
141         ObjectName clientDispatcher = findConfigBean(NetconfClientDispatcherModuleFactory.NAME, transaction);
142         if (clientDispatcher != null) {
143             mxBean.setClientDispatcher(clientDispatcher);
144         } else {
145             LOG.debug("No ClientDispatcher instance found");
146             return false;
147         }
148
149         return true;
150
151     }
152
153     /**
154      * Uses a ConfigTransactionJMXClient to find the first object name of an already instantiated MBean
155      * based on the string name of the class.
156      * @param name
157      * @param transaction
158      * @return
159      */
160     private ObjectName findConfigBean(String name, ConfigTransactionJMXClient transaction) {
161         Set<ObjectName> set = transaction.lookupConfigBeans(name);
162         if (set.size() > 0) {
163             return set.iterator().next();
164         } else {
165             return null;
166         }
167     }
168
169
170 }