summaryrefslogtreecommitdiffstats
path: root/dcaedt_tools/src/main/java/tools/DeployTemplate.java
blob: 556316bf8e83b0fcbe3d47d4abe401097b5190d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package tools;
import com.google.gson.JsonObject;
import json.templateInfo.TemplateInfo;
import org.onap.sdc.dcae.composition.restmodels.CreateVFCMTRequest;
import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed;
import org.onap.sdc.dcae.composition.util.DcaeBeConstants;
import org.springframework.web.client.HttpServerErrorException;
import utilities.IDcaeRestClient;
import utilities.IReport;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;


public class DeployTemplate {
    private static final String FAILED_UPDATE_VFCMT = "Failed update vfcmt: ";
    private static final String FAILED = "failed";
    private final IReport report;
    private final IDcaeRestClient dcaeRestClient;
    private LoggerError errLogger = LoggerError.getInstance();
    private LoggerDebug debugLogger = LoggerDebug.getInstance();

    DeployTemplate(IReport report, IDcaeRestClient dcaeRestClient) {

        this.report = report;
        this.dcaeRestClient = dcaeRestClient;
    }

    public void deploy(Map<TemplateInfo, JsonObject> templateInfoToJsonObjectMap) {
        ArrayList<ResourceDetailed> vfcmtList = new ArrayList<>();
        List<ResourceDetailed> regularVfcmtList = dcaeRestClient.getAllVfcmts();
        if (regularVfcmtList != null) {
            vfcmtList.addAll(regularVfcmtList);
        }
        List<ResourceDetailed> baseVfcmtList = dcaeRestClient.getAllBaseVfcmts();
        if (baseVfcmtList != null) {
            vfcmtList.addAll(baseVfcmtList);
        }

        List<TemplateInfo> updatedTemplateInfos = new ArrayList<>();
        vfcmtList.forEach(vfcmt ->
                templateInfoToJsonObjectMap.keySet().stream().filter(templateInfo -> templateInfo.getName().equalsIgnoreCase(vfcmt.getName())).forEach(templateInfo -> {
                    update(vfcmt, templateInfo, templateInfoToJsonObjectMap.get(templateInfo));
                    updatedTemplateInfos.add(templateInfo);
                }));
        templateInfoToJsonObjectMap.keySet().stream()
                .filter(templateInfo -> !updatedTemplateInfos.contains(templateInfo))
                .forEach(templateInfo -> createNew(templateInfo, templateInfoToJsonObjectMap.get(templateInfo)));

        verify(templateInfoToJsonObjectMap);
    }

    private void verify(Map<TemplateInfo, JsonObject> templateInfoToJsonObjectMap) {
        AtomicInteger foundCount = new AtomicInteger();
        debugLogger.log("Starting verify deployment");
        ArrayList<ResourceDetailed> vfcmtList = new ArrayList<>();
        List<ResourceDetailed> regularVfcmtList = dcaeRestClient.getAllVfcmts();
        if (regularVfcmtList != null) {
            vfcmtList.addAll(regularVfcmtList);
        }
        List<ResourceDetailed> baseVfcmtList = dcaeRestClient.getAllBaseVfcmts();
        if (baseVfcmtList != null) {
            vfcmtList.addAll(baseVfcmtList);
        }

        templateInfoToJsonObjectMap.keySet()
                .forEach(templateInfo -> vfcmtList.stream()
                        .filter(vfcmt -> vfcmt.getName().equalsIgnoreCase(templateInfo.getName()))
                        .forEach(vfcmt -> foundCount.getAndIncrement()));
        if (foundCount.get() == templateInfoToJsonObjectMap.size()) {
            debugLogger.log("Deployment verify finished successfully");
        } else {
            errLogger.log("Deployment verify finished successfully");
            String msg = "Deployment verify finished with errors, found only: " +
                    foundCount.get() + " of " + templateInfoToJsonObjectMap.size() + " vfcmts";
            report.addErrorMessage(msg);
            errLogger.log(msg);
        }
    }

    private void createNew(TemplateInfo templateInfo, JsonObject jsonObject) {
        try {
            CreateVFCMTRequest createVFCMTRequest = new CreateVFCMTRequest();
            createVFCMTRequest.setName(templateInfo.getName());
            createVFCMTRequest.setDescription(templateInfo.getDescription());
            createVFCMTRequest.setSubcategory(templateInfo.getSubCategory());
            createVFCMTRequest.setCategory(templateInfo.getCategory());
            ResourceDetailed vfcmt = dcaeRestClient.createResource(createVFCMTRequest);

            saveAndCertify(jsonObject, vfcmt);

        } catch (Exception e) {
            String msg = FAILED_UPDATE_VFCMT + templateInfo.getName() + ", With general message: " + e.getMessage();
            report.addErrorMessage(msg);
            errLogger.log(msg + " " + e);
            report.setStatusCode(2);
        }
    }

    private void update(ResourceDetailed vfcmt, TemplateInfo templateInfo, JsonObject jsonObject) {
        ResourceDetailed checkedoutVfcmt = vfcmt;
        try {
            boolean vfcmtIsCheckedOut = isCheckedOut(vfcmt);
            if (vfcmtIsCheckedOut && differentUserCannotCheckout(dcaeRestClient.getUserId(), vfcmt)){
                report.addErrorMessage(FAILED_UPDATE_VFCMT + vfcmt.getName() + ", cannot checkout vfcmt");
                return;
            }
            if (templateInfo.getUpdateIfExist()) {
                if (!vfcmtIsCheckedOut) {
                    checkedoutVfcmt = dcaeRestClient.checkoutVfcmt(vfcmt.getUuid());
                }
                if (checkedoutVfcmt != null) {
                    checkedoutVfcmt.setSubCategory(templateInfo.getSubCategory());
                    checkedoutVfcmt.setCategory(templateInfo.getCategory());
                    checkedoutVfcmt.setDescription(templateInfo.getDescription());
                    dcaeRestClient.updateResource(checkedoutVfcmt);
                    saveAndCertify(jsonObject, checkedoutVfcmt);
                }
            } else {
                report.addNotUpdatedMessage("vfcmt: " + vfcmt.getName() + " found, but didn't update.");
            }
        } catch (Exception e) {
            String msg = FAILED_UPDATE_VFCMT + vfcmt.getName() + ", With general message: " + e.getMessage();
            report.addErrorMessage(msg);
            errLogger.log( msg + " " + e);
			report.setStatusCode(2);
        }
    }

    private void saveAndCertify(JsonObject jsonObject, ResourceDetailed checkedoutVfcmt) {
		jsonObject.addProperty("cid", checkedoutVfcmt.getUuid());
        if (saveCompositionAndCertify(checkedoutVfcmt, jsonObject)) {
            report.addUpdatedMessage("vfcmt: " + checkedoutVfcmt.getName() + " updated successfully");
        } else {
            report.addErrorMessage("VFCMT " + checkedoutVfcmt.getName() + " failed to update");
        }
    }

    private boolean saveCompositionAndCertify(ResourceDetailed vfcmt, JsonObject jsonObject) {
        if (vfcmt.getUuid() == null) {
            return false;
        }

        debugLogger.log("Saving cdump of: " + vfcmt.getName() + " vfcmt");
        debugLogger.log(jsonObject.toString());

        String responseEntity = dcaeRestClient.saveComposition(vfcmt.getUuid(), jsonObject.toString());
        if (responseEntity.equalsIgnoreCase(FAILED)) {
            String msg = "Failed saving vfcmt: " + vfcmt.getName();
            report.addErrorMessage(msg);
            errLogger.log(msg);
            return false;
        }
        dcaeRestClient.certifyVfcmt(vfcmt.getUuid());
        return true;
    }

    private boolean isCheckedOut(ResourceDetailed asset) {
        return DcaeBeConstants.LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT == DcaeBeConstants.LifecycleStateEnum.findState(asset.getLifecycleState());
    }

    private Boolean differentUserCannotCheckout(String userId, ResourceDetailed asset) {
        String lastUpdaterUserId = asset.getLastUpdaterUserId();
        if (lastUpdaterUserId != null && !lastUpdaterUserId.equals(userId)) {
            String msg = "User conflicts. Operation not allowed for user "+userId+" on resource checked out by "+lastUpdaterUserId;
            report.addErrorMessage(msg);
            errLogger.log(msg);
            return true;
        } else {
            return false;
        }
    }
}