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

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