ee70939138220936196cc4db882eee2815b439ca
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / entity / model / AbstractModelArtifact.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright (c) 2017-2019 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.aai.modelloader.entity.model;
23
24 import java.io.IOException;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28 import javax.ws.rs.core.MediaType;
29 import org.onap.aai.cl.api.Logger;
30 import org.onap.aai.cl.eelf.LoggerFactory;
31 import org.onap.aai.modelloader.config.ModelLoaderConfig;
32 import org.onap.aai.modelloader.entity.Artifact;
33 import org.onap.aai.modelloader.entity.ArtifactType;
34 import org.onap.aai.modelloader.restclient.AaiRestClient;
35 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
36 import org.onap.aai.modelloader.util.GizmoTranslator;
37 import org.onap.aai.restclient.client.OperationResult;
38 import org.springframework.http.HttpStatus;
39
40 public abstract class AbstractModelArtifact extends Artifact implements IModelArtifact {
41
42     private static Logger logger = LoggerFactory.getInstance().getLogger(AbstractModelArtifact.class.getName());
43
44     private String modelNamespace;
45     private String modelNamespaceVersion;
46     private Set<String> referencedModelIds = new HashSet<>();
47
48     public AbstractModelArtifact(ArtifactType type) {
49         super(type);
50     }
51
52     public Set<String> getDependentModelIds() {
53         return referencedModelIds;
54     }
55
56     @Override
57     public void addDependentModelId(String dependentModelId) {
58         this.referencedModelIds.add(dependentModelId);
59     }
60
61     public String getModelNamespace() {
62         return modelNamespace;
63     }
64
65     @Override
66     public void setModelNamespace(String modelNamespace) {
67         this.modelNamespace = modelNamespace;
68
69         // Get the version from the namespace (in format 'http://org.onap.aai.inventory/v14')
70         String[] parts = modelNamespace.split("/");
71         modelNamespaceVersion = parts[parts.length - 1].trim();
72     }
73
74     public String getModelNamespaceVersion() {
75         return modelNamespaceVersion;
76     }
77
78     public abstract String getUniqueIdentifier();
79
80     public abstract boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId,
81             List<Artifact> completedArtifacts);
82
83     public abstract void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId);
84
85     protected boolean pushToGizmo(AaiRestClient aaiClient, ModelLoaderConfig config, String distId) {
86         try {
87             String gizmoPayload = GizmoTranslator.translate(getPayload());
88             OperationResult postResponse = aaiClient.postResource(config.getAaiBaseUrl().trim(), gizmoPayload, distId,
89                     MediaType.APPLICATION_JSON_TYPE);
90
91             if (postResponse.getResultCode() != HttpStatus.OK.value()) {
92                 return false;
93             }
94         } catch (IOException e) {
95             logErrorMsg("Ingest failed for " + getType() + " " + getUniqueIdentifier() + ": " + e.getMessage());
96             return false;
97         }
98
99         return true;
100     }
101
102     protected void logInfoMsg(String infoMsg) {
103         logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, infoMsg);
104     }
105
106     protected void logErrorMsg(String errorMsg) {
107         logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, errorMsg);
108     }
109
110     @Override
111     public String toString() {
112         StringBuilder sb = new StringBuilder();
113         sb.append("\n").append("Type=").append(getType()) //
114                 .append("\n").append("Id=").append(getUniqueIdentifier()) //
115                 .append("\n").append("Version=").append(getModelNamespaceVersion());
116
117         sb.append("\n").append("Dependant models: ");
118         referencedModelIds.forEach(dep -> sb.append(dep).append("  "));
119
120         return sb.toString();
121     }
122
123 }