9cb566f49b5700d6e704284e76c01adb4a2e60f2
[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.Collections;
32 import java.util.List;
33 import java.util.Optional;
34 import java.util.Spliterator;
35
36 public final class JsonUtilForPnfCorrelationId {
37
38     private static final String JSON_PNF_CORRELATION_ID_FIELD_NAME = "correlationId";
39
40     private JsonUtilForPnfCorrelationId() {
41         throw new IllegalStateException("Utility class");
42     }
43
44     static List<String> parseJsonToGelAllPnfCorrelationId(List<String> list) {
45         if (list == null || list.isEmpty()) {
46             return Collections.emptyList();
47         }
48
49         List<String> newList = new ArrayList<>();
50         list.forEach(je -> handleEscapedCharacters(new JsonParser().parse(je))
51                 .ifPresent(jsonObject -> getPnfCorrelationId(jsonObject)
52                         .ifPresent(pnfCorrelationId -> newList.add(pnfCorrelationId))));
53         return newList;
54     }
55
56     private static Optional<JsonObject> handleEscapedCharacters(JsonElement jsonElement) {
57         if (jsonElement.isJsonObject()) {
58             return Optional.ofNullable(jsonElement.getAsJsonObject());
59         }
60         return Optional.ofNullable(new JsonParser().parse(jsonElement.getAsString()).getAsJsonObject());
61     }
62
63     private static Optional<String> getPnfCorrelationId(JsonObject jsonObject) {
64         if (jsonObject.has(JSON_PNF_CORRELATION_ID_FIELD_NAME)) {
65             return Optional.ofNullable(jsonObject.get(JSON_PNF_CORRELATION_ID_FIELD_NAME).getAsString());
66         }
67         return Optional.empty();
68     }
69 }