2 * Copyright (c) 2015 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 com.google.common.base.Preconditions;
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;
21 final class EnumAttributeResolvingStrategy extends AbstractAttributeResolvingStrategy<Object, CompositeType> {
23 private static final Logger LOG = LoggerFactory.getLogger(EnumAttributeResolvingStrategy.class);
24 private final EnumResolver enumResolver;
26 EnumAttributeResolvingStrategy(CompositeType simpleType, final EnumResolver enumResolver) {
28 this.enumResolver = enumResolver;
32 public String toString() {
33 return "ResolvedEnumAttribute [" + getOpenType().getClassName() + "]";
37 public Optional<Object> parseAttribute(String attrName, Object value) throws DocumentedException {
39 return Optional.absent();
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);
48 final String className = getOpenType().getTypeName();
49 final Object parsedValue = enumResolver.fromYang(className, ((String) innerValue));
51 LOG.debug("Attribute {} : {} parsed to enum type {} with value {}", attrName, innerValue, getOpenType(), parsedValue);
52 return Optional.of(parsedValue);