Merge "Bug 509: Fixed incorrect merging of Data Store Writes / Events"
[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.Collection;
11 import java.util.concurrent.ExecutionException;
12
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     private final NetconfDevice device;
32
33     public NetconfRemoteSchemaSourceProvider(NetconfDevice device) {
34         this.device = Preconditions.checkNotNull(device);
35     }
36
37     @Override
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());
45         }
46
47         device.logger.trace("Loading YANG schema source for {}:{}", moduleName, revision);
48         try {
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);
55                 }
56             }
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);
60         }
61         return Optional.absent();
62     }
63
64     private String getSchemaFromRpc(CompositeNode result) {
65         if (result == null) {
66             return null;
67         }
68         SimpleNode<?> simpleNode = result.getFirstSimpleByName(GET_DATA_QNAME.withoutRevision());
69         Object potential = simpleNode.getValue();
70         if (potential instanceof String) {
71             return (String) potential;
72         }
73         return null;
74     }
75
76     public static final boolean isSupportedFor(Collection<QName> capabilities) {
77         return capabilities.contains(IETF_NETCONF_MONITORING);
78     }
79 }