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 final 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 private 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 = CurrentSchemaContext.create(requireNonNull(schemaService),
69 rootSchemaSourceProviderDependency);
70 this.netconfOperationServiceFactoryListener = netconfOperationServiceFactoryListener;
73 // keep spotbugs from complaining about overridable method in constructor
74 public static MdsalNetconfOperationServiceFactory create(
75 final DOMSchemaService schemaService,
76 final NetconfOperationServiceFactoryListener netconfOperationServiceFactoryListener,
77 final DOMDataBroker dataBroker,
78 final DOMRpcService rpcService) {
80 var factory = new MdsalNetconfOperationServiceFactory(schemaService, netconfOperationServiceFactoryListener,
81 dataBroker, rpcService);
82 netconfOperationServiceFactoryListener.onAddNetconfOperationServiceFactory(factory);
88 public MdsalNetconfOperationService createService(final String netconfSessionIdForReporting) {
89 checkState(dataBroker != null, "MD-SAL provider not yet initialized");
90 return new MdsalNetconfOperationService(currentSchemaContext, netconfSessionIdForReporting, dataBroker,
94 @SuppressWarnings("checkstyle:IllegalCatch")
98 currentSchemaContext.close();
99 if (netconfOperationServiceFactoryListener != null) {
100 netconfOperationServiceFactoryListener.onRemoveNetconfOperationServiceFactory(this);
102 } catch (Exception e) {
103 LOG.error("Failed to close resources correctly - ignore", e);
108 public Set<Capability> getCapabilities() {
109 return transformCapabilities(currentSchemaContext.getCurrentContext(), rootSchemaSourceProviderDependency);
112 static Set<Capability> transformCapabilities(
113 final SchemaContext currentContext,
114 final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency) {
115 final Set<Capability> capabilities = new HashSet<>();
117 // Added by netconf-impl by default
118 // capabilities.add(new BasicCapability("urn:ietf:params:netconf:capability:candidate:1.0"));
120 for (final Module module : currentContext.getModules()) {
121 Optional<YangModuleCapability> cap = moduleToCapability(module, rootSchemaSourceProviderDependency);
122 if (cap.isPresent()) {
123 capabilities.add(cap.get());
125 for (final Submodule submodule : module.getSubmodules()) {
126 cap = moduleToCapability(submodule, rootSchemaSourceProviderDependency);
127 if (cap.isPresent()) {
128 capabilities.add(cap.get());
136 private static Optional<YangModuleCapability> moduleToCapability(final ModuleLike module,
137 final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency) {
138 final SourceIdentifier moduleSourceIdentifier = RevisionSourceIdentifier.create(module.getName(),
139 module.getRevision());
141 InputStream sourceStream = null;
144 sourceStream = rootSchemaSourceProviderDependency.getSource(moduleSourceIdentifier).get().openStream();
145 source = CharStreams.toString(new InputStreamReader(sourceStream, StandardCharsets.UTF_8));
146 } catch (ExecutionException | InterruptedException | IOException e) {
147 LOG.warn("Ignoring source for module {}. Unable to read content", moduleSourceIdentifier, e);
152 if (sourceStream != null) {
153 sourceStream.close();
155 } catch (IOException e) {
156 LOG.warn("Error closing yang source stream {}. Ignoring", moduleSourceIdentifier, e);
159 if (source != null) {
160 return Optional.of(new YangModuleCapability(module, source));
163 LOG.warn("Missing source for module {}. This module will not be available from netconf server",
164 moduleSourceIdentifier);
165 return Optional.empty();
169 public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
170 // Advertise validate capability only if DOMDataBroker provides DOMDataTransactionValidator
171 if (dataBroker.getExtensions().get(DOMDataTransactionValidator.class) != null) {
172 listener.onCapabilitiesChanged(Collections.singleton(VALIDATE_CAPABILITY), Collections.emptySet());
174 // Advertise namespaces of supported YANG models as NETCONF capabilities
175 return currentSchemaContext.registerCapabilityListener(listener);