X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fconfig%2Fconfig-manager-facade-xml%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Ffacade%2Fxml%2Fmapping%2Fattributes%2Fmapping%2FObjectMapper.java;fp=opendaylight%2Fconfig%2Fconfig-manager-facade-xml%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Ffacade%2Fxml%2Fmapping%2Fattributes%2Fmapping%2FObjectMapper.java;h=65a3367a6b39af8e651763b914c77a0e89743daf;hb=23fe9ca678ada6263fec5dd996f4025e4a32fcf5;hp=0000000000000000000000000000000000000000;hpb=071a641d7c12c0e6112d5ce0afe806b54f116ed2;p=controller.git diff --git a/opendaylight/config/config-manager-facade-xml/src/main/java/org/opendaylight/controller/config/facade/xml/mapping/attributes/mapping/ObjectMapper.java b/opendaylight/config/config-manager-facade-xml/src/main/java/org/opendaylight/controller/config/facade/xml/mapping/attributes/mapping/ObjectMapper.java new file mode 100644 index 0000000000..65a3367a6b --- /dev/null +++ b/opendaylight/config/config-manager-facade-xml/src/main/java/org/opendaylight/controller/config/facade/xml/mapping/attributes/mapping/ObjectMapper.java @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2015 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.config.facade.xml.mapping.attributes.mapping; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Maps; +import java.util.Map; +import java.util.Map.Entry; +import javax.management.openmbean.ArrayType; +import javax.management.openmbean.CompositeType; +import javax.management.openmbean.OpenType; +import javax.management.openmbean.SimpleType; +import org.opendaylight.controller.config.facade.xml.mapping.attributes.AttributeIfcSwitchStatement; +import org.opendaylight.controller.config.facade.xml.osgi.EnumResolver; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute; + +public class ObjectMapper extends AttributeIfcSwitchStatement>> { + + private EnumResolver enumResolver; + + public Map>> prepareMapping( + Map configDefinition, EnumResolver enumResolver) { + this.enumResolver = Preconditions.checkNotNull(enumResolver); + Map>> strategies = Maps.newHashMap(); + + for (Entry attrEntry : configDefinition.entrySet()) { + strategies.put(attrEntry.getKey(), prepareStrategy(attrEntry.getValue())); + } + + return strategies; + } + + public AttributeMappingStrategy> prepareStrategy(AttributeIfc attributeIfc) { + + if(attributeIfc instanceof DependencyAttribute) { + namespaceOfDepAttr = ((DependencyAttribute)attributeIfc).getDependency().getSie().getQName().getNamespace().toString(); + } else if (attributeIfc instanceof ListDependenciesAttribute) { + namespaceOfDepAttr = ((ListDependenciesAttribute)attributeIfc).getDependency().getSie().getQName().getNamespace().toString(); + } + + return switchAttribute(attributeIfc); + } + + private Map createJmxToYangMapping(TOAttribute attributeIfc) { + Map retVal = Maps.newHashMap(); + for (Entry entry : attributeIfc.getJmxPropertiesToTypesMap().entrySet()) { + retVal.put(entry.getKey(), (entry.getValue()).getAttributeYangName()); + } + return retVal; + } + + @Override + protected AttributeMappingStrategy> caseJavaSimpleAttribute(SimpleType openType) { + return new SimpleAttributeMappingStrategy(openType); + } + + @Override + protected AttributeMappingStrategy> caseJavaEnumAttribute(final OpenType openType) { + return new EnumAttributeMappingStrategy(((CompositeType) openType), enumResolver); + } + + @Override + protected AttributeMappingStrategy> caseJavaArrayAttribute(ArrayType openType) { + + AttributeMappingStrategy> innerStrategy = new SimpleAttributeMappingStrategy( + (SimpleType) openType.getElementOpenType()); + return new ArrayAttributeMappingStrategy(openType, innerStrategy); + } + + @Override + protected AttributeMappingStrategy> caseJavaCompositeAttribute(CompositeType openType) { + Map>> innerStrategies = Maps.newHashMap(); + + Map attributeMapping = Maps.newHashMap(); + + for (String innerAttributeKey : openType.keySet()) { + + innerStrategies.put(innerAttributeKey, caseJavaAttribute(openType.getType(innerAttributeKey))); + attributeMapping.put(innerAttributeKey, innerAttributeKey); + } + + return new CompositeAttributeMappingStrategy(openType, innerStrategies, attributeMapping); + } + + @Override + protected AttributeMappingStrategy> caseJavaUnionAttribute(OpenType openType) { + Map>> innerStrategies = Maps.newHashMap(); + + Map attributeMapping = Maps.newHashMap(); + + CompositeType compositeType = (CompositeType) openType; + for (String innerAttributeKey : compositeType.keySet()) { + + innerStrategies.put(innerAttributeKey, caseJavaAttribute(compositeType.getType(innerAttributeKey))); + attributeMapping.put(innerAttributeKey, innerAttributeKey); + } + + return new UnionCompositeAttributeMappingStrategy(compositeType, innerStrategies, attributeMapping); + } + + private String namespaceOfDepAttr; + + @Override + protected AttributeMappingStrategy> caseDependencyAttribute( + SimpleType openType) { + return new ObjectNameAttributeMappingStrategy(openType, namespaceOfDepAttr); + } + + @Override + protected AttributeMappingStrategy> caseTOAttribute(CompositeType openType) { + Map>> innerStrategies = Maps.newHashMap(); + + Preconditions.checkState(getLastAttribute() instanceof TOAttribute); + TOAttribute lastTO = (TOAttribute) getLastAttribute(); + + for (Entry innerAttrEntry : ((TOAttribute)getLastAttribute()).getJmxPropertiesToTypesMap().entrySet()) { + innerStrategies.put(innerAttrEntry.getKey(), prepareStrategy(innerAttrEntry.getValue())); + } + + return new CompositeAttributeMappingStrategy(openType, innerStrategies, + createJmxToYangMapping(lastTO)); + } + + @Override + protected AttributeMappingStrategy> caseListAttribute(ArrayType openType) { + Preconditions.checkState(getLastAttribute() instanceof ListAttribute); + return new ArrayAttributeMappingStrategy(openType, + prepareStrategy(((ListAttribute) getLastAttribute()).getInnerAttribute())); + } + + @Override + protected AttributeMappingStrategy> caseListDependeciesAttribute(ArrayType openType) { + Preconditions.checkState(getLastAttribute() instanceof ListDependenciesAttribute); + return new ArrayAttributeMappingStrategy(openType, caseDependencyAttribute(SimpleType.OBJECTNAME)); + } + +}