Fix star import and enable checkstyle rule to prevent it.
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / fromxml / ObjectXmlReader.java
1 /*
2  * Copyright (c) 2013 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.netconf.confignetconfconnector.mapping.attributes.fromxml;
10
11 import com.google.common.collect.Maps;
12 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
13 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
14 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
15 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
16 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
17 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.AttributeIfcSwitchStatement;
18
19 import javax.management.openmbean.ArrayType;
20 import javax.management.openmbean.SimpleType;
21 import java.util.Map;
22 import java.util.Map.Entry;
23
24 public class ObjectXmlReader extends AttributeIfcSwitchStatement<AttributeReadingStrategy> {
25
26     private String key;
27
28     public Map<String, AttributeReadingStrategy> prepareReading(Map<String, AttributeIfc> yangToAttrConfig) {
29         Map<String, AttributeReadingStrategy> strategies = Maps.newHashMap();
30
31         for (Entry<String, AttributeIfc> attributeEntry : yangToAttrConfig.entrySet()) {
32             AttributeReadingStrategy strat = prepareReadingStrategy(attributeEntry.getKey(), attributeEntry.getValue());
33             strategies.put(attributeEntry.getKey(), strat);
34         }
35         return strategies;
36     }
37
38     private AttributeReadingStrategy prepareReadingStrategy(String key, AttributeIfc attributeIfc) {
39         this.key = key;
40         return switchAttribute(attributeIfc);
41     }
42
43     @Override
44     protected AttributeReadingStrategy caseJavaAttribute(JavaAttribute attributeIfc) {
45         if (attributeIfc.getOpenType() instanceof SimpleType<?>)
46             return new SimpleAttributeReadingStrategy(attributeIfc);
47         else if (attributeIfc.getOpenType() instanceof ArrayType<?>) {
48             SimpleAttributeReadingStrategy innerStrategy = new SimpleAttributeReadingStrategy(
49                     ((ArrayType<?>) attributeIfc.getOpenType()).getElementOpenType());
50             return new ArrayAttributeReadingStrategy(attributeIfc, innerStrategy);
51         }
52         throw new IllegalStateException(JavaAttribute.class + " can only provide open type " + SimpleType.class
53                 + " or " + ArrayType.class);
54     }
55
56     @Override
57     protected AttributeReadingStrategy caseDependencyAttribute(DependencyAttribute attributeIfc) {
58         return new ObjectNameAttributeReadingStrategy(attributeIfc);
59     }
60
61     @Override
62     protected AttributeReadingStrategy caseTOAttribute(TOAttribute attributeIfc) {
63         Map<String, AttributeIfc> inner = attributeIfc.getYangPropertiesToTypesMap();
64         Map<String, AttributeReadingStrategy> innerStrategies = Maps.newHashMap();
65
66         for (Entry<String, AttributeIfc> innerAttrEntry : inner.entrySet()) {
67             AttributeReadingStrategy innerStrat = prepareReadingStrategy(innerAttrEntry.getKey(),
68                     innerAttrEntry.getValue());
69             innerStrategies.put(innerAttrEntry.getKey(), innerStrat);
70         }
71
72         return new CompositeAttributeReadingStrategy(attributeIfc, innerStrategies);
73     }
74
75     @Override
76     protected AttributeReadingStrategy caseListAttribute(ListAttribute attributeIfc) {
77         AttributeIfc innerAttr = attributeIfc.getInnerAttribute();
78         AttributeReadingStrategy innerStrategy = prepareReadingStrategy(key, innerAttr);
79         return new ArrayAttributeReadingStrategy(attributeIfc, innerStrategy);
80     }
81
82 }

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