Changed model versions to dependencies
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / NetconfRemoteSchemaSourceProvider.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.connect.netconf;
9
10 import java.util.Set;
11
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;
20
21 import com.google.common.base.Optional;
22 import com.google.common.base.Preconditions;
23
24 class NetconfRemoteSchemaSourceProvider implements SchemaSourceProvider<String> {
25
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");
30
31     NetconfDevice device;
32
33     public NetconfRemoteSchemaSourceProvider(NetconfDevice device) {
34         super();
35         this.device = device;
36     }
37
38     @Override
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());
46         }
47
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);
55             }
56         }
57         device.logger.warn("YANG shcema was not successfully retrieved.");
58         return Optional.absent();
59     }
60
61     private String getSchemaFromRpc(CompositeNode result) {
62         if (result == null) {
63             return null;
64         }
65         SimpleNode<?> simpleNode = result.getFirstSimpleByName(GET_DATA_QNAME.withoutRevision());
66         Object potential = simpleNode.getValue();
67         if (potential instanceof String) {
68             return (String) potential;
69         }
70         return null;
71     }
72
73     public static final boolean isSupportedFor(Set<QName> capabilities) {
74         return capabilities.contains(IETF_NETCONF_MONITORING);
75     }
76 }