2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2018 Nokia.
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.so.bpmn.infrastructure.pnf.dmaap;
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
29 import com.google.gson.JsonParser;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Optional;
34 import java.util.Spliterator;
36 public final class JsonUtilForPnfCorrelationId {
38 private static final String JSON_PNF_CORRELATION_ID_FIELD_NAME = "correlationId";
40 private JsonUtilForPnfCorrelationId() {
41 throw new IllegalStateException("Utility class");
44 static List<String> parseJsonToGelAllPnfCorrelationId(String json) {
45 if (json == null || json.isEmpty()) {
46 return Collections.emptyList();
48 JsonElement je = new JsonParser().parse(json);
49 JsonArray array = je.getAsJsonArray();
50 List<String> list = new ArrayList<>();
51 Spliterator<JsonElement> spliterator = array.spliterator();
52 spliterator.forEachRemaining(jsonElement -> handleEscapedCharacters(jsonElement)
53 .ifPresent(jsonObject -> getPnfCorrelationId(jsonObject)
54 .ifPresent(pnfCorrelationId -> list.add(pnfCorrelationId))));
58 private static Optional<JsonObject> handleEscapedCharacters(JsonElement jsonElement) {
59 if (jsonElement.isJsonObject()) {
60 return Optional.ofNullable(jsonElement.getAsJsonObject());
62 return Optional.ofNullable(new JsonParser().parse(jsonElement.getAsString()).getAsJsonObject());
65 private static Optional<String> getPnfCorrelationId(JsonObject jsonObject) {
66 if (jsonObject.has(JSON_PNF_CORRELATION_ID_FIELD_NAME)) {
67 return Optional.ofNullable(jsonObject.get(JSON_PNF_CORRELATION_ID_FIELD_NAME).getAsString());
69 return Optional.empty();