aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/a1pesimulator/service/fileready/FileReadyEventService.java
blob: b5e7a9cdb4c23a345d290613f2137985d22815c5 (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
package org.onap.a1pesimulator.service.fileready;

import static org.onap.a1pesimulator.service.fileready.FtpServerService.deletePMBulkFile;
import static org.onap.a1pesimulator.util.Constants.FILE_READY_CHANGE_IDENTIFIER;
import static org.onap.a1pesimulator.util.Constants.FILE_READY_CHANGE_TYPE;

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.onap.a1pesimulator.data.fileready.FileData;
import org.onap.a1pesimulator.data.fileready.FileReadyEvent;
import org.onap.a1pesimulator.data.fileready.NotificationFields;
import org.onap.a1pesimulator.data.fileready.NotificationFields.ArrayOfNamedHashMap;
import org.onap.a1pesimulator.data.ves.CommonEventHeader;
import org.onap.a1pesimulator.util.RanVesUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import reactor.core.publisher.Mono;

/**
 * Service for PM Bulk File creation and its handling
 */

@Service
public class FileReadyEventService {

    private final FtpServerService ftpServerService;

    @Value("${file.ready.version}")
    private String version;

    @Value("${file.ready.vesEventListenerVersion}")
    private String vesEventListenerVersion;

    @Value("${file.ready.domain}")
    private String domain;

    @Value("${file.ready.eventName}")
    private String eventName;

    @Value("${file.ready.fileFormatType}")
    private String fileFormatType;

    @Value("${file.ready.fileFormatVersion}")
    private String fileFormatVersion;

    @Value("${file.ready.notificationFieldsVersion}")
    private String notificationFieldsVersion;

    public FileReadyEventService(FtpServerService ftpServerService) {
        this.ftpServerService = ftpServerService;
    }

    /**
     * It will create FileReadyEvent.json which will go to VES Collector
     *
     * @return created FileReadyEvent
     */
    protected Mono<FileData> createFileReadyEventAndDeleteTmpFile(Mono<FileData> fileMono) {
        return fileMono
                .map(this::createFileReadyEvent)
                .doOnNext(file -> deleteTempArchivedBulkFile(file.getArchivedPmBulkFile()));
    }

    /**
     * Creates File Ready Event
     *
     * @param fileData information about PM Bulk Files created in previous steps
     * @return added newly created FileReadyEvent to FileData
     */
    protected FileData createFileReadyEvent(FileData fileData) {
        FileReadyEvent event = new FileReadyEvent();
        event.setCommonEventHeader(getCommonHeader());
        RanVesUtils.updateHeader(event);
        event.setNotificationFields(getNotificationFields(fileData.getArchivedPmBulkFile().getName()));
        fileData.setFileReadyEvent(event);
        return fileData;
    }

    /**
     * Creates NotificationFields section in FileReadyEvent
     *
     * @param fileName name of archived PM Bulk File
     * @return NotificationFields object
     */
    private NotificationFields getNotificationFields(String fileName) {
        NotificationFields notificationFields = NotificationFields.builder()
                .changeIdentifier(FILE_READY_CHANGE_IDENTIFIER)
                .changeType(FILE_READY_CHANGE_TYPE)
                .notificationFieldsVersion(notificationFieldsVersion).build();

        ArrayOfNamedHashMap arrayOfNamedHashMap = new ArrayOfNamedHashMap();
        Map<String, String> hashMapItems = new HashMap<>();
        hashMapItems.put("location", ftpServerService.getFtpPath() + fileName);
        hashMapItems.put("compression", "gzip");
        hashMapItems.put("fileFormatType", fileFormatType);
        hashMapItems.put("fileFormatVersion", fileFormatVersion);

        arrayOfNamedHashMap.setName(fileName);
        arrayOfNamedHashMap.setHashMap(hashMapItems);
        notificationFields.setArrayOfNamedHashMap(Collections.singletonList(arrayOfNamedHashMap));
        return notificationFields;
    }

    /**
     * Creates CommonEventHeader
     *
     * @return created CommonEventHeader
     */
    private CommonEventHeader getCommonHeader() {
        return CommonEventHeader.builder()
                .version(version)
                .vesEventListenerVersion(vesEventListenerVersion)
                .domain(domain)
                .eventName(eventName)
                .eventId(UUID.randomUUID().toString())
                .build();
    }

    /**
     * Deletes temporary archived PM Bulk File
     *
     * @param fileMono temporary archived PM Bulk File
     */
    private void deleteTempArchivedBulkFile(File fileMono) {
        deletePMBulkFile(fileMono);
    }
}