1 package org.opendaylight.controller.sal.connect.netconf;
3 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DATA_QNAME;
4 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
5 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
6 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
8 import com.google.common.annotations.VisibleForTesting;
9 import com.google.common.base.Function;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Strings;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.Sets;
16 import java.util.Collections;
18 import java.util.concurrent.ExecutionException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
20 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionPreferences;
21 import org.opendaylight.controller.sal.connect.netconf.sal.NetconfDeviceRpc;
22 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
23 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.Yang;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
32 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Holds QNames for all yang modules reported by ietf-netconf-monitoring/state/schemas
45 public final class NetconfStateSchemas {
47 private static final Logger LOG = LoggerFactory.getLogger(NetconfStateSchemas.class);
50 * Factory for NetconfStateSchemas
52 public interface NetconfStateSchemasResolver {
53 NetconfStateSchemas resolve(final NetconfDeviceRpc deviceRpc, final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId id);
57 * Default implementation resolving schemas QNames from netconf-state
59 public static final class NetconfStateSchemasResolverImpl implements NetconfStateSchemasResolver {
62 public NetconfStateSchemas resolve(final NetconfDeviceRpc deviceRpc, final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId id) {
63 return NetconfStateSchemas.create(deviceRpc, remoteSessionCapabilities, id);
67 public static final NetconfStateSchemas EMPTY = new NetconfStateSchemas(Collections.<RemoteYangSchema>emptySet());
69 private static final YangInstanceIdentifier STATE_SCHEMAS_IDENTIFIER =
70 YangInstanceIdentifier.builder().node(NetconfState.QNAME).node(Schemas.QNAME).build();
72 private static final ContainerNode GET_SCHEMAS_RPC;
74 final DataContainerChild<?, ?> filter = NetconfMessageTransformUtil.toFilterStructure(STATE_SCHEMAS_IDENTIFIER, NetconfDevice.INIT_SCHEMA_CTX);
76 = Builders.containerBuilder().withNodeIdentifier(toId(NETCONF_GET_QNAME)).withChild(filter).build();
79 private final Set<RemoteYangSchema> availableYangSchemas;
81 public NetconfStateSchemas(final Set<RemoteYangSchema> availableYangSchemas) {
82 this.availableYangSchemas = availableYangSchemas;
85 public Set<RemoteYangSchema> getAvailableYangSchemas() {
86 return availableYangSchemas;
89 public Set<QName> getAvailableYangSchemasQNames() {
90 return Sets.newHashSet(Collections2.transform(getAvailableYangSchemas(), new Function<RemoteYangSchema, QName>() {
92 public QName apply(final RemoteYangSchema input) {
93 return input.getQName();
99 * Issue get request to remote device and parse response to find all schemas under netconf-state/schemas
101 private static NetconfStateSchemas create(final NetconfDeviceRpc deviceRpc, final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId id) {
102 if(remoteSessionCapabilities.isMonitoringSupported() == false) {
103 LOG.warn("{}: Netconf monitoring not supported on device, cannot detect provided schemas");
107 final DOMRpcResult schemasNodeResult;
109 schemasNodeResult = deviceRpc.invokeRpc(toPath(NETCONF_GET_QNAME), GET_SCHEMAS_RPC).get();
110 } catch (final InterruptedException e) {
111 Thread.currentThread().interrupt();
112 throw new RuntimeException(id + ": Interrupted while waiting for response to " + STATE_SCHEMAS_IDENTIFIER, e);
113 } catch (final ExecutionException e) {
114 LOG.warn("{}: Unable to detect available schemas, get to {} failed", id, STATE_SCHEMAS_IDENTIFIER, e);
118 if(schemasNodeResult.getErrors().isEmpty() == false) {
119 LOG.warn("{}: Unable to detect available schemas, get to {} failed, {}", id, STATE_SCHEMAS_IDENTIFIER, schemasNodeResult.getErrors());
123 final Optional<? extends NormalizedNode<?, ?>> schemasNode = findSchemasNode(schemasNodeResult.getResult());
125 if(schemasNode.isPresent()) {
126 Preconditions.checkState(schemasNode.get() instanceof ContainerNode,
127 "Expecting container containing schemas, but was %s", schemasNode.get());
128 return create(id, ((ContainerNode) schemasNode.get()));
130 LOG.warn("{}: Unable to detect available schemas, get to {} was empty", id, STATE_SCHEMAS_IDENTIFIER);
135 private static Optional<? extends NormalizedNode<?, ?>> findSchemasNode(final NormalizedNode<?, ?> result) {
137 return Optional.absent();
139 final Optional<DataContainerChild<?, ?>> dataNode = ((DataContainerNode<?>) result).getChild(toId(NETCONF_DATA_QNAME));
140 if(dataNode.isPresent() == false) {
141 return Optional.absent();
144 final Optional<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> nStateNode =
145 ((DataContainerNode<?>) dataNode.get()).getChild(toId(NetconfState.QNAME));
146 if(nStateNode.isPresent() == false) {
147 return Optional.absent();
150 return ((DataContainerNode<?>) nStateNode.get()).getChild(toId(Schemas.QNAME));
154 * Parse response of get(netconf-state/schemas) to find all schemas under netconf-state/schemas
157 protected static NetconfStateSchemas create(final RemoteDeviceId id, final ContainerNode schemasNode) {
158 final Set<RemoteYangSchema> availableYangSchemas = Sets.newHashSet();
160 final Optional<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> child = schemasNode.getChild(toId(Schema.QNAME));
161 Preconditions.checkState(child.isPresent(), "Unable to find list: %s in response: %s", Schema.QNAME.withoutRevision(), schemasNode);
162 Preconditions.checkState(child.get() instanceof MapNode, "Unexpected structure for container: %s in response: %s. Expecting a list", Schema.QNAME.withoutRevision(), schemasNode);
164 for (final MapEntryNode schemaNode : ((MapNode) child.get()).getValue()) {
165 final Optional<RemoteYangSchema> fromCompositeNode = RemoteYangSchema.createFromNormalizedNode(id, schemaNode);
166 if(fromCompositeNode.isPresent()) {
167 availableYangSchemas.add(fromCompositeNode.get());
171 return new NetconfStateSchemas(availableYangSchemas);
174 public final static class RemoteYangSchema {
175 private final QName qname;
177 RemoteYangSchema(final QName qname) {
181 public QName getQName() {
185 static Optional<RemoteYangSchema> createFromNormalizedNode(final RemoteDeviceId id, final MapEntryNode schemaNode) {
186 Preconditions.checkArgument(schemaNode.getNodeType().equals(Schema.QNAME), "Wrong QName %s", schemaNode.getNodeType());
188 QName childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_FORMAT;
190 String formatAsString = getSingleChildNodeValue(schemaNode, childNode).get();
192 if(formatAsString.equals(Yang.QNAME.toString()) == false) {
193 LOG.debug("{}: Ignoring schema due to unsupported format: {}", id, formatAsString);
194 return Optional.absent();
197 childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_LOCATION;
198 final Set<String> locationsAsString = getAllChildNodeValues(schemaNode, childNode);
199 if(locationsAsString.contains(Schema.Location.Enumeration.NETCONF.toString()) == false) {
200 LOG.debug("{}: Ignoring schema due to unsupported location: {}", id, locationsAsString);
201 return Optional.absent();
204 childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_NAMESPACE;
205 final String namespaceAsString = getSingleChildNodeValue(schemaNode, childNode).get();
207 childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_VERSION;
208 // Revision does not have to be filled
209 final Optional<String> revisionAsString = getSingleChildNodeValue(schemaNode, childNode);
211 childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_IDENTIFIER;
212 final String moduleNameAsString = getSingleChildNodeValue(schemaNode, childNode).get();
214 final QName moduleQName = revisionAsString.isPresent()
215 ? QName.create(namespaceAsString, revisionAsString.get(), moduleNameAsString)
216 : QName.create(URI.create(namespaceAsString), null, moduleNameAsString);
218 return Optional.of(new RemoteYangSchema(moduleQName));
222 * Extracts all values of a leaf-list node as a set of strings
224 private static Set<String> getAllChildNodeValues(final DataContainerNode<?> schemaNode, final QName childNodeQName) {
225 final Set<String> extractedValues = Sets.newHashSet();
226 final Optional<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> child = schemaNode.getChild(toId(childNodeQName));
227 Preconditions.checkArgument(child.isPresent(), "Child nodes %s not present", childNodeQName);
228 Preconditions.checkArgument(child.get() instanceof LeafSetNode<?>, "Child nodes %s not present", childNodeQName);
229 for (final LeafSetEntryNode<?> childNode : ((LeafSetNode<?>) child.get()).getValue()) {
230 extractedValues.add(getValueOfSimpleNode(childNode).get());
232 return extractedValues;
235 private static Optional<String> getSingleChildNodeValue(final DataContainerNode<?> schemaNode, final QName childNode) {
236 final Optional<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> node = schemaNode.getChild(toId(childNode));
237 Preconditions.checkArgument(node.isPresent(), "Child node %s not present", childNode);
238 return getValueOfSimpleNode(node.get());
241 private static Optional<String> getValueOfSimpleNode(final NormalizedNode<? extends YangInstanceIdentifier.PathArgument, ?> node) {
242 final Object value = node.getValue();
243 return value == null || Strings.isNullOrEmpty(value.toString()) ? Optional.<String>absent() : Optional.of(value.toString().trim());
247 public boolean equals(final Object o) {
251 if (o == null || getClass() != o.getClass()) {
255 final RemoteYangSchema that = (RemoteYangSchema) o;
257 if (!qname.equals(that.qname)) {
265 public int hashCode() {
266 return qname.hashCode();