Fixed the CLM Issues
[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-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
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.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;
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.*;
38
39 @Service
40 @Slf4j
41 public class AaiQuery4Ccvpn2 {
42
43     private MultivaluedMap<String, Object> headers;
44
45     static public AaiQuery4Ccvpn2 newInstance() {
46         return new AaiQuery4Ccvpn2();
47     }
48
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");
56     }
57
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();
69
70                     String vnfId = null;
71                     Pattern pattern = Pattern.compile("/aai/v\\d+/network/generic-vnfs/generic-vnf/(.+)");
72                     Matcher matcher = pattern.matcher(vnfPath);
73                     if (matcher.find()) {
74                         vnfId = matcher.group(1);
75                     }
76
77                     return vnfId;
78                 }
79             }
80         }
81         return null;
82     }
83
84     private JsonObject getServiceInstanceByVnfId(String vnfId) throws CorrelationException {
85         Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SITE_VNF_QUERY,
86                                                        "vnfId", vnfId));
87         String resStr = response.readEntity(String.class);
88         return getInfo(resStr, "service-instance");
89     }
90
91     public JsonObject getSiteServiceInstance(String siteService) throws CorrelationException {
92         String vnfId = getSiteVNFId(siteService);
93         if (vnfId == null) {
94             return null;
95         }
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);
109         }
110         instance.addProperty("globalSubscriberId", params[0]);
111         instance.addProperty("serviceType", params[1]);
112         instance.addProperty("vnfId", vnfId);
113         return instance;
114     }
115 }