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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.holmes.common.aai;
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;
32 import java.util.HashMap;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
37 import static org.onap.holmes.common.aai.AaiJsonParserUtil.*;
41 public class AaiQuery4Ccvpn2 {
43 private Map<String, Object> headers;
45 static public AaiQuery4Ccvpn2 newInstance() {
46 return new AaiQuery4Ccvpn2();
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");
58 private String getSiteVNFId(String siteService) {
59 String response = JerseyClient.newInstance()
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();
73 Pattern pattern = Pattern.compile("/aai/v\\d+/network/generic-vnfs/generic-vnf/(.+)");
74 Matcher matcher = pattern.matcher(vnfPath);
76 vnfId = matcher.group(1);
86 private JsonObject getServiceInstanceByVnfId(String vnfId) {
87 String resStr = JerseyClient.newInstance()
89 .path(getPath(AaiConfig.AaiConsts.AAI_SITE_VNF_QUERY,
91 .get(MicroServiceConfig.getAaiAddr());
92 return getInfo(resStr, "service-instance");
95 public JsonObject getSiteServiceInstance(String siteService) {
96 String vnfId = getSiteVNFId(siteService);
100 JsonObject serviceInstanceInfo = getServiceInstanceByVnfId(vnfId);
101 String serviceInstancePath = serviceInstanceInfo.get("related-link").getAsString();
102 String res = JerseyClient.newInstance()
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);
116 instance.addProperty("globalSubscriberId", params[0]);
117 instance.addProperty("serviceType", params[1]);
118 instance.addProperty("vnfId", vnfId);