Add netconf.api.CapabilityURN
[netconf.git] / plugins / netconf-server-mdsal / src / main / java / org / opendaylight / netconf / server / mdsal / MdsalNetconfOperationServiceFactory.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.server.mdsal;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.io.CharStreams;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.InputStreamReader;
16 import java.nio.charset.StandardCharsets;
17 import java.util.HashSet;
18 import java.util.Optional;
19 import java.util.Set;
20 import java.util.concurrent.ExecutionException;
21 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
22 import org.opendaylight.mdsal.dom.api.DOMRpcService;
23 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
24 import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
25 import org.opendaylight.netconf.api.CapabilityURN;
26 import org.opendaylight.netconf.server.api.monitoring.BasicCapability;
27 import org.opendaylight.netconf.server.api.monitoring.Capability;
28 import org.opendaylight.netconf.server.api.monitoring.CapabilityListener;
29 import org.opendaylight.netconf.server.api.monitoring.YangModuleCapability;
30 import org.opendaylight.netconf.server.api.operations.NetconfOperationService;
31 import org.opendaylight.netconf.server.api.operations.NetconfOperationServiceFactory;
32 import org.opendaylight.netconf.server.api.operations.NetconfOperationServiceFactoryListener;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
34 import org.opendaylight.yangtools.concepts.Registration;
35 import org.opendaylight.yangtools.yang.common.Revision;
36 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37 import org.opendaylight.yangtools.yang.model.api.ModuleLike;
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.osgi.service.component.annotations.Activate;
42 import org.osgi.service.component.annotations.Component;
43 import org.osgi.service.component.annotations.Deactivate;
44 import org.osgi.service.component.annotations.Reference;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 @Component(service = NetconfOperationServiceFactory.class, immediate = true, property = "type=mdsal-netconf-connector")
49 public final class MdsalNetconfOperationServiceFactory implements NetconfOperationServiceFactory, AutoCloseable {
50     private static final Logger LOG = LoggerFactory.getLogger(MdsalNetconfOperationServiceFactory.class);
51     private static final BasicCapability VALIDATE_CAPABILITY = new BasicCapability(CapabilityURN.VALIDATE);
52
53     private final DOMDataBroker dataBroker;
54     private final DOMRpcService rpcService;
55
56     private final CurrentSchemaContext currentSchemaContext;
57     private final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency;
58     private final NetconfOperationServiceFactoryListener netconfOperationServiceFactoryListener;
59
60     @Activate
61     public MdsalNetconfOperationServiceFactory(@Reference final DOMSchemaService schemaService,
62             @Reference final DOMDataBroker dataBroker, @Reference final DOMRpcService rpcService,
63             @Reference(target = "(type=mapper-aggregator-registry)")
64             final NetconfOperationServiceFactoryListener netconfOperationServiceFactoryListener) {
65         this.dataBroker = requireNonNull(dataBroker);
66         this.rpcService = requireNonNull(rpcService);
67         this.netconfOperationServiceFactoryListener = requireNonNull(netconfOperationServiceFactoryListener);
68
69         rootSchemaSourceProviderDependency = schemaService.getExtensions()
70                 .getInstance(DOMYangTextSourceProvider.class);
71         currentSchemaContext = CurrentSchemaContext.create(requireNonNull(schemaService),
72                 rootSchemaSourceProviderDependency);
73         netconfOperationServiceFactoryListener.onAddNetconfOperationServiceFactory(this);
74     }
75
76     @Deactivate
77     @Override
78     public void close() {
79         netconfOperationServiceFactoryListener.onRemoveNetconfOperationServiceFactory(this);
80         currentSchemaContext.close();
81     }
82
83     @Override
84     public NetconfOperationService createService(final SessionIdType sessionId) {
85         return new MdsalNetconfOperationService(currentSchemaContext, sessionId, dataBroker, rpcService);
86     }
87
88     @Override
89     public Set<Capability> getCapabilities() {
90         // FIXME: cache returned set
91         return transformCapabilities(currentSchemaContext.getCurrentContext(), rootSchemaSourceProviderDependency);
92     }
93
94     // FIXME: ImmutableSet
95     static Set<Capability> transformCapabilities(
96             final EffectiveModelContext currentContext,
97             final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency) {
98         final var capabilities = new HashSet<Capability>();
99
100         // Added by netconf-impl by default
101         // capabilities.add(new BasicCapability(CapabilityURN.CANDIDATE));
102
103         // FIXME: rework in terms of ModuleEffectiveStatement
104         for (var module : currentContext.getModules()) {
105             moduleToCapability(module, rootSchemaSourceProviderDependency).ifPresent(capabilities::add);
106             for (var submodule : module.getSubmodules()) {
107                 moduleToCapability(submodule, rootSchemaSourceProviderDependency).ifPresent(capabilities::add);
108             }
109         }
110
111         return capabilities;
112     }
113
114     private static Optional<YangModuleCapability> moduleToCapability(final ModuleLike module,
115             final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency) {
116         final String moduleNamespace = module.getNamespace().toString();
117         final String moduleName = module.getName();
118         final String revision = module.getRevision().map(Revision::toString).orElse(null);
119         final SourceIdentifier moduleSourceIdentifier = new SourceIdentifier(moduleName, revision);
120
121         InputStream sourceStream = null;
122         String source;
123         try {
124             sourceStream = rootSchemaSourceProviderDependency.getSource(moduleSourceIdentifier).get().openStream();
125             source = CharStreams.toString(new InputStreamReader(sourceStream, StandardCharsets.UTF_8));
126         } catch (ExecutionException | InterruptedException | IOException e) {
127             LOG.warn("Ignoring source for module {}. Unable to read content", moduleSourceIdentifier, e);
128             source = null;
129         }
130
131         try {
132             if (sourceStream != null) {
133                 sourceStream.close();
134             }
135         } catch (IOException e) {
136             LOG.warn("Error closing yang source stream {}. Ignoring", moduleSourceIdentifier, e);
137         }
138
139         if (source != null) {
140             return Optional.of(new YangModuleCapability(moduleNamespace, moduleName, revision, source));
141         }
142
143         LOG.warn("Missing source for module {}. This module will not be available from netconf server",
144             moduleSourceIdentifier);
145         return Optional.empty();
146     }
147
148     @Override
149     public Registration registerCapabilityListener(final CapabilityListener listener) {
150         // Advertise validate capability only if DOMDataBroker provides DOMDataTransactionValidator
151         if (dataBroker.getExtensions().get(DOMDataTransactionValidator.class) != null) {
152             // FIXME: support VALIDATE_1_1 as well!
153             listener.onCapabilitiesChanged(Set.of(VALIDATE_CAPABILITY), Set.of());
154         }
155         // Advertise namespaces of supported YANG models as NETCONF capabilities
156         return currentSchemaContext.registerCapabilityListener(listener);
157     }
158 }