Merge "HostTracker hosts DB key scheme implementation"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / mapping / ObjectMapper.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.mapping;
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 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services;
19
20 import javax.management.openmbean.ArrayType;
21 import javax.management.openmbean.CompositeType;
22 import javax.management.openmbean.OpenType;
23 import javax.management.openmbean.SimpleType;
24 import java.util.Map;
25 import java.util.Map.Entry;
26
27 public class ObjectMapper extends AttributeIfcSwitchStatement<AttributeMappingStrategy<?, ? extends OpenType<?>>> {
28
29     private final Services dependencyTracker;
30
31     public ObjectMapper(Services depTracker) {
32         this.dependencyTracker = depTracker;
33     }
34
35     public Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> prepareMapping(
36             Map<String, AttributeIfc> configDefinition) {
37         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> strategies = Maps.newHashMap();
38
39         for (Entry<String, AttributeIfc> attrEntry : configDefinition.entrySet()) {
40             strategies.put(attrEntry.getKey(), prepareStrategy(attrEntry.getValue()));
41         }
42
43         return strategies;
44     }
45
46     public AttributeMappingStrategy<?, ? extends OpenType<?>> prepareStrategy(AttributeIfc attributeIfc) {
47         return switchAttribute(attributeIfc);
48     }
49
50     private Map<String, String> createJmxToYangMapping(TOAttribute attributeIfc) {
51         Map<String, String> retVal = Maps.newHashMap();
52         for (Entry<String, AttributeIfc> entry : attributeIfc.getJmxPropertiesToTypesMap().entrySet()) {
53             retVal.put(entry.getKey(), (entry.getValue()).getAttributeYangName());
54         }
55         return retVal;
56     }
57
58     @Override
59     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaAttribute(JavaAttribute attributeIfc) {
60
61         if (attributeIfc.getOpenType() instanceof SimpleType<?>)
62             return new SimpleAttributeMappingStrategy((SimpleType<?>) attributeIfc.getOpenType());
63         else if (attributeIfc.getOpenType() instanceof ArrayType<?>) {
64             ArrayType<?> arrayType = (ArrayType<?>) attributeIfc.getOpenType();
65             AttributeMappingStrategy<?, ? extends OpenType<?>> innerStrategy = new SimpleAttributeMappingStrategy(
66                     (SimpleType<?>) arrayType.getElementOpenType());
67             return new ArrayAttributeMappingStrategy(arrayType, innerStrategy);
68         }
69         throw new IllegalStateException(JavaAttribute.class + " can only provide open type " + SimpleType.class
70                 + " or " + ArrayType.class);
71     }
72
73     @Override
74     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseDependencyAttribute(
75             DependencyAttribute attributeIfc) {
76         String serviceName = attributeIfc.getDependency().getSie().getQName().getLocalName();
77         String namespace = attributeIfc.getDependency().getSie().getQName().getNamespace().toString();
78         return new ObjectNameAttributeMappingStrategy((SimpleType<?>) attributeIfc.getOpenType(), dependencyTracker,
79                 serviceName, namespace);
80     }
81
82     @Override
83     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseTOAttribute(TOAttribute attributeIfc) {
84         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
85
86         for (Entry<String, AttributeIfc> innerAttrEntry : attributeIfc.getJmxPropertiesToTypesMap().entrySet()) {
87             innerStrategies.put(innerAttrEntry.getKey(), prepareStrategy(innerAttrEntry.getValue()));
88         }
89
90         return new CompositeAttributeMappingStrategy((CompositeType) attributeIfc.getOpenType(), innerStrategies,
91                 createJmxToYangMapping(attributeIfc));
92     }
93
94     @Override
95     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListAttribute(ListAttribute attributeIfc) {
96         return new ArrayAttributeMappingStrategy(attributeIfc.getOpenType(),
97                 prepareStrategy(attributeIfc.getInnerAttribute()));
98     }
99
100 }