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.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
36 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
37 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
38 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class MdsalNetconfOperationServiceFactory implements NetconfOperationServiceFactory, AutoCloseable {
44 private static final Logger LOG = LoggerFactory.getLogger(MdsalNetconfOperationServiceFactory.class);
45 private static final BasicCapability VALIDATE_CAPABILITY =
46 new BasicCapability("urn:ietf:params:netconf:capability:validate:1.0");
48 private final DOMDataBroker dataBroker;
49 private final DOMRpcService rpcService;
51 private final CurrentSchemaContext currentSchemaContext;
52 private final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency;
53 private final NetconfOperationServiceFactoryListener netconfOperationServiceFactoryListener;
55 public MdsalNetconfOperationServiceFactory(
56 final DOMSchemaService schemaService,
57 final NetconfOperationServiceFactoryListener netconfOperationServiceFactoryListener,
58 final DOMDataBroker dataBroker,
59 final DOMRpcService rpcService) {
61 this.dataBroker = dataBroker;
62 this.rpcService = rpcService;
64 this.rootSchemaSourceProviderDependency = schemaService.getExtensions()
65 .getInstance(DOMYangTextSourceProvider.class);
66 this.currentSchemaContext = new CurrentSchemaContext(requireNonNull(schemaService),
67 rootSchemaSourceProviderDependency);
68 this.netconfOperationServiceFactoryListener = netconfOperationServiceFactoryListener;
69 this.netconfOperationServiceFactoryListener.onAddNetconfOperationServiceFactory(this);
73 public MdsalNetconfOperationService createService(final String netconfSessionIdForReporting) {
74 checkState(dataBroker != null, "MD-SAL provider not yet initialized");
75 return new MdsalNetconfOperationService(currentSchemaContext, netconfSessionIdForReporting, dataBroker,
79 @SuppressWarnings("checkstyle:IllegalCatch")
83 currentSchemaContext.close();
84 if (netconfOperationServiceFactoryListener != null) {
85 netconfOperationServiceFactoryListener.onRemoveNetconfOperationServiceFactory(this);
87 } catch (Exception e) {
88 LOG.error("Failed to close resources correctly - ignore", e);
93 public Set<Capability> getCapabilities() {
94 return transformCapabilities(currentSchemaContext.getCurrentContext(), rootSchemaSourceProviderDependency);
97 static Set<Capability> transformCapabilities(
98 final SchemaContext currentContext,
99 final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency) {
100 final Set<Capability> capabilities = new HashSet<>();
102 // Added by netconf-impl by default
103 // capabilities.add(new BasicCapability("urn:ietf:params:netconf:capability:candidate:1.0"));
105 for (final Module module : currentContext.getModules()) {
106 Optional<YangModuleCapability> cap = moduleToCapability(module, rootSchemaSourceProviderDependency);
107 if (cap.isPresent()) {
108 capabilities.add(cap.get());
110 for (final Module submodule : module.getSubmodules()) {
111 cap = moduleToCapability(submodule, rootSchemaSourceProviderDependency);
112 if (cap.isPresent()) {
113 capabilities.add(cap.get());
121 private static Optional<YangModuleCapability> moduleToCapability(
122 final Module module, final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency) {
124 final SourceIdentifier moduleSourceIdentifier = RevisionSourceIdentifier.create(module.getName(),
125 module.getRevision());
127 InputStream sourceStream = null;
130 sourceStream = rootSchemaSourceProviderDependency.getSource(moduleSourceIdentifier).get().openStream();
131 source = CharStreams.toString(new InputStreamReader(sourceStream, StandardCharsets.UTF_8));
132 } catch (ExecutionException | InterruptedException | IOException e) {
133 LOG.warn("Ignoring source for module {}. Unable to read content", moduleSourceIdentifier, e);
138 if (sourceStream != null) {
139 sourceStream.close();
141 } catch (IOException e) {
142 LOG.warn("Error closing yang source stream {}. Ignoring", moduleSourceIdentifier, e);
145 if (source != null) {
146 return Optional.of(new YangModuleCapability(module, source));
149 LOG.warn("Missing source for module {}. This module will not be available from netconf server",
150 moduleSourceIdentifier);
151 return Optional.empty();
155 public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
156 // Advertise validate capability only if DOMDataBroker provides DOMDataTransactionValidator
157 if (dataBroker.getExtensions().get(DOMDataTransactionValidator.class) != null) {
158 listener.onCapabilitiesChanged(Collections.singleton(VALIDATE_CAPABILITY), Collections.emptySet());
160 // Advertise namespaces of supported YANG models as NETCONF capabilities
161 return currentSchemaContext.registerCapabilityListener(listener);