Removed `which` dependency, now using proper shell builtin.
[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 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 class NetconfRemoteSchemaSourceProvider implements SchemaSourceProvider<String> {
27
28     public static final QName IETF_NETCONF_MONITORING = QName.create(
29             "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", "2010-10-04", "ietf-netconf-monitoring");
30     public static final QName GET_SCHEMA_QNAME = QName.create(IETF_NETCONF_MONITORING, "get-schema");
31     public static final QName GET_DATA_QNAME = QName.create(IETF_NETCONF_MONITORING, "data");
32
33     private final NetconfDevice device;
34
35     private final Logger logger;
36
37     public NetconfRemoteSchemaSourceProvider(NetconfDevice device) {
38         this.device = Preconditions.checkNotNull(device);
39         logger = LoggerFactory.getLogger(NetconfDevice.class + "#" + device.getName());
40     }
41
42     @Override
43     public Optional<String> getSchemaSource(String moduleName, Optional<String> revision) {
44         CompositeNodeBuilder<ImmutableCompositeNode> request = ImmutableCompositeNode.builder(); //
45         request.setQName(GET_SCHEMA_QNAME) //
46                 .addLeaf("format", "yang") //
47                 .addLeaf("identifier", moduleName); //
48         if (revision.isPresent()) {
49             request.addLeaf("version", revision.get());
50         }
51
52         logger.trace("Loading YANG schema source for {}:{}", moduleName, revision);
53         try {
54             RpcResult<CompositeNode> schemaReply = device.invokeRpc(GET_SCHEMA_QNAME, request.toInstance()).get();
55             if (schemaReply.isSuccessful()) {
56                 String schemaBody = getSchemaFromRpc(schemaReply.getResult());
57                 if (schemaBody != null) {
58                     device.logger.trace("YANG Schema successfully retrieved from remote for {}:{}", moduleName, revision);
59                     return Optional.of(schemaBody);
60                 }
61             }
62             logger.warn("YANG shcema was not successfully retrieved. Errors: {}", schemaReply.getErrors());
63         } catch (InterruptedException | ExecutionException e) {
64             logger.warn("YANG shcema was not successfully retrieved.", e);
65         }
66         return Optional.absent();
67     }
68
69     private String getSchemaFromRpc(CompositeNode result) {
70         if (result == null) {
71             return null;
72         }
73         SimpleNode<?> simpleNode = result.getFirstSimpleByName(GET_DATA_QNAME.withoutRevision());
74         Object potential = simpleNode.getValue();
75         if (potential instanceof String) {
76             return (String) potential;
77         }
78         return null;
79     }
80
81     public static final boolean isSupportedFor(Collection<QName> capabilities) {
82         return capabilities.contains(IETF_NETCONF_MONITORING);
83     }
84 }