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