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 org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 import javax.management.openmbean.ArrayType;
17 import javax.management.openmbean.CompositeDataSupport;
18 import javax.management.openmbean.CompositeType;
19 import javax.management.openmbean.OpenType;
20 import java.lang.reflect.Array;
21 import java.util.List;
23 final class ArrayAttributeResolvingStrategy extends AbstractAttributeResolvingStrategy<Object, ArrayType<?>> {
25 private final AttributeResolvingStrategy<?, ? extends OpenType<?>> innerTypeResolvingStrategy;
27 private static final Logger logger = LoggerFactory.getLogger(ArrayAttributeResolvingStrategy.class);
29 public ArrayAttributeResolvingStrategy(AttributeResolvingStrategy<?, ? extends OpenType<?>> innerTypeResolved,
30 ArrayType<?> openType) {
32 this.innerTypeResolvingStrategy = innerTypeResolved;
36 public Optional<Object> parseAttribute(String attrName, Object value) {
38 return Optional.absent();
41 Util.checkType(value, List.class);
42 List<?> valueList = (List<?>) value;
44 Class<?> innerTypeClass = null;
46 if (innerTypeResolvingStrategy.getOpenType() instanceof CompositeType) {
47 innerTypeClass = CompositeDataSupport.class;
50 innerTypeClass = Class.forName(getOpenType().getElementOpenType().getClassName());
51 } catch (ClassNotFoundException e) {
52 throw new RuntimeException("Unable to locate class for "
53 + getOpenType().getElementOpenType().getClassName(), e);
56 Object parsedArray = null;
58 if (getOpenType().isPrimitiveArray()) {
59 Class<?> primitiveType = getPrimitiveType(innerTypeClass);
60 parsedArray = Array.newInstance(primitiveType, valueList.size());
62 parsedArray = Array.newInstance(innerTypeClass, valueList.size());
65 for (Object innerValue : valueList) {
66 Optional<?> parsedElement = innerTypeResolvingStrategy.parseAttribute(attrName + "_" + i, innerValue);
68 if (!parsedElement.isPresent())
71 Array.set(parsedArray, i, parsedElement.get());
72 // parsedArray[i] = parsedElement.get();
76 logger.debug("Attribute {} : {} parsed to type {} as {}", attrName, value, getOpenType(),
77 toStringArray(parsedArray));
79 return Optional.of(parsedArray);
82 private static String toStringArray(Object array) {
83 StringBuilder build = new StringBuilder(array.toString());
85 for (int i = 0; i < Array.getLength(array); i++) {
86 build.append(Array.get(array, i).toString());
90 return build.toString();
93 private static Class<?> getPrimitiveType(Class<?> innerTypeClass) {
95 return (Class<?>) innerTypeClass.getField("TYPE").get(null);
96 } catch (Exception e) {
97 throw new RuntimeException("Unable to determine primitive type to " + innerTypeClass);
102 public String toString() {
103 return "ResolvedArrayTypeAttributeType [innerTypeResolved=" + innerTypeResolvingStrategy + "]";