2 * Copyright (c) 2014 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
9 package org.opendaylight.lispflowmapping.netconf.impl;
11 import java.lang.management.ManagementFactory;
14 import javax.management.InstanceAlreadyExistsException;
15 import javax.management.InstanceNotFoundException;
16 import javax.management.MBeanServer;
17 import javax.management.ObjectName;
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;
35 public class LispNetconfConnector {
36 private ConfigRegistryJMXClient configRegistryClient;
38 private static final Logger LOG = LoggerFactory.getLogger(LispNetconfConnector.class);
40 private MBeanServer platformMBeanServer;
42 public LispNetconfConnector() {
44 // Obtain the platform's MBeanServer (should've been previously created)
45 // and create a ConfigRegistry JMX client via which modules can be created
47 platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
48 configRegistryClient = new ConfigRegistryJMXClient(platformMBeanServer);
53 * Build a sal-netconf-connector to device using given credentials. Module instantiation and dependency resolution
54 * are done via a JMX ConfigTransactionClient
60 * @throws InstanceAlreadyExistsException
61 * @throws ConflictingVersionException
62 * @throws ValidationException
64 public void createNetconfConnector(String instanceName, Host host, Integer port, String username, String password) throws InstanceAlreadyExistsException, ConflictingVersionException, ValidationException {
66 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
68 if (transaction == null) {
69 LOG.error("Could not create transaction with ConfigRegistry! Cannot build NETCONF connector!");
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);
78 mxBean.setAddress(host);
79 mxBean.setPassword(password);
80 mxBean.setPort(new PortNumber(port));
81 mxBean.setUsername(username);
82 mxBean.setTcpOnly(false);
84 if (solveDependencies(transaction, mxBean) != true) {
85 LOG.error("Failed to solve dependencies! Aborting!");
93 public void removeNetconfConnector(String instanceName) throws InstanceNotFoundException, ValidationException, ConflictingVersionException {
94 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
95 transaction.destroyModule(NetconfConnectorModuleFactory.NAME, instanceName);
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.
107 private boolean solveDependencies(ConfigTransactionJMXClient transaction, NetconfConnectorModuleMXBean mxBean) {
109 ObjectName bindingBrokerRegistry = findConfigBean(BindingBrokerImplModuleFactory.NAME, transaction);
110 if (bindingBrokerRegistry != null ) {
111 mxBean.setBindingRegistry(bindingBrokerRegistry);
113 LOG.debug("No BindingBroker instance found");
117 ObjectName domRegistry = findConfigBean(DomBrokerImplModuleFactory.NAME, transaction);
118 if (domRegistry != null) {
119 mxBean.setDomRegistry(domRegistry);
121 LOG.debug("No DomRegistryBroker instance found");
125 ObjectName eventExecutor = findConfigBean(GlobalEventExecutorModuleFactory.NAME, transaction);
126 if (eventExecutor != null) {
127 mxBean.setEventExecutor(eventExecutor);
129 LOG.debug("No EventExecutor instance found");
133 ObjectName threadpool = findConfigBean(FlexibleThreadPoolModuleFactory.NAME, transaction);
134 if (threadpool != null) {
135 mxBean.setProcessingExecutor(threadpool);
137 LOG.debug("No ThreadPool instance found");
141 ObjectName clientDispatcher = findConfigBean(NetconfClientDispatcherModuleFactory.NAME, transaction);
142 if (clientDispatcher != null) {
143 mxBean.setClientDispatcher(clientDispatcher);
145 LOG.debug("No ClientDispatcher instance found");
154 * Uses a ConfigTransactionJMXClient to find the first object name of an already instantiated MBean
155 * based on the string name of the class.
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();