2 * ============LICENSE_START===================================================
3 * Copyright (c) 2018 Amdocs
4 * ============================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=====================================================
18 package org.onap.sdnc.apps.pomba.networkdiscovery.service;
20 import java.text.MessageFormat;
21 import java.util.ArrayList;
22 import java.util.List;
25 import javax.ws.rs.client.Client;
26 import javax.ws.rs.core.HttpHeaders;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.Response.Status;
30 import javax.ws.rs.core.Response.Status.Family;
32 import org.onap.logging.ref.slf4j.ONAPLogAdapter;
33 import org.onap.pomba.common.datatypes.DataQuality;
34 import org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException;
35 import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.Attribute;
36 import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.NetworkDiscoveryNotification;
37 import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.NetworkDiscoveryResponse;
38 import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.Resource;
39 import org.onap.sdnc.apps.pomba.networkdiscovery.service.util.TransformationUtil;
40 import org.slf4j.Logger;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Service;
45 public class SpringServiceImpl implements SpringService {
46 private static final String OPENSTACK_HEADER_TOKEN = "X-Auth-Token";
47 private static final String OPENSTACK_HEADER_API_VERSION = "X-OpenStack-Nova-API-Version";
50 private Client openstackClient;
53 private String openstackIdentityUrl;
56 private String openstackIdentityUser;
59 private String openstackIdentityPassword;
62 private String openstackApiMicroversion;
64 @javax.annotation.Resource
65 private Map<String, String> openstackTypeURLs;
68 public NetworkDiscoveryResponse findbyResourceIdAndType(String transactionId, String requestId, String resourceType,
69 List<String> resourceIds, ONAPLogAdapter adapter) throws ApplicationException {
71 NetworkDiscoveryResponse response = new NetworkDiscoveryResponse();
72 response.setRequestId(requestId);
74 // check if resourceType is supported
75 String openstackURL = this.openstackTypeURLs.get(resourceType);
76 if (openstackURL == null) {
77 throw new ApplicationException(ApplicationException.Error.GENERAL_FAILURE, Status.BAD_REQUEST,
78 "Unsupported resourceType " + resourceType);
80 String token = OSAuthentication.getToken(openstackIdentityUrl, openstackIdentityUser, openstackIdentityPassword,
81 openstackClient, adapter);
83 NetworkDiscoveryNotification discoveryResponse = new NetworkDiscoveryNotification();
84 discoveryResponse.setRequestId(requestId);
86 List<Resource> resources = new ArrayList<>();
87 MessageFormat format = new MessageFormat(openstackURL);
89 for (String resourceId : resourceIds) {
90 String url = format.format(new Object[] { resourceId });
91 Resource resource = new Resource();
92 resource.setType(resourceType);
93 resource.setId(resourceId);
94 resources.add(resource);
98 result = openstackClient.target(url).request().header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
99 .header(OPENSTACK_HEADER_TOKEN, token)
100 .header(OPENSTACK_HEADER_API_VERSION, openstackApiMicroversion).get();
101 } catch (Exception e) {
102 // in case of time-out, exit the loop and return a failure.
103 throw new ApplicationException(ApplicationException.Error.GENERAL_FAILURE, Status.NOT_FOUND,
104 "Openstack API GET failed - " + e.getMessage());
107 String jsonResult = result.readEntity(String.class);
108 Logger log = adapter.unwrap();
110 log.info("Openstack GET result for resourceID {}: {}", resourceId, jsonResult);
112 if (result.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
113 String transformedOutput = TransformationUtil.transform(jsonResult, resourceType);
115 log.debug("Jolt transformed output: {}", transformedOutput);
117 resource.setDataQuality(DataQuality.ok());
118 List<Attribute> attributeList = TransformationUtil.toAttributeList(transformedOutput);
119 resource.setAttributeList(attributeList);
121 resource.setDataQuality(DataQuality.error(jsonResult));
125 discoveryResponse.setResources(resources);
126 discoveryResponse.setCode(Status.OK.getStatusCode());
127 discoveryResponse.setMessage(Status.OK.getReasonPhrase());
128 discoveryResponse.setAckFinalIndicator(true);
130 return discoveryResponse;