summaryrefslogtreecommitdiffstats
path: root/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/service/SpringServiceImpl.java
diff options
context:
space:
mode:
Diffstat (limited to 'pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/service/SpringServiceImpl.java')
-rw-r--r--pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/service/SpringServiceImpl.java245
1 files changed, 50 insertions, 195 deletions
diff --git a/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/service/SpringServiceImpl.java b/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/service/SpringServiceImpl.java
index f9de2be..5577398 100644
--- a/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/service/SpringServiceImpl.java
+++ b/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/service/SpringServiceImpl.java
@@ -17,37 +17,19 @@
*/
package org.onap.sdnc.apps.pomba.networkdiscovery.service;
-import com.bazaarvoice.jolt.JsonUtils;
-import com.google.common.util.concurrent.ThreadFactoryBuilder;
-
-import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import javax.annotation.PreDestroy;
import javax.ws.rs.client.Client;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.MultivaluedHashMap;
-import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
-import javax.ws.rs.core.Response.StatusType;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.Response.Status.Family;
-import javax.xml.parsers.ParserConfigurationException;
-import org.onap.aai.restclient.client.OperationResult;
-import org.onap.aai.restclient.client.RestClient;
import org.onap.logging.ref.slf4j.ONAPLogAdapter;
-import org.onap.logging.ref.slf4j.ONAPLogAdapter.RequestBuilder;
-import org.onap.logging.ref.slf4j.ONAPLogConstants;
-import org.onap.logging.ref.slf4j.ONAPLogConstants.InvocationMode;
import org.onap.pomba.common.datatypes.DataQuality;
import org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException;
import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.Attribute;
@@ -63,14 +45,9 @@ import org.springframework.stereotype.Service;
public class SpringServiceImpl implements SpringService {
private static final String OPENSTACK_HEADER_TOKEN = "X-Auth-Token";
private static final String OPENSTACK_HEADER_API_VERSION = "X-OpenStack-Nova-API-Version";
- private static final int DEFAULT_WORKER_THREADS = 3;
-
- private ExecutorService executor = Executors.newFixedThreadPool(
- Integer.getInteger("discovery.threads", DEFAULT_WORKER_THREADS),
- new ThreadFactoryBuilder().setNameFormat("discovery-worker-%d").build());
@Autowired
- private RestClient openstackClient;
+ private Client openstackClient;
@Autowired
private String openstackIdentityUrl;
@@ -85,194 +62,72 @@ public class SpringServiceImpl implements SpringService {
private String openstackApiMicroversion;
@javax.annotation.Resource
- private Client callbackClient;
-
- @javax.annotation.Resource
private Map<String, String> openstackTypeURLs;
- public SpringServiceImpl() throws ParserConfigurationException {
- }
-
@Override
public NetworkDiscoveryResponse findbyResourceIdAndType(String transactionId, String requestId, String resourceType,
- List<String> resourceIds, String notificationURL, String notificationAuthorization, ONAPLogAdapter adapter)
- throws ApplicationException {
+ List<String> resourceIds, ONAPLogAdapter adapter) throws ApplicationException {
NetworkDiscoveryResponse response = new NetworkDiscoveryResponse();
response.setRequestId(requestId);
- String openstackURL = this.openstackTypeURLs.get(resourceType);
// check if resourceType is supported
+ String openstackURL = this.openstackTypeURLs.get(resourceType);
if (openstackURL == null) {
- // don't know what to do with this - return empty response
- response.setCode(Status.NO_CONTENT.getStatusCode());
- response.setMessage("Unsupported resourceType " + resourceType);
- response.setAckFinalIndicator(true);
- return response;
+ throw new ApplicationException(ApplicationException.Error.GENERAL_FAILURE, Status.BAD_REQUEST,
+ "Unsupported resourceType " + resourceType);
}
-
- // schedule discovery of specified resources
- Runnable task = new ResourceTask(transactionId, requestId, resourceType, resourceIds, notificationURL,
- notificationAuthorization, openstackURL, adapter);
- this.executor.submit(task);
-
- response.setCode(Status.ACCEPTED.getStatusCode());
- response.setMessage(Status.ACCEPTED.getReasonPhrase());
- response.setAckFinalIndicator(false);
- return response;
- }
-
- @PreDestroy
- public void shutdown() {
- this.executor.shutdown();
- }
-
- private void sendNotification(String url, String transactionId, String authorization, Object notification,
- ONAPLogAdapter adapter) {
-
- Invocation.Builder request = this.callbackClient.target(url).request().accept(MediaType.TEXT_PLAIN_TYPE);
-
- if (authorization != null) {
- request.header(HttpHeaders.AUTHORIZATION, authorization);
- request.header(ONAPLogConstants.Headers.REQUEST_ID, transactionId);
- }
- Logger log = adapter.unwrap();
- adapter.invoke(new RequestBuilderWrapper(request), InvocationMode.SYNCHRONOUS);
- try {
- log.info("Posting notfication to url = {} , payload: {}", url,
- JsonUtils.toJsonString(Entity.json(notification).getEntity()));
-
- Response result = request.post(Entity.json(notification));
-
- StatusType status = result.getStatusInfo();
-
- if (status.getFamily().equals(Family.SUCCESSFUL)) {
- log.info("request at url = {} resulted in http status code {}",
- url, status.getStatusCode());
- } else {
- log.error("request at url = {} resulted in http status code {}, reason: {}",
- url, status.getStatusCode(), status.getReasonPhrase());
- }
-
-
- } catch (Exception x) {
- log.error("Error during {} operation to endpoint at url = {} with error = {}", "POST", url,
- x.getLocalizedMessage());
- }
- }
-
- private class ResourceTask implements Runnable {
- private final String transactionId;
- private final String requestId;
- private final String resourceType;
- private final List<String> resourceIds;
- private final String notificationURL;
- private final String notificationAuthorization;
- private final String resourceURL;
- private final ONAPLogAdapter adapter;
-
- public ResourceTask(String transactionId, String requestId, String resourceType, List<String> resourceIds,
- String notificationURL, String notificationAuthorization, String resourceURL, ONAPLogAdapter adapter) {
- this.transactionId = transactionId;
- this.requestId = requestId;
- this.resourceType = resourceType;
- this.resourceIds = resourceIds;
- this.notificationURL = notificationURL;
- this.notificationAuthorization = notificationAuthorization;
- this.resourceURL = resourceURL;
- this.adapter = adapter;
- }
-
- @Override
- public void run() {
+ String token = OSAuthentication.getToken(openstackIdentityUrl, openstackIdentityUser, openstackIdentityPassword,
+ openstackClient, adapter);
+
+ NetworkDiscoveryNotification discoveryResponse = new NetworkDiscoveryNotification();
+ discoveryResponse.setRequestId(requestId);
+
+ List<Resource> resources = new ArrayList<>();
+ MessageFormat format = new MessageFormat(openstackURL);
+
+ for (String resourceId : resourceIds) {
+ String url = format.format(new Object[] { resourceId });
+ Resource resource = new Resource();
+ resource.setType(resourceType);
+ resource.setId(resourceId);
+ resources.add(resource);
+
+ Response result;
try {
- runResourceDiscoveryTask();
+ result = openstackClient.target(url).request().header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
+ .header(OPENSTACK_HEADER_TOKEN, token)
+ .header(OPENSTACK_HEADER_API_VERSION, openstackApiMicroversion).get();
} catch (Exception e) {
- Logger log = adapter.unwrap();
- log.error("Failure in resource task", e);
-
- // Try to send out a notification of the failure:
- NetworkDiscoveryNotification notification = new NetworkDiscoveryNotification();
- notification.setRequestId(this.requestId);
- notification.setCode(Status.INTERNAL_SERVER_ERROR.getStatusCode());
- notification.setMessage(e.getMessage());
- notification.setAckFinalIndicator(true);
-
- // call client back with resource details
- sendNotification(this.notificationURL, this.transactionId, this.notificationAuthorization, notification,
- adapter);
+ // in case of time-out, exit the loop and return a failure.
+ throw new ApplicationException(ApplicationException.Error.GENERAL_FAILURE, Status.NOT_FOUND,
+ "Openstack API GET failed - " + e.getMessage());
}
- }
-
- private void runResourceDiscoveryTask() throws IOException, ApplicationException {
-
- String token = OSAuthentication.getToken(openstackIdentityUrl, openstackIdentityUser,
- openstackIdentityPassword, openstackClient, adapter);
-
- NetworkDiscoveryNotification notification = new NetworkDiscoveryNotification();
- notification.setRequestId(this.requestId);
-
- List<Resource> resources = null;
- MessageFormat format = new MessageFormat(this.resourceURL);
- MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
- headers.add(OPENSTACK_HEADER_TOKEN, token);
- headers.add(OPENSTACK_HEADER_API_VERSION, openstackApiMicroversion);
-
- for (String resourceId : this.resourceIds) {
- String url = format.format(new Object[] { resourceId });
- OperationResult result = SpringServiceImpl.this.openstackClient.get(url, headers,
- MediaType.APPLICATION_JSON_TYPE);
-
- Resource resource = new Resource();
- resource.setType(this.resourceType);
- resource.setId(resourceId);
- if (resources == null) {
- resources = new ArrayList<>();
- notification.setResources(resources);
- }
- resources.add(resource);
-
- Logger log = adapter.unwrap();
-
- if (result.wasSuccessful()) {
- log.info("Openstack GET result code: {}", result.getResultCode());
-
- String transformedOutput = TransformationUtil.transform(result.getResult(), this.resourceType);
-
- log.debug("Jolt transformed output: {}", transformedOutput);
-
- resource.setDataQuality(DataQuality.ok());
- List<Attribute> attributeList = TransformationUtil.toAttributeList(transformedOutput);
- resource.setAttributeList(attributeList);
- } else {
- log.error("Openstack GET result code: {}. Failure cause: {}",
- result.getResultCode(), result.getFailureCause());
- resource.setDataQuality(DataQuality.error(result.getFailureCause()));
- }
+
+ String jsonResult = result.readEntity(String.class);
+ Logger log = adapter.unwrap();
+
+ log.info("Openstack GET result for resourceID {}: {}", resourceId, jsonResult);
+
+ if (result.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
+ String transformedOutput = TransformationUtil.transform(jsonResult, resourceType);
+
+ log.debug("Jolt transformed output: {}", transformedOutput);
+
+ resource.setDataQuality(DataQuality.ok());
+ List<Attribute> attributeList = TransformationUtil.toAttributeList(transformedOutput);
+ resource.setAttributeList(attributeList);
+ } else {
+ resource.setDataQuality(DataQuality.error(jsonResult));
}
- notification.setCode(Status.OK.getStatusCode());
- notification.setMessage(Status.OK.getReasonPhrase());
- notification.setAckFinalIndicator(true);
-
- // call client back with resource details
- sendNotification(this.notificationURL, this.transactionId, this.notificationAuthorization, notification,
- adapter);
}
- }
-
- private static class RequestBuilderWrapper implements RequestBuilder<RequestBuilderWrapper> {
- private Invocation.Builder builder;
-
- private RequestBuilderWrapper(Invocation.Builder builder) {
- this.builder = builder;
- }
-
- @Override
- public RequestBuilderWrapper setHeader(String name, String value) {
- this.builder.header(name, value);
- return this;
- }
+ discoveryResponse.setResources(resources);
+ discoveryResponse.setCode(Status.OK.getStatusCode());
+ discoveryResponse.setMessage(Status.OK.getReasonPhrase());
+ discoveryResponse.setAckFinalIndicator(true);
+ return discoveryResponse;
}
+
} \ No newline at end of file