Add hasWidgetType() helper method
[aai/babel.git] / src / main / java / org / onap / aai / babel / xml / generator / model / Model.java
index 3f081df..b2020cd 100644 (file)
@@ -2,8 +2,8 @@
  * ============LICENSE_START=======================================================
  * org.onap.aai
  * ================================================================================
- * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
- * Copyright © 2017-2019 European Software Marketing Ltd.
+ * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (c) 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.
@@ -30,9 +30,6 @@ import java.util.Optional;
 import java.util.Set;
 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.model.Widget.Type;
-import org.onap.aai.babel.xml.generator.types.ModelType;
 
 public abstract class Model {
 
@@ -101,9 +98,9 @@ public abstract class Model {
         public abstract void populate(Model model, String value);
     }
 
-    private String modelId;
+    private String modelId; // model-invariant-id
     private String modelName;
-    private String modelNameVersionId;
+    private String modelNameVersionId; // model-version-id
     private String modelVersion;
     private String modelDescription;
 
@@ -150,23 +147,37 @@ public abstract class Model {
      */
     public static Resource getModelFor(String toscaType, String metaDataType) {
         if ("Configuration".equals(metaDataType)) {
-            return new Resource(Type.CONFIGURATION, true);
+            return new Resource(WidgetType.valueOf("CONFIGURATION"), true);
         } else if ("CR".equals(metaDataType)) {
-            return new Resource(Type.CR, true);
+            return new Resource(WidgetType.valueOf("CR"), true);
         } else {
             return getModelFor(toscaType);
         }
     }
 
-    public abstract boolean addResource(Resource resource);
-
     public abstract boolean addWidget(Widget resource) throws XmlArtifactGenerationException;
 
-    public abstract Widget.Type getWidgetType();
+    /**
+     * @return the Widget Type of this model.
+     */
+    public abstract WidgetType getWidgetType();
 
-    public abstract Map<String, Object> getProperties();
+    public abstract String getModelTypeName();
 
-    public abstract boolean isResource();
+    /**
+     * Check whether the model's Widget Type matches the supplied type.
+     *
+     * @param type
+     *            the Widget Type to compare
+     * @return true if the Widget Type of this model matches the supplied type
+     */
+    public boolean hasWidgetType(String type) {
+        return getWidgetType() == WidgetType.valueOf(type);
+    }
+
+    public boolean addResource(Resource resource) {
+        return resources.add(resource);
+    }
 
     /**
      * Gets delete flag.
@@ -182,7 +193,6 @@ public abstract class Model {
     }
 
     public String getModelId() {
-        checkSupported();
         return modelId;
     }
 
@@ -195,27 +205,9 @@ 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.
      *
@@ -243,6 +235,9 @@ public abstract class Model {
      *            the model ident info
      */
     public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
+        if (modelIdentInfo == null) {
+            return;
+        }
         Iterator<String> iter = modelIdentInfo.keySet().iterator();
         String property;
         while (iter.hasNext()) {
@@ -266,10 +261,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() + "]";
     }
 
 }