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
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.jvnet.hk2.annotations.Service;
28 import org.onap.holmes.common.aai.config.AaiConfig;
29 import org.onap.holmes.common.exception.CorrelationException;
31 import javax.ws.rs.core.MultivaluedHashMap;
32 import javax.ws.rs.core.MultivaluedMap;
33 import javax.ws.rs.core.Response;
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 MultivaluedMap<String, Object> headers;
45 static public AaiQuery4Ccvpn2 newInstance() {
46 return new AaiQuery4Ccvpn2();
49 private AaiQuery4Ccvpn2() {
50 headers = new MultivaluedHashMap<>();
51 headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
52 headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
53 headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
54 headers.add("Accept", "application/json");
55 headers.add("Content-Type", "application/json");
58 private String getSiteVNFId(String siteService) throws CorrelationException {
59 Response response = get(getHostAddr(), AaiConfig.MsbConsts.AAI_SITE_RESOURCES_QUERY);
60 String resStr = response.readEntity(String.class);
61 JsonObject resObj = JsonParser.parseString(resStr).getAsJsonObject();
62 JsonArray siteResources = extractJsonArray(resObj, "site-resource");
63 if (siteResources != null) {
64 for (int i = 0; i < siteResources.size(); i++) {
65 final JsonObject object = siteResources.get(i).getAsJsonObject();
66 if (siteService.equals(object.get("site-resource-name").getAsString())) {
67 JsonObject vnfInfo = getInfo(object.toString(), "generic-vnf");
68 String vnfPath = vnfInfo.get("related-link").getAsString();
71 Pattern pattern = Pattern.compile("/aai/v\\d+/network/generic-vnfs/generic-vnf/(.+)");
72 Matcher matcher = pattern.matcher(vnfPath);
74 vnfId = matcher.group(1);
84 private JsonObject getServiceInstanceByVnfId(String vnfId) throws CorrelationException {
85 Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SITE_VNF_QUERY,
87 String resStr = response.readEntity(String.class);
88 return getInfo(resStr, "service-instance");
91 public JsonObject getSiteServiceInstance(String siteService) throws CorrelationException {
92 String vnfId = getSiteVNFId(siteService);
96 JsonObject serviceInstanceInfo = getServiceInstanceByVnfId(vnfId);
97 String serviceInstancePath = serviceInstanceInfo.get("related-link").getAsString();
98 Response response = get(getHostAddr(), getPath(serviceInstancePath));
99 String res = response.readEntity(String.class);
100 JsonObject instance = JsonParser.parseString(res).getAsJsonObject();
101 String[] params = new String[2];
102 Pattern pattern = Pattern.compile("/aai/v\\d+/business/customers/customer/(.+)" +
103 "/service-subscriptions/service-subscription/(.+)" +
104 "/service-instances/service-instance/(.+)");
105 Matcher matcher = pattern.matcher(serviceInstancePath);
106 if (matcher.find()) {
107 params[0] = matcher.group(1);
108 params[1] = matcher.group(2);
110 instance.addProperty("globalSubscriberId", params[0]);
111 instance.addProperty("serviceType", params[1]);
112 instance.addProperty("vnfId", vnfId);