Improve NETCONF session ID handling
[netconf.git] / plugins / netconf-server-mdsal / src / main / java / org / opendaylight / netconf / mdsal / connector / 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.mdsal.connector;
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.capability.BasicCapability;
26 import org.opendaylight.netconf.api.capability.Capability;
27 import org.opendaylight.netconf.api.capability.YangModuleCapability;
28 import org.opendaylight.netconf.server.api.monitoring.CapabilityListener;
29 import org.opendaylight.netconf.server.api.operations.NetconfOperationService;
30 import org.opendaylight.netconf.server.api.operations.NetconfOperationServiceFactory;
31 import org.opendaylight.netconf.server.api.operations.NetconfOperationServiceFactoryListener;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
33 import org.opendaylight.yangtools.concepts.Registration;
34 import org.opendaylight.yangtools.yang.common.Revision;
35 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
36 import org.opendaylight.yangtools.yang.model.api.ModuleLike;
37 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
38 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
39 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
40 import org.osgi.service.component.annotations.Activate;
41 import org.osgi.service.component.annotations.Component;
42 import org.osgi.service.component.annotations.Deactivate;
43 import org.osgi.service.component.annotations.Reference;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 @Component(service = NetconfOperationServiceFactory.class, immediate = true, property = "type=mdsal-netconf-connector")
48 public final class MdsalNetconfOperationServiceFactory implements NetconfOperationServiceFactory, AutoCloseable {
49     private static final Logger LOG = LoggerFactory.getLogger(MdsalNetconfOperationServiceFactory.class);
50     private static final BasicCapability VALIDATE_CAPABILITY =
51             new BasicCapability("urn:ietf:params:netconf:capability:validate:1.0");
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("urn:ietf:params:netconf:capability:candidate:1.0"));
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 SourceIdentifier moduleSourceIdentifier = new SourceIdentifier(module.getName(),
117                 module.getRevision().map(Revision::toString).orElse(null));
118
119         InputStream sourceStream = null;
120         String source;
121         try {
122             sourceStream = rootSchemaSourceProviderDependency.getSource(moduleSourceIdentifier).get().openStream();
123             source = CharStreams.toString(new InputStreamReader(sourceStream, StandardCharsets.UTF_8));
124         } catch (ExecutionException | InterruptedException | IOException e) {
125             LOG.warn("Ignoring source for module {}. Unable to read content", moduleSourceIdentifier, e);
126             source = null;
127         }
128
129         try {
130             if (sourceStream != null) {
131                 sourceStream.close();
132             }
133         } catch (IOException e) {
134             LOG.warn("Error closing yang source stream {}. Ignoring", moduleSourceIdentifier, e);
135         }
136
137         if (source != null) {
138             return Optional.of(new YangModuleCapability(module, source));
139         }
140
141         LOG.warn("Missing source for module {}. This module will not be available from netconf server",
142             moduleSourceIdentifier);
143         return Optional.empty();
144     }
145
146     @Override
147     public Registration registerCapabilityListener(final CapabilityListener listener) {
148         // Advertise validate capability only if DOMDataBroker provides DOMDataTransactionValidator
149         if (dataBroker.getExtensions().get(DOMDataTransactionValidator.class) != null) {
150             listener.onCapabilitiesChanged(Set.of(VALIDATE_CAPABILITY), Set.of());
151         }
152         // Advertise namespaces of supported YANG models as NETCONF capabilities
153         return currentSchemaContext.registerCapabilityListener(listener);
154     }
155 }