Fix yang source provisioning from MD-SAL netconf northbound
[netconf.git] / opendaylight / netconf / mdsal-netconf-connector / 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
9 package org.opendaylight.netconf.mdsal.connector;
10
11 import com.google.common.base.Charsets;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.io.CharStreams;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.InputStreamReader;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.HashSet;
21 import java.util.Set;
22 import org.opendaylight.controller.config.util.capability.Capability;
23 import org.opendaylight.controller.config.util.capability.YangModuleCapability;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
25 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
26 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession;
27 import org.opendaylight.controller.sal.core.api.Consumer;
28 import org.opendaylight.controller.sal.core.api.model.SchemaService;
29 import org.opendaylight.netconf.api.monitoring.CapabilityListener;
30 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
31 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
35 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
36 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
37 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class MdsalNetconfOperationServiceFactory implements NetconfOperationServiceFactory, Consumer, AutoCloseable {
42
43     private static final Logger LOG = LoggerFactory.getLogger(MdsalNetconfOperationServiceFactory.class);
44
45     private ConsumerSession session = null;
46     private DOMDataBroker dataBroker = null;
47     private DOMRpcService rpcService = null;
48     private final CurrentSchemaContext currentSchemaContext;
49     private final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency;
50
51     public MdsalNetconfOperationServiceFactory(final SchemaService schemaService, final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency) {
52         this.rootSchemaSourceProviderDependency = rootSchemaSourceProviderDependency;
53         this.currentSchemaContext = new CurrentSchemaContext(Preconditions.checkNotNull(schemaService), rootSchemaSourceProviderDependency);
54     }
55
56     @Override
57     public MdsalNetconfOperationService createService(final String netconfSessionIdForReporting) {
58         Preconditions.checkState(dataBroker != null, "MD-SAL provider not yet initialized");
59         return new MdsalNetconfOperationService(currentSchemaContext, netconfSessionIdForReporting, dataBroker, rpcService);
60     }
61
62     @Override
63     public void close() throws Exception {
64         currentSchemaContext.close();
65     }
66
67     @Override
68     public Set<Capability> getCapabilities() {
69         return transformCapabilities(currentSchemaContext.getCurrentContext(), rootSchemaSourceProviderDependency);
70     }
71
72     static Set<Capability> transformCapabilities(final SchemaContext currentContext, final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency) {
73         final Set<Capability> capabilities = new HashSet<>();
74
75         // Added by netconf-impl by default
76 //        capabilities.add(new BasicCapability("urn:ietf:params:netconf:capability:candidate:1.0"));
77
78         final Set<Module> modules = currentContext.getModules();
79         for (final Module module : modules) {
80             Optional<YangModuleCapability> cap = moduleToCapability(module, rootSchemaSourceProviderDependency);
81             if(cap.isPresent()) {
82                 capabilities.add(cap.get());
83             }
84             for (final Module submodule : module.getSubmodules()) {
85                 cap = moduleToCapability(submodule, rootSchemaSourceProviderDependency);
86                 if(cap.isPresent()) {
87                     capabilities.add(cap.get());
88                 }
89             }
90         }
91
92         return capabilities;
93     }
94
95     private static Optional<YangModuleCapability> moduleToCapability(
96             final Module module, final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency) {
97
98         final SourceIdentifier moduleSourceIdentifier = SourceIdentifier.create(module.getName(),
99                 (SimpleDateFormatUtil.DEFAULT_DATE_REV == module.getRevision() ? Optional.<String>absent() :
100                         Optional.of(SimpleDateFormatUtil.getRevisionFormat().format(module.getRevision()))));
101
102         InputStream sourceStream = null;
103         String source;
104         try {
105             sourceStream = rootSchemaSourceProviderDependency.getSource(moduleSourceIdentifier).checkedGet().openStream();
106             source = CharStreams.toString(new InputStreamReader(sourceStream, Charsets.UTF_8));
107         } catch (IOException | SchemaSourceException e) {
108             LOG.warn("Ignoring source for module {}. Unable to read content", moduleSourceIdentifier, e);
109             source = null;
110         }
111
112         try {
113             if (sourceStream != null) {
114                 sourceStream.close();
115             }
116         } catch (IOException e) {
117             LOG.warn("Error closing yang source stream {}. Ignoring", moduleSourceIdentifier, e);
118         }
119
120         if(source !=null) {
121             return Optional.of(new YangModuleCapability(module, source));
122         } else {
123             LOG.warn("Missing source for module {}. This module will not be available from netconf server",
124                     moduleSourceIdentifier);
125         }
126         return Optional.absent();
127     }
128
129     @Override
130     public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
131         return currentSchemaContext.registerCapabilityListener(listener);
132     }
133
134     @Override
135     public void onSessionInitiated(ConsumerSession session) {
136         this.session = Preconditions.checkNotNull(session);
137         this.dataBroker = this.session.getService(DOMDataBroker.class);
138         this.rpcService = this.session.getService(DOMRpcService.class);
139     }
140
141     @Override
142     public Collection<ConsumerFunctionality> getConsumerFunctionality() {
143         return Collections.emptySet();
144     }
145 }