aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/a1pesimulator/service/pm/FileReadyEventService.java
blob: df2ce97a5fa0cb8cdad875fa136ce9cea7eb988d (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
/*
 * Copyright (C) 2021 Samsung Electronics
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package org.onap.a1pesimulator.service.pm;

import static org.onap.a1pesimulator.service.pm.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.time.Instant;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
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.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;

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

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

    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();
        CommonEventHeader commonEventHeader = getCommonHeader();
        event.setCommonEventHeader(commonEventHeader);
        commonEventHeader.setStartEpochMicrosec(ChronoUnit.MICROS.between(Instant.EPOCH, fileData.getStartEventDate()));
        commonEventHeader.setLastEpochMicrosec(ChronoUnit.MICROS.between(Instant.EPOCH, fileData.getEndEventDate()));
        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())
                .priority(priority)
                .reportingEntityName(reportingEntityName)
                .sequence(0)
                .timeZoneOffset(ZonedDateTime.now().getOffset().toString())
                .build();
    }

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