4fa679645f127c7dbcae4bff0921dbaddf976bf2
[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, 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.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     @SuppressWarnings("checkstyle:hiddenField")
35     public Map<String, AttributeReadingStrategy> prepareReading(final Map<String, AttributeIfc> yangToAttrConfig,
36             final Map<String, Map<Date, IdentityMapping>> identityMap) {
37         Map<String, AttributeReadingStrategy> strategies = Maps.newHashMap();
38         this.identityMap = identityMap;
39
40         for (Entry<String, AttributeIfc> attributeEntry : yangToAttrConfig.entrySet()) {
41             AttributeReadingStrategy strat = prepareReadingStrategy(attributeEntry.getKey(), attributeEntry.getValue());
42             strategies.put(attributeEntry.getKey(), strat);
43         }
44         return strategies;
45     }
46
47     private AttributeReadingStrategy prepareReadingStrategy(final String attrKey, final AttributeIfc attributeIfc) {
48         this.key = attrKey;
49         return switchAttribute(attributeIfc);
50     }
51
52     @Override
53     protected AttributeReadingStrategy caseJavaBinaryAttribute(final OpenType<?> openType) {
54         return new SimpleBinaryAttributeReadingStrategy(getLastAttribute().getNullableDefault());
55     }
56
57     @Override
58     protected AttributeReadingStrategy caseJavaUnionAttribute(final OpenType<?> openType) {
59         String mappingKey = JavaAttribute.DESCRIPTION_OF_VALUE_ATTRIBUTE_FOR_UNION;
60         return new SimpleUnionAttributeReadingStrategy(getLastAttribute().getNullableDefault(), mappingKey);
61     }
62
63     @Override
64     public AttributeReadingStrategy caseJavaSimpleAttribute(final SimpleType<?> openType) {
65         return new SimpleAttributeReadingStrategy(getLastAttribute().getNullableDefault());
66     }
67
68     @Override
69     public AttributeReadingStrategy caseJavaArrayAttribute(final ArrayType<?> openType) {
70         SimpleAttributeReadingStrategy innerStrategy = new SimpleAttributeReadingStrategy(
71                 getLastAttribute().getNullableDefault());
72         return new ArrayAttributeReadingStrategy(getLastAttribute().getNullableDefault(), innerStrategy);
73     }
74
75     @Override
76     public AttributeReadingStrategy caseJavaCompositeAttribute(final CompositeType openType) {
77         Preconditions.checkState(openType.keySet().size() == 1,
78                 "Unexpected number of elements for open type %s, should be 1", openType);
79         String mappingKey = openType.keySet().iterator().next();
80         return new SimpleCompositeAttributeReadingStrategy(getLastAttribute().getNullableDefault(), mappingKey);
81     }
82
83     @Override
84     protected AttributeReadingStrategy caseJavaIdentityRefAttribute(final OpenType<?> openType) {
85         Preconditions.checkState(openType instanceof CompositeType);
86         Set<String> keys = ((CompositeType) openType).keySet();
87         Preconditions.checkState(keys.size() == 1, "Unexpected number of elements for open type %s, should be 1",
88                 openType);
89         String mappingKey = keys.iterator().next();
90         return new SimpleIdentityRefAttributeReadingStrategy(getLastAttribute().getNullableDefault(), mappingKey,
91                 identityMap);
92     }
93
94     @Override
95     protected AttributeReadingStrategy caseDependencyAttribute(final SimpleType<?> openType) {
96         return new ObjectNameAttributeReadingStrategy(getLastAttribute().getNullableDefault());
97     }
98
99     @Override
100     protected AttributeReadingStrategy caseTOAttribute(final CompositeType openType) {
101         AttributeIfc lastAttribute = getLastAttribute();
102         Preconditions.checkState(lastAttribute instanceof TOAttribute);
103         Map<String, AttributeIfc> inner = ((TOAttribute) lastAttribute).getYangPropertiesToTypesMap();
104
105         Map<String, AttributeReadingStrategy> innerStrategies = Maps.newHashMap();
106
107         for (Entry<String, AttributeIfc> innerAttrEntry : inner.entrySet()) {
108             AttributeReadingStrategy innerStrat = prepareReadingStrategy(innerAttrEntry.getKey(),
109                     innerAttrEntry.getValue());
110             innerStrategies.put(innerAttrEntry.getKey(), innerStrat);
111         }
112         return new CompositeAttributeReadingStrategy(lastAttribute.getNullableDefault(), innerStrategies);
113     }
114
115     @Override
116     protected AttributeReadingStrategy caseListAttribute(final ArrayType<?> openType) {
117         AttributeIfc lastAttribute = getLastAttribute();
118         Preconditions.checkState(lastAttribute instanceof ListAttribute);
119         AttributeReadingStrategy innerStrategy = prepareReadingStrategy(key,
120                 ((ListAttribute) lastAttribute).getInnerAttribute());
121         return new ArrayAttributeReadingStrategy(lastAttribute.getNullableDefault(), innerStrategy);
122     }
123
124     @Override
125     protected AttributeReadingStrategy caseListDependeciesAttribute(final ArrayType<?> openType) {
126         AttributeIfc lastAttribute = getLastAttribute();
127         Preconditions.checkState(lastAttribute instanceof ListDependenciesAttribute);
128         AttributeReadingStrategy innerStrategy = caseDependencyAttribute(SimpleType.OBJECTNAME);
129         return new ArrayAttributeReadingStrategy(lastAttribute.getNullableDefault(), innerStrategy);
130     }
131 }

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