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;
14 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
18 import javax.management.openmbean.CompositeDataSupport;
19 import javax.management.openmbean.CompositeType;
20 import javax.management.openmbean.OpenDataException;
21 import javax.management.openmbean.OpenType;
24 class CompositeAttributeResolvingStrategy extends
25 AbstractAttributeResolvingStrategy<CompositeDataSupport, CompositeType> {
26 private final Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerTypes;
27 private final Map<String, String> yangToJavaAttrMapping;
29 private static final Logger logger = LoggerFactory.getLogger(CompositeAttributeResolvingStrategy.class);
31 CompositeAttributeResolvingStrategy(Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerTypes,
32 CompositeType openType, Map<String, String> yangToJavaAttrMapping) {
34 this.innerTypes = innerTypes;
35 this.yangToJavaAttrMapping = yangToJavaAttrMapping;
39 public String toString() {
40 return "ResolvedCompositeAttribute [" + innerTypes + "]";
44 public Optional<CompositeDataSupport> parseAttribute(String attrName, Object value) {
47 return Optional.absent();
50 Util.checkType(value, Map.class);
51 Map<?, ?> valueMap = (Map<?, ?>) value;
52 valueMap = preprocessValueMap(valueMap);
54 Map<String, Object> items = Maps.newHashMap();
55 Map<String, OpenType<?>> openTypes = Maps.newHashMap();
57 for (Object innerAttrName : innerTypes.keySet()) {
58 Preconditions.checkState(innerAttrName instanceof String, "Attribute name must be string");
59 String innerAttrNameStr = (String) innerAttrName;
61 AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy = innerTypes
64 Object valueToParse = valueMap.get(innerAttrName);
66 Optional<?> parsedInnerValue = attributeResolvingStrategy.parseAttribute(innerAttrNameStr, valueToParse);
68 openTypes.put(innerAttrNameStr, attributeResolvingStrategy.getOpenType());
70 items.put(yangToJavaAttrMapping.get(innerAttrNameStr),
71 parsedInnerValue.isPresent() ? parsedInnerValue.get() : null);
74 CompositeDataSupport parsedValue;
76 parsedValue = new CompositeDataSupport(getOpenType(), items);
77 } catch (OpenDataException e) {
78 throw new RuntimeException("An error occured during restoration of composite type " + this
79 + " for attribute " + attrName + " from value " + value, e);
82 logger.debug("Attribute {} : {} parsed to type {} as {}", attrName, value, getOpenType(), parsedValue);
84 return Optional.of(parsedValue);
87 protected Map<?, ?> preprocessValueMap(Map<?, ?> valueMap) {