2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping;
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;
16 import javax.management.openmbean.CompositeDataSupport;
17 import javax.management.openmbean.CompositeType;
18 import javax.management.openmbean.OpenType;
22 public class CompositeAttributeMappingStrategy extends
23 AbstractAttributeMappingStrategy<Map<String, Object>, CompositeType> {
25 private final Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies;
26 private final Map<String, String> jmxToJavaNameMapping;
28 public CompositeAttributeMappingStrategy(CompositeType compositeType,
29 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies,
30 Map<String, String> jmxToJavaNameMapping) {
32 this.innerStrategies = innerStrategies;
33 this.jmxToJavaNameMapping = jmxToJavaNameMapping;
37 public Optional<Map<String, Object>> mapAttribute(Object value) {
39 return Optional.absent();
41 Util.checkType(value, CompositeDataSupport.class);
43 CompositeDataSupport compositeData = (CompositeDataSupport) value;
44 CompositeType currentType = compositeData.getCompositeType();
45 CompositeType expectedType = getOpenType();
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);
53 Map<String, Object> retVal = Maps.newHashMap();
55 for (String jmxName : jmxToJavaNameMapping.keySet()) {
56 String innerAttrJmxName = jmxName;
57 Object innerValue = compositeData.get(innerAttrJmxName);
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());
66 return Optional.of(retVal);