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 / resolving / ObjectResolver.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.resolving;
10
11 import java.util.Map;
12 import java.util.Map.Entry;
13
14 import javax.management.openmbean.ArrayType;
15 import javax.management.openmbean.CompositeType;
16 import javax.management.openmbean.OpenType;
17 import javax.management.openmbean.SimpleType;
18
19 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
20 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
21 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
22 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
23 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
24 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.AttributeIfcSwitchStatement;
25 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services;
26
27 import com.google.common.base.Preconditions;
28 import com.google.common.collect.Maps;
29
30 public class ObjectResolver extends AttributeIfcSwitchStatement<AttributeResolvingStrategy<?, ? extends OpenType<?>>> {
31
32     private final Services serviceTracker;
33     private OpenType<?> openType;
34
35     public ObjectResolver(Services serviceTracker) {
36         this.serviceTracker = serviceTracker;
37     }
38
39     public Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> prepareResolving(
40             Map<String, AttributeIfc> configDefinition) {
41         Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> strategies = Maps.newHashMap();
42
43         for (Entry<String, AttributeIfc> attrEntry : configDefinition.entrySet()) {
44             strategies.put(attrEntry.getKey(),
45                     prepareStrategy(attrEntry.getValue(), attrEntry.getValue().getOpenType()));
46         }
47
48         return strategies;
49     }
50
51     private AttributeResolvingStrategy<?, ? extends OpenType<?>> prepareStrategy(AttributeIfc attributeIfc,
52             OpenType<?> openType) {
53
54         this.openType = openType;
55         return switchAttribute(attributeIfc);
56     }
57
58     private Map<String, String> createYangToJmxMapping(TOAttribute attributeIfc) {
59         Map<String, String> retVal = Maps.newHashMap();
60         for (Entry<String, AttributeIfc> entry : attributeIfc.getYangPropertiesToTypesMap().entrySet()) {
61             retVal.put(entry.getKey(), (entry.getValue()).getLowerCaseCammelCase());
62         }
63         return retVal;
64     }
65
66     @Override
67     protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseJavaAttribute(JavaAttribute attributeIfc) {
68         if (attributeIfc.getOpenType() instanceof SimpleType<?>)
69             return new SimpleAttributeResolvingStrategy((SimpleType<?>) openType);
70         else if (attributeIfc.getOpenType() instanceof ArrayType<?>) {
71             ArrayType<?> arrayType = (ArrayType<?>) openType;
72             SimpleType<?> innerType = (SimpleType<?>) arrayType.getElementOpenType();
73             AttributeResolvingStrategy<?, ? extends OpenType<?>> strat = new SimpleAttributeResolvingStrategy(innerType);
74             return new ArrayAttributeResolvingStrategy(strat, arrayType);
75         }
76         throw new IllegalStateException(JavaAttribute.class + " can only provide open type " + SimpleType.class
77                 + " or " + ArrayType.class);
78     }
79
80     @Override
81     protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseDependencyAttribute(
82             DependencyAttribute attributeIfc) {
83         return new ObjectNameAttributeResolvingStrategy(serviceTracker);
84     }
85
86     @Override
87     protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseTOAttribute(TOAttribute attributeIfc) {
88         CompositeType compositeType = (CompositeType) openType;
89         Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerMap = Maps.newHashMap();
90         for (String innerName : compositeType.keySet()) {
91             Preconditions.checkState(attributeIfc instanceof TOAttribute, "Unexpected state, " + attributeIfc
92                     + " should be instance of " + TOAttribute.class.getName());
93             AttributeIfc innerAttributeIfc = attributeIfc.getJmxPropertiesToTypesMap().get(innerName);
94             innerMap.put(innerAttributeIfc.getAttributeYangName(),
95                     prepareStrategy(innerAttributeIfc, compositeType.getType(innerName)));
96         }
97         return new CompositeAttributeResolvingStrategy(innerMap, compositeType, createYangToJmxMapping(attributeIfc));
98     }
99
100     @Override
101     protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseListAttribute(ListAttribute attributeIfc) {
102         ArrayType<?> arrayType = (ArrayType<?>) openType;
103         OpenType<?> innerType = arrayType.getElementOpenType();
104         AttributeIfc inner = attributeIfc.getInnerAttribute();
105         return new ArrayAttributeResolvingStrategy(prepareStrategy(inner, innerType), arrayType);
106     }
107
108 }

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