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;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
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");
33 public NetconfRemoteSchemaSourceProvider(NetconfDevice device) {
39 public Optional<String> getSchemaSource(String moduleName, Optional<String> revision) {
40 CompositeNodeBuilder<ImmutableCompositeNode> request = ImmutableCompositeNode.builder(); //
41 request.setQName(GET_SCHEMA_QNAME) //
42 .addLeaf("format", "yang") //
43 .addLeaf("identifier", moduleName); //
44 if (revision.isPresent()) {
45 request.addLeaf("version", revision.get());
48 device.logger.trace("Loading YANG schema source for {}:{}", moduleName, revision);
49 RpcResult<CompositeNode> schemaReply = device.invokeRpc(GET_SCHEMA_QNAME, request.toInstance());
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 return Optional.absent();
61 private String getSchemaFromRpc(CompositeNode result) {
65 SimpleNode<?> simpleNode = result.getFirstSimpleByName(GET_DATA_QNAME.withoutRevision());
66 Object potential = simpleNode.getValue();
67 if (potential instanceof String) {
68 return (String) potential;
73 public static final boolean isSupportedFor(Set<QName> capabilities) {
74 return capabilities.contains(IETF_NETCONF_MONITORING);