252b13bf6850c498d6c122565044359647dd5520
[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             String innerAttrJmxName = jmxName;
57             Object innerValue = compositeData.get(innerAttrJmxName);
58
59             AttributeMappingStrategy<?, ? extends OpenType<?>> attributeMappingStrategy = innerStrategies
60                     .get(innerAttrJmxName);
61             Optional<?> mapAttribute = attributeMappingStrategy.mapAttribute(innerValue);
62             if (mapAttribute.isPresent())
63                 retVal.put(jmxToJavaNameMapping.get(innerAttrJmxName), mapAttribute.get());
64         }
65
66         return Optional.of(retVal);
67     }
68
69 }