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.api.NetconfDocumentedException;
13 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 import javax.management.openmbean.ArrayType;
18 import javax.management.openmbean.CompositeDataSupport;
19 import javax.management.openmbean.CompositeType;
20 import javax.management.openmbean.OpenType;
21 import java.lang.reflect.Array;
22 import java.util.List;
24 final class ArrayAttributeResolvingStrategy extends AbstractAttributeResolvingStrategy<Object, ArrayType<?>> {
26 private final AttributeResolvingStrategy<?, ? extends OpenType<?>> innerTypeResolvingStrategy;
28 private static final Logger logger = LoggerFactory.getLogger(ArrayAttributeResolvingStrategy.class);
30 public ArrayAttributeResolvingStrategy(AttributeResolvingStrategy<?, ? extends OpenType<?>> innerTypeResolved,
31 ArrayType<?> openType) {
33 this.innerTypeResolvingStrategy = innerTypeResolved;
37 public Optional<Object> parseAttribute(String attrName, Object value) throws NetconfDocumentedException {
39 return Optional.absent();
42 Util.checkType(value, List.class);
43 List<?> valueList = (List<?>) value;
45 Class<?> innerTypeClass = null;
47 if (innerTypeResolvingStrategy.getOpenType() instanceof CompositeType) {
48 innerTypeClass = CompositeDataSupport.class;
51 innerTypeClass = Class.forName(getOpenType().getElementOpenType().getClassName());
52 } catch (ClassNotFoundException e) {
53 throw new IllegalStateException("Unable to locate class for "
54 + getOpenType().getElementOpenType().getClassName(), e);
58 Object parsedArray = null;
60 if (getOpenType().isPrimitiveArray()) {
61 Class<?> primitiveType = getPrimitiveType(innerTypeClass);
62 parsedArray = Array.newInstance(primitiveType, valueList.size());
64 parsedArray = Array.newInstance(innerTypeClass, valueList.size());
68 for (Object innerValue : valueList) {
69 Optional<?> parsedElement = innerTypeResolvingStrategy.parseAttribute(attrName + "_" + i, innerValue);
70 if (!parsedElement.isPresent()){
73 Array.set(parsedArray, i, parsedElement.get());
77 logger.debug("Attribute {} : {} parsed to type {} as {}", attrName, value, getOpenType(),
78 toStringArray(parsedArray));
80 return Optional.of(parsedArray);
83 private static String toStringArray(Object array) {
84 StringBuilder build = new StringBuilder(array.toString());
86 for (int i = 0; i < Array.getLength(array); i++) {
87 build.append(Array.get(array, i).toString());
91 return build.toString();
94 private static Class<?> getPrimitiveType(Class<?> innerTypeClass) {
96 return (Class<?>) innerTypeClass.getField("TYPE").get(null);
97 } catch (Exception e) {
98 throw new IllegalStateException("Unable to determine primitive type to " + innerTypeClass);
103 public String toString() {
104 return "ResolvedArrayTypeAttributeType [innerTypeResolved=" + innerTypeResolvingStrategy + "]";