summaryrefslogtreecommitdiffstats
path: root/datacollector/src/main/java/org/onap/rapp/datacollector/service/FileReadyParserImpl.java
blob: 831b02627d0abda730038b8e378f43ebbdc75bd0 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * 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.rapp.datacollector.service;

import static java.util.Objects.nonNull;
import static org.onap.rapp.datacollector.service.PMService.CELL_FIELD_NAME;
import static org.onap.rapp.datacollector.service.PMService.VALUE_NAME;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.onap.rapp.datacollector.entity.fileready.FileReadyEvent;
import org.onap.rapp.datacollector.entity.fileready.MeasDataCollection;
import org.onap.rapp.datacollector.entity.fileready.MeasDataCollection.MeasInfo;
import org.onap.rapp.datacollector.entity.fileready.MeasDataCollection.MeasInfo.MeasValue;
import org.onap.rapp.datacollector.entity.ves.AdditionalMeasurements;
import org.onap.rapp.datacollector.entity.ves.CommonEventHeader;
import org.onap.rapp.datacollector.entity.ves.Event;
import org.onap.rapp.datacollector.entity.ves.MeasurementFields;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class FileReadyParserImpl extends ParserAbstractClass implements VesParser {

    private static final Logger logger = LoggerFactory.getLogger(FileReadyParserImpl.class);

    public static final String MAP_ENTITY_DELIMITER = ",";
    public static final String MAP_VALUES_DELIMITER = ":";

    /**
     * Parse incoming Json string into list of Events
     *
     * @param eventString json from PM Mapper
     * @return list of events
     */
    @Override
    public List<Event> parse(final String eventString) {
        logger.debug("parsing ves event {}", eventString);
        FileReadyEvent fileReadyEvent = gson.fromJson(eventString, FileReadyEvent.class);
        return convertFileReadyEventToEventList(fileReadyEvent, eventString);
    }

    /**
     * Convert FileReadyEvent event into list of events which will be stored in database
     *
     * @param fileReadyEvent object created from PM Mapper response
     * @param eventString Json event in string
     * @return list of events
     */
    private List<Event> convertFileReadyEventToEventList(FileReadyEvent fileReadyEvent, String eventString) {
        List<Event> events = new ArrayList<>();
        long averageMeasInterval = getAverageMeasInterval(fileReadyEvent);
        fileReadyEvent.getMeasDataCollection().getMeasInfoList()
                .forEach(measInfo -> measInfo.getMeasValuesList().stream()
                        .filter(measValue -> hasListOfTypesSameSizeAsListOfResults(measInfo, measValue))
                        .forEach(measValue -> events.add(createEvent(fileReadyEvent, measInfo, measValue, eventString, averageMeasInterval))));
        return events;
    }

    /**
     * Creates individual event from FileReadyEvent data
     *
     * @param fileReadyEvent bject created from PM Mapper response
     * @param measInfo measurement Info object
     * @param measValue measurement Value object
     * @param eventString Json event in string
     * @param averageMeasInterval calculated average interval
     * @return Event object
     */
    private Event createEvent(FileReadyEvent fileReadyEvent, MeasInfo measInfo, MeasValue measValue, String eventString, long averageMeasInterval) {
        List<AdditionalMeasurements> additionalMeasList = new ArrayList<>();
        // Adding measurement's results to additionalMeasList
        measValue.getMeasResults()
                .forEach(measResult -> {
                            Map<String, String> hashMap = createAdditionalMeasurementHashMap(measResult.getSValue());
                            additionalMeasList.add(AdditionalMeasurements.builder()
                                    .withName(measInfo.getMeasTypes().getSMeasTypesList().get(measResult.getP() - 1))
                                    .withHashMap(hashMap).build());
                        }
                );
        // Adding cell identifier record to additionalMeasList
        additionalMeasList.add(AdditionalMeasurements.builder()
                .withName(CELL_FIELD_NAME)
                .withHashMap(Collections.singletonMap(CELL_FIELD_NAME, measValue.getMeasObjInstId())).build());

        MeasurementFields measurementFields = MeasurementFields.builder()
                .measurementInterval(averageMeasInterval)
                .additionalMeasurements(additionalMeasList)
                .build();
        Event createdEvent = Event.of(createEventHeader(fileReadyEvent, averageMeasInterval), measurementFields);
        createdEvent.raw = eventString;
        return createdEvent;
    }

    private Map<String, String> createAdditionalMeasurementHashMap(String value) {
        if (!value.contains(MAP_ENTITY_DELIMITER)) {
            return Collections.singletonMap(VALUE_NAME, value);
        } else {
            return Stream.of(value.split(MAP_ENTITY_DELIMITER))
                           .map(m -> m.split(MAP_VALUES_DELIMITER))
                           .collect(Collectors.toMap(v -> v[0], v -> v.length > 1 ? v[1] : ""));
        }
    }

    /**
     * Creates CommonEventHeader as new copy of initial CommonEventHeader and sets its start/end date by average interval
     *
     * @param fileReadyEvent object created from PM Mapper response
     * @param averageMeasInterval calculated average interval
     * @return created CommonEventHeader
     */
    private CommonEventHeader createEventHeader(FileReadyEvent fileReadyEvent, long averageMeasInterval) {
        CommonEventHeader headerCopy = gson.fromJson(gson.toJson(fileReadyEvent.getCommonEventHeader()), CommonEventHeader.class);
        headerCopy.setStartEpochMicrosec(headerCopy.getStartEpochMicrosec() - averageMeasInterval);
        headerCopy.setLastEpochMicrosec(fileReadyEvent.getCommonEventHeader().getStartEpochMicrosec());
        fileReadyEvent.getCommonEventHeader().setStartEpochMicrosec(headerCopy.getLastEpochMicrosec() + averageMeasInterval);
        return headerCopy;
    }

    /**
     * As MeansType will be selected by its position in the list we need to make sure that MeasTypesList's size is the same size of MeasResults
     *
     * @param measInfo measurement Info object
     * @param measValue measurement Value object
     * @return true=size is the same, false=size is different we can not process it
     */
    private boolean hasListOfTypesSameSizeAsListOfResults(MeasInfo measInfo, MeasValue measValue) {
        return measInfo.getMeasTypes().getSMeasTypesList().size() == measValue.getMeasResults().size();
    }

    /**
     * Average interval between last and start day, divided by number of measurements
     *
     * @param fileReadyEvent object created from PM Mapper response
     * @return Average interval in microseconds
     */
    private long getAverageMeasInterval(FileReadyEvent fileReadyEvent) {
        int noOfMeasurment = fileReadyEvent.getMeasDataCollection().getMeasInfoList().size();
        int dividedBy = (noOfMeasurment == 0 || noOfMeasurment == 1) ? 1 : (noOfMeasurment - 1);
        long difference = fileReadyEvent.getCommonEventHeader().getLastEpochMicrosec() - fileReadyEvent.getCommonEventHeader().getStartEpochMicrosec();
        return difference / dividedBy;
    }

    /**
     * Class which deserialize json event into FileReadyEvent object
     */
    private static class FileReadyEventDeserializer implements JsonDeserializer<FileReadyEvent> {

        @Override
        public FileReadyEvent deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext)
                throws JsonParseException {
            Optional<JsonObject> eventJsonObject = getEventJsonObject(jsonElement);
            CommonEventHeader header = getHeaderJsonObject(eventJsonObject.orElse(null), jsonDeserializationContext);
            header.setStartEpochMicrosec(header.getStartEpochMicrosec() * 1000);
            header.setLastEpochMicrosec(header.getLastEpochMicrosec() * 1000);

            Optional<JsonObject> measDataCollectionJson = getMeasDataCollectionJson(eventJsonObject.orElse(null));
            if (measDataCollectionJson.isPresent()) {
                MeasDataCollection measDataCollection = jsonDeserializationContext.deserialize(measDataCollectionJson.get(), MeasDataCollection.class);
                logger.trace("measDataCollection {}", measDataCollection);
                return FileReadyEvent.builder().commonEventHeader(header).measDataCollection(measDataCollection).build();
            } else {
                logger.error("MeasDataCollection was not found {}", eventJsonObject);
                throw new JsonParseException("MeasDataCollection was not found");
            }
        }

        private Optional<JsonObject> getMeasDataCollectionJson(JsonObject obj) {
            if (nonNull(obj)) {
                Optional<JsonObject> fileReadyJson = Optional.ofNullable(obj.getAsJsonObject(FILE_READY_EVENT_UNIQUE_ELEMENT));
                if (fileReadyJson.isPresent()) {
                    return Optional.ofNullable(fileReadyJson.get().getAsJsonObject("measDataCollection"));
                }
            }
            return Optional.empty();
        }
    }

    private final Gson gson = new GsonBuilder()
            .registerTypeAdapter(FileReadyEvent.class, new FileReadyEventDeserializer()).create();

}