1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
/*-
* ============LICENSE_START=======================================================
* org.onap.holmes.common.aai
* ================================================================================
* Copyright (C) 2018-2019 Huawei. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.holmes.common.aai;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jvnet.hk2.annotations.Service;
import org.onap.holmes.common.aai.config.AaiConfig;
import org.onap.holmes.common.exception.CorrelationException;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.onap.holmes.common.aai.AaiJsonParserUtil.extractJsonArray;
import static org.onap.holmes.common.aai.AaiJsonParserUtil.get;
import static org.onap.holmes.common.aai.AaiJsonParserUtil.getHostAddr;
import static org.onap.holmes.common.aai.AaiJsonParserUtil.getInfo;
import static org.onap.holmes.common.aai.AaiJsonParserUtil.getPath;
@Service
@Slf4j
public class AaiQuery4Ccvpn2 {
private MultivaluedMap<String, Object> headers;
static public AaiQuery4Ccvpn2 newInstance() {
return new AaiQuery4Ccvpn2();
}
private AaiQuery4Ccvpn2() {
headers = new MultivaluedHashMap<>();
headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
headers.add("Accept", "application/json");
headers.add("Content-Type", "application/json");
}
private String getSiteVNFId(String siteService) throws CorrelationException {
Response response = get(getHostAddr(), AaiConfig.MsbConsts.AAI_SITE_RESOURCES_QUERY);
String resStr = response.readEntity(String.class);
JSONObject resObj = JSON.parseObject(resStr);
JSONArray siteResources = extractJsonArray(resObj, "site-resource");
if (siteResources != null) {
for (int i = 0; i < siteResources.size(); i++) {
final JSONObject object = siteResources.getJSONObject(i);
if (siteService.equals(object.getString("site-resource-name"))) {
JSONObject vnfInfo = getInfo(object.toJSONString(), "vnf-instance");
String vnfPath = vnfInfo.getString("related-link");
String vnfId = null;
Pattern pattern = Pattern.compile("/aai/v\\d+/business/customers/customer/(.+)" +
"/service-subscriptions/service-subscription/(.+)" +
"/vnf-instances/vnf-instance/(.+)");
Matcher matcher = pattern.matcher(vnfPath);
if (matcher.find()) {
vnfId = matcher.group(3);
}
return vnfId;
}
}
}
return null;
}
private JSONObject getServiceInstanceByVnfId(String vnfId) throws CorrelationException {
Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SITE_VNF_QUERY,
"vnfId", vnfId));
String resStr = response.readEntity(String.class);
return getInfo(resStr, "service-instance");
}
public JSONObject getSiteServiceInstance(String siteService) throws CorrelationException {
String vnfId = getSiteVNFId(siteService);
JSONObject serviceInstanceInfo = getServiceInstanceByVnfId(vnfId);
String serviceInstancePath = serviceInstanceInfo.getString("related-link");
Response response = get(getHostAddr(), getPath(serviceInstancePath));
String res = response.readEntity(String.class);
JSONObject instance = JSON.parseObject(res);
String[] params = new String[2];
Pattern pattern = Pattern.compile("/aai/v\\d+/business/customers/customer/(.+)" +
"/service-subscriptions/service-subscription/(.+)" +
"/service-instances/service-instance/(.+)");
Matcher matcher = pattern.matcher(serviceInstancePath);
if (matcher.find()) {
params[0] = matcher.group(1);
params[1] = matcher.group(2);
}
instance.put("globalSubscriberId", params[0]);
instance.put("serviceType", params[1]);
instance.put("vnfId", vnfId);
return instance;
}
}
|