/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.resolving; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.collect.Maps; import org.opendaylight.controller.netconf.api.NetconfDocumentedException; import org.opendaylight.controller.netconf.confignetconfconnector.util.Util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.management.openmbean.CompositeDataSupport; import javax.management.openmbean.CompositeType; import javax.management.openmbean.OpenDataException; import javax.management.openmbean.OpenType; import java.util.Map; class CompositeAttributeResolvingStrategy extends AbstractAttributeResolvingStrategy { private final Map>> innerTypes; private final Map yangToJavaAttrMapping; private static final Logger logger = LoggerFactory.getLogger(CompositeAttributeResolvingStrategy.class); CompositeAttributeResolvingStrategy(Map>> innerTypes, CompositeType openType, Map yangToJavaAttrMapping) { super(openType); this.innerTypes = innerTypes; this.yangToJavaAttrMapping = yangToJavaAttrMapping; } @Override public String toString() { return "ResolvedCompositeAttribute [" + innerTypes + "]"; } @Override public Optional parseAttribute(String attrName, Object value) throws NetconfDocumentedException { if (value == null) { return Optional.absent(); } Util.checkType(value, Map.class); Map valueMap = (Map) value; valueMap = preprocessValueMap(valueMap); Map items = Maps.newHashMap(); Map> openTypes = Maps.newHashMap(); for (Object innerAttrName : innerTypes.keySet()) { Preconditions.checkState(innerAttrName instanceof String, "Attribute name must be string"); String innerAttrNameStr = (String) innerAttrName; AttributeResolvingStrategy> attributeResolvingStrategy = innerTypes .get(innerAttrName); Object valueToParse = valueMap.get(innerAttrName); Optional parsedInnerValue = attributeResolvingStrategy.parseAttribute(innerAttrNameStr, valueToParse); openTypes.put(innerAttrNameStr, attributeResolvingStrategy.getOpenType()); items.put(yangToJavaAttrMapping.get(innerAttrNameStr), parsedInnerValue.isPresent() ? parsedInnerValue.get() : null); } CompositeDataSupport parsedValue; try { parsedValue = new CompositeDataSupport(getOpenType(), items); } catch (OpenDataException e) { throw new IllegalStateException("An error occured during restoration of composite type " + this + " for attribute " + attrName + " from value " + value, e); } logger.debug("Attribute {} : {} parsed to type {} as {}", attrName, value, getOpenType(), parsedValue); return Optional.of(parsedValue); } protected Map preprocessValueMap(Map valueMap) { return valueMap; } }