BUG 2596: Use base service name in service serialization.
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / mapping / ObjectMapper.java
index 3e63b928848d617a2b9fbd29278a1eb5e7f7e88d..2608c57e000316e1cd2df3aa80d38a34b5c72890 100644 (file)
@@ -8,32 +8,29 @@
 
 package org.opendaylight.controller.netconf.confignetconfconnector.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.yangjmxgenerator.attribute.AttributeIfc;
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
-import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
+import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.AttributeIfcSwitchStatement;
-import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services;
-
-import javax.management.openmbean.ArrayType;
-import javax.management.openmbean.CompositeType;
-import javax.management.openmbean.OpenType;
-import javax.management.openmbean.SimpleType;
-import java.util.Map;
-import java.util.Map.Entry;
+import org.opendaylight.controller.netconf.confignetconfconnector.osgi.EnumResolver;
 
 public class ObjectMapper extends AttributeIfcSwitchStatement<AttributeMappingStrategy<?, ? extends OpenType<?>>> {
 
-    private final Services dependencyTracker;
-
-    public ObjectMapper(Services depTracker) {
-        this.dependencyTracker = depTracker;
-    }
+    private EnumResolver enumResolver;
 
     public Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> prepareMapping(
-            Map<String, AttributeIfc> configDefinition) {
+            Map<String, AttributeIfc> configDefinition, EnumResolver enumResolver) {
+        this.enumResolver = Preconditions.checkNotNull(enumResolver);
         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> strategies = Maps.newHashMap();
 
         for (Entry<String, AttributeIfc> attrEntry : configDefinition.entrySet()) {
@@ -44,6 +41,13 @@ public class ObjectMapper extends AttributeIfcSwitchStatement<AttributeMappingSt
     }
 
     public AttributeMappingStrategy<?, ? extends OpenType<?>> 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);
     }
 
@@ -56,45 +60,88 @@ public class ObjectMapper extends AttributeIfcSwitchStatement<AttributeMappingSt
     }
 
     @Override
-    protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaAttribute(JavaAttribute attributeIfc) {
-
-        if (attributeIfc.getOpenType() instanceof SimpleType<?>)
-            return new SimpleAttributeMappingStrategy((SimpleType<?>) attributeIfc.getOpenType());
-        else if (attributeIfc.getOpenType() instanceof ArrayType<?>) {
-            ArrayType<?> arrayType = (ArrayType<?>) attributeIfc.getOpenType();
-            AttributeMappingStrategy<?, ? extends OpenType<?>> innerStrategy = new SimpleAttributeMappingStrategy(
-                    (SimpleType<?>) arrayType.getElementOpenType());
-            return new ArrayAttributeMappingStrategy(arrayType, innerStrategy);
+    protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaSimpleAttribute(SimpleType<?> openType) {
+        return new SimpleAttributeMappingStrategy(openType);
+    }
+
+    @Override
+    protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaEnumAttribute(final OpenType<?> openType) {
+        return new EnumAttributeMappingStrategy(((CompositeType) openType), enumResolver);
+    }
+
+    @Override
+    protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaArrayAttribute(ArrayType<?> openType) {
+
+        AttributeMappingStrategy<?, ? extends OpenType<?>> innerStrategy = new SimpleAttributeMappingStrategy(
+                (SimpleType<?>) openType.getElementOpenType());
+        return new ArrayAttributeMappingStrategy(openType, innerStrategy);
+    }
+
+    @Override
+    protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaCompositeAttribute(CompositeType openType) {
+        Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
+
+        Map<String, String> attributeMapping = Maps.newHashMap();
+
+        for (String innerAttributeKey : openType.keySet()) {
+
+            innerStrategies.put(innerAttributeKey, caseJavaAttribute(openType.getType(innerAttributeKey)));
+            attributeMapping.put(innerAttributeKey, innerAttributeKey);
         }
-        throw new IllegalStateException(JavaAttribute.class + " can only provide open type " + SimpleType.class
-                + " or " + ArrayType.class);
+
+        return new CompositeAttributeMappingStrategy(openType, innerStrategies, attributeMapping);
     }
 
+    @Override
+    protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaUnionAttribute(OpenType<?> openType) {
+        Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
+
+        Map<String, String> 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<?, ? extends OpenType<?>> caseDependencyAttribute(
-            DependencyAttribute attributeIfc) {
-        String serviceName = attributeIfc.getDependency().getSie().getQName().getLocalName();
-        String namespace = attributeIfc.getDependency().getSie().getQName().getNamespace().toString();
-        return new ObjectNameAttributeMappingStrategy((SimpleType<?>) attributeIfc.getOpenType(), dependencyTracker,
-                serviceName, namespace);
+            SimpleType<?> openType) {
+        return new ObjectNameAttributeMappingStrategy(openType, namespaceOfDepAttr);
     }
 
     @Override
-    protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseTOAttribute(TOAttribute attributeIfc) {
+    protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseTOAttribute(CompositeType openType) {
         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
 
-        for (Entry<String, AttributeIfc> innerAttrEntry : attributeIfc.getJmxPropertiesToTypesMap().entrySet()) {
+        Preconditions.checkState(getLastAttribute() instanceof TOAttribute);
+        TOAttribute lastTO = (TOAttribute) getLastAttribute();
+
+        for (Entry<String, AttributeIfc> innerAttrEntry : ((TOAttribute)getLastAttribute()).getJmxPropertiesToTypesMap().entrySet()) {
             innerStrategies.put(innerAttrEntry.getKey(), prepareStrategy(innerAttrEntry.getValue()));
         }
 
-        return new CompositeAttributeMappingStrategy((CompositeType) attributeIfc.getOpenType(), innerStrategies,
-                createJmxToYangMapping(attributeIfc));
+        return new CompositeAttributeMappingStrategy(openType, innerStrategies,
+                createJmxToYangMapping(lastTO));
+    }
+
+    @Override
+    protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListAttribute(ArrayType<?> openType) {
+        Preconditions.checkState(getLastAttribute() instanceof ListAttribute);
+        return new ArrayAttributeMappingStrategy(openType,
+                prepareStrategy(((ListAttribute) getLastAttribute()).getInnerAttribute()));
     }
 
     @Override
-    protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListAttribute(ListAttribute attributeIfc) {
-        return new ArrayAttributeMappingStrategy(attributeIfc.getOpenType(),
-                prepareStrategy(attributeIfc.getInnerAttribute()));
+    protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListDependeciesAttribute(ArrayType<?> openType) {
+        Preconditions.checkState(getLastAttribute() instanceof ListDependenciesAttribute);
+        return new ArrayAttributeMappingStrategy(openType, caseDependencyAttribute(SimpleType.OBJECTNAME));
     }
 
 }