aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/services/ChangeManagementServiceImpl.java
blob: 13525f842803279ab9fa0a6e6cfda62dec3cc3d7 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package org.onap.vid.services;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.hibernate.NonUniqueObjectException;
import org.json.JSONObject;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.portalsdk.core.service.DataAccessService;
import org.onap.portalsdk.core.util.SystemProperties;
import org.onap.vid.changeManagement.*;
import org.onap.vid.exceptions.GenericUncheckedException;
import org.onap.vid.exceptions.NotFoundException;
import org.onap.vid.model.VNFDao;
import org.onap.vid.model.VidWorkflow;
import org.onap.vid.mso.MsoBusinessLogic;
import org.onap.vid.mso.MsoResponseWrapperInterface;
import org.onap.vid.mso.RestObject;
import org.onap.vid.mso.RestObjectWithRequestInfo;
import org.onap.vid.mso.rest.Request;
import org.onap.vid.scheduler.SchedulerProperties;
import org.onap.vid.scheduler.SchedulerRestInterfaceIfc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.BadRequestException;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class ChangeManagementServiceImpl implements ChangeManagementService {

    private static final String PRIMARY_KEY = "payload";
    private static final Set<String> REQUIRED_KEYS = new HashSet<>(Arrays.asList("request-parameters", "configuration-parameters"));
    private final DataAccessService dataAccessService;
    private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ChangeManagementServiceImpl.class);
    private MsoBusinessLogic msoBusinessLogic;
    private final SchedulerRestInterfaceIfc restClient;
    private final CloudOwnerService cloudOwnerService;

    @Autowired
    private CsvService csvService;

    @Autowired
    public ChangeManagementServiceImpl(DataAccessService dataAccessService, MsoBusinessLogic msoBusinessLogic, SchedulerRestInterfaceIfc schedulerRestInterface, CloudOwnerService cloudOwnerService) {
        this.dataAccessService = dataAccessService;
        this.msoBusinessLogic = msoBusinessLogic;
        this.restClient = schedulerRestInterface;
        this.cloudOwnerService = cloudOwnerService;
    }

    @Override
    public Collection<Request> getMSOChangeManagements() {
            return msoBusinessLogic.getOrchestrationRequestsForDashboard();
    }

    protected RequestDetails findRequestByVnfName(List<RequestDetails> requests, String vnfName) {

        if (requests == null)
            return null;

        for (RequestDetails requestDetails : requests) {
            if (requestDetails.getVnfName().equals(vnfName)) {
                return requestDetails;
            }
        }

        return null;
    }

    @Override
    public ResponseEntity<String> doChangeManagement(ChangeManagementRequest request, String vnfName) {
        if (request == null)
            return null;
        ResponseEntity<String> response;
        RequestDetails currentRequestDetails = findRequestByVnfName(request.getRequestDetails(), vnfName);
        MsoResponseWrapperInterface msoResponseWrapperObject = null;
        if (currentRequestDetails != null) {

            String serviceInstanceId = extractServiceInstanceId(currentRequestDetails, request.getRequestType());
            String vnfInstanceId = extractVnfInstanceId(currentRequestDetails, request.getRequestType());
            String requestType = request.getRequestType();
            try {
                switch (requestType.toLowerCase()) {
                    case ChangeManagementRequest.UPDATE: {
                        cloudOwnerService.enrichRequestWithCloudOwner(currentRequestDetails);
                        msoResponseWrapperObject = msoBusinessLogic.updateVnf(currentRequestDetails, serviceInstanceId, vnfInstanceId);
                        break;
                    }
                    case ChangeManagementRequest.REPLACE: {
                        cloudOwnerService.enrichRequestWithCloudOwner(currentRequestDetails);
                        msoResponseWrapperObject = msoBusinessLogic.replaceVnf(currentRequestDetails, serviceInstanceId, vnfInstanceId);
                        break;
                    }
                    case ChangeManagementRequest.VNF_IN_PLACE_SOFTWARE_UPDATE: {
                        cloudOwnerService.enrichRequestWithCloudOwner(currentRequestDetails);
                        msoResponseWrapperObject = msoBusinessLogic.updateVnfSoftware(currentRequestDetails, serviceInstanceId, vnfInstanceId);
                        break;
                    }
                    case ChangeManagementRequest.CONFIG_UPDATE: {
                        msoResponseWrapperObject = msoBusinessLogic.updateVnfConfig(currentRequestDetails, serviceInstanceId, vnfInstanceId);
                        break;
                    }
                    case ChangeManagementRequest.SCALE_OUT:{
                        msoResponseWrapperObject = msoBusinessLogic.scaleOutVfModuleInstance(currentRequestDetails, serviceInstanceId, vnfInstanceId);
                        break;
                    }
                    default:
                        throw new GenericUncheckedException("Failure during doChangeManagement with request " + request.toString());
                }
                response = new ResponseEntity<>(msoResponseWrapperObject.getResponse(), HttpStatus.OK);
                return response;
            } catch (Exception e) {
                logger.error("Failure during doChangeManagement with request " + request.toString(), e);
                throw e;
            }

        }

        // AH:TODO: return ChangeManagementResponse
        return null;
    }

    private String extractVnfInstanceId(RequestDetails currentRequestDetails, String requestType) {
        if (currentRequestDetails.getVnfInstanceId() == null) {
            logger.error("Failed to extract vnfInstanceId");
            throw new BadRequestException("No vnfInstanceId in request " + requestType);
        }
        return currentRequestDetails.getVnfInstanceId();
    }

    protected String extractServiceInstanceId(RequestDetails currentRequestDetails, String requestType) {
        try {
            String serviceInstanceId = currentRequestDetails.getRelatedInstList().get(0).getRelatedInstance().getInstanceId();
            serviceInstanceId.toString(); //throw exception in case that serviceInstanceId is null...
            return serviceInstanceId;
        } catch (Exception e) {
            logger.error("Failed to extract serviceInstanceId");
            throw new BadRequestException("No instanceId in request " + requestType);
        }
    }

    @Override
    public RestObjectWithRequestInfo<ArrayNode> getSchedulerChangeManagementsWithRequestInfo() {
        String path = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_GET_SCHEDULES);
        RestObject<ArrayNode> restObject = new RestObject<>();
        ArrayNode jsonArray = new ArrayNode(new JsonNodeFactory(true));
        restObject.set(jsonArray);
        return restClient.Get(jsonArray, path, restObject);
    }

    @Override
    public ArrayNode getSchedulerChangeManagements() {
        RestObjectWithRequestInfo<ArrayNode> responseWithRequestInfo = getSchedulerChangeManagementsWithRequestInfo();
        return responseWithRequestInfo.getRestObject().get();
    }

    @Override
    public Pair<String, Integer> deleteSchedule(String scheduleId) {
        try {
            String path = String.format(SystemProperties.getProperty(SchedulerProperties.SCHEDULER_DELETE_SCHEDULE), scheduleId);
            RestObject<String> restObject = new RestObject<>();
            String str = "";
            restObject.set(str);
            restClient.Delete(str, "", path, restObject);
            String restCallResult = restObject.get();
            return new ImmutablePair<>(restCallResult, restObject.getStatusCode());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new ImmutablePair<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR.value());
        }
    }

    @Override
    public VnfWorkflowRelationResponse addVnfWorkflowRelation(VnfWorkflowRelationRequest vnfWorkflowRelationRequest) {
        VnfWorkflowRelationResponse vnfWorkflowRelationResponse = new VnfWorkflowRelationResponse();
        for (WorkflowsDetail workflowsDetail : vnfWorkflowRelationRequest.getWorkflowsDetails()) {
            if (StringUtils.isEmpty(workflowsDetail.getVnfDetails().getUUID()) ||
                    StringUtils.isEmpty(workflowsDetail.getVnfDetails().getInvariantUUID())) {
                vnfWorkflowRelationResponse.getErrors().add("Using empty UUID or invariantUUID is not allowed. Relation details: " + workflowsDetail.toString());
                continue;
            }
            @SuppressWarnings("unchecked") List<VNFDao> vnfList = dataAccessService.getList(VNFDao.class, getVnfQueryString(workflowsDetail.getVnfDetails().getUUID(), workflowsDetail.getVnfDetails().getInvariantUUID()), null, null);
            if (vnfList.isEmpty()) {
                vnfList.add(saveNewVnf(workflowsDetail));
            }
            @SuppressWarnings("unchecked") List<VidWorkflow> workflowList = dataAccessService.getList(VidWorkflow.class, String.format(" where wokflowName = '%s'", workflowsDetail.getWorkflowName()), null, null);
            if (workflowList.isEmpty()) {
                vnfWorkflowRelationResponse.getErrors().add("Not Found instance of workflow " + workflowsDetail.getWorkflowName() + " for vnf with UUID " + workflowsDetail.getVnfDetails().getUUID() + " and with invariantUUID " + workflowsDetail.getVnfDetails().getInvariantUUID());
                continue;
            }
            vnfList.get(0).getWorkflows().add(workflowList.get(0));
            try {
                dataAccessService.saveDomainObject(vnfList.get(0), null);
            } catch (NonUniqueObjectException e) {
                //In case the relation already exists, we continue running on the list
            }
        }
        return vnfWorkflowRelationResponse;
    }

    @Override
    public VnfWorkflowRelationResponse deleteVnfWorkflowRelation(VnfWorkflowRelationRequest vnfWorkflowRelationRequest) {
        VnfWorkflowRelationResponse vnfWorkflowRelationResponse = new VnfWorkflowRelationResponse();
        for (WorkflowsDetail workflowsDetail : vnfWorkflowRelationRequest.getWorkflowsDetails()) {
            @SuppressWarnings("unchecked") List<VNFDao> vnfList = dataAccessService.getList(VNFDao.class, getVnfQueryString(workflowsDetail.getVnfDetails().getUUID(), workflowsDetail.getVnfDetails().getInvariantUUID()), null, null);
            if (vnfList.size() != 1) {
                vnfWorkflowRelationResponse.getErrors().add("Found " + vnfList.size() + " instances of vnf with UUID " + workflowsDetail.getVnfDetails().getUUID() + " and vnfInvariantUUID " + workflowsDetail.getVnfDetails().getInvariantUUID());
                continue;
            }
            VidWorkflow vidWorkflow = getWorkflowOfVnf(vnfList.get(0), workflowsDetail.getWorkflowName());
            if (vidWorkflow == null) {
                vnfWorkflowRelationResponse.getErrors().add("Not Found instance of workflow " + workflowsDetail.getWorkflowName() + " for vnf with UUID " + workflowsDetail.getVnfDetails().getUUID() + " and with invariantUUID " + workflowsDetail.getVnfDetails().getInvariantUUID());
                continue;
            }
            vnfList.get(0).getWorkflows().remove(vidWorkflow);
            dataAccessService.saveDomainObject(vnfList.get(0), null);
        }
        return vnfWorkflowRelationResponse;

    }

    @Override
    public List<String> getWorkflowsForVnf(GetVnfWorkflowRelationRequest getVnfWorkflowRelationRequest) {
        List<VNFDao> vnfDaoList = new ArrayList<>();
        List<Set<String>> workflowsList = new ArrayList<>();
        getVnfDaoList(vnfDaoList, getVnfWorkflowRelationRequest);
        getWorkflowsList(workflowsList, vnfDaoList);
        return intersectWorkflows(workflowsList);
    }

    private void getVnfDaoList(List<VNFDao> vnfDaoList, GetVnfWorkflowRelationRequest getVnfWorkflowRelationRequest) {
        for (VnfDetails vnfDetails : getVnfWorkflowRelationRequest.getVnfDetails()) {
            @SuppressWarnings("unchecked") List<VNFDao> vnfList = dataAccessService.getList(VNFDao.class, getVnfQueryString(vnfDetails.getUUID(), vnfDetails.getInvariantUUID()), null, null);
            if (vnfList.size() != 1) {
                throw new NotFoundException("Found" + vnfList.size() + " instances of vnf with UUID" + vnfDetails.getUUID() + " and vnfInvariantUUID" + vnfDetails.getInvariantUUID());
            }
            vnfDaoList.add(vnfList.get(0));
        }
    }

    private void getWorkflowsList(List<Set<String>> workflowsList, List<VNFDao> vnfDaoList) {
        for (VNFDao vnfDao : vnfDaoList) {
            Set<String> tempWorkflows = vnfDao.getWorkflows().stream().map(VidWorkflow::getWokflowName).collect(Collectors.toSet());
            workflowsList.add(tempWorkflows);
        }
    }

    private List<String> intersectWorkflows(List<Set<String>> workflowsList) {
        Set<String> workflows = workflowsList.get(0);
        for (Set<String> workflow : workflowsList) {
            workflows.retainAll(workflow);
        }
        return new ArrayList<>(workflows);
    }

    private String getVnfQueryString(String UUID, String invariantUUID) {
        return " where vnfInvariantUUID = '" + invariantUUID + "' and vnfUUID = '" + UUID + "'";
    }

    private VidWorkflow getWorkflowOfVnf(VNFDao vnfDao, String workflowName) {
        VidWorkflow vidWorkflowRes = null;
        for (VidWorkflow vidWorkflow : vnfDao.getWorkflows()) {
            if (vidWorkflow.getWokflowName().equals(workflowName)) {
                vidWorkflowRes = vidWorkflow;
            }
        }
        return vidWorkflowRes;
    }

    private VNFDao saveNewVnf(WorkflowsDetail workflowsDetail) {
        VNFDao vnfDao = new VNFDao();
        vnfDao.setVnfUUID(workflowsDetail.getVnfDetails().getUUID());
        vnfDao.setVnfInvariantUUID(workflowsDetail.getVnfDetails().getInvariantUUID());
        dataAccessService.saveDomainObject(vnfDao, null);
        return vnfDao;
    }

    @Override
    public VnfWorkflowRelationAllResponse getAllVnfWorkflowRelations() {
        @SuppressWarnings("unchecked") List<VNFDao> vnfList = dataAccessService.getList(VNFDao.class, null);
        return new VnfWorkflowRelationAllResponse(
                vnfList.stream()
                        .map(VnfDetailsWithWorkflows::new)
                        .collect(Collectors.toList()));
    }

    @Override
    public String uploadConfigUpdateFile(MultipartFile file) {
        JSONObject json = null;
        try {
            json = csvService.convertCsvToJson(csvService.readCsv(file));
        } catch (InstantiationException | IllegalAccessException | IOException e) {
            throw new BadRequestException("Invalid csv file", e);
        }
        if (!validateJsonOutput(json))
            throw new BadRequestException("Invalid csv file");
        json = json.getJSONObject(PRIMARY_KEY);
        json = new JSONObject().put(PRIMARY_KEY, json.toString());
        return json.toString();
    }

    private boolean validateJsonOutput(org.json.JSONObject json) {
        if (!json.has(PRIMARY_KEY) || !json.getJSONObject(PRIMARY_KEY).keySet().containsAll(REQUIRED_KEYS))
            return false;
        return true;
    }
}