2 * Copyright (c) 2015, 2017 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.config.facade.xml.mapping.attributes.mapping;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Maps;
16 import javax.management.openmbean.CompositeDataSupport;
17 import javax.management.openmbean.CompositeType;
18 import javax.management.openmbean.OpenType;
19 import org.opendaylight.controller.config.facade.xml.util.Util;
21 public class CompositeAttributeMappingStrategy
22 extends AbstractAttributeMappingStrategy<Map<String, Object>, CompositeType> {
24 private final Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies;
25 private final Map<String, String> jmxToJavaNameMapping;
27 public CompositeAttributeMappingStrategy(final CompositeType compositeType,
28 final Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies,
29 final Map<String, String> jmxToJavaNameMapping) {
31 this.innerStrategies = innerStrategies;
32 this.jmxToJavaNameMapping = jmxToJavaNameMapping;
36 public Optional<Map<String, Object>> mapAttribute(final Object value) {
38 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 Optional<?> mapped = mapInnerAttribute(compositeData, jmxName, expectedType.getDescription(jmxName));
57 if (mapped.isPresent()) {
58 retVal.put(jmxToJavaNameMapping.get(jmxName), mapped.get());
62 return Optional.of(retVal);
65 protected Optional<?> mapInnerAttribute(final CompositeDataSupport compositeData, final String jmxName,
66 final String description) {
67 Object innerValue = compositeData.get(jmxName);
69 AttributeMappingStrategy<?, ? extends OpenType<?>> attributeMappingStrategy = innerStrategies.get(jmxName);
70 return attributeMappingStrategy.mapAttribute(innerValue);