CDS: Add stress test RPC to the cars model
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / resolving / EnumAttributeResolvingStrategy.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 com.google.common.base.Preconditions;
13 import java.util.Map;
14 import javax.management.openmbean.CompositeType;
15 import org.opendaylight.controller.config.facade.xml.osgi.EnumResolver;
16 import org.opendaylight.controller.config.facade.xml.util.Util;
17 import org.opendaylight.controller.config.util.xml.DocumentedException;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 final class EnumAttributeResolvingStrategy extends AbstractAttributeResolvingStrategy<Object, CompositeType> {
22
23     private static final Logger LOG = LoggerFactory.getLogger(EnumAttributeResolvingStrategy.class);
24     private final EnumResolver enumResolver;
25
26     EnumAttributeResolvingStrategy(CompositeType simpleType, final EnumResolver enumResolver) {
27         super(simpleType);
28         this.enumResolver = enumResolver;
29     }
30
31     @Override
32     public String toString() {
33         return "ResolvedEnumAttribute [" + getOpenType().getClassName() + "]";
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, Map.class);
43         Map<?, ?> valueMap = (Map<?, ?>) value;
44         Preconditions.checkArgument(valueMap.size() == 1, "Unexpected value size " + value + " should be just 1 foe enum");
45         final Object innerValue = valueMap.values().iterator().next();
46         Util.checkType(innerValue, String.class);
47
48         final String className = getOpenType().getTypeName();
49         final Object parsedValue = enumResolver.fromYang(className, ((String) innerValue));
50
51         LOG.debug("Attribute {} : {} parsed to enum type {} with value {}", attrName, innerValue, getOpenType(), parsedValue);
52         return Optional.of(parsedValue);
53     }
54
55 }