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