Merge "Fix warnings reported in toaster"
[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.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.common.RpcResult;
14 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
15 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
16 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
17 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
18 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProvider;
19
20 import com.google.common.base.Optional;
21
22 class NetconfRemoteSchemaSourceProvider implements SchemaSourceProvider<String> {
23
24     public static final QName IETF_NETCONF_MONITORING = QName.create(
25             "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", "2010-10-04", "ietf-netconf-monitoring");
26     public static final QName GET_SCHEMA_QNAME = QName.create(IETF_NETCONF_MONITORING, "get-schema");
27     public static final QName GET_DATA_QNAME = QName.create(IETF_NETCONF_MONITORING, "data");
28
29     NetconfDevice device;
30
31     public NetconfRemoteSchemaSourceProvider(NetconfDevice device) {
32         super();
33         this.device = device;
34     }
35
36     @Override
37     public Optional<String> getSchemaSource(String moduleName, Optional<String> revision) {
38         CompositeNodeBuilder<ImmutableCompositeNode> request = ImmutableCompositeNode.builder(); //
39         request.setQName(GET_SCHEMA_QNAME) //
40                 .addLeaf("format", "yang") //
41                 .addLeaf("identifier", moduleName); //
42         if (revision.isPresent()) {
43             request.addLeaf("version", revision.get());
44         }
45
46         device.logger.trace("Loading YANG schema source for {}:{}", moduleName, revision);
47         RpcResult<CompositeNode> schemaReply = device.invokeRpc(GET_SCHEMA_QNAME, request.toInstance());
48         if (schemaReply.isSuccessful()) {
49             String schemaBody = getSchemaFromRpc(schemaReply.getResult());
50             if (schemaBody != null) {
51                 device.logger.trace("YANG Schema successfully retrieved from remote for {}:{}", moduleName, revision);
52                 return Optional.of(schemaBody);
53             }
54         }
55         device.logger.warn("YANG shcema was not successfully retrieved.");
56         return Optional.absent();
57     }
58
59     private String getSchemaFromRpc(CompositeNode result) {
60         if (result == null) {
61             return null;
62         }
63         SimpleNode<?> simpleNode = result.getFirstSimpleByName(GET_DATA_QNAME.withoutRevision());
64         Object potential = simpleNode.getValue();
65         if (potential instanceof String) {
66             return (String) potential;
67         }
68         return null;
69     }
70
71     public static final boolean isSupportedFor(Set<QName> capabilities) {
72         return capabilities.contains(IETF_NETCONF_MONITORING);
73     }
74 }