Replace Resource sub-classes with configuration
[aai/babel.git] / src / main / java / org / onap / aai / babel / xml / generator / model / Model.java
index 8e9c062..d4da6df 100644 (file)
@@ -2,8 +2,8 @@
  * ============LICENSE_START=======================================================
  * org.onap.aai
  * ================================================================================
- * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
- * Copyright © 2017-2018 European Software Marketing Ltd.
+ * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2019 European Software Marketing Ltd.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
  */
 package org.onap.aai.babel.xml.generator.model;
 
-import java.lang.reflect.InvocationTargetException;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -28,35 +27,15 @@ import java.util.Iterator;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
-import org.onap.aai.babel.logging.ApplicationMsgs;
-import org.onap.aai.babel.logging.LogHelper;
+import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
 import org.onap.aai.babel.xml.generator.error.IllegalAccessException;
-import org.onap.aai.babel.xml.generator.types.Cardinality;
+import org.onap.aai.babel.xml.generator.model.Widget.Type;
 import org.onap.aai.babel.xml.generator.types.ModelType;
-import org.onap.aai.cl.api.Logger;
 
 public abstract class Model {
 
     public static final String GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION = "Operation Not Supported for Widgets";
 
-    private static Logger log = LogHelper.INSTANCE;
-
-    private static Map<String, Class<? extends Model>> typeToModel = new HashMap<>();
-    static {
-        typeToModel.put("org.openecomp.resource.vf.allottedResource", AllotedResource.class);
-        typeToModel.put("org.openecomp.resource.vfc.AllottedResource", ProvidingService.class);
-        typeToModel.put("org.openecomp.resource.vfc", VServerWidget.class);
-        typeToModel.put("org.openecomp.resource.cp", LIntfWidget.class);
-        typeToModel.put("org.openecomp.cp", LIntfWidget.class);
-        typeToModel.put("org.openecomp.resource.vl", L3Network.class);
-        typeToModel.put("org.openecomp.resource.vf", VirtualFunction.class);
-        typeToModel.put("org.openecomp.groups.vfmodule", VfModule.class);
-        typeToModel.put("org.openecomp.groups.VfModule", VfModule.class);
-        typeToModel.put("org.openecomp.resource.vfc.nodes.heat.cinder", VolumeWidget.class);
-        typeToModel.put("org.openecomp.nodes.PortMirroringConfiguration", Configuration.class);
-        typeToModel.put("org.openecomp.resource.cr.Kk1806Cr1", CR.class);
-    }
-
     private enum ModelIdentification {
         ID("vfModuleModelInvariantUUID", "serviceInvariantUUID", "resourceInvariantUUID", "invariantUUID",
                 "providing_service_invariant_uuid") {
@@ -133,38 +112,48 @@ public abstract class Model {
      * Gets the object (model) corresponding to the supplied TOSCA type.
      *
      * @param toscaType
-     *            the tosca type
+     *     the tosca type
      * @return the model for the type, or null
      */
-    public static Model getModelFor(String toscaType) {
-        Model model = null;
+    public static Resource getModelFor(String toscaType) {
+        Resource resource = null;
         if (toscaType != null && !toscaType.isEmpty()) {
-            model = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
+            resource = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
         }
-        return model;
+        return resource;
     }
 
-    private static Model getModelFromPrefix(String toscaType) {
-        Model model = null;
+    private static Resource getModelFromPrefix(String toscaType) {
+        Resource resource = null;
         int lastSeparator = toscaType.lastIndexOf('.');
         if (lastSeparator != -1) {
-            model = getModelFor(toscaType.substring(0, lastSeparator));
+            resource = getModelFor(toscaType.substring(0, lastSeparator));
         }
-        return model;
+        return resource;
     }
 
-    private static Optional<Model> getModelFromType(String typePrefix) {
-        Optional<Model> modelToBeReturned = Optional.empty();
-        Class<? extends Model> clazz = typeToModel.get(typePrefix);
-        if (clazz != null) {
-            try {
-                modelToBeReturned = Optional.ofNullable(clazz.getConstructor().newInstance());
-            } catch (InstantiationException | java.lang.IllegalAccessException | IllegalArgumentException
-                    | InvocationTargetException | NoSuchMethodException | SecurityException e) {
-                log.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
-            }
+    private static Optional<Resource> getModelFromType(String typePrefix) {
+        return WidgetConfigurationUtil.createModelFromType(typePrefix);
+    }
+
+    /**
+     * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata
+     * information.
+     *
+     * @param toscaType
+     *     the TOSCA type
+     * @param metaDataType
+     *     the type from the TOSCA metadata
+     * @return the model for the type, or null
+     */
+    public static Resource getModelFor(String toscaType, String metaDataType) {
+        if ("Configuration".equals(metaDataType)) {
+            return new Resource(Type.CONFIGURATION, true);
+        } else if ("CR".equals(metaDataType)) {
+            return new Resource(Type.CR, true);
+        } else {
+            return getModelFor(toscaType);
         }
-        return modelToBeReturned;
     }
 
     public abstract boolean addResource(Resource resource);
@@ -173,16 +162,9 @@ public abstract class Model {
 
     public abstract Widget.Type getWidgetType();
 
-    /**
-     * Gets cardinality.
-     *
-     * @return the cardinality
-     */
-    public Cardinality getCardinality() {
-        org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
-                .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
-        return model.cardinality();
-    }
+    public abstract Map<String, Object> getProperties();
+
+    public abstract boolean isResource();
 
     /**
      * Gets delete flag.
@@ -190,8 +172,8 @@ public abstract class Model {
      * @return the delete flag
      */
     public boolean getDeleteFlag() {
-        org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
-                .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
+        org.onap.aai.babel.xml.generator.types.Model model =
+                this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
         return model.dataDeleteFlag();
     }
 
@@ -240,8 +222,8 @@ public abstract class Model {
      * @return the widget version id
      */
     public String getWidgetId() {
-        org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
-                .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
+        org.onap.aai.babel.xml.generator.types.Model model =
+                this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
         return Widget.getWidget(model.widget()).getId();
     }
 
@@ -251,8 +233,8 @@ public abstract class Model {
      * @return the invariant id
      */
     public String getWidgetInvariantId() {
-        org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
-                .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
+        org.onap.aai.babel.xml.generator.types.Model model =
+                this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
         return Widget.getWidget(model.widget()).getWidgetId();
     }
 
@@ -260,7 +242,7 @@ public abstract class Model {
      * Populate model identification information.
      *
      * @param modelIdentInfo
-     *            the model ident info
+     *     the model ident info
      */
     public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
         Iterator<String> iter = modelIdentInfo.keySet().iterator();
@@ -291,4 +273,5 @@ public abstract class Model {
             throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
         }
     }
+
 }