summaryrefslogtreecommitdiffstats
path: root/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2.java
blob: 41eb2c0f94ccc06e0dcc9190836c498c8529156e (plain)
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
119
120
121
/*-
 * ============LICENSE_START=======================================================
 * org.onap.holmes.common.aai
 * ================================================================================
 * Copyright (C) 2018-2020 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.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
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 org.onap.holmes.common.utils.JerseyClient;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.onap.holmes.common.aai.AaiJsonParserUtil.*;

@Service
@Slf4j
public class AaiQuery4Ccvpn2 {

    private Map<String, Object> headers;

    static public AaiQuery4Ccvpn2 newInstance() {
        return new AaiQuery4Ccvpn2();
    }

    private AaiQuery4Ccvpn2() {
        headers = new HashMap<>();
        headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
        headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
        headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
        headers.put("Accept", "application/json");
        headers.put("Content-Type", "application/json");
    }

    private String getSiteVNFId(String siteService) {
        String response = JerseyClient.newInstance()
                .headers(headers)
                .path(AaiConfig.MsbConsts.AAI_SITE_RESOURCES_QUERY)
                .get(getHostAddr());
        JsonObject resObj = JsonParser.parseString(response).getAsJsonObject();
        JsonArray siteResources = extractJsonArray(resObj, "site-resource");
        if (siteResources != null) {
            for (int i = 0; i < siteResources.size(); i++) {
                final JsonObject object = siteResources.get(i).getAsJsonObject();
                if (siteService.equals(object.get("site-resource-name").getAsString())) {
                    JsonObject vnfInfo = getInfo(object.toString(), "generic-vnf");
                    String vnfPath = vnfInfo.get("related-link").getAsString();

                    String vnfId = null;
                    Pattern pattern = Pattern.compile("/aai/v\\d+/network/generic-vnfs/generic-vnf/(.+)");
                    Matcher matcher = pattern.matcher(vnfPath);
                    if (matcher.find()) {
                        vnfId = matcher.group(1);
                    }

                    return vnfId;
                }
            }
        }
        return null;
    }

    private JsonObject getServiceInstanceByVnfId(String vnfId) {
        String resStr = JerseyClient.newInstance()
                .headers(headers)
                .path(getPath(AaiConfig.MsbConsts.AAI_SITE_VNF_QUERY,
                "vnfId", vnfId))
                .get(getHostAddr());
        return getInfo(resStr, "service-instance");
    }

    public JsonObject getSiteServiceInstance(String siteService) {
        String vnfId = getSiteVNFId(siteService);
        if (vnfId == null) {
            return null;
        }
        JsonObject serviceInstanceInfo = getServiceInstanceByVnfId(vnfId);
        String serviceInstancePath = serviceInstanceInfo.get("related-link").getAsString();
        String res = JerseyClient.newInstance()
                .headers(headers)
                .path(getPath(serviceInstancePath))
                .get(getHostAddr());
        JsonObject instance = JsonParser.parseString(res).getAsJsonObject();
        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.addProperty("globalSubscriberId", params[0]);
        instance.addProperty("serviceType", params[1]);
        instance.addProperty("vnfId", vnfId);
        return instance;
    }
}