+import org.onap.aai.modelloader.service.ModelLoaderMsgs;
+import org.onap.aai.modelloader.util.GizmoTranslator;
+import org.onap.aai.restclient.client.OperationResult;
+import org.springframework.http.HttpStatus;
+
+public abstract class AbstractModelArtifact extends Artifact implements IModelArtifact {
+
+ private static Logger logger = LoggerFactory.getInstance().getLogger(AbstractModelArtifact.class.getName());
+
+ private String modelNamespace;
+ private String modelNamespaceVersion;
+ private Set<String> referencedModelIds = new HashSet<>();
+
+ public AbstractModelArtifact(ArtifactType type) {
+ super(type);
+ }
+
+ public Set<String> getDependentModelIds() {
+ return referencedModelIds;
+ }
+
+ @Override
+ public void addDependentModelId(String dependentModelId) {
+ this.referencedModelIds.add(dependentModelId);
+ }
+
+ public String getModelNamespace() {
+ return modelNamespace;
+ }
+
+ @Override
+ public void setModelNamespace(String modelNamespace) {
+ this.modelNamespace = modelNamespace;
+
+ // Get the version from the namespace (in format 'http://org.onap.aai.inventory/v14')
+ String[] parts = modelNamespace.split("/");
+ modelNamespaceVersion = parts[parts.length - 1].trim();
+ }
+
+ public String getModelNamespaceVersion() {
+ return modelNamespaceVersion;
+ }
+
+ public abstract String getUniqueIdentifier();
+
+ public abstract boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId,
+ List<Artifact> completedArtifacts);
+
+ public abstract void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId);
+
+ protected boolean pushToGizmo(AaiRestClient aaiClient, ModelLoaderConfig config, String distId) {
+ try {
+ String gizmoPayload = GizmoTranslator.translate(getPayload());
+ OperationResult postResponse = aaiClient.postResource(config.getAaiBaseUrl().trim(), gizmoPayload, distId,
+ MediaType.APPLICATION_JSON_TYPE);
+
+ if (postResponse.getResultCode() != HttpStatus.OK.value()) {
+ return false;
+ }
+ } catch (IOException e) {
+ logErrorMsg("Ingest failed for " + getType() + " " + getUniqueIdentifier() + ": " + e.getMessage());
+ return false;
+ }
+
+ return true;
+ }
+
+ protected void logInfoMsg(String infoMsg) {
+ logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, infoMsg);
+ }
+
+ protected void logErrorMsg(String errorMsg) {
+ logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, errorMsg);
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("\n").append("Type=").append(getType()) //
+ .append("\n").append("Id=").append(getUniqueIdentifier()) //
+ .append("\n").append("Version=").append(getModelNamespaceVersion());
+
+ sb.append("\n").append("Dependant models: ");
+ referencedModelIds.forEach(dep -> sb.append(dep).append(" "));
+
+ return sb.toString();
+ }