Configurable option to load models via Gizmo
[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 © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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 package org.onap.aai.modelloader.entity.model;
22
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26
27 import javax.ws.rs.core.MediaType;
28
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
41 public abstract class AbstractModelArtifact extends Artifact implements IModelArtifact {
42
43     private static Logger logger = LoggerFactory.getInstance().getLogger(AbstractModelArtifact.class.getName());
44     
45     private String modelNamespace;
46     private String modelNamespaceVersion;
47     private Set<String> referencedModelIds = new HashSet<>();
48
49     public AbstractModelArtifact(ArtifactType type) {
50         super(type);
51     }
52
53     public Set<String> getDependentModelIds() {
54         return referencedModelIds;
55     }
56
57     @Override
58     public void addDependentModelId(String dependentModelId) {
59         this.referencedModelIds.add(dependentModelId);
60     }
61
62     public String getModelNamespace() {
63         return modelNamespace;
64     }
65
66     @Override
67     public void setModelNamespace(String modelNamespace) {
68         this.modelNamespace = modelNamespace;
69
70         // Get the version from the namespace (in format 'http://org.openecomp.aai.inventory/v9')
71         String[] parts = modelNamespace.split("/");
72         modelNamespaceVersion = parts[parts.length - 1].trim();
73     }
74
75     public String getModelNamespaceVersion() {
76         return modelNamespaceVersion;
77     }
78
79     public abstract String getUniqueIdentifier();
80
81     public abstract boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId,
82             List<Artifact> completedArtifacts);
83
84     public abstract void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId);
85
86     protected boolean pushToGizmo(AaiRestClient aaiClient, ModelLoaderConfig config, String distId, List<Artifact> completedArtifacts) { 
87         try {
88             String gizmoPayload = GizmoTranslator.translate(getPayload());
89             OperationResult postResponse =
90                     aaiClient.postResource(config.getAaiBaseUrl().trim(), gizmoPayload, distId, MediaType.APPLICATION_JSON_TYPE);
91             
92             if (postResponse.getResultCode() != HttpStatus.OK.value()) {
93                 return false;
94             }
95             
96         } catch (Exception e) {
97             logErrorMsg("Ingest failed for " + getType().toString() + " " + getUniqueIdentifier() + ": " + e.getMessage());
98             return false;
99         }
100
101         return true;
102     }
103     
104     protected void logInfoMsg(String infoMsg) {
105         logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, infoMsg);
106     }
107
108     protected void logErrorMsg(String errorMsg) {
109         logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, errorMsg);
110     }
111     
112     @Override
113     public String toString() {
114         StringBuilder sb = new StringBuilder();
115         sb.append("\nType=" + getType().toString() + "\nId=" + getUniqueIdentifier() + "\nVersion="
116                 + getModelNamespaceVersion() + "\nDependant models: ");
117         for (String dep : referencedModelIds) {
118             sb.append(dep + "  ");
119         }
120
121         return sb.toString();
122     }
123
124 }