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.resolving;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Maps;
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.netconf.api.NetconfDocumentedException;
21 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 class CompositeAttributeResolvingStrategy extends
26 AbstractAttributeResolvingStrategy<CompositeDataSupport, CompositeType> {
27 private final Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerTypes;
28 private final Map<String, String> yangToJavaAttrMapping;
30 private static final Logger LOG = LoggerFactory.getLogger(CompositeAttributeResolvingStrategy.class);
32 CompositeAttributeResolvingStrategy(Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerTypes,
33 CompositeType openType, Map<String, String> yangToJavaAttrMapping) {
35 this.innerTypes = innerTypes;
36 this.yangToJavaAttrMapping = yangToJavaAttrMapping;
40 public String toString() {
41 return "ResolvedCompositeAttribute [" + innerTypes + "]";
45 public Optional<CompositeDataSupport> parseAttribute(String attrName, Object value) throws NetconfDocumentedException {
48 return Optional.absent();
51 Util.checkType(value, Map.class);
52 Map<?, ?> valueMap = (Map<?, ?>) value;
53 valueMap = preprocessValueMap(valueMap);
55 Map<String, Object> items = Maps.newHashMap();
56 Map<String, OpenType<?>> openTypes = Maps.newHashMap();
58 final String[] names = new String[getOpenType().keySet().size()];
59 final String[] descriptions = new String[getOpenType().keySet().size()];
60 OpenType<?>[] itemTypes = new OpenType[names.length];
63 for (Object innerAttrName : innerTypes.keySet()) {
64 Preconditions.checkState(innerAttrName instanceof String, "Attribute name must be string");
65 String innerAttrNameStr = (String) innerAttrName;
67 AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy = innerTypes
70 Object valueToParse = valueMap.get(innerAttrName);
72 Optional<?> parsedInnerValue = attributeResolvingStrategy.parseAttribute(innerAttrNameStr, valueToParse);
74 if(attributeResolvingStrategy instanceof EnumAttributeResolvingStrategy) {
75 // Open type for enum contain the class name necessary for its resolution, however in a DTO
76 // the open type need to be just SimpleType.STRING so that JMX is happy
77 // After the enum attribute is resolved, change its open type back to STRING
78 openTypes.put(innerAttrNameStr, SimpleType.STRING);
80 openTypes.put(innerAttrNameStr, attributeResolvingStrategy.getOpenType());
83 items.put(yangToJavaAttrMapping.get(innerAttrNameStr),
84 parsedInnerValue.isPresent() ? parsedInnerValue.get() : null);
86 // fill names + item types in order to reconstruct the open type for current attribute
87 names[i] = yangToJavaAttrMapping.get(innerAttrNameStr);
88 descriptions[i] = getOpenType().getDescription(names[i]);
89 itemTypes[i] = openTypes.get(innerAttrNameStr);
93 CompositeDataSupport parsedValue;
95 LOG.trace("Attribute {} with open type {}. Reconstructing open type.", attrName, getOpenType());
96 setOpenType(new CompositeType(getOpenType().getTypeName(), getOpenType().getDescription(), names, descriptions, itemTypes));
97 LOG.debug("Attribute {}. Open type reconstructed to {}", attrName, getOpenType(), getOpenType());
98 parsedValue = new CompositeDataSupport(getOpenType(), items);
99 } catch (OpenDataException e) {
100 throw new IllegalStateException("An error occurred during restoration of composite type " + this
101 + " for attribute " + attrName + " from value " + value, e);
104 LOG.debug("Attribute {} : {} parsed to type {} as {}", attrName, value, getOpenType(), parsedValue);
106 return Optional.of(parsedValue);
110 protected Map<?, ?> preprocessValueMap(Map<?, ?> valueMap) {