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
8 package org.opendaylight.netconf.mdsal.connector;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.io.CharStreams;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.InputStreamReader;
17 import java.nio.charset.StandardCharsets;
18 import java.util.Collections;
19 import java.util.HashSet;
20 import java.util.Optional;
22 import java.util.concurrent.ExecutionException;
23 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
24 import org.opendaylight.mdsal.dom.api.DOMRpcService;
25 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
26 import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
27 import org.opendaylight.netconf.api.capability.BasicCapability;
28 import org.opendaylight.netconf.api.capability.Capability;
29 import org.opendaylight.netconf.api.capability.YangModuleCapability;
30 import org.opendaylight.netconf.api.monitoring.CapabilityListener;
31 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
32 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.ModuleLike;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.api.Submodule;
37 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
38 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
39 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
40 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 public class MdsalNetconfOperationServiceFactory implements NetconfOperationServiceFactory, AutoCloseable {
46 private static final Logger LOG = LoggerFactory.getLogger(MdsalNetconfOperationServiceFactory.class);
47 private static final BasicCapability VALIDATE_CAPABILITY =
48 new BasicCapability("urn:ietf:params:netconf:capability:validate:1.0");
50 private final DOMDataBroker dataBroker;
51 private final DOMRpcService rpcService;
53 private final CurrentSchemaContext currentSchemaContext;
54 private final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency;
55 private final NetconfOperationServiceFactoryListener netconfOperationServiceFactoryListener;
57 public MdsalNetconfOperationServiceFactory(
58 final DOMSchemaService schemaService,
59 final NetconfOperationServiceFactoryListener netconfOperationServiceFactoryListener,
60 final DOMDataBroker dataBroker,
61 final DOMRpcService rpcService) {
63 this.dataBroker = dataBroker;
64 this.rpcService = rpcService;
66 this.rootSchemaSourceProviderDependency = schemaService.getExtensions()
67 .getInstance(DOMYangTextSourceProvider.class);
68 this.currentSchemaContext = new CurrentSchemaContext(requireNonNull(schemaService),
69 rootSchemaSourceProviderDependency);
70 this.netconfOperationServiceFactoryListener = netconfOperationServiceFactoryListener;
71 this.netconfOperationServiceFactoryListener.onAddNetconfOperationServiceFactory(this);
75 public MdsalNetconfOperationService createService(final String netconfSessionIdForReporting) {
76 checkState(dataBroker != null, "MD-SAL provider not yet initialized");
77 return new MdsalNetconfOperationService(currentSchemaContext, netconfSessionIdForReporting, dataBroker,
81 @SuppressWarnings("checkstyle:IllegalCatch")
85 currentSchemaContext.close();
86 if (netconfOperationServiceFactoryListener != null) {
87 netconfOperationServiceFactoryListener.onRemoveNetconfOperationServiceFactory(this);
89 } catch (Exception e) {
90 LOG.error("Failed to close resources correctly - ignore", e);
95 public Set<Capability> getCapabilities() {
96 return transformCapabilities(currentSchemaContext.getCurrentContext(), rootSchemaSourceProviderDependency);
99 static Set<Capability> transformCapabilities(
100 final SchemaContext currentContext,
101 final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency) {
102 final Set<Capability> capabilities = new HashSet<>();
104 // Added by netconf-impl by default
105 // capabilities.add(new BasicCapability("urn:ietf:params:netconf:capability:candidate:1.0"));
107 for (final Module module : currentContext.getModules()) {
108 Optional<YangModuleCapability> cap = moduleToCapability(module, rootSchemaSourceProviderDependency);
109 if (cap.isPresent()) {
110 capabilities.add(cap.get());
112 for (final Submodule submodule : module.getSubmodules()) {
113 cap = moduleToCapability(submodule, rootSchemaSourceProviderDependency);
114 if (cap.isPresent()) {
115 capabilities.add(cap.get());
123 private static Optional<YangModuleCapability> moduleToCapability(final ModuleLike module,
124 final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency) {
125 final SourceIdentifier moduleSourceIdentifier = RevisionSourceIdentifier.create(module.getName(),
126 module.getRevision());
128 InputStream sourceStream = null;
131 sourceStream = rootSchemaSourceProviderDependency.getSource(moduleSourceIdentifier).get().openStream();
132 source = CharStreams.toString(new InputStreamReader(sourceStream, StandardCharsets.UTF_8));
133 } catch (ExecutionException | InterruptedException | IOException e) {
134 LOG.warn("Ignoring source for module {}. Unable to read content", moduleSourceIdentifier, e);
139 if (sourceStream != null) {
140 sourceStream.close();
142 } catch (IOException e) {
143 LOG.warn("Error closing yang source stream {}. Ignoring", moduleSourceIdentifier, e);
146 if (source != null) {
147 return Optional.of(new YangModuleCapability(module, source));
150 LOG.warn("Missing source for module {}. This module will not be available from netconf server",
151 moduleSourceIdentifier);
152 return Optional.empty();
156 public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
157 // Advertise validate capability only if DOMDataBroker provides DOMDataTransactionValidator
158 if (dataBroker.getExtensions().get(DOMDataTransactionValidator.class) != null) {
159 listener.onCapabilitiesChanged(Collections.singleton(VALIDATE_CAPABILITY), Collections.emptySet());
161 // Advertise namespaces of supported YANG models as NETCONF capabilities
162 return currentSchemaContext.registerCapabilityListener(listener);