Merge "Removed MsoLogger from 'asdc-controller'"
[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  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.pnf.dmaap;
24
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonParser;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Optional;
32 import java.util.Spliterator;
33
34 public final class JsonUtilForPnfCorrelationId {
35
36     private static final String JSON_PNF_CORRELATION_ID_FIELD_NAME = "pnfCorrelationId";
37
38     static List<String> parseJsonToGelAllPnfCorrelationId(String json) {
39         JsonElement je = new JsonParser().parse(json);
40         JsonArray array = je.getAsJsonArray();
41         List<String> list = new ArrayList<>();
42         Spliterator<JsonElement> spliterator = array.spliterator();
43         spliterator.forEachRemaining(jsonElement -> {
44             handleEscapedCharacters(jsonElement)
45                     .ifPresent(jsonObject -> getPnfCorrelationId(jsonObject)
46                             .ifPresent(pnfCorrelationId -> list.add(pnfCorrelationId)));
47         });
48         return list;
49     }
50
51     private static Optional<JsonObject> handleEscapedCharacters(JsonElement jsonElement) {
52         if (jsonElement.isJsonObject()) {
53             return Optional.ofNullable(jsonElement.getAsJsonObject());
54         }
55         return Optional.ofNullable(new JsonParser().parse(jsonElement.getAsString()).getAsJsonObject());
56     }
57
58     private static Optional<String> getPnfCorrelationId(JsonObject jsonObject) {
59         if (jsonObject.has(JSON_PNF_CORRELATION_ID_FIELD_NAME)) {
60             return Optional.ofNullable(jsonObject.get(JSON_PNF_CORRELATION_ID_FIELD_NAME).getAsString());
61         }
62         return Optional.empty();
63     }
64 }