Added support for list, leaf-list and container in output under rpc node in yang...
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / rpc / InstanceRuntimeRpc.java
1 /*
2  * Copyright (c) 2013 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
9 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc;
10
11 import com.google.common.collect.Maps;
12 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc;
13 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
14 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
15 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement;
16 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeReadingStrategy;
17 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.ObjectXmlReader;
18 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.resolving.AttributeResolvingStrategy;
19 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.resolving.ObjectResolver;
20 import org.opendaylight.controller.netconf.util.xml.XmlElement;
21
22 import javax.management.openmbean.OpenType;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Map.Entry;
26
27 public final class InstanceRuntimeRpc {
28
29     private final Map<String, AttributeIfc> yangToAttrConfig;
30     private final Rpc rpc;
31
32     public InstanceRuntimeRpc(Rpc rpc) {
33         this.yangToAttrConfig = map(rpc.getParameters());
34         this.rpc = rpc;
35     }
36
37     private Map<String, AttributeIfc> map(List<JavaAttribute> parameters) {
38         Map<String, AttributeIfc> mapped = Maps.newHashMap();
39         for (JavaAttribute javaAttribute : parameters) {
40             mapped.put(javaAttribute.getAttributeYangName(), javaAttribute);
41         }
42         return mapped;
43     }
44
45     private void resolveConfiguration(Map<String, AttributeConfigElement> mappedConfig) {
46
47         // TODO make field, resolvingStrategies can be instantiated only once
48         Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> resolvingStrategies = new ObjectResolver(null)
49                 .prepareResolving(yangToAttrConfig);
50         // TODO make constructor for object resolver without service tracker
51         for (Entry<String, AttributeConfigElement> configDefEntry : mappedConfig.entrySet()) {
52             try {
53
54                 AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy = resolvingStrategies
55                         .get(configDefEntry.getKey());
56
57                 configDefEntry.getValue().resolveValue(attributeResolvingStrategy, configDefEntry.getKey());
58                 configDefEntry.getValue().setJmxName(
59                         yangToAttrConfig.get(configDefEntry.getKey()).getUpperCaseCammelCase());
60             } catch (Exception e) {
61                 throw new IllegalStateException("Unable to resolve value " + configDefEntry.getValue()
62                         + " to attribute " + configDefEntry.getKey(), e);
63             }
64         }
65     }
66
67     public Map<String, AttributeConfigElement> fromXml(XmlElement configRootNode) {
68         Map<String, AttributeConfigElement> retVal = Maps.newHashMap();
69
70         Map<String, AttributeReadingStrategy> strats = new ObjectXmlReader().prepareReading(yangToAttrConfig);
71
72         for (Entry<String, AttributeReadingStrategy> readStratEntry : strats.entrySet()) {
73             List<XmlElement> configNodes = configRootNode.getChildElements(readStratEntry.getKey());
74             AttributeConfigElement readElement = readStratEntry.getValue().readElement(configNodes);
75             retVal.put(readStratEntry.getKey(), readElement);
76         }
77
78         resolveConfiguration(retVal);
79         return retVal;
80     }
81
82     public String getName() {
83         return rpc.getName();
84     }
85
86     public AttributeIfc getReturnType() {
87         return rpc.getReturnType();
88     }
89
90 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.