Switched from Dropwizard to Springboot
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / aai / AaiQuery4Ccvpn2.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.holmes.common.aai
4  * ================================================================================
5  * Copyright (C) 2018-2021 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.onap.holmes.common.aai.config.AaiConfig;
28 import org.onap.holmes.common.utils.JerseyClient;
29 import org.springframework.stereotype.Service;
30
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35
36 import static org.onap.holmes.common.aai.AaiJsonParserUtil.*;
37
38 @Slf4j
39 @Service
40 public class AaiQuery4Ccvpn2 {
41
42     private Map<String, Object> headers;
43
44     static public AaiQuery4Ccvpn2 newInstance() {
45         return new AaiQuery4Ccvpn2();
46     }
47
48     private AaiQuery4Ccvpn2() {
49         headers = new HashMap<>();
50         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
51         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
52         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
53         headers.put("Accept", "application/json");
54         headers.put("Content-Type", "application/json");
55     }
56
57     private String getSiteVNFId(String siteService) {
58         String response = JerseyClient.newInstance()
59                 .headers(headers)
60                 .path(AaiConfig.MsbConsts.AAI_SITE_RESOURCES_QUERY)
61                 .get(getHostAddr());
62         JsonObject resObj = JsonParser.parseString(response).getAsJsonObject();
63         JsonArray siteResources = extractJsonArray(resObj, "site-resource");
64         if (siteResources != null) {
65             for (int i = 0; i < siteResources.size(); i++) {
66                 final JsonObject object = siteResources.get(i).getAsJsonObject();
67                 if (siteService.equals(object.get("site-resource-name").getAsString())) {
68                     JsonObject vnfInfo = getInfo(object.toString(), "generic-vnf");
69                     String vnfPath = vnfInfo.get("related-link").getAsString();
70
71                     String vnfId = null;
72                     Pattern pattern = Pattern.compile("/aai/v\\d+/network/generic-vnfs/generic-vnf/(.+)");
73                     Matcher matcher = pattern.matcher(vnfPath);
74                     if (matcher.find()) {
75                         vnfId = matcher.group(1);
76                     }
77
78                     return vnfId;
79                 }
80             }
81         }
82         return null;
83     }
84
85     private JsonObject getServiceInstanceByVnfId(String vnfId) {
86         String resStr = JerseyClient.newInstance()
87                 .headers(headers)
88                 .path(getPath(AaiConfig.MsbConsts.AAI_SITE_VNF_QUERY,
89                 "vnfId", vnfId))
90                 .get(getHostAddr());
91         return getInfo(resStr, "service-instance");
92     }
93
94     public JsonObject getSiteServiceInstance(String siteService) {
95         String vnfId = getSiteVNFId(siteService);
96         if (vnfId == null) {
97             return null;
98         }
99         JsonObject serviceInstanceInfo = getServiceInstanceByVnfId(vnfId);
100         String serviceInstancePath = serviceInstanceInfo.get("related-link").getAsString();
101         String res = JerseyClient.newInstance()
102                 .headers(headers)
103                 .path(getPath(serviceInstancePath))
104                 .get(getHostAddr());
105         JsonObject instance = JsonParser.parseString(res).getAsJsonObject();
106         String[] params = new String[2];
107         Pattern pattern = Pattern.compile("/aai/v\\d+/business/customers/customer/(.+)" +
108                 "/service-subscriptions/service-subscription/(.+)" +
109                 "/service-instances/service-instance/(.+)");
110         Matcher matcher = pattern.matcher(serviceInstancePath);
111         if (matcher.find()) {
112             params[0] = matcher.group(1);
113             params[1] = matcher.group(2);
114         }
115         instance.addProperty("globalSubscriberId", params[0]);
116         instance.addProperty("serviceType", params[1]);
117         instance.addProperty("vnfId", vnfId);
118         return instance;
119     }
120 }