From 29cdab1f28ff20935a4de232684344fa8e59e48c Mon Sep 17 00:00:00 2001 From: Phillip Leigh Date: Mon, 20 Aug 2018 17:12:56 -0400 Subject: Translate Enricher Attribute Names to POMBA names Issue-ID: SDNC-317 Change-Id: I01deaa08fd645b81c971adc8b1230e1be744f50a Signed-off-by: Phillip Leigh --- .../networkdiscovery/EnricherConfiguration.java | 29 +++--- .../service/SpringServiceImpl.java | 104 ++++++++++++--------- 2 files changed, 78 insertions(+), 55 deletions(-) (limited to 'pomba/network-discovery/src/main') diff --git a/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/EnricherConfiguration.java b/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/EnricherConfiguration.java index 0fee505..0257687 100644 --- a/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/EnricherConfiguration.java +++ b/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/EnricherConfiguration.java @@ -26,6 +26,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; +import com.google.common.base.Splitter; @Configuration public class EnricherConfiguration { @@ -47,24 +48,23 @@ public class EnricherConfiguration { @Value("${enricher.readTimeout:60000}") private int readTimeout; - @Bean(name="enricherClient") + @Bean(name = "enricherClient") public RestClient restClient() { - return new RestClient() - .validateServerHostname(false) - .validateServerCertChain(false) - .connectTimeoutMs(this.connectionTimeout) - .readTimeoutMs(this.readTimeout) - .clientCertFile(this.keyStorePath) - .clientCertPassword( - org.eclipse.jetty.util.security.Password.deobfuscate(this.keyStorePassword)); + return new RestClient().validateServerHostname(false) + .validateServerCertChain(false) + .connectTimeoutMs(this.connectionTimeout) + .readTimeoutMs(this.readTimeout) + .clientCertFile(this.keyStorePath) + .clientCertPassword(org.eclipse.jetty.util.security.Password.deobfuscate( + this.keyStorePassword)); } - @Bean(name="enricherBaseUrl") + @Bean(name = "enricherBaseUrl") public String getURL() { return this.url; } - @Bean(name="enricherTypeURLs") + @Bean(name = "enricherTypeURLs") public Map enricherTypeURLs() { Map result = new HashMap<>(); @@ -83,5 +83,12 @@ public class EnricherConfiguration { return result; } + @Value("${enricher.attributeNameMappingList}") + private String enricherAttributeNameMappingList; + @Bean(name = "enricherAttributeNameMapping") + public Map getAttributeNameMap() { + String noWhiteSpaceString = enricherAttributeNameMappingList.replaceAll("\\s", ""); + return (Splitter.on(";").withKeyValueSeparator(":").split(noWhiteSpaceString)); + } } 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 3230a52..8b25e47 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 @@ -70,8 +70,8 @@ public class SpringServiceImpl implements SpringService { 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()); + Integer.getInteger("discovery.threads", DEFAULT_WORKER_THREADS), + new ThreadFactoryBuilder().setNameFormat("discovery-worker-%d").build()); @Autowired private RestClient enricherClient; @@ -87,6 +87,8 @@ public class SpringServiceImpl implements SpringService { private DocumentBuilder parser; + @javax.annotation.Resource + private Map enricherAttributeNameMapping; public SpringServiceImpl() throws ParserConfigurationException { this.parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); @@ -94,12 +96,12 @@ public class SpringServiceImpl implements SpringService { @Override public NetworkDiscoveryResponse findbyResourceIdAndType(String transactionId, - String requestId, - String resourceType, - List resourceIds, - String notificationURL, - String notificationAuthorization, - ONAPLogAdapter adapter) throws ApplicationException { + String requestId, + String resourceType, + List resourceIds, + String notificationURL, + String notificationAuthorization, + ONAPLogAdapter adapter) throws ApplicationException { NetworkDiscoveryResponse response = new NetworkDiscoveryResponse(); response.setRequestId(requestId); @@ -115,8 +117,8 @@ public class SpringServiceImpl implements SpringService { } // schedule discovery of specified resources - Runnable task = new ResourceTask(transactionId, requestId, resourceType, resourceIds, - notificationURL, notificationAuthorization, enricherURL, adapter); + Runnable task = new ResourceTask(transactionId, requestId, resourceType, resourceIds, notificationURL, + notificationAuthorization, enricherURL, adapter); this.executor.submit(task); response.setCode(Status.ACCEPTED.getStatusCode()); @@ -130,19 +132,31 @@ public class SpringServiceImpl implements SpringService { this.executor.shutdown(); } - private List toAttributeList(String xml) throws SAXException, IOException { - // TODO don't return raw A&AI attributes but coerce to swagger-defined enums + private List toAttributeList(String xml, ONAPLogAdapter adapter) throws SAXException, IOException { + Logger log = adapter.unwrap(); Document doc = this.parser.parse(new InputSource(new StringReader(xml))); NodeList children = doc.getDocumentElement().getChildNodes(); List result = new ArrayList<>(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { - Attribute attr = new Attribute(); - attr.setName(((Element)child).getTagName()); - attr.setValue(((Element)child).getTextContent()); - attr.setDataQuality(DataQuality.ok()); - result.add(attr); + + // remove white space before conversion + String attributeName = ((Element) child).getTagName().replaceAll("\\s", ""); + + // If the incoming attribute name is not listed in the + // attributeNameMapping, then this attribute will be removed. + String newName = enricherAttributeNameMapping.get(attributeName); + if (newName != null) { + Attribute attr = new Attribute(); + attr.setName(newName); + attr.setValue(((Element) child).getTextContent()); + attr.setDataQuality(DataQuality.ok()); + result.add(attr); + } else { + log.debug("[" + ((Element) child).getTagName() + + "] was removed due to not listed in enricherAttributeNameMapping."); + } } } return result; @@ -150,10 +164,7 @@ public class SpringServiceImpl implements SpringService { private void sendNotification(String url, String authorization, Object notification, ONAPLogAdapter adapter) { - Invocation.Builder request = this.callbackClient - .target(url) - .request() - .accept(MediaType.TEXT_PLAIN_TYPE); + Invocation.Builder request = this.callbackClient.target(url).request().accept(MediaType.TEXT_PLAIN_TYPE); if (authorization != null) { request.header(HttpHeaders.AUTHORIZATION, authorization); @@ -168,21 +179,23 @@ public class SpringServiceImpl implements SpringService { StringBuilder debugRequest = new StringBuilder("REQUEST:\n"); debugRequest.append("URL: ").append(url).append("\n"); debugRequest.append("Payload: ").append(notification).append("\n"); -// if (headers != null) { -// debugRequest.append("Headers: "); -// for (Entry> header : headers.entrySet()) { -// debugRequest.append("\n\t").append(header.getKey()).append(":"); -// for (String headerEntry : header.getValue()) { -// debugRequest.append("\"").append(headerEntry).append("\" "); -// } -// } -// } + // if (headers != null) { + // debugRequest.append("Headers: "); + // for (Entry> header : headers.entrySet()) + // { + // debugRequest.append("\n\t").append(header.getKey()).append(":"); + // for (String headerEntry : header.getValue()) { + // debugRequest.append("\"").append(headerEntry).append("\" "); + // } + // } + // } log.debug(debugRequest.toString()); } Response result = request.post(Entity.json(notification)); - adapter.unwrap().info("request at url = {} resulted in http response: {}", url, result.getStatusInfo().getStatusCode() + " " + result.getStatusInfo().getReasonPhrase()); + adapter.unwrap().info("request at url = {} resulted in http response: {}", url, + result.getStatusInfo().getStatusCode() + " " + result.getStatusInfo().getReasonPhrase()); if (log.isDebugEnabled()) { StringBuilder debugResponse = new StringBuilder("RESPONSE:\n"); @@ -206,8 +219,8 @@ public class SpringServiceImpl implements SpringService { } } catch (Exception x) { - log.error("Error during {} operation to endpoint at url = {} with error = {}", - "POST", url, x.getLocalizedMessage()); + log.error("Error during {} operation to endpoint at url = {} with error = {}", "POST", url, + x.getLocalizedMessage()); } } @@ -222,13 +235,13 @@ public class SpringServiceImpl implements SpringService { private final ONAPLogAdapter adapter; public ResourceTask(String transactionId, - String requestId, - String resourceType, - List resourceIds, - String notificationURL, - String notificationAuthorization, - String resourceURL, - ONAPLogAdapter adapter) { + String requestId, + String resourceType, + List resourceIds, + String notificationURL, + String notificationAuthorization, + String resourceURL, + ONAPLogAdapter adapter) { this.transactionId = transactionId; this.requestId = requestId; this.resourceType = resourceType; @@ -249,9 +262,11 @@ public class SpringServiceImpl implements SpringService { MultivaluedMap headers = new MultivaluedHashMap<>(); headers.add(ENRICHER_HEADER_APPLICATION, RestService.SERVICE_NAME); headers.add(ENRICHER_HEADER_TRANSACTION, this.transactionId); + for (String resourceId : this.resourceIds) { String url = format.format(new Object[] { resourceId }); - OperationResult result = SpringServiceImpl.this.enricherClient.get(url, headers, MediaType.APPLICATION_XML_TYPE); + OperationResult result = SpringServiceImpl.this.enricherClient.get(url, headers, + MediaType.APPLICATION_XML_TYPE); Resource resource = new Resource(); resource.setType(this.resourceType); @@ -265,12 +280,12 @@ public class SpringServiceImpl implements SpringService { if (result.wasSuccessful()) { resource.setDataQuality(DataQuality.ok()); try { - resource.setAttributeList(toAttributeList(result.getResult())); + List attributeList = toAttributeList(result.getResult(), adapter); + resource.setAttributeList(attributeList); } catch (Exception x) { resource.setDataQuality(DataQuality.error(x.getMessage())); } - } - else { + } else { resource.setDataQuality(DataQuality.error(result.getFailureCause())); } } @@ -285,6 +300,7 @@ public class SpringServiceImpl implements SpringService { private static class RequestBuilderWrapper implements RequestBuilder { private Invocation.Builder builder; + private RequestBuilderWrapper(Invocation.Builder builder) { this.builder = builder; } -- cgit 1.2.3-korg