Added support for union yang types to netconf.
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / mapping / CompositeAttributeMappingStrategy.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.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Maps;
14 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
15
16 import javax.management.openmbean.CompositeDataSupport;
17 import javax.management.openmbean.CompositeType;
18 import javax.management.openmbean.OpenType;
19 import java.util.Map;
20 import java.util.Set;
21
22 public class CompositeAttributeMappingStrategy extends
23         AbstractAttributeMappingStrategy<Map<String, Object>, CompositeType> {
24
25     private final Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies;
26     private final Map<String, String> jmxToJavaNameMapping;
27
28     public CompositeAttributeMappingStrategy(CompositeType compositeType,
29             Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies,
30             Map<String, String> jmxToJavaNameMapping) {
31         super(compositeType);
32         this.innerStrategies = innerStrategies;
33         this.jmxToJavaNameMapping = jmxToJavaNameMapping;
34     }
35
36     @Override
37     public Optional<Map<String, Object>> mapAttribute(Object value) {
38         if (value == null)
39             return Optional.absent();
40
41         Util.checkType(value, CompositeDataSupport.class);
42
43         CompositeDataSupport compositeData = (CompositeDataSupport) value;
44         CompositeType currentType = compositeData.getCompositeType();
45         CompositeType expectedType = getOpenType();
46
47         Set<String> expectedCompositeTypeKeys = expectedType.keySet();
48         Set<String> currentCompositeTypeKeys = currentType.keySet();
49         Preconditions.checkArgument(expectedCompositeTypeKeys.equals(currentCompositeTypeKeys),
50                 "Composite type mismatch, expected composite type with attributes " + expectedCompositeTypeKeys
51                         + " but was " + currentCompositeTypeKeys);
52
53         Map<String, Object> retVal = Maps.newHashMap();
54
55         for (String jmxName : jmxToJavaNameMapping.keySet()) {
56             Optional<?> mapped = mapInnerAttribute(compositeData, jmxName, expectedType.getDescription(jmxName));
57             if(mapped.isPresent())
58                 retVal.put(jmxToJavaNameMapping.get(jmxName), mapped.get());
59         }
60
61         return Optional.of(retVal);
62     }
63
64     protected Optional<?> mapInnerAttribute(CompositeDataSupport compositeData, String jmxName, String description) {
65         Object innerValue = compositeData.get(jmxName);
66
67         AttributeMappingStrategy<?, ? extends OpenType<?>> attributeMappingStrategy = innerStrategies
68                 .get(jmxName);
69         Optional<?> mapAttribute = attributeMappingStrategy.mapAttribute(innerValue);
70         return mapAttribute;
71     }
72
73 }