DCAE-D be initial commit
[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.Artifact;
6 import org.onap.sdc.dcae.composition.restmodels.sdc.ExternalReferencesMap;
7 import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceInstance;
8 import org.onap.sdc.dcae.composition.restmodels.sdc.ServiceDetailed;
9 import org.onap.sdc.dcae.errormng.ActionStatus;
10 import org.onap.sdc.dcae.errormng.ErrConfMgr;
11 import org.onap.sdc.dcae.utils.Normalizers;
12 import org.springframework.http.HttpStatus;
13 import org.springframework.http.ResponseEntity;
14 import org.springframework.stereotype.Component;
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             Optional<ResourceInstance> resourceInstance = serviceDetailed.getResources().stream().filter(item -> item.getResourceInstanceName().equalsIgnoreCase(vfiName)).findAny();
29             if (resourceInstance.isPresent() && resourceInstance.get().getArtifacts() != null) {
30                 Optional<Artifact> artifact = resourceInstance.get().getArtifacts().stream().filter(item -> item.getArtifactName().contains(monitoringComponentName)).findAny();
31                 artifact.ifPresent(artifact1 -> sdcRestClient.deleteInstanceResourceArtifact(userId, context, serviceUuid, normalizedVfiName, artifact1.getArtifactUUID(), requestId));
32             }
33         } catch (Exception e) {
34             debugLogger.log(LogLevel.DEBUG, this.getClass().getName(),"Failed to delete blueprint with serviceUuid {}, vfcmtUuid . message: {} ", serviceUuid, vfcmtUuid, e);
35             return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.DELETE_BLUEPRINT_FAILED);
36         }
37         return new ResponseEntity<>(HttpStatus.OK);
38     }
39
40     public void deleteVfcmtReference(String userId, String context, String serviceUuid, String vfiName, String vfcmtUuid, String requestId) {
41         debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Going to delete vfcmt reference, vfiName = {}", vfiName);
42         String normalizedVfiName = Normalizers.normalizeComponentInstanceName(vfiName);
43             sdcRestClient.deleteExternalMonitoringReference(userId, context, serviceUuid, normalizedVfiName, vfcmtUuid, requestId);
44             debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Finished to delete vfcmt reference. serviceUuid {}, vfcmtUuid {}", serviceUuid, vfcmtUuid);
45     }
46
47     // 1806 US381853 Return a list of monitoring components by external reference id. Support partial success
48     public Map<String, List<MonitoringComponent>> fetchMonitoringComponents(ExternalReferencesMap mcRefs, String requestId) {
49
50         debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Going to fetch monitoring components metadata for vfis {}", mcRefs.keySet());
51         Map<String, List<MonitoringComponent>> result = new LinkedHashMap<>();
52         List<MonitoringComponent> monitoringComponents = Collections.synchronizedList(new ArrayList<>());
53         List<MonitoringComponent> unavailable = Collections.synchronizedList(new ArrayList<>());
54         mcRefs.entrySet().parallelStream().forEach(entry ->
55                 entry.getValue().parallelStream().forEach(id -> {
56                     try{
57                         monitoringComponents.add(new MonitoringComponent(getSdcRestClient().getResource(id, requestId), entry.getKey()));
58                     } catch (Exception e) {
59                         debugLogger.log(LogLevel.DEBUG, this.getClass().getName(),"Failed to fetch monitoring component with uuid {}. message: {} ", id, e);
60                         unavailable.add(new MonitoringComponent(id, entry.getKey(), "unavailable"));
61                     }
62
63                 })
64         );
65         result.put("monitoringComponents", monitoringComponents);
66         if(!isEmpty(unavailable)) {
67             result.put("unavailable", unavailable);
68         }
69         debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Finished fetching monitoring components metadata for vfis {}", mcRefs.keySet());
70         return result;
71     }
72
73
74 }