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