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