2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 package org.o.ran.oam.nf.oam.adopter.pm.rest.manager.mapper;
22 import com.fasterxml.jackson.core.JsonParser;
23 import com.fasterxml.jackson.dataformat.csv.CsvMapper;
24 import com.fasterxml.jackson.dataformat.csv.CsvSchema;
25 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
26 import io.reactivex.rxjava3.core.Single;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
32 import java.util.zip.ZipEntry;
33 import java.util.zip.ZipInputStream;
34 import org.o.ran.oam.nf.oam.adopter.api.CommonEventFormat302ONAP;
35 import org.o.ran.oam.nf.oam.adopter.api.Event;
36 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.PerformanceManagementMapperConfigProvider;
37 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.pojos.CsvConfiguration;
38 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.pojos.VesMappingConfiguration;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Service;
45 public class PerformanceManagementFile2VesMapper {
46 private static final Logger LOG = LoggerFactory.getLogger(PerformanceManagementFile2VesMapper.class);
48 private static final String CSV_EXTENSION = ".csv";
49 private final PerformanceManagementMapperConfigProvider pmConfigProvider;
52 public PerformanceManagementFile2VesMapper(final PerformanceManagementMapperConfigProvider pmConfigProvider) {
53 this.pmConfigProvider = pmConfigProvider;
57 * Translate CSV in ZipInputStream format to list of CommonEventFormat302ONAP events.
59 * @param zipInputStream csv
60 * @param hostIp source Ip Address
61 * @return CommonEventFormat302ONAP events
63 @SuppressFBWarnings("REC_CATCH_EXCEPTION")
64 public Single<List<CommonEventFormat302ONAP>> map(final ZipInputStream zipInputStream, final String hostIp) {
65 LOG.info("Converting ZIP files to VES Message started");
66 final List<CommonEventFormat302ONAP> listOfNotifications = new ArrayList<>();
67 final CsvSchema schema = CsvSchema.emptySchema().withHeader();
68 final CsvMapper mapper = new CsvMapper();
69 mapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
72 final VesMappingConfiguration mappingConfiguration = pmConfigProvider.getVesMappingConfiguration();
73 while ((entry = zipInputStream.getNextEntry()) != null) {
74 final String entryName = entry.getName();
75 if (!entryName.endsWith(CSV_EXTENSION)) {
76 return Single.error(new Exception("Wrong file type :" + entryName));
79 final Iterator<Map<String, String>> iterator =
80 mapper.readerFor(Map.class).with(schema).readValues(zipInputStream);
81 final List<List<Event>> mappedEvents = toEvent(mappingConfiguration, hostIp, iterator);
83 mappedEvents.forEach(mapped -> {
84 final CommonEventFormat302ONAP eventFormat = new CommonEventFormat302ONAP();
85 eventFormat.setEventList(mapped);
86 listOfNotifications.add(eventFormat);
89 } catch (final Exception e) {
90 return Single.error(new Exception("Failed to process file", e));
93 zipInputStream.closeEntry();
94 } catch (final IOException e) {
95 LOG.warn("Failed to close zip stream", e);
98 LOG.info("Converting ZIP files to VES Message finished");
99 return Single.just(listOfNotifications);
102 private static List<List<Event>> toEvent(final VesMappingConfiguration mappingConfiguration, final String hostIp,
103 final Iterator<Map<String, String>> iterator) {
104 final List<List<Event>> globalList = new ArrayList<>();
105 final int batchSize = mappingConfiguration.getBatchSize();
107 List<Event> events = new ArrayList<>();
108 final CsvConfiguration csv = mappingConfiguration.getCsv();
109 while (iterator.hasNext()) {
110 final Event event = new Event();
111 final Map<String, String> record = iterator.next();
112 event.setCommonEventHeader(
113 CommonEventHeaderHandler.toCommonEventHeader(mappingConfiguration, hostIp, csv, record, sequence));
114 event.setMeasurementFields(MeasurementFieldsHandler.toMeasurementFields(mappingConfiguration, record));
117 if (sequence % batchSize == 0) {
118 globalList.add(events);
119 events = new ArrayList<>();
122 if (!events.isEmpty()) {
123 globalList.add(events);