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