Remove yang-test
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / mapping / CompositeAttributeMappingStrategy.java
1 /*
2  * Copyright (c) 2015, 2017 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.config.facade.xml.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 java.util.Map;
15 import java.util.Set;
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;
20
21 public class CompositeAttributeMappingStrategy
22         extends AbstractAttributeMappingStrategy<Map<String, Object>, CompositeType> {
23
24     private final Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies;
25     private final Map<String, String> jmxToJavaNameMapping;
26
27     public CompositeAttributeMappingStrategy(final CompositeType compositeType,
28             final Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies,
29             final Map<String, String> jmxToJavaNameMapping) {
30         super(compositeType);
31         this.innerStrategies = innerStrategies;
32         this.jmxToJavaNameMapping = jmxToJavaNameMapping;
33     }
34
35     @Override
36     public Optional<Map<String, Object>> mapAttribute(final Object value) {
37         if (value == null) {
38             return Optional.absent();
39         }
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
62         return Optional.of(retVal);
63     }
64
65     protected Optional<?> mapInnerAttribute(final CompositeDataSupport compositeData, final String jmxName,
66             final String description) {
67         Object innerValue = compositeData.get(jmxName);
68
69         AttributeMappingStrategy<?, ? extends OpenType<?>> attributeMappingStrategy = innerStrategies.get(jmxName);
70         return attributeMappingStrategy.mapAttribute(innerValue);
71     }
72 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.