Merge "Reorder modifiers"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / java / org / openecomp / mso / bpmn / infrastructure / pnf / dmaap / JsonUtilForCorrelationId.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.bpmn.infrastructure.pnf.dmaap;
22
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Optional;
30 import java.util.Spliterator;
31
32 final class JsonUtilForCorrelationId {
33
34     private static final String JSON_HEADER = "pnfRegistrationFields";
35     private static final String JSON_CORRELATION_ID_FIELD_NAME = "correlationId";
36
37     static List<String> parseJsonToGelAllCorrelationId(String json) {
38         List<String> list = new ArrayList<>();
39         JsonElement je = new JsonParser().parse(json);
40         if (je.isJsonObject()) {
41             getCorrelationIdFromJsonObject(je.getAsJsonObject()).ifPresent(corr -> list.add(corr));
42         } else {
43             JsonArray array = je.getAsJsonArray();
44             Spliterator<JsonElement> spliterator = array.spliterator();
45             spliterator.forEachRemaining(jsonElement -> {
46                 parseJsonElementToJsonObject(jsonElement)
47                         .ifPresent(jsonObject -> getCorrelationIdFromJsonObject(jsonObject)
48                                 .ifPresent(correlationId -> list.add(correlationId)));
49             });
50         }
51         return list;
52     }
53
54     private static Optional<JsonObject> parseJsonElementToJsonObject(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> getCorrelationIdFromJsonObject(JsonObject jsonObject) {
62         if (jsonObject.has(JSON_HEADER)) {
63             JsonObject jo = jsonObject.getAsJsonObject(JSON_HEADER);
64             if (jo.has(JSON_CORRELATION_ID_FIELD_NAME)) {
65                 return Optional.ofNullable(jo.get(JSON_CORRELATION_ID_FIELD_NAME).getAsString());
66             }
67         }
68         return Optional.empty();
69     }
70
71 }