Upgrade dt-be-main
[sdc/dcae-d/dt-be-main.git] / dcaedt_be / src / main / java / org / onap / sdc / dcae / composition / impl / ReferenceBusinessLogic.java
1 package org.onap.sdc.dcae.composition.impl;
2
3 import org.onap.sdc.common.onaplog.Enums.LogLevel;
4 import org.onap.sdc.dcae.composition.restmodels.MonitoringComponent;
5 import org.onap.sdc.dcae.composition.restmodels.sdc.ExternalReferencesMap;
6 import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceInstance;
7 import org.onap.sdc.dcae.composition.restmodels.sdc.ServiceDetailed;
8 import org.onap.sdc.dcae.errormng.ActionStatus;
9 import org.onap.sdc.dcae.errormng.ErrConfMgr;
10 import org.onap.sdc.dcae.utils.Normalizers;
11 import org.springframework.http.HttpStatus;
12 import org.springframework.http.ResponseEntity;
13 import org.springframework.stereotype.Component;
14 import org.springframework.util.StringUtils;
15
16 import java.util.*;
17
18 import static org.springframework.util.CollectionUtils.isEmpty;
19
20 @Component
21 public class ReferenceBusinessLogic extends BaseBusinessLogic {
22
23     public ResponseEntity deleteVfcmtReferenceBlueprint(String userId, String context, String monitoringComponentName, String serviceUuid, String vfiName, String vfcmtUuid, String requestId) {
24         debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Going to delete blueprint, monitoringComponentName = {}, vfiName = {}", monitoringComponentName, vfiName);
25         try {
26             String normalizedVfiName = Normalizers.normalizeComponentInstanceName(vfiName);
27             ServiceDetailed serviceDetailed = sdcRestClient.getService(serviceUuid, requestId);
28             ResourceInstance resourceInstance = findVfiOnService(serviceDetailed, vfiName);
29             if (resourceInstance != null && resourceInstance.getArtifacts() != null) {
30                 String artifactNameEndsWith = generateBlueprintFileName("", monitoringComponentName);
31                 resourceInstance.getArtifacts().stream()
32                         .filter(item -> StringUtils.endsWithIgnoreCase(item.getArtifactName(), artifactNameEndsWith))
33                         .findAny()
34                         .ifPresent(artifact -> sdcRestClient.deleteInstanceArtifact(userId, context, serviceUuid, normalizedVfiName, artifact.getArtifactUUID(), requestId));
35             }
36         } catch (Exception e) {
37             debugLogger.log(LogLevel.DEBUG, this.getClass().getName(),"Failed to delete blueprint with serviceUuid {}, vfcmtUuid . message: {} ", serviceUuid, vfcmtUuid, e);
38             return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.DELETE_BLUEPRINT_FAILED, e.getMessage());
39         }
40         return new ResponseEntity<>(HttpStatus.OK);
41     }
42
43
44     public void deleteVfcmtReference(String userId, String context, String serviceUuid, String vfiName, String vfcmtUuid, String requestId) {
45         debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Going to delete vfcmt reference, vfiName = {}", vfiName);
46         String normalizedVfiName = Normalizers.normalizeComponentInstanceName(vfiName);
47         sdcRestClient.deleteExternalMonitoringReference(userId, context, serviceUuid, normalizedVfiName, vfcmtUuid, requestId);
48         debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Finished to delete vfcmt reference. serviceUuid {}, vfcmtUuid {}", serviceUuid, vfcmtUuid);
49     }
50
51     // 1806 US381853 Return a list of monitoring components by external reference id. Support partial success
52     public Map<String, List<MonitoringComponent>> fetchMonitoringComponents(ExternalReferencesMap mcRefs, String requestId) {
53
54         debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Going to fetch monitoring components metadata for vfis {}", mcRefs.keySet());
55         Map<String, List<MonitoringComponent>> result = new LinkedHashMap<>();
56         List<MonitoringComponent> monitoringComponents = Collections.synchronizedList(new ArrayList<>());
57         List<MonitoringComponent> unavailable = Collections.synchronizedList(new ArrayList<>());
58         mcRefs.entrySet().parallelStream().forEach(entry ->
59                 entry.getValue().parallelStream().forEach(id -> {
60                     try{
61                         monitoringComponents.add(new MonitoringComponent(getSdcRestClient().getResource(id, requestId), entry.getKey()));
62                     } catch (Exception e) {
63                         debugLogger.log(LogLevel.DEBUG, this.getClass().getName(),"Failed to fetch monitoring component with uuid {}. message: {} ", id, e);
64                         unavailable.add(new MonitoringComponent(id, entry.getKey(), "unavailable"));
65                     }
66
67                 })
68         );
69         result.put("monitoringComponents", monitoringComponents);
70         if(!isEmpty(unavailable)) {
71             result.put("unavailable", unavailable);
72         }
73         debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Finished fetching monitoring components metadata for vfis {}", mcRefs.keySet());
74         return result;
75     }
76
77 }