Released Version 1.4.7
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / aai / AaiJsonParserUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.holmes.common.aai
4  * ================================================================================
5  * Copyright (C) 2018-2023 Huawei, ZTE. 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.onap.holmes.common.aai;
22
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonParser;
26 import lombok.extern.slf4j.Slf4j;
27 import org.springframework.stereotype.Component;
28
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32
33 @Slf4j
34 @Component
35 public class AaiJsonParserUtil {
36
37     public static String getPath(String urlTemplate, String paramName, String paramValue) {
38         return urlTemplate.replaceAll("\\{" + paramName + "\\}", paramValue);
39     }
40
41     public static String getPath(String serviceInstancePath) {
42         Pattern pattern = Pattern.compile("/aai/(v\\d+)/([A-Za-z0-9\\-]+[^/])(/*.*)");
43         Matcher matcher = pattern.matcher(serviceInstancePath);
44         String ret = "/api";
45         if (matcher.find()) {
46             ret += "/aai-" + matcher.group(2) + "/" + matcher.group(1) + matcher.group(3);
47         }
48
49         return ret;
50     }
51
52     public static JsonObject getInfo(String response, String field) {
53         JsonObject jObject = JsonParser.parseString(response).getAsJsonObject();
54         JsonObject relationshipList = extractJsonObject(jObject, "relationship-list");
55         JsonArray relationShip = extractJsonArray(relationshipList, "relationship");
56         if (relationShip != null) {
57             for (int i = 0; i < relationShip.size(); ++i) {
58                 final JsonObject object = relationShip.get(i).getAsJsonObject();
59                 if (object.get("related-to").getAsString().equals(field)) {
60                     return object;
61                 }
62             }
63         }
64         return null;
65     }
66
67     public static JsonObject extractJsonObject(JsonObject obj, String key) {
68         if (obj != null && key != null && obj.has(key)) {
69             return obj.get(key).getAsJsonObject();
70         }
71         return null;
72     }
73
74     public static JsonArray extractJsonArray(JsonObject obj, String key) {
75         if (obj != null && key != null && obj.has(key)) {
76             return obj.get(key).getAsJsonArray();
77         }
78         return null;
79     }
80 }