Remove yang-test
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / resolving / CompositeAttributeResolvingStrategy.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.resolving;
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 javax.management.openmbean.CompositeDataSupport;
16 import javax.management.openmbean.CompositeType;
17 import javax.management.openmbean.OpenDataException;
18 import javax.management.openmbean.OpenType;
19 import javax.management.openmbean.SimpleType;
20 import org.opendaylight.controller.config.facade.xml.util.Util;
21 import org.opendaylight.controller.config.util.xml.DocumentedException;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 class CompositeAttributeResolvingStrategy
26         extends AbstractAttributeResolvingStrategy<CompositeDataSupport, CompositeType> {
27     private final Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerTypes;
28     private final Map<String, String> yangToJavaAttrMapping;
29
30     private static final Logger LOG = LoggerFactory.getLogger(CompositeAttributeResolvingStrategy.class);
31
32     CompositeAttributeResolvingStrategy(
33             final Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerTypes,
34             final CompositeType openType, final Map<String, String> yangToJavaAttrMapping) {
35         super(openType);
36         this.innerTypes = innerTypes;
37         this.yangToJavaAttrMapping = yangToJavaAttrMapping;
38     }
39
40     @Override
41     public String toString() {
42         return "ResolvedCompositeAttribute [" + innerTypes + "]";
43     }
44
45     @Override
46     public Optional<CompositeDataSupport> parseAttribute(final String attrName, final Object value)
47             throws DocumentedException {
48
49         if (value == null) {
50             return Optional.absent();
51         }
52
53         Util.checkType(value, Map.class);
54         Map<?, ?> valueMap = (Map<?, ?>) value;
55         valueMap = preprocessValueMap(valueMap);
56
57         Map<String, Object> items = Maps.newHashMap();
58         Map<String, OpenType<?>> openTypes = Maps.newHashMap();
59
60         final String[] names = new String[getOpenType().keySet().size()];
61         final String[] descriptions = new String[getOpenType().keySet().size()];
62         OpenType<?>[] itemTypes = new OpenType[names.length];
63         int index = 0;
64
65         for (Object innerAttrName : innerTypes.keySet()) {
66             Preconditions.checkState(innerAttrName instanceof String, "Attribute name must be string");
67             String innerAttrNameStr = (String) innerAttrName;
68
69             AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy = innerTypes
70                     .get(innerAttrName);
71
72             Object valueToParse = valueMap.get(innerAttrName);
73
74             Optional<?> parsedInnerValue = attributeResolvingStrategy.parseAttribute(innerAttrNameStr, valueToParse);
75
76             if (attributeResolvingStrategy instanceof EnumAttributeResolvingStrategy) {
77                 // Open type for enum contain the class name necessary for its resolution,
78                 // however in a DTO
79                 // the open type need to be just SimpleType.STRING so that JMX is happy
80                 // After the enum attribute is resolved, change its open type back to STRING
81                 openTypes.put(innerAttrNameStr, SimpleType.STRING);
82             } else {
83                 openTypes.put(innerAttrNameStr, attributeResolvingStrategy.getOpenType());
84             }
85
86             items.put(yangToJavaAttrMapping.get(innerAttrNameStr),
87                     parsedInnerValue.isPresent() ? parsedInnerValue.get() : null);
88
89             // fill names + item types in order to reconstruct the open type for current
90             // attribute
91             names[index] = yangToJavaAttrMapping.get(innerAttrNameStr);
92             descriptions[index] = getOpenType().getDescription(names[index]);
93             itemTypes[index] = openTypes.get(innerAttrNameStr);
94             index++;
95         }
96
97         CompositeDataSupport parsedValue;
98         try {
99             LOG.trace("Attribute {} with open type {}. Reconstructing open type.", attrName, getOpenType());
100             setOpenType(new CompositeType(getOpenType().getTypeName(), getOpenType().getDescription(), names,
101                     descriptions, itemTypes));
102             LOG.debug("Attribute {}. Open type reconstructed to {}", attrName, getOpenType(), getOpenType());
103             parsedValue = new CompositeDataSupport(getOpenType(), items);
104         } catch (final OpenDataException e) {
105             throw new IllegalStateException("An error occurred during restoration of composite type " + this
106                     + " for attribute " + attrName + " from value " + value, e);
107         }
108
109         LOG.debug("Attribute {} : {} parsed to type {} as {}", attrName, value, getOpenType(), parsedValue);
110
111         return Optional.of(parsedValue);
112     }
113
114     protected Map<?, ?> preprocessValueMap(final Map<?, ?> valueMap) {
115         return valueMap;
116     }
117 }