DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_be / src / main / java / org / onap / sdc / dcae / composition / impl / BaseBusinessLogic.java
1 package org.onap.sdc.dcae.composition.impl;
2
3 import org.onap.sdc.common.onaplog.OnapLoggerDebug;
4 import org.onap.sdc.common.onaplog.OnapLoggerError;
5 import org.onap.sdc.dcae.client.ISdcClient;
6 import org.onap.sdc.dcae.composition.restmodels.sdc.Artifact;
7 import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed;
8 import org.onap.sdc.dcae.utils.SdcRestClientUtils;
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.stereotype.Component;
11 import org.springframework.util.Base64Utils;
12 import org.springframework.util.CollectionUtils;
13
14 @Component
15 public class BaseBusinessLogic {
16     @Autowired
17     protected ISdcClient sdcRestClient;
18
19     protected static OnapLoggerError errLogger = OnapLoggerError.getInstance();
20     protected static OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();
21
22         public ISdcClient getSdcRestClient() {
23                 return sdcRestClient;
24         }
25
26         void setSdcRestClient(ISdcClient sdcRestClient) {
27                 this.sdcRestClient = sdcRestClient;
28         }
29
30         Artifact cloneArtifactToTarget(String userId, String targetId, String payload, Artifact artifactToClone, String requestId) throws Exception {
31                 Artifact cloned = SdcRestClientUtils.generateDeploymentArtifact(artifactToClone.getArtifactDescription(), artifactToClone.getArtifactName(), artifactToClone.getArtifactType(), artifactToClone.getArtifactLabel(), payload.getBytes());
32                 return sdcRestClient.createResourceArtifact(userId, targetId, cloned, requestId);
33         }
34
35         public void cloneArtifactToTarget(String userId, String targetId, String payload, Artifact artifactToClone, Artifact artifactToOverride, String requestId) throws Exception{
36                 if (null != artifactToOverride) {
37                         artifactToOverride.setDescription(artifactToOverride.getArtifactDescription());
38                         artifactToOverride.setPayloadData(Base64Utils.encodeToString(payload.getBytes()));
39                         sdcRestClient.updateResourceArtifact(userId, targetId, artifactToOverride, requestId);
40                 } else {
41                         cloneArtifactToTarget(userId, targetId, payload, artifactToClone, requestId);
42                 }
43         }
44
45         Artifact findArtifactDataByArtifactName(ResourceDetailed vfcmt, String artifactName) {
46                 return null == vfcmt ? null : CollectionUtils.isEmpty(vfcmt.getArtifacts()) ? null : vfcmt.getArtifacts().stream()
47                                 .filter(p -> artifactName.equals(p.getArtifactName())).findAny().orElse(null);
48         }
49 }