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

©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.