a9c07238692de2d75ebb2fa171bcb820d6cf6c3c
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / fromxml / ObjectXmlReader.java
1 /*
2  * Copyright (c) 2015 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.mapping.attributes.fromxml;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Maps;
13 import java.util.Date;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import java.util.Set;
17 import javax.management.openmbean.ArrayType;
18 import javax.management.openmbean.CompositeType;
19 import javax.management.openmbean.OpenType;
20 import javax.management.openmbean.SimpleType;
21 import org.opendaylight.controller.config.facade.xml.mapping.IdentityMapping;
22 import org.opendaylight.controller.config.facade.xml.mapping.attributes.AttributeIfcSwitchStatement;
23 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
24 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
25 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
26 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
27 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
28
29 public class ObjectXmlReader extends AttributeIfcSwitchStatement<AttributeReadingStrategy> {
30
31     private String key;
32     private Map<String, Map<Date, IdentityMapping>> identityMap;
33
34     public Map<String, AttributeReadingStrategy> prepareReading(Map<String, AttributeIfc> yangToAttrConfig, Map<String, Map<Date, IdentityMapping>> identityMap) {
35         Map<String, AttributeReadingStrategy> strategies = Maps.newHashMap();
36         this.identityMap = identityMap;
37
38         for (Entry<String, AttributeIfc> attributeEntry : yangToAttrConfig.entrySet()) {
39             AttributeReadingStrategy strat = prepareReadingStrategy(attributeEntry.getKey(), attributeEntry.getValue());
40             strategies.put(attributeEntry.getKey(), strat);
41         }
42         return strategies;
43     }
44
45     private AttributeReadingStrategy prepareReadingStrategy(String key, AttributeIfc attributeIfc) {
46         this.key = key;
47         return switchAttribute(attributeIfc);
48     }
49
50     @Override
51     protected AttributeReadingStrategy caseJavaBinaryAttribute(OpenType<?> openType) {
52         return new SimpleBinaryAttributeReadingStrategy(getLastAttribute().getNullableDefault());
53     }
54
55     @Override
56     protected AttributeReadingStrategy caseJavaUnionAttribute(OpenType<?> openType) {
57         String mappingKey = JavaAttribute.DESCRIPTION_OF_VALUE_ATTRIBUTE_FOR_UNION;
58         return new SimpleUnionAttributeReadingStrategy(getLastAttribute().getNullableDefault(), mappingKey);
59     }
60
61     @Override
62     public AttributeReadingStrategy caseJavaSimpleAttribute(SimpleType<?> openType) {
63         return new SimpleAttributeReadingStrategy(getLastAttribute().getNullableDefault());
64     }
65
66     @Override
67     public AttributeReadingStrategy caseJavaArrayAttribute(ArrayType<?> openType) {
68         SimpleAttributeReadingStrategy innerStrategy = new SimpleAttributeReadingStrategy(getLastAttribute().getNullableDefault());
69         return new ArrayAttributeReadingStrategy(getLastAttribute().getNullableDefault(), innerStrategy);
70     }
71
72     @Override
73     public AttributeReadingStrategy caseJavaCompositeAttribute(CompositeType openType) {
74         Preconditions.checkState(openType.keySet().size() == 1, "Unexpected number of elements for open type %s, should be 1", openType);
75         String mappingKey = openType.keySet().iterator().next();
76         return new SimpleCompositeAttributeReadingStrategy(getLastAttribute().getNullableDefault(), mappingKey);
77     }
78
79     @Override
80     protected AttributeReadingStrategy caseJavaIdentityRefAttribute(OpenType<?> openType) {
81         Preconditions.checkState(openType instanceof CompositeType);
82         Set<String> keys = ((CompositeType) openType).keySet();
83         Preconditions.checkState(keys.size() == 1, "Unexpected number of elements for open type %s, should be 1", openType);
84         String mappingKey = keys.iterator().next();
85         return new SimpleIdentityRefAttributeReadingStrategy(getLastAttribute().getNullableDefault(), mappingKey, identityMap);
86     }
87
88     @Override
89     protected AttributeReadingStrategy caseDependencyAttribute(SimpleType<?> openType) {
90         return new ObjectNameAttributeReadingStrategy(getLastAttribute().getNullableDefault());
91     }
92
93     @Override
94     protected AttributeReadingStrategy caseTOAttribute(CompositeType openType) {
95         AttributeIfc lastAttribute = getLastAttribute();
96         Preconditions.checkState(lastAttribute instanceof TOAttribute);
97         Map<String, AttributeIfc> inner = ((TOAttribute)lastAttribute).getYangPropertiesToTypesMap();
98
99         Map<String, AttributeReadingStrategy> innerStrategies = Maps.newHashMap();
100
101         for (Entry<String, AttributeIfc> innerAttrEntry : inner.entrySet()) {
102             AttributeReadingStrategy innerStrat = prepareReadingStrategy(innerAttrEntry.getKey(),
103                     innerAttrEntry.getValue());
104             innerStrategies.put(innerAttrEntry.getKey(), innerStrat);
105         }
106
107         return new CompositeAttributeReadingStrategy(lastAttribute.getNullableDefault(), innerStrategies);
108     }
109
110     @Override
111     protected AttributeReadingStrategy caseListAttribute(ArrayType<?> openType) {
112         AttributeIfc lastAttribute = getLastAttribute();
113         Preconditions.checkState(lastAttribute instanceof ListAttribute);
114         AttributeReadingStrategy innerStrategy = prepareReadingStrategy(key, ((ListAttribute) lastAttribute).getInnerAttribute());
115         return new ArrayAttributeReadingStrategy(lastAttribute.getNullableDefault(), innerStrategy);
116     }
117
118     @Override
119     protected AttributeReadingStrategy caseListDependeciesAttribute(ArrayType<?> openType) {
120         AttributeIfc lastAttribute = getLastAttribute();
121         Preconditions.checkState(lastAttribute instanceof ListDependenciesAttribute);
122         AttributeReadingStrategy innerStrategy = caseDependencyAttribute(SimpleType.OBJECTNAME);
123         return new ArrayAttributeReadingStrategy(lastAttribute.getNullableDefault(), innerStrategy);
124     }
125
126 }

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