Update AAI Assistant Tools for CCVPN Extension
[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-2019 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
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.alibaba.fastjson.JSON;
24 import com.alibaba.fastjson.JSONArray;
25 import com.alibaba.fastjson.JSONObject;
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;
30
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;
36
37 import static org.onap.holmes.common.aai.AaiJsonParserUtil.extractJsonArray;
38 import static org.onap.holmes.common.aai.AaiJsonParserUtil.get;
39 import static org.onap.holmes.common.aai.AaiJsonParserUtil.getHostAddr;
40 import static org.onap.holmes.common.aai.AaiJsonParserUtil.getInfo;
41 import static org.onap.holmes.common.aai.AaiJsonParserUtil.getPath;
42
43 @Service
44 @Slf4j
45 public class AaiQuery4Ccvpn2 {
46
47     private MultivaluedMap<String, Object> headers;
48
49     static public AaiQuery4Ccvpn2 newInstance() {
50         return new AaiQuery4Ccvpn2();
51     }
52
53     private AaiQuery4Ccvpn2() {
54         headers = new MultivaluedHashMap<>();
55         headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
56         headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
57         headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
58         headers.add("Accept", "application/json");
59         headers.add("Content-Type", "application/json");
60     }
61
62     private String getSiteVNFId(String siteService) throws CorrelationException {
63         Response response = get(getHostAddr(), AaiConfig.MsbConsts.AAI_SITE_RESOURCES_QUERY);
64         String resStr = response.readEntity(String.class);
65         JSONObject resObj = JSON.parseObject(resStr);
66         JSONArray siteResources = extractJsonArray(resObj, "site-resource");
67         if (siteResources != null) {
68             for (int i = 0; i < siteResources.size(); i++) {
69                 final JSONObject object = siteResources.getJSONObject(i);
70                 if (siteService.equals(object.getString("site-resource-name"))) {
71                     JSONObject vnfInfo = getInfo(object.toJSONString(), "vnf-instance");
72                     String vnfPath = vnfInfo.getString("related-link");
73
74                     String vnfId = null;
75                     Pattern pattern = Pattern.compile("/aai/v\\d+/business/customers/customer/(.+)" +
76                                                               "/service-subscriptions/service-subscription/(.+)" +
77                                                               "/vnf-instances/vnf-instance/(.+)");
78                     Matcher matcher = pattern.matcher(vnfPath);
79                     if (matcher.find()) {
80                         vnfId = matcher.group(3);
81                     }
82
83                     return vnfId;
84                 }
85             }
86         }
87         return null;
88     }
89
90     private JSONObject getServiceInstanceByVnfId(String vnfId) throws CorrelationException {
91         Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SITE_VNF_QUERY,
92                                                        "vnfId", vnfId));
93         String resStr = response.readEntity(String.class);
94         return getInfo(resStr, "service-instance");
95     }
96
97     public JSONObject getSiteServiceInstance(String siteService) throws CorrelationException {
98         String vnfId = getSiteVNFId(siteService);
99         JSONObject serviceInstanceInfo = getServiceInstanceByVnfId(vnfId);
100         String serviceInstancePath = serviceInstanceInfo.getString("related-link");
101         Response response = get(getHostAddr(), getPath(serviceInstancePath));
102         String res = response.readEntity(String.class);
103         JSONObject instance = JSON.parseObject(res);
104         String[] params = new String[2];
105         Pattern pattern = Pattern.compile("/aai/v\\d+/business/customers/customer/(.+)" +
106                                                   "/service-subscriptions/service-subscription/(.+)" +
107                                                   "/service-instances/service-instance/(.+)");
108         Matcher matcher = pattern.matcher(serviceInstancePath);
109         if (matcher.find()) {
110             params[0] = matcher.group(1);
111             params[1] = matcher.group(2);
112         }
113         instance.put("globalSubscriberId", params[0]);
114         instance.put("serviceType", params[1]);
115         instance.put("vnfId", vnfId);
116         return instance;
117     }
118 }