ba66628ebccc7cf102f76675639a9547fd3b1c99
[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-2020 Huawei. 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.jvnet.hk2.annotations.Service;
28 import org.onap.holmes.common.aai.config.AaiConfig;
29 import org.onap.holmes.common.config.MicroServiceConfig;
30
31 import javax.ws.rs.core.MultivaluedHashMap;
32 import javax.ws.rs.core.MultivaluedMap;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35
36
37 @Service
38 @Slf4j
39 public class AaiJsonParserUtil {
40
41     public static String getPath(String urlTemplate, String paramName, String paramValue) {
42         return urlTemplate.replaceAll("\\{" + paramName + "\\}", paramValue);
43     }
44
45     public static String getPath(String serviceInstancePath) {
46         Pattern pattern = Pattern.compile("/aai/(v\\d+)/([A-Za-z0-9\\-]+[^/])(/*.*)");
47         Matcher matcher = pattern.matcher(serviceInstancePath);
48         String ret = "/api";
49         if (matcher.find()) {
50             ret += "/aai-" + matcher.group(2) + "/" + matcher.group(1) + matcher.group(3);
51         }
52
53         return ret;
54     }
55
56     public static JsonObject getInfo(String response, String field) {
57         JsonObject jObject = JsonParser.parseString(response).getAsJsonObject();
58         JsonObject relationshipList = extractJsonObject(jObject, "relationship-list");
59         JsonArray relationShip = extractJsonArray(relationshipList, "relationship");
60         if (relationShip != null) {
61             for (int i = 0; i < relationShip.size(); ++i) {
62                 final JsonObject object = relationShip.get(i).getAsJsonObject();
63                 if (object.get("related-to").getAsString().equals(field)) {
64                     return object;
65                 }
66             }
67         }
68         return null;
69     }
70
71     public static JsonObject extractJsonObject(JsonObject obj, String key) {
72         if (obj != null && key != null && obj.has(key)) {
73             return obj.get(key).getAsJsonObject();
74         }
75         return null;
76     }
77
78     public static JsonArray extractJsonArray(JsonObject obj, String key) {
79         if (obj != null && key != null && obj.has(key)) {
80             return obj.get(key).getAsJsonArray();
81         }
82         return null;
83     }
84
85     public static String getHostAddr() {
86         return MicroServiceConfig.getMsbServerAddrWithHttpPrefix();
87     }
88 }