/* * 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.fromxml; import com.google.common.base.Preconditions; import com.google.common.collect.Maps; import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc; import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute; import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute; import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.AttributeIfcSwitchStatement; import javax.management.openmbean.ArrayType; import javax.management.openmbean.CompositeType; import javax.management.openmbean.SimpleType; import java.util.Map; import java.util.Map.Entry; public class ObjectXmlReader extends AttributeIfcSwitchStatement { private String key; public Map prepareReading(Map yangToAttrConfig) { Map strategies = Maps.newHashMap(); for (Entry attributeEntry : yangToAttrConfig.entrySet()) { AttributeReadingStrategy strat = prepareReadingStrategy(attributeEntry.getKey(), attributeEntry.getValue()); strategies.put(attributeEntry.getKey(), strat); } return strategies; } private AttributeReadingStrategy prepareReadingStrategy(String key, AttributeIfc attributeIfc) { this.key = key; return switchAttribute(attributeIfc); } @Override public AttributeReadingStrategy caseJavaSimpleAttribute(SimpleType openType) { return new SimpleAttributeReadingStrategy(lastAttribute.getNullableDefault()); } @Override public AttributeReadingStrategy caseJavaArrayAttribute(ArrayType openType) { SimpleAttributeReadingStrategy innerStrategy = new SimpleAttributeReadingStrategy(lastAttribute.getNullableDefault()); return new ArrayAttributeReadingStrategy(lastAttribute.getNullableDefault(), innerStrategy); } @Override public AttributeReadingStrategy caseJavaCompositeAttribute(CompositeType openType) { Preconditions.checkState(openType.keySet().size() == 1, "Unexpected number of elements for open type %s, should be 1", openType); String mappingKey = openType.keySet().iterator().next(); return new SimpleCompositeAttributeReadingStrategy(lastAttribute.getNullableDefault(), mappingKey); } @Override protected AttributeReadingStrategy caseDependencyAttribute(SimpleType openType) { return new ObjectNameAttributeReadingStrategy(lastAttribute.getNullableDefault()); } @Override protected AttributeReadingStrategy caseTOAttribute(CompositeType openType) { Preconditions.checkState(lastAttribute instanceof TOAttribute); Map inner = ((TOAttribute)lastAttribute).getYangPropertiesToTypesMap(); Map innerStrategies = Maps.newHashMap(); for (Entry innerAttrEntry : inner.entrySet()) { AttributeReadingStrategy innerStrat = prepareReadingStrategy(innerAttrEntry.getKey(), innerAttrEntry.getValue()); innerStrategies.put(innerAttrEntry.getKey(), innerStrat); } return new CompositeAttributeReadingStrategy(lastAttribute.getNullableDefault(), innerStrategies); } @Override protected AttributeReadingStrategy caseListAttribute(ArrayType openType) { Preconditions.checkState(lastAttribute instanceof ListAttribute); AttributeReadingStrategy innerStrategy = prepareReadingStrategy(key, ((ListAttribute) lastAttribute).getInnerAttribute()); return new ArrayAttributeReadingStrategy(lastAttribute.getNullableDefault(), innerStrategy); } }