e0c78f8d48d702bf7e6250d3041f9376338df6e8
[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.collect.Maps;
12 import java.util.Collections;
13 import java.util.Date;
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
31 public final class InstanceRuntimeRpc {
32
33     private final Map<String, AttributeIfc> yangToAttrConfig;
34     private final Rpc rpc;
35     private final EnumResolver enumResolver;
36
37     public InstanceRuntimeRpc(final Rpc rpc, final EnumResolver enumResolver) {
38         this.enumResolver = enumResolver;
39         this.yangToAttrConfig = map(rpc.getParameters());
40         this.rpc = rpc;
41     }
42
43     private Map<String, AttributeIfc> map(final List<JavaAttribute> parameters) {
44         Map<String, AttributeIfc> mapped = Maps.newHashMap();
45         for (JavaAttribute javaAttribute : parameters) {
46             mapped.put(javaAttribute.getAttributeYangName(), javaAttribute);
47         }
48         return mapped;
49     }
50
51     private void resolveConfiguration(final Map<String, AttributeConfigElement> mappedConfig) {
52
53         // TODO make field, resolvingStrategies can be instantiated only once
54         Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> resolvingStrategies = new ObjectResolver(null)
55                 .prepareResolving(yangToAttrConfig, enumResolver);
56         // TODO make constructor for object resolver without service tracker
57         for (Entry<String, AttributeConfigElement> configDefEntry : mappedConfig.entrySet()) {
58             try {
59
60                 AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy = resolvingStrategies
61                         .get(configDefEntry.getKey());
62
63                 configDefEntry.getValue().resolveValue(attributeResolvingStrategy, configDefEntry.getKey());
64                 configDefEntry.getValue()
65                         .setJmxName(yangToAttrConfig.get(configDefEntry.getKey()).getUpperCaseCammelCase());
66             } catch (final DocumentedException e) {
67                 throw new IllegalStateException("Unable to resolve value " + configDefEntry.getValue()
68                         + " to attribute " + configDefEntry.getKey(), e);
69             }
70         }
71     }
72
73     public Map<String, AttributeConfigElement> fromXml(final XmlElement configRootNode) throws DocumentedException {
74         Map<String, AttributeConfigElement> retVal = Maps.newHashMap();
75
76         // FIXME add identity map to runtime data
77         Map<String, AttributeReadingStrategy> strats = new ObjectXmlReader().prepareReading(yangToAttrConfig,
78                 Collections.<String, Map<Date, IdentityMapping>>emptyMap());
79
80         for (Entry<String, AttributeReadingStrategy> readStratEntry : strats.entrySet()) {
81             List<XmlElement> configNodes = configRootNode.getChildElements(readStratEntry.getKey());
82             AttributeConfigElement readElement = readStratEntry.getValue().readElement(configNodes);
83             retVal.put(readStratEntry.getKey(), readElement);
84         }
85
86         resolveConfiguration(retVal);
87         return retVal;
88     }
89
90     public String getName() {
91         return rpc.getName();
92     }
93
94     public AttributeIfc getReturnType() {
95         return rpc.getReturnType();
96     }
97 }

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