summaryrefslogtreecommitdiffstats
path: root/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn.java
blob: de3cbf9d72ccd863b4a65f6af986cd04c786f94b (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
 * Copyright 2018 ZTE Corporation.
 * <p>
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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.
 */

package org.onap.holmes.common.aai;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.onap.holmes.common.aai.config.AaiConfig;
import org.onap.holmes.common.config.MicroServiceConfig;
import org.onap.holmes.common.exception.CorrelationException;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.HttpUrlConnectorProvider;

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

public class AaiQuery4Ccvpn {

    private MultivaluedMap<String, Object> headers;

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

    private AaiQuery4Ccvpn() {
        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");
    }

    /**
     * Query the logic link information for AAI. This method is based on the API:
     * https://<AAI host>:<AAI port>/aai/v14/network/network-resources/network-resource/{networkId}/pnfs/pnf/{pnfName}/p-interfaces?interface-name={ifName}&operational-status={status}
     * provided by AAI.
     *
     * @param networkId
     * @param pnfName
     * @param ifName
     * @param status
     * @return the ID of the logic link
     */
    public String getLogicLink(String networkId, String pnfName, String ifName, String status) throws CorrelationException {
        Map<String, String> params = new HashMap<>();
        params.put("networkId", networkId);
        params.put("pnfName", pnfName);
        params.put("ifName", ifName);

        Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_LINK_QUERY, params)
                + (status == null ? "" : String.format("&operational-status=%s", status)));
        JSONObject linkInfo = getInfo(response.readEntity(String.class), "p-interface", "logical-link");
        return extractValueFromJsonArray(linkInfo.getJSONArray("relationship-data"), "logical-link.link-name");
    }

    /**
     * Query all the instances related to a terminal point. This method is mainly based on the API:
     * https://<AAI host>:<AAI port>/aai/v14/network/connectivities?connectivity-id={connectivityId}
     * and
     * https://<AAI host>:<AAI port>/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}
     * provided by AAI. The path for getting the required instance information is: p-interface → vpn-vpnbinding → connectivity → service instance
     *
     * @param networkId
     * @param pnfName
     * @param ifName
     * @param status
     * @return all related service instances in JSONArray format
     */
    public JSONArray getServiceInstances(String networkId, String pnfName, String ifName, String status) {
        try {
            JSONObject vpnBindingInfo = getVpnBindingInfo(networkId, pnfName, ifName, status);
            String vpnBindingId = extractValueFromJsonArray(vpnBindingInfo.getJSONArray("relationship-data"),
                    "vpn-binding.vpn-id");
            JSONObject connectivityInfo = getConnectivityInfo(vpnBindingId);
            String connectivityId = extractValueFromJsonArray(connectivityInfo.getJSONArray("relationship-data"),
                    "connectivity. connectivity-id");
            JSONObject serviceInstanceInfo = getServiceInstanceByConn(connectivityId);
            String serviceInstancePath = serviceInstanceInfo.getString("related-link");
            serviceInstancePath = serviceInstancePath.substring(0, serviceInstancePath.lastIndexOf('/'));

            String[] params = new String[2];

            Pattern pattern = Pattern.compile("/aai/v\\d+/business/customers/customer/(.+)/service-subscriptions/service-subscription/(.+)");
            Matcher matcher = pattern.matcher(serviceInstancePath);
            if (matcher.find()) {
                params[0] = matcher.group(1);
                params[1] = matcher.group(2);
            }

            Response response = get(getHostAddr(), getPath(serviceInstancePath));
            JSONArray instances = getInstances(response.readEntity(String.class));
            for (int i = 0; i < instances.size(); ++i) {
                JSONObject instance = instances.getJSONObject(i);
                Response res = get(getHostAddr(), serviceInstancePath + "/service-instances?service-instance-id="
                        + instance.getString("service-instance-id"));
                String inputParams = JSONObject.parseObject(res.readEntity(String.class)).getString("input-parameters");
                instance.put("input-parameters", inputParams);
                instance.put("globalSubscriberId", params[0]);
                instance.put("serviceType", params[1]);
            }

            return instances;
        } catch (CorrelationException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public void updateTerminalPointStatus(String networkId, String pnfName, String ifName,
                                          Map<String, Object> body) throws CorrelationException {
        Map<String, String> params = new HashMap<>();
        params.put("networkId", networkId);
        params.put("pnfName", pnfName);
        params.put("ifName", ifName);
        patch(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, params), body);
    }

    public void updateLogicLinkStatus(String linkName, Map<String, Object> body) throws CorrelationException {
        patch(getHostAddr(),
                getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, "linkName", linkName), body);
    }

    private JSONObject getVpnBindingInfo(String networkId, String pnfName,
                                         String ifName, String status) throws CorrelationException {
        Map<String, String> params = new HashMap();
        params.put("networkId", networkId);
        params.put("pnfName", pnfName);
        params.put("ifName", ifName);
        params.put("status", status);
        Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_VPN_ADDR, params));
        return getInfo(response.readEntity(String.class), "p-interface", "vpn-binding");
    }

    private JSONObject getConnectivityInfo(String vpnId) throws CorrelationException {
        Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_CONN_ADDR, "vpnId", vpnId));
        return getInfo(response.readEntity(String.class), "vpn-binding", "connectivity");
    }

    private JSONObject getServiceInstanceByConn(String connectivityId) throws CorrelationException {
        Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SERVICE_INSTANCE_ADDR_4_CCVPN,
                "connectivityId", connectivityId));
        return getInfo(response.readEntity(String.class), "connectivity", "service-instance");
    }

    private JSONArray getServiceInstances(String globalCustomerId, String serviceType) throws CorrelationException {
        Map<String, String> params = new HashMap();
        params.put("global-customer-id", globalCustomerId);
        params.put("service-type", serviceType);
        Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SERVICE_INSTANCES_ADDR_4_CCVPN, params));
        return getInstances(response.readEntity(String.class));
    }

    private String getPath(String urlTemplate, Map<String, String> pathParams) {
        String url = urlTemplate;
        for (String key : pathParams.keySet()) {
            url = url.replaceAll("\\{" + key + "\\}", pathParams.get(key));
        }
        return url;
    }

    private String getPath(String urlTemplate, String paramName, String paramValue) {
        return urlTemplate.replaceAll("\\{" + paramName + "\\}", paramValue);
    }

    private String getPath(String serviceInstancePath) {
        Pattern pattern = Pattern.compile("/aai/(v\\d+)/([A-Za-z0-9\\-]+[^/])(/*.*)");
        Matcher matcher = pattern.matcher(serviceInstancePath);
        String ret = "/api";
        if (matcher.find()) {
            ret += "/aai-" + matcher.group(2) + "/" + matcher.group(1) + matcher.group(3);
        }

        return ret;
    }

    private Response get(String host, String path) throws CorrelationException {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(host).path(path);
        try {
            Response response = target.request().headers(getAaiHeaders()).get();
            if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
                throw new CorrelationException("Failed to connect to AAI. \nCause: "
                        + response.getStatusInfo().getReasonPhrase() + "\nDetails: \n"
                        + getErrorMsg(String.format("%s%s", host, path), null, response));
            }
            return response;
        } catch (CorrelationException e) {
            throw e;
        } catch (Exception e) {
            throw new CorrelationException(e.getMessage() + "More info: "
                    + getErrorMsg(String.format("%s%s", host, path), null, null), e);
        }
    }

    private void patch(String host, String path, Map<String, Object> body) throws CorrelationException {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(host).path(path);
        try {
            Response response = target.request().headers(getAaiHeaders()).build("PATCH", Entity.json(body))
                    .property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true).invoke();
            if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
                throw new CorrelationException("Failed to connecto to AAI. \nCause: "
                        + response.getStatusInfo().getReasonPhrase() + "\nDetails: \n"
                        + getErrorMsg(String.format("%s%s", host, path), body, response));
            }
        } catch (CorrelationException e) {
            throw e;
        } catch (Exception e) {
            throw new CorrelationException(e.getMessage() + "More info: "
                    + getErrorMsg(String.format("%s%s", host, path), body, null), e);
        }
    }

    private JSONObject getInfo(String response, String pField, String field) {
        JSONArray results = extractJsonArray(JSONObject.parseObject(response), "results");
        JSONObject pInterface = extractJsonObject(results.getJSONObject(0), pField);
        JSONObject relationshipList = extractJsonObject(pInterface, "relationship-list");
        JSONArray relationShip = extractJsonArray(relationshipList, "relationship");
        if (relationShip != null) {
            for (int i = 0; i < relationShip.size(); ++i) {
                final JSONObject object = relationShip.getJSONObject(i);
                if (object.getString("related-to").equals(field)) {
                    return object;
                }
            }
        }
        return null;
    }

    private JSONArray getInstances(String response) {
        JSONArray results = extractJsonArray(JSONObject.parseObject(response), "results");
        JSONObject pInterface = extractJsonObject(results.getJSONObject(0), "service-subscription");
        JSONObject serviceInstances = extractJsonObject(pInterface, "service-instances");
        JSONArray instance = extractJsonArray(serviceInstances, "service-instance");
        return instance;
    }

    private JSONObject extractJsonObject(JSONObject obj, String key) {
        if (obj != null && key != null && obj.containsKey(key)) {
            return obj.getJSONObject(key);
        }
        return null;
    }

    private JSONArray extractJsonArray(JSONObject obj, String key) {
        if (obj != null && key != null && obj.containsKey(key)) {
            return obj.getJSONArray(key);
        }
        return null;
    }

    private MultivaluedMap getAaiHeaders() {
        return headers;
    }

    private String getHostAddr() {
        return MicroServiceConfig.getMsbServerAddrWithHttpPrefix();
    }

    private String extractValueFromJsonArray(JSONArray relationshipData, String keyName) {
        for (int i = 0; i < relationshipData.size(); ++i) {
            JSONObject item = relationshipData.getJSONObject(i);
            if (item.getString("relationship-key").equals(keyName)) {
                return item.getString("relationship-value");
            }
        }
        return null;
    }

    private String getErrorMsg(String url, Map<String, Object> body, Response response) {
        StringBuilder sb = new StringBuilder();
        sb.append("Rerquest URL: ").append(url).append("\n");
        sb.append("Request Header: ").append(JSONObject.toJSONString(headers)).append("\n");
        if (body != null) {
            sb.append("Request Body: ").append(JSONObject.toJSONString(body)).append("\n");
        }
        if (response != null) {
            sb.append("Request Body: ").append(response.readEntity(String.class));
        }
        return sb.toString();
    }
}