2 * Copyright (c) 2013 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.controller.netconf.confignetconfconnector.osgi;
11 import com.google.common.base.Optional;
12 import java.lang.management.ManagementFactory;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.HashSet;
17 import javax.management.MBeanServer;
18 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
19 import org.opendaylight.controller.netconf.api.Capability;
20 import org.opendaylight.controller.netconf.api.monitoring.CapabilityListener;
21 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
22 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class NetconfOperationServiceFactoryImpl implements NetconfOperationServiceFactory {
29 public static final int ATTEMPT_TIMEOUT_MS = 1000;
30 private static final int SILENT_ATTEMPTS = 30;
32 private final YangStoreService yangStoreService;
33 private final ConfigRegistryJMXClient jmxClient;
35 private static final Logger LOG = LoggerFactory.getLogger(NetconfOperationServiceFactoryImpl.class);
37 public NetconfOperationServiceFactoryImpl(YangStoreService yangStoreService) {
38 this(yangStoreService, ManagementFactory.getPlatformMBeanServer());
41 public NetconfOperationServiceFactoryImpl(YangStoreService yangStoreService, MBeanServer mBeanServer) {
42 this.yangStoreService = yangStoreService;
44 ConfigRegistryJMXClient configRegistryJMXClient;
46 // Config registry might not be present yet, but will be eventually
50 configRegistryJMXClient = new ConfigRegistryJMXClient(mBeanServer);
52 } catch (IllegalStateException e) {
54 if (i > SILENT_ATTEMPTS) {
55 LOG.info("JMX client not created after {} attempts, still trying", i, e);
57 LOG.debug("JMX client could not be created, reattempting, try {}", i, e);
60 Thread.sleep(ATTEMPT_TIMEOUT_MS);
61 } catch (InterruptedException e1) {
62 Thread.currentThread().interrupt();
63 throw new IllegalStateException("Interrupted while reattempting connection", e1);
68 jmxClient = configRegistryJMXClient;
69 if (i > SILENT_ATTEMPTS) {
70 LOG.info("Created JMX client after {} attempts", i);
72 LOG.debug("Created JMX client after {} attempts", i);
77 public NetconfOperationServiceImpl createService(String netconfSessionIdForReporting) {
78 return new NetconfOperationServiceImpl(yangStoreService, jmxClient, netconfSessionIdForReporting);
83 public Set<Capability> getCapabilities() {
84 return setupCapabilities(yangStoreService);
88 public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
89 return yangStoreService.registerCapabilityListener(listener);
92 public static Set<Capability> setupCapabilities(final YangStoreContext yangStoreSnapshot) {
93 Set<Capability> capabilities = new HashSet<>();
94 // [RFC6241] 8.3. Candidate Configuration Capability
95 capabilities.add(new BasicCapability("urn:ietf:params:netconf:capability:candidate:1.0"));
97 // TODO rollback on error not supported EditConfigXmlParser:100
98 // [RFC6241] 8.5. Rollback-on-Error Capability
99 // capabilities.add(new BasicCapability("urn:ietf:params:netconf:capability:rollback-on-error:1.0"));
101 Set<Module> modules = yangStoreSnapshot.getModules();
102 for (Module module : modules) {
103 capabilities.add(new YangStoreCapability(module, yangStoreSnapshot.getModuleSource(module)));
109 private static class BasicCapability implements Capability {
111 private final String capability;
113 private BasicCapability(final String capability) {
114 this.capability = capability;
118 public String getCapabilityUri() {
123 public Optional<String> getModuleNamespace() {
124 return Optional.absent();
128 public Optional<String> getModuleName() {
129 return Optional.absent();
133 public Optional<String> getRevision() {
134 return Optional.absent();
138 public Optional<String> getCapabilitySchema() {
139 return Optional.absent();
143 public Collection<String> getLocation() {
144 return Collections.emptyList();
148 public String toString() {
153 static final class YangStoreCapability extends BasicCapability {
155 private final String content;
156 private final String revision;
157 private final String moduleName;
158 private final String moduleNamespace;
160 public YangStoreCapability(final Module module, final String moduleContent) {
161 super(toCapabilityURI(module));
162 this.content = moduleContent;
163 this.moduleName = module.getName();
164 this.moduleNamespace = module.getNamespace().toString();
165 this.revision = Util.writeDate(module.getRevision());
169 public Optional<String> getCapabilitySchema() {
170 return Optional.of(content);
173 private static String toCapabilityURI(final Module module) {
174 return String.valueOf(module.getNamespace()) + "?module="
175 + module.getName() + "&revision=" + Util.writeDate(module.getRevision());
179 public Optional<String> getModuleName() {
180 return Optional.of(moduleName);
184 public Optional<String> getModuleNamespace() {
185 return Optional.of(moduleNamespace);
189 public Optional<String> getRevision() {
190 return Optional.of(revision);