Re-implement model type value for Resource Mapping
[aai/babel.git] / src / main / java / org / onap / aai / babel / xml / generator / model / Model.java
index 0e2b8d5..c67d67f 100644 (file)
@@ -18,6 +18,7 @@
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
+
 package org.onap.aai.babel.xml.generator.model;
 
 import java.util.Collections;
@@ -27,20 +28,14 @@ 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.XmlArtifactGenerationException;
 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.types.ModelType;
-import org.onap.aai.cl.api.Logger;
+import org.onap.aai.babel.xml.generator.model.Widget.Type;
 
 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 enum ModelIdentification {
         ID("vfModuleModelInvariantUUID", "serviceInvariantUUID", "resourceInvariantUUID", "invariantUUID",
                 "providing_service_invariant_uuid") {
@@ -114,40 +109,31 @@ public abstract class Model {
     protected Set<Widget> widgets = new HashSet<>();
 
     /**
-     * Gets the object (model) corresponding to the supplied TOSCA type.
+     * Gets the Resource Model corresponding to the supplied TOSCA type.
      *
      * @param toscaType
      *            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 = WidgetConfigurationUtil.getModelFromType(typePrefix);
-        if (clazz != null) {
-            try {
-                modelToBeReturned = Optional.ofNullable(clazz.getConstructor().newInstance());
-            } catch (Exception e) {
-                log.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
-            }
-        }
-        return modelToBeReturned;
+    private static Optional<Resource> getModelFromType(String typePrefix) {
+        return WidgetConfigurationUtil.createModelFromType(typePrefix);
     }
 
     /**
@@ -160,31 +146,24 @@ public abstract class Model {
      *            the type from the TOSCA metadata
      * @return the model for the type, or null
      */
-    public static Model getModelFor(String toscaType, String metaDataType) {
+    public static Resource getModelFor(String toscaType, String metaDataType) {
         if ("Configuration".equals(metaDataType)) {
-            return new Configuration();
+            return new Resource(Type.CONFIGURATION, true);
         } else if ("CR".equals(metaDataType)) {
-            return new CR();
+            return new Resource(Type.CR, true);
         } else {
             return getModelFor(toscaType);
         }
     }
 
-    public abstract boolean addResource(Resource resource);
-
-    public abstract boolean addWidget(Widget resource);
+    public abstract boolean addWidget(Widget resource) throws XmlArtifactGenerationException;
 
     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 String getModelTypeName();
+
+    public boolean addResource(Resource resource) {
+        return resources.add(resource);
     }
 
     /**
@@ -193,9 +172,7 @@ 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);
-        return model.dataDeleteFlag();
+        return true;
     }
 
     public String getModelDescription() {
@@ -203,7 +180,6 @@ public abstract class Model {
     }
 
     public String getModelId() {
-        checkSupported();
         return modelId;
     }
 
@@ -216,47 +192,27 @@ public abstract class Model {
     }
 
     public String getModelNameVersionId() {
-        checkSupported();
         return modelNameVersionId;
     }
 
-    /**
-     * Gets model type.
-     *
-     * @return the model type
-     */
-    public ModelType getModelType() {
-        if (this instanceof Service) {
-            return ModelType.SERVICE;
-        } else if (this instanceof Resource) {
-            return ModelType.RESOURCE;
-        } else if (this instanceof Widget) {
-            return ModelType.WIDGET;
-        } else {
-            return null;
-        }
-    }
-
     /**
      * Gets widget version id.
      *
      * @return the widget version id
+     * @throws XmlArtifactGenerationException
      */
-    public String getWidgetId() {
-        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();
+    public String getWidgetId() throws XmlArtifactGenerationException {
+        return Widget.getWidget(getWidgetType()).getId();
     }
 
     /**
      * Gets invariant id.
      *
      * @return the invariant id
+     * @throws XmlArtifactGenerationException
      */
-    public String getWidgetInvariantId() {
-        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();
+    public String getWidgetInvariantId() throws XmlArtifactGenerationException {
+        return Widget.getWidget(getWidgetType()).getWidgetId();
     }
 
     /**
@@ -289,10 +245,9 @@ public abstract class Model {
         return widgets;
     }
 
-    private void checkSupported() {
-        if (this instanceof Widget) {
-            throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
-        }
+    @Override
+    public String toString() {
+        return "Model [type=" + getModelTypeName() + ", name=" + getModelName() + "]";
     }
 
 }