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