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

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