Mechanical code cleanup (config)
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / resolving / ArrayAttributeResolvingStrategy.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.config.facade.xml.mapping.attributes.resolving;
10
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;
23
24 final class ArrayAttributeResolvingStrategy extends AbstractAttributeResolvingStrategy<Object, ArrayType<?>> {
25
26     private final AttributeResolvingStrategy<?, ? extends OpenType<?>> innerTypeResolvingStrategy;
27
28     private static final Logger LOG = LoggerFactory.getLogger(ArrayAttributeResolvingStrategy.class);
29
30     public ArrayAttributeResolvingStrategy(AttributeResolvingStrategy<?, ? extends OpenType<?>> innerTypeResolved,
31             ArrayType<?> openType) {
32         super(openType);
33         this.innerTypeResolvingStrategy = innerTypeResolved;
34     }
35
36     @Override
37     public Optional<Object> parseAttribute(String attrName, Object value) throws DocumentedException {
38         if (value == null) {
39             return Optional.absent();
40         }
41
42         Util.checkType(value, List.class);
43         List<?> valueList = (List<?>) value;
44
45         Class<?> innerTypeClass = null;
46
47         if (innerTypeResolvingStrategy.getOpenType() instanceof CompositeType) {
48             innerTypeClass = CompositeDataSupport.class;
49         } else {
50             try {
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);
55             }
56         }
57
58         Object parsedArray = null;
59
60         if (getOpenType().isPrimitiveArray()) {
61             Class<?> primitiveType = getPrimitiveType(innerTypeClass);
62             parsedArray = Array.newInstance(primitiveType, valueList.size());
63         } else {
64             parsedArray = Array.newInstance(innerTypeClass, valueList.size());
65         }
66
67         int i = 0;
68         for (Object innerValue : valueList) {
69             Optional<?> parsedElement = innerTypeResolvingStrategy.parseAttribute(attrName + "_" + i, innerValue);
70             if (!parsedElement.isPresent()){
71                 continue;
72             }
73             Array.set(parsedArray, i, parsedElement.get());
74             i++;
75         }
76
77         // Rebuild open type. Underlying composite types might have changed
78         if (innerTypeResolvingStrategy.getOpenType() instanceof CompositeType) {
79             try {
80                 final ArrayType<?> openType =
81                         new ArrayType<>(getOpenType().getDimension(), innerTypeResolvingStrategy.getOpenType());
82                 setOpenType(openType);
83             } catch (OpenDataException e) {
84                 throw new IllegalStateException("An error occurred during restoration of array type " + this
85                         + " for attribute " + attrName + " from value " + value, e);
86             }
87         }
88
89         LOG.debug("Attribute {} : {} parsed to type {} as {}", attrName, value, getOpenType(),
90                 toStringArray(parsedArray));
91
92         return Optional.of(parsedArray);
93     }
94
95     private static String toStringArray(Object array) {
96         StringBuilder build = new StringBuilder(array.toString());
97         build.append(" [");
98         for (int i = 0; i < Array.getLength(array); i++) {
99             build.append(Array.get(array, i).toString());
100             build.append(",");
101         }
102         build.append("]]");
103         return build.toString();
104     }
105
106     private static Class<?> getPrimitiveType(Class<?> innerTypeClass) {
107         try {
108             return (Class<?>) innerTypeClass.getField("TYPE").get(null);
109         } catch (Exception e) {
110             throw new IllegalStateException("Unable to determine primitive type to " + innerTypeClass);
111         }
112     }
113
114     @Override
115     public String toString() {
116         return "ResolvedArrayTypeAttributeType [innerTypeResolved=" + innerTypeResolvingStrategy + "]";
117     }
118 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.