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.utils.JerseyClient;
29 import org.springframework.stereotype.Service;
31 import java.util.HashMap;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
36 import static org.onap.holmes.common.aai.AaiJsonParserUtil.*;
40 public class AaiQuery4Ccvpn2 {
42 private Map<String, Object> headers;
44 static public AaiQuery4Ccvpn2 newInstance() {
45 return new AaiQuery4Ccvpn2();
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");
57 private String getSiteVNFId(String siteService) {
58 String response = JerseyClient.newInstance()
60 .path(AaiConfig.MsbConsts.AAI_SITE_RESOURCES_QUERY)
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();
72 Pattern pattern = Pattern.compile("/aai/v\\d+/network/generic-vnfs/generic-vnf/(.+)");
73 Matcher matcher = pattern.matcher(vnfPath);
75 vnfId = matcher.group(1);
85 private JsonObject getServiceInstanceByVnfId(String vnfId) {
86 String resStr = JerseyClient.newInstance()
88 .path(getPath(AaiConfig.MsbConsts.AAI_SITE_VNF_QUERY,
91 return getInfo(resStr, "service-instance");
94 public JsonObject getSiteServiceInstance(String siteService) {
95 String vnfId = getSiteVNFId(siteService);
99 JsonObject serviceInstanceInfo = getServiceInstanceByVnfId(vnfId);
100 String serviceInstancePath = serviceInstanceInfo.get("related-link").getAsString();
101 String res = JerseyClient.newInstance()
103 .path(getPath(serviceInstancePath))
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);
115 instance.addProperty("globalSubscriberId", params[0]);
116 instance.addProperty("serviceType", params[1]);
117 instance.addProperty("vnfId", vnfId);