1 package org.opendaylight.controller.sal.connect.netconf;
3 import com.google.common.annotations.VisibleForTesting;
4 import com.google.common.base.Function;
5 import com.google.common.base.Optional;
6 import com.google.common.base.Preconditions;
7 import com.google.common.collect.Collections2;
8 import com.google.common.collect.Lists;
9 import com.google.common.collect.Sets;
11 import java.util.Collections;
13 import java.util.concurrent.ExecutionException;
14 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionCapabilities;
15 import org.opendaylight.controller.sal.connect.netconf.sal.NetconfDeviceRpc;
16 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
17 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.Yang;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
25 import org.opendaylight.yangtools.yang.data.api.Node;
26 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Holds QNames for all yang modules reported by ietf-netconf-monitoring/state/schemas
35 public final class NetconfStateSchemas {
37 private static final Logger logger = LoggerFactory.getLogger(NetconfStateSchemas.class);
40 * Factory for NetconfStateSchemas
42 public interface NetconfStateSchemasResolver {
43 NetconfStateSchemas resolve(final NetconfDeviceRpc deviceRpc, final NetconfSessionCapabilities remoteSessionCapabilities, final RemoteDeviceId id);
47 * Default implementation resolving schemas QNames from netconf-state
49 public static final class NetconfStateSchemasResolverImpl implements NetconfStateSchemasResolver {
52 public NetconfStateSchemas resolve(final NetconfDeviceRpc deviceRpc, final NetconfSessionCapabilities remoteSessionCapabilities, final RemoteDeviceId id) {
53 return NetconfStateSchemas.create(deviceRpc, remoteSessionCapabilities, id);
57 public static final NetconfStateSchemas EMPTY = new NetconfStateSchemas(Collections.<RemoteYangSchema>emptySet());
59 private static final YangInstanceIdentifier STATE_SCHEMAS_IDENTIFIER =
60 YangInstanceIdentifier.builder().node(NetconfState.QNAME).node(Schemas.QNAME).build();
61 private static final YangInstanceIdentifier DATA_STATE_SCHEMAS_IDENTIFIER =
62 YangInstanceIdentifier.builder().node(NetconfMessageTransformUtil.NETCONF_DATA_QNAME)
63 .node(NetconfState.QNAME).node(Schemas.QNAME).build();
65 private static final CompositeNode GET_SCHEMAS_RPC;
67 final Node<?> filter = NetconfMessageTransformUtil.toFilterStructure(STATE_SCHEMAS_IDENTIFIER);
69 = NodeFactory.createImmutableCompositeNode(NetconfMessageTransformUtil.NETCONF_GET_QNAME, null, Lists.<Node<?>>newArrayList(filter));
72 private final Set<RemoteYangSchema> availableYangSchemas;
74 public NetconfStateSchemas(final Set<RemoteYangSchema> availableYangSchemas) {
75 this.availableYangSchemas = availableYangSchemas;
78 public Set<RemoteYangSchema> getAvailableYangSchemas() {
79 return availableYangSchemas;
82 public Set<QName> getAvailableYangSchemasQNames() {
83 return Sets.newHashSet(Collections2.transform(getAvailableYangSchemas(), new Function<RemoteYangSchema, QName>() {
85 public QName apply(final RemoteYangSchema input) {
86 return input.getQName();
92 * Issue get request to remote device and parse response to find all schemas under netconf-state/schemas
94 private static NetconfStateSchemas create(final NetconfDeviceRpc deviceRpc, final NetconfSessionCapabilities remoteSessionCapabilities, final RemoteDeviceId id) {
95 if(remoteSessionCapabilities.isMonitoringSupported() == false) {
96 logger.warn("{}: Netconf monitoring not supported on device, cannot detect provided schemas");
100 final RpcResult<CompositeNode> schemasNodeResult;
102 schemasNodeResult = deviceRpc.invokeRpc(NetconfMessageTransformUtil.NETCONF_GET_QNAME, GET_SCHEMAS_RPC).get();
103 } catch (final InterruptedException e) {
104 Thread.currentThread().interrupt();
105 throw new RuntimeException(id + ": Interrupted while waiting for response to " + STATE_SCHEMAS_IDENTIFIER, e);
106 } catch (final ExecutionException e) {
107 logger.warn("{}: Unable to detect available schemas, get to {} failed", id, STATE_SCHEMAS_IDENTIFIER, e);
111 if(schemasNodeResult.isSuccessful() == false) {
112 logger.warn("{}: Unable to detect available schemas, get to {} failed, {}", id, STATE_SCHEMAS_IDENTIFIER, schemasNodeResult.getErrors());
116 final CompositeNode schemasNode =
117 (CompositeNode) NetconfMessageTransformUtil.findNode(schemasNodeResult.getResult(), DATA_STATE_SCHEMAS_IDENTIFIER);
118 return create(id, schemasNode);
122 * Parse response of get(netconf-state/schemas) to find all schemas under netconf-state/schemas
125 protected static NetconfStateSchemas create(final RemoteDeviceId id, final CompositeNode schemasNode) {
126 final Set<RemoteYangSchema> availableYangSchemas = Sets.newHashSet();
128 for (final CompositeNode schemaNode : schemasNode.getCompositesByName(Schema.QNAME.withoutRevision())) {
129 final Optional<RemoteYangSchema> fromCompositeNode = RemoteYangSchema.createFromCompositeNode(id, schemaNode);
130 if(fromCompositeNode.isPresent()) {
131 availableYangSchemas.add(fromCompositeNode.get());
135 return new NetconfStateSchemas(availableYangSchemas);
138 public final static class RemoteYangSchema {
139 private final QName qname;
141 private RemoteYangSchema(final QName qname) {
145 public QName getQName() {
149 static Optional<RemoteYangSchema> createFromCompositeNode(final RemoteDeviceId id, final CompositeNode schemaNode) {
150 Preconditions.checkArgument(schemaNode.getKey().equals(Schema.QNAME.withoutRevision()), "Wrong QName %s", schemaNode.getKey());
152 QName childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_FORMAT.withoutRevision();
154 final String formatAsString = getSingleChildNodeValue(schemaNode, childNode).get();
155 if(formatAsString.equals(Yang.QNAME.getLocalName()) == false) {
156 logger.debug("{}: Ignoring schema due to unsupported format: {}", id, formatAsString);
157 return Optional.absent();
160 childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_LOCATION.withoutRevision();
161 final Set<String> locationsAsString = getAllChildNodeValues(schemaNode, childNode);
162 if(locationsAsString.contains(Schema.Location.Enumeration.NETCONF.toString()) == false) {
163 logger.debug("{}: Ignoring schema due to unsupported location: {}", id, locationsAsString);
164 return Optional.absent();
167 childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_NAMESPACE.withoutRevision();
168 final String namespaceAsString = getSingleChildNodeValue(schemaNode, childNode).get();
170 childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_VERSION.withoutRevision();
171 // Revision does not have to be filled
172 final Optional<String> revisionAsString = getSingleChildNodeValue(schemaNode, childNode);
174 childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_IDENTIFIER.withoutRevision();
175 final String moduleNameAsString = getSingleChildNodeValue(schemaNode, childNode).get();
177 final QName moduleQName = revisionAsString.isPresent()
178 ? QName.create(namespaceAsString, revisionAsString.get(), moduleNameAsString)
179 : QName.create(URI.create(namespaceAsString), null, moduleNameAsString).withoutRevision();
181 return Optional.of(new RemoteYangSchema(moduleQName));
184 private static Set<String> getAllChildNodeValues(final CompositeNode schemaNode, final QName childNodeQName) {
185 final Set<String> extractedValues = Sets.newHashSet();
186 for (final SimpleNode<?> childNode : schemaNode.getSimpleNodesByName(childNodeQName)) {
187 extractedValues.add(getValueOfSimpleNode(childNodeQName, childNode).get());
189 return extractedValues;
192 private static Optional<String> getSingleChildNodeValue(final CompositeNode schemaNode, final QName childNode) {
193 final SimpleNode<?> node = schemaNode.getFirstSimpleByName(childNode);
194 return getValueOfSimpleNode(childNode, node);
197 private static Optional<String> getValueOfSimpleNode(final QName childNode, final SimpleNode<?> node) {
198 Preconditions.checkNotNull(node, "Child node %s not present", childNode);
199 final Object value = node.getValue();
200 return value == null ? Optional.<String>absent() : Optional.of(value.toString().trim());
204 public boolean equals(final Object o) {
205 if (this == o) return true;
206 if (o == null || getClass() != o.getClass()) return false;
208 final RemoteYangSchema that = (RemoteYangSchema) o;
210 if (!qname.equals(that.qname)) return false;
216 public int hashCode() {
217 return qname.hashCode();