2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.connect.netconf.schema;
10 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.GET_SCHEMA_QNAME;
11 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DATA_QNAME;
12 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
14 import com.google.common.base.Charsets;
15 import com.google.common.base.Function;
16 import com.google.common.base.MoreObjects;
17 import com.google.common.base.Optional;
18 import com.google.common.base.Preconditions;
19 import com.google.common.util.concurrent.CheckedFuture;
20 import com.google.common.util.concurrent.Futures;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import java.io.ByteArrayInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import javax.xml.transform.dom.DOMSource;
26 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
27 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
28 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
29 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.Yang;
31 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
42 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
43 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
44 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
45 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.w3c.dom.Element;
50 public final class NetconfRemoteSchemaYangSourceProvider implements SchemaSourceProvider<YangTextSchemaSource> {
52 private static final Logger LOG = LoggerFactory.getLogger(NetconfRemoteSchemaYangSourceProvider.class);
54 private static final ExceptionMapper<SchemaSourceException> MAPPER = new ExceptionMapper<SchemaSourceException>(
55 "schemaDownload", SchemaSourceException.class) {
57 protected SchemaSourceException newWithCause(final String s, final Throwable throwable) {
58 return new SchemaSourceException(s, throwable);
62 private final DOMRpcService rpc;
63 private final RemoteDeviceId id;
65 public NetconfRemoteSchemaYangSourceProvider(final RemoteDeviceId id, final DOMRpcService rpc) {
67 this.rpc = Preconditions.checkNotNull(rpc);
70 public static ContainerNode createGetSchemaRequest(final String moduleName, final Optional<String> revision) {
71 final QName identifierQName = QName.cachedReference(QName.create(NetconfMessageTransformUtil.GET_SCHEMA_QNAME, "identifier"));
72 final YangInstanceIdentifier.NodeIdentifier identifierId = new YangInstanceIdentifier.NodeIdentifier(identifierQName);
73 final LeafNode<String> identifier = Builders.<String>leafBuilder().withNodeIdentifier(identifierId).withValue(moduleName).build();
75 final QName formatQName = QName.cachedReference(QName.create(NetconfMessageTransformUtil.GET_SCHEMA_QNAME, "format"));
76 final YangInstanceIdentifier.NodeIdentifier formatId = new YangInstanceIdentifier.NodeIdentifier(formatQName);
77 final LeafNode<QName> format = Builders.<QName>leafBuilder().withNodeIdentifier(formatId).withValue(Yang.QNAME).build();
79 final DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> builder = Builders.containerBuilder();
81 builder.withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.GET_SCHEMA_QNAME))
82 .withChild(identifier).withChild(format);
84 if(revision.isPresent()) {
85 final QName revisionQName = QName.cachedReference(QName.create(NetconfMessageTransformUtil.GET_SCHEMA_QNAME, "version"));
86 final YangInstanceIdentifier.NodeIdentifier revisionId = new YangInstanceIdentifier.NodeIdentifier(revisionQName);
87 final LeafNode<String> revisionNode = Builders.<String>leafBuilder().withNodeIdentifier(revisionId).withValue(revision.get()).build();
89 builder.withChild(revisionNode);
92 return builder.build();
95 private static Optional<String> getSchemaFromRpc(final RemoteDeviceId id, final NormalizedNode<?, ?> result) {
97 return Optional.absent();
100 final QName schemaWrapperNode = QName.cachedReference(QName.create(GET_SCHEMA_QNAME, NETCONF_DATA_QNAME.getLocalName()));
101 final Optional<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> child = ((ContainerNode) result).getChild(toId(schemaWrapperNode));
103 Preconditions.checkState(child.isPresent() && child.get() instanceof AnyXmlNode,
104 "%s Unexpected response to get-schema, expected response with one child %s, but was %s", id,
105 schemaWrapperNode, result);
107 final DOMSource wrappedNode = ((AnyXmlNode) child.get()).getValue();
108 Preconditions.checkNotNull(wrappedNode.getNode());
109 final Element dataNode = (Element) wrappedNode.getNode();
111 return Optional.of(dataNode.getTextContent().trim());
115 public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
116 final String moduleName = sourceIdentifier.getName();
118 // If formatted revision is SourceIdentifier.NOT_PRESENT_FORMATTED_REVISION, we have to omit it from request
119 final String formattedRevision = sourceIdentifier.getRevision().equals(SourceIdentifier.NOT_PRESENT_FORMATTED_REVISION) ? null : sourceIdentifier.getRevision();
120 final Optional<String> revision = Optional.fromNullable(formattedRevision);
121 final NormalizedNode<?, ?> getSchemaRequest = createGetSchemaRequest(moduleName, revision);
123 LOG.trace("{}: Loading YANG schema source for {}:{}", id, moduleName,
126 final ListenableFuture<YangTextSchemaSource> transformed = Futures.transform(
127 rpc.invokeRpc(SchemaPath.create(true, NetconfMessageTransformUtil.GET_SCHEMA_QNAME), getSchemaRequest),
128 new ResultToYangSourceTransformer(id, sourceIdentifier, moduleName, revision));
130 final CheckedFuture<YangTextSchemaSource, SchemaSourceException> checked = Futures.makeChecked(transformed, MAPPER);
132 // / FIXME remove this get, it is only present to wait until source is retrieved
133 // (goal is to limit concurrent schema download, since NetconfDevice listener does not handle concurrent messages properly)
136 LOG.trace("{}: Blocking for {}", id, sourceIdentifier);
137 checked.checkedGet();
138 } catch (final SchemaSourceException e) {
139 return Futures.immediateFailedCheckedFuture(e);
146 * Transform composite node to string schema representation and then to ASTSchemaSource
148 private static final class ResultToYangSourceTransformer implements
149 Function<DOMRpcResult, YangTextSchemaSource> {
151 private final RemoteDeviceId id;
152 private final SourceIdentifier sourceIdentifier;
153 private final String moduleName;
154 private final Optional<String> revision;
156 public ResultToYangSourceTransformer(final RemoteDeviceId id, final SourceIdentifier sourceIdentifier,
157 final String moduleName, final Optional<String> revision) {
159 this.sourceIdentifier = sourceIdentifier;
160 this.moduleName = moduleName;
161 this.revision = revision;
165 public YangTextSchemaSource apply(final DOMRpcResult input) {
167 if (input.getErrors().isEmpty()) {
169 final Optional<String> schemaString = getSchemaFromRpc(id, input.getResult());
171 Preconditions.checkState(schemaString.isPresent(),
172 "%s: Unexpected response to get-schema, schema not present in message for: %s", id, sourceIdentifier);
174 LOG.debug("{}: YANG Schema successfully retrieved for {}:{}",
175 id, moduleName, revision);
176 return new NetconfYangTextSchemaSource(id, sourceIdentifier, schemaString);
180 "{}: YANG schema was not successfully retrieved for {}. Errors: {}",
181 id, sourceIdentifier, input.getErrors());
183 throw new IllegalStateException(String.format(
184 "%s: YANG schema was not successfully retrieved for %s. Errors: %s", id, sourceIdentifier,
190 private static class NetconfYangTextSchemaSource extends YangTextSchemaSource {
191 private final RemoteDeviceId id;
192 private final Optional<String> schemaString;
194 public NetconfYangTextSchemaSource(final RemoteDeviceId id, final SourceIdentifier sId, final Optional<String> schemaString) {
197 this.schemaString = schemaString;
201 protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
202 return toStringHelper.add("device", id);
206 public InputStream openStream() throws IOException {
207 return new ByteArrayInputStream(schemaString.get().getBytes(Charsets.UTF_8));