2 * Copyright (c) 2015, 2017 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.config.facade.xml.mapping.attributes.resolving;
11 import com.google.common.base.Optional;
12 import java.lang.reflect.Array;
13 import java.util.List;
14 import javax.management.openmbean.ArrayType;
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 org.opendaylight.controller.config.facade.xml.util.Util;
20 import org.opendaylight.controller.config.util.xml.DocumentedException;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public final class ArrayAttributeResolvingStrategy extends AbstractAttributeResolvingStrategy<Object, ArrayType<?>> {
26 private final AttributeResolvingStrategy<?, ? extends OpenType<?>> innerTypeResolvingStrategy;
28 private static final Logger LOG = LoggerFactory.getLogger(ArrayAttributeResolvingStrategy.class);
30 public ArrayAttributeResolvingStrategy(final AttributeResolvingStrategy<?, ? extends OpenType<?>> innerTypeResolved,
31 final ArrayType<?> openType) {
33 this.innerTypeResolvingStrategy = innerTypeResolved;
37 public Optional<Object> parseAttribute(final String attrName, final Object value) throws DocumentedException {
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 (final ClassNotFoundException e) {
53 throw new IllegalStateException(
54 "Unable to locate class for " + 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 + "_" + index, innerValue);
70 if (!parsedElement.isPresent()) {
73 Array.set(parsedArray, index, parsedElement.get());
77 // Rebuild open type. Underlying composite types might have changed
78 if (innerTypeResolvingStrategy.getOpenType() instanceof CompositeType) {
80 final ArrayType<?> openType = new ArrayType<>(getOpenType().getDimension(),
81 innerTypeResolvingStrategy.getOpenType());
82 setOpenType(openType);
83 } catch (final OpenDataException e) {
84 throw new IllegalStateException("An error occurred during restoration of array type " + this
85 + " for attribute " + attrName + " from value " + value, e);
89 LOG.debug("Attribute {} : {} parsed to type {} as {}", attrName, value, getOpenType(),
90 toStringArray(parsedArray));
92 return Optional.of(parsedArray);
95 private static String toStringArray(final Object array) {
96 StringBuilder build = new StringBuilder(array.toString());
98 for (int i = 0; i < Array.getLength(array); i++) {
99 build.append(Array.get(array, i).toString());
103 return build.toString();
106 private static Class<?> getPrimitiveType(final Class<?> innerTypeClass) {
108 return (Class<?>) innerTypeClass.getField("TYPE").get(null);
109 } catch (final IllegalAccessException | IllegalArgumentException | NoSuchFieldException e) {
110 throw new IllegalStateException("Unable to determine primitive type to " + innerTypeClass, e);
115 public String toString() {
116 return "ResolvedArrayTypeAttributeType [innerTypeResolved=" + innerTypeResolvingStrategy + "]";