Merge "Improvements in JsonUtilForPnfCorrelationId"
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / java / org / onap / so / bpmn / infrastructure / pnf / dmaap / JsonUtilForPnfCorrelationId.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
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=========================================================
22  */
23
24 package org.onap.so.bpmn.infrastructure.pnf.dmaap;
25
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.List;
32 import java.util.Optional;
33 import java.util.Spliterator;
34
35 public final class JsonUtilForPnfCorrelationId {
36
37     private static final String JSON_PNF_CORRELATION_ID_FIELD_NAME = "correlationId";
38
39     private JsonUtilForPnfCorrelationId() {
40         throw new IllegalStateException("Utility class");
41     }
42
43     static List<String> parseJsonToGelAllPnfCorrelationId(String json) {
44         JsonElement je = new JsonParser().parse(json);
45         JsonArray array = je.getAsJsonArray();
46         List<String> list = new ArrayList<>();
47         Spliterator<JsonElement> spliterator = array.spliterator();
48         spliterator.forEachRemaining(jsonElement -> handleEscapedCharacters(jsonElement)
49                 .ifPresent(jsonObject -> getPnfCorrelationId(jsonObject)
50                         .ifPresent(pnfCorrelationId -> list.add(pnfCorrelationId))));
51         return list;
52     }
53
54     private static Optional<JsonObject> handleEscapedCharacters(JsonElement jsonElement) {
55         if (jsonElement.isJsonObject()) {
56             return Optional.ofNullable(jsonElement.getAsJsonObject());
57         }
58         return Optional.ofNullable(new JsonParser().parse(jsonElement.getAsString()).getAsJsonObject());
59     }
60
61     private static Optional<String> getPnfCorrelationId(JsonObject jsonObject) {
62         if (jsonObject.has(JSON_PNF_CORRELATION_ID_FIELD_NAME)) {
63             return Optional.ofNullable(jsonObject.get(JSON_PNF_CORRELATION_ID_FIELD_NAME).getAsString());
64         }
65         return Optional.empty();
66     }
67 }