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.controller.sal.connect.netconf;
10 import java.util.Collection;
11 import java.util.concurrent.ExecutionException;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.common.RpcResult;
15 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
16 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
17 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
18 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
19 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProvider;
21 import com.google.common.base.Optional;
22 import com.google.common.base.Preconditions;
24 class NetconfRemoteSchemaSourceProvider implements SchemaSourceProvider<String> {
26 public static final QName IETF_NETCONF_MONITORING = QName.create(
27 "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", "2010-10-04", "ietf-netconf-monitoring");
28 public static final QName GET_SCHEMA_QNAME = QName.create(IETF_NETCONF_MONITORING, "get-schema");
29 public static final QName GET_DATA_QNAME = QName.create(IETF_NETCONF_MONITORING, "data");
31 private final NetconfDevice device;
33 public NetconfRemoteSchemaSourceProvider(NetconfDevice device) {
34 this.device = Preconditions.checkNotNull(device);
38 public Optional<String> getSchemaSource(String moduleName, Optional<String> revision) {
39 CompositeNodeBuilder<ImmutableCompositeNode> request = ImmutableCompositeNode.builder(); //
40 request.setQName(GET_SCHEMA_QNAME) //
41 .addLeaf("format", "yang") //
42 .addLeaf("identifier", moduleName); //
43 if (revision.isPresent()) {
44 request.addLeaf("version", revision.get());
47 device.logger.trace("Loading YANG schema source for {}:{}", moduleName, revision);
49 RpcResult<CompositeNode> schemaReply = device.invokeRpc(GET_SCHEMA_QNAME, request.toInstance()).get();
50 if (schemaReply.isSuccessful()) {
51 String schemaBody = getSchemaFromRpc(schemaReply.getResult());
52 if (schemaBody != null) {
53 device.logger.trace("YANG Schema successfully retrieved from remote for {}:{}", moduleName, revision);
54 return Optional.of(schemaBody);
57 device.logger.warn("YANG shcema was not successfully retrieved.");
58 } catch (InterruptedException | ExecutionException e) {
59 device.logger.warn("YANG shcema was not successfully retrieved.", e);
61 return Optional.absent();
64 private String getSchemaFromRpc(CompositeNode result) {
68 SimpleNode<?> simpleNode = result.getFirstSimpleByName(GET_DATA_QNAME.withoutRevision());
69 Object potential = simpleNode.getValue();
70 if (potential instanceof String) {
71 return (String) potential;
76 public static final boolean isSupportedFor(Collection<QName> capabilities) {
77 return capabilities.contains(IETF_NETCONF_MONITORING);