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
|
/*
* ============LICENSE_START===================================================
* Copyright (c) 2018 Amdocs
* ============================================================================
* 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.pomba.contextbuilder.sdnc.handlers;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ws.rs.client.Client;
import org.apache.camel.Exchange;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.onap.aai.restclient.client.RestClient;
import org.onap.pomba.common.datatypes.ModelContext;
import org.onap.pomba.common.datatypes.Service;
import org.onap.pomba.contextbuilder.sdnc.exception.AuditError;
import org.onap.pomba.contextbuilder.sdnc.exception.AuditException;
import org.onap.pomba.contextbuilder.sdnc.model.ServiceEntity;
import org.onap.pomba.contextbuilder.sdnc.model.Vnf;
import org.onap.pomba.contextbuilder.sdnc.model.VnfInstance;
import org.onap.pomba.contextbuilder.sdnc.util.RestUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class VnfApiHandler {
private static Logger log = LoggerFactory.getLogger(VnfApiHandler.class);
@Autowired
private String aaiBasicAuthorization;
@Autowired
private RestClient aaiClient;
@Autowired
private String aaiBaseUrl;
@Autowired
private String aaiServiceInstancePath;
@Autowired
private Client jerseyClient;
@Autowired
private String sdncBaseUrl;
@Autowired
private String sdncBasicAuthorization;
@Autowired
private String sdncVnfPath;
private static final String JSON_RELATIONSHIP_LIST = "relationship-list";
private static final String JSON_RELATIONSHIP = "relationship";
private static final String JSON_RELATED_TO = "related-to";
private static final String JSON_RELATED_LINK = "related-link";
private static final String JSON_GENERIC_VNF = "generic-vnf";
private static final String EMPTY_JSON_STRING = "{}";
public ModelContext process(Exchange exchange) throws Exception {
log.info("in VNF-API HANDLER: ");
Service service = new Service();
ServiceEntity serviceEntity = (ServiceEntity)exchange.getIn().getBody();
service.setUuid(serviceEntity.getServiceInstanceId());
// GET the list of VNF related-to links from AAI
String url= aaiBaseUrl+generateServiceInstanceURL(aaiServiceInstancePath, serviceEntity.getCustomerId(), serviceEntity.getServiceType(), serviceEntity.getServiceInstanceId());
String serviceInstancePayload = RestUtil.getAaiResource(aaiClient, url, aaiBasicAuthorization, serviceEntity.getTransactionId());
List<String> genericVnfLinks = extractGenericVnfRelatedLink(serviceInstancePayload);
// GET the VNF list (module-id) from AAI
List <VnfInstance> vnfList = retrieveAaiVnfList(aaiClient, aaiBaseUrl, aaiBasicAuthorization, serviceEntity.getTransactionId(), genericVnfLinks);
// GET the module-id from SDNC using VNF-API
Map<String,List<Vnf>> sdncVnfMap = RestUtil.getSdncVnfList(jerseyClient, sdncBaseUrl, sdncVnfPath,
sdncBasicAuthorization, vnfList);
// Transform the AAI and SDNC models to the audit common model
ModelContext context = RestUtil.transformVnfList(vnfList, sdncVnfMap);
context.setService(service);
return context;
}
private static String generateServiceInstanceURL(String siPath, String customerId, String serviceType, String serviceInstanceId) {
return MessageFormat.format(siPath, customerId, serviceType, serviceInstanceId);
}
private static List<String> extractGenericVnfRelatedLink(String serviceInstancePayload) throws AuditException {
List<String> genericVnfLinks = new ArrayList<>();;
try {
JSONObject relationshipList = new JSONObject(serviceInstancePayload).getJSONObject(JSON_RELATIONSHIP_LIST);
JSONArray relationship = relationshipList.getJSONArray(JSON_RELATIONSHIP);
if (relationship != null) {
for (int i = 0; i < relationship.length(); i++) {
JSONObject relationshipInstance = relationship.optJSONObject(i);
if (relationshipInstance.has(JSON_RELATED_TO) && (relationshipInstance.getString(JSON_RELATED_TO).equals(JSON_GENERIC_VNF))) {
genericVnfLinks.add(relationshipInstance.getString(JSON_RELATED_LINK));
}
}
}
} catch (JSONException e) {
log.error(e.getMessage());
throw new AuditException(AuditError.JSON_PARSE_ERROR + e.getMessage());
}
return genericVnfLinks;
}
private static List<VnfInstance> retrieveAaiVnfList(RestClient aaiClient, String aaiBaseUrl, String aaiBasicAuthorization, String transactionId, List <String>genericVnfLinks) throws AuditException {
List<VnfInstance> vnfList = new ArrayList<>();
for (String genericVnfLink : genericVnfLinks) {
String genericVnfUrl = RestUtil.generateAaiUrl(aaiBaseUrl, genericVnfLink, "?depth=all");
String genericVnfPayload = RestUtil.getAaiResource(aaiClient, genericVnfUrl, aaiBasicAuthorization, transactionId);
if (genericVnfPayload.equals(EMPTY_JSON_STRING)) {
log.info("retrieveAaiVnfList "+ genericVnfPayload +" is not found, " + "return empty Json ");
} else {
// Create the AAI VnfInstance from the AAI generic-vnf json
VnfInstance vnfInstance = VnfInstance.fromJson(genericVnfPayload);
vnfList.add(vnfInstance);
}
}
log.debug("The size of vnfList:"+ vnfList.size());
return vnfList;
}
}
|