30436bb42f077c72de371e45fc5453849a40f9bc
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / mapping / ArrayAttributeMappingStrategy.java
1 /*
2  * Copyright (c) 2013 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.netconf.confignetconfconnector.mapping.attributes.mapping;
10
11 import java.lang.reflect.Array;
12 import java.util.List;
13
14 import javax.management.openmbean.ArrayType;
15 import javax.management.openmbean.OpenType;
16
17 import com.google.common.base.Optional;
18 import com.google.common.base.Preconditions;
19 import com.google.common.collect.Lists;
20
21 public class ArrayAttributeMappingStrategy extends AbstractAttributeMappingStrategy<List<Object>, ArrayType<?>> {
22
23     private final AttributeMappingStrategy<?, ? extends OpenType<?>> innerElementStrategy;
24
25     public ArrayAttributeMappingStrategy(ArrayType<?> arrayType,
26             AttributeMappingStrategy<?, ? extends OpenType<?>> innerElementStrategy) {
27         super(arrayType);
28         this.innerElementStrategy = innerElementStrategy;
29     }
30
31     @Override
32     public Optional<List<Object>> mapAttribute(Object value) {
33         if (value == null)
34             return Optional.absent();
35
36         Preconditions.checkArgument(value.getClass().isArray(), "Value has to be instanceof Array ");
37
38         List<Object> retVal = Lists.newArrayList();
39
40         for (int i = 0; i < Array.getLength(value); i++) {
41             Object innerValue = Array.get(value, i);
42             // String expectedClassName =
43             // getOpenType().getElementOpenType().getClassName();
44             // String realClassName = value.getClass().getName();
45
46             // Preconditions.checkState(realClassName.contains(expectedClassName),
47             // "Element in collection/array should be of type " +
48             // expectedClassName + " but was "
49             // + realClassName + " for attribute: " + getOpenType());
50
51             Optional<?> mapAttribute = innerElementStrategy.mapAttribute(innerValue);
52
53             if (mapAttribute.isPresent())
54                 retVal.add(mapAttribute.get());
55         }
56
57         return Optional.of(retVal);
58     }
59
60 }