diff options
author | 2018-11-27 22:18:16 +0000 | |
---|---|---|
committer | 2018-11-28 14:24:42 -0500 | |
commit | 9a0c9790a1dc31dcc2db199b6f0ff76e796c2597 (patch) | |
tree | 836a34fb2e4a8d1dd78cc9ed18553278c5a2ca1f /pomba/network-discovery/src/main | |
parent | 2f93fe9cf3c05d6606e37576121801dbdb98dd68 (diff) |
ND get VM info from Openstack
Issue-ID: SDNC-533
Change-Id: If50c4ae02349824a4e179e30ea1ee29d3afe6ab2
Signed-off-by: Norm Traxler <normant@amdocs.com>
Diffstat (limited to 'pomba/network-discovery/src/main')
8 files changed, 306 insertions, 184 deletions
diff --git a/pomba/network-discovery/src/main/docker/Dockerfile b/pomba/network-discovery/src/main/docker/Dockerfile index e9be214..45a8c5a 100644 --- a/pomba/network-discovery/src/main/docker/Dockerfile +++ b/pomba/network-discovery/src/main/docker/Dockerfile @@ -6,6 +6,7 @@ ARG MICROSERVICE_HOME=/opt/app/ RUN mkdir -p $MICROSERVICE_HOME RUN mkdir -p $MICROSERVICE_HOME/config/ RUN mkdir -p $MICROSERVICE_HOME/config/auth/ +RUN mkdir -p $MICROSERVICE_HOME/config/jolt/ RUN mkdir -p $MICROSERVICE_HOME/lib/ RUN mkdir -p $MICROSERVICE_HOME/bin/ @@ -17,4 +18,4 @@ RUN chmod 755 $MICROSERVICE_HOME/config/* RUN chmod 755 $MICROSERVICE_HOME/lib/* RUN chmod 755 $MICROSERVICE_HOME/bin/* -CMD ["/opt/app/bin/startService.sh"]
\ No newline at end of file +CMD ["/opt/app/bin/startService.sh"] diff --git a/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/Application.java b/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/Application.java index c976678..e7e2208 100644 --- a/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/Application.java +++ b/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/Application.java @@ -38,7 +38,7 @@ public class Application extends SpringBootServletInitializer { return application.sources(Application.class); } - public static void main(String[] args) throws Exception { + public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
\ No newline at end of file diff --git a/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/ApplicationException.java b/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/ApplicationException.java index fa29645..98bb2e7 100644 --- a/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/ApplicationException.java +++ b/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/ApplicationException.java @@ -45,11 +45,7 @@ public class ApplicationException extends Exception { private static final long serialVersionUID = -4874149714911165454L; private final Status httpStatus; - private String responseCode; - - public ApplicationException(String message) { - this(message, Status.INTERNAL_SERVER_ERROR); - } + private final String responseCode; public ApplicationException(Error errorCode, Status httpStatus, Object... args) { super(errorCode.getMessage(args)); @@ -61,19 +57,6 @@ public class ApplicationException extends Exception { this.httpStatus = httpStatus; } - public ApplicationException(String message, Status httpStatus) { - super(message); - if (httpStatus == null) { - throw new NullPointerException("httpStatus"); - } - this.httpStatus = httpStatus; - } - - public ApplicationException(String message, Exception cause) { - super(message, cause); - this.httpStatus = Status.INTERNAL_SERVER_ERROR; - } - public Status getHttpStatus() { return this.httpStatus; } 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/OpenstackConfiguration.java index 62ea3f7..ee18397 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/OpenstackConfiguration.java @@ -20,58 +20,77 @@ package org.onap.sdnc.apps.pomba.networkdiscovery; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; + +import org.eclipse.jetty.util.security.Password; import org.onap.aai.restclient.client.RestClient; import org.springframework.beans.factory.annotation.Autowired; 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 { +public class OpenstackConfiguration { @Autowired private Environment env; - @Value("${enricher.url:https://localhost:9505}") - private String url; + @Value("${openstack.identity.url:http://localhost:5000/v3}") + private String identityUrl; + + @Value("${openstack.identity.user}") + private String identityUser; + + @Value("${openstack.identity.password}") + private String identityPassword; - @Value("${enricher.keyStorePath:config/auth/client-cert-onap.p12}") + @Value("${openstack.api.microversion}") + private String apiMicroversion; + + @Value("${openstack.identity.keyStorePath:config/auth/client-cert-onap.p12}") private String keyStorePath; - @Value("${enricher.keyStorePassword:OBF:1y0q1uvc1uum1uvg1pil1pjl1uuq1uvk1uuu1y10}") + @Value("${openstack.identity.keyStorePassword:OBF:1y0q1uvc1uum1uvg1pil1pjl1uuq1uvk1uuu1y10}") private String keyStorePassword; - @Value("${enricher.connectionTimeout:5000}") + @Value("${openstack.connectionTimeout:5000}") private int connectionTimeout; - @Value("${enricher.readTimeout:60000}") + @Value("${openstack.readTimeout:60000}") private int readTimeout; - @Value("${enricher.attributeNameMappingList}") - private String enricherAttributeNameMappingList; - - @Bean(name = "enricherClient") + @Bean(name = "openstackClient") 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)); + .readTimeoutMs(this.readTimeout); + } + + @Bean(name = "openstackIdentityUrl") + public String getIdentityURL() { + return this.identityUrl; + } + + @Bean(name = "openstackIdentityUser") + public String getIdentityUser() { + return this.identityUser; } - @Bean(name = "enricherBaseUrl") - public String getURL() { - return this.url; + @Bean(name = "openstackIdentityPassword") + public String getIdentityPassword() { + return Password.deobfuscate(this.identityPassword); } - @Bean(name = "enricherTypeURLs") - public Map<String, String> enricherTypeURLs() { + @Bean(name = "openstackApiMicroversion") + public String getApiMicroversion() { + return this.apiMicroversion; + } + + @Bean(name = "openstackTypeURLs") + public Map<String, String> getOpenstackTypeURLs() { Map<String, String> result = new HashMap<>(); - String types = this.env.getProperty("enricher.types"); + String types = this.env.getProperty("openstack.types"); if (types == null) { return result; } @@ -79,16 +98,11 @@ public class EnricherConfiguration { StringTokenizer tokenizer = new StringTokenizer(types, ", "); while (tokenizer.hasMoreTokens()) { String type = tokenizer.nextToken(); - String enricherUrl = this.env.getProperty("enricher.type." + type + ".url"); - result.put(type, enricherUrl); + String openstackUrl = this.env.getProperty("openstack.type." + type + ".url"); + result.put(type, openstackUrl); } return result; } - @Bean(name = "enricherAttributeNameMapping") - public Map<String, String> 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/OSAuthentication.java b/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/service/OSAuthentication.java new file mode 100644 index 0000000..fed52a0 --- /dev/null +++ b/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/service/OSAuthentication.java @@ -0,0 +1,78 @@ +/*
+ * ============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.sdnc.apps.pomba.networkdiscovery.service;
+
+import static org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException.Error.UNAUTHORIZED;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedHashMap;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response.Status;
+
+import org.apache.commons.io.IOUtils;
+import org.onap.aai.restclient.client.OperationResult;
+import org.onap.aai.restclient.client.RestClient;
+import org.onap.logging.ref.slf4j.ONAPLogAdapter;
+import org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException;
+
+public class OSAuthentication {
+
+ private static final String CONFIG_AUTH_DIR = "config/auth";
+ private static final String X_SUBJECT_TOKEN = "X-Subject-Token";
+
+ private static final String USER_PATTERN = "%USER%";
+ private static final String PASSWORD_PATTERN = "%PASSWORD%";
+
+ private OSAuthentication() {
+ throw new IllegalStateException("Utility class");
+ }
+
+ public static String getToken(String openstackIdentityUrl, String userId, String password, RestClient openstackClient, ONAPLogAdapter adapter)
+ throws IOException, ApplicationException {
+
+ MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
+
+ String mappingConfigPath = CONFIG_AUTH_DIR + File.separator + "osauth.json";
+
+ File file = new File(mappingConfigPath);
+ String payload = IOUtils.toString(new FileInputStream(file), "UTF-8");
+ payload = payload.replaceAll(USER_PATTERN, userId);
+ payload = payload.replaceAll(PASSWORD_PATTERN, password);
+
+ OperationResult result = openstackClient.post(openstackIdentityUrl, payload, headers,
+ MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
+
+ adapter.unwrap().info("request at url = {} resulted in http response: {}", openstackIdentityUrl,
+ result.getResult());
+
+ String token = result.getHeaders().getFirst(X_SUBJECT_TOKEN);
+
+ if (token == null) {
+ throw new ApplicationException(UNAUTHORIZED, Status.UNAUTHORIZED);
+ }
+
+ adapter.unwrap().debug("Got token: {}", token);
+
+ return token;
+ }
+
+}
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 8d0de03..1be8fbc 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,16 +17,17 @@ */ 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.io.StringReader; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.Map.Entry; 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; @@ -37,13 +38,13 @@ import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; 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; @@ -51,64 +52,56 @@ import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.Attribute; import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.NetworkDiscoveryNotification; import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.NetworkDiscoveryResponse; import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.Resource; -import org.onap.sdnc.apps.pomba.networkdiscovery.service.rs.RestService; +import org.onap.sdnc.apps.pomba.networkdiscovery.service.util.TransformationUtil; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; @Service public class SpringServiceImpl implements SpringService { - private static final String ENRICHER_HEADER_APPLICATION = "X-FromAppId"; - private static final String ENRICHER_HEADER_TRANSACTION = "X-TransactionId"; - + 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()); + Integer.getInteger("discovery.threads", DEFAULT_WORKER_THREADS), + new ThreadFactoryBuilder().setNameFormat("discovery-worker-%d").build()); @Autowired - private RestClient enricherClient; + private RestClient openstackClient; @Autowired - private String enricherBaseUrl; + private String openstackIdentityUrl; - @javax.annotation.Resource - private Client callbackClient; + @Autowired + private String openstackIdentityUser; - @javax.annotation.Resource - private Map<String, String> enricherTypeURLs; + @Autowired + private String openstackIdentityPassword; + + @Autowired + private String openstackApiMicroversion; - private DocumentBuilder parser; + @javax.annotation.Resource + private Client callbackClient; @javax.annotation.Resource - private Map<String, String> enricherAttributeNameMapping; + private Map<String, String> openstackTypeURLs; public SpringServiceImpl() throws ParserConfigurationException { - this.parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); } @Override - public NetworkDiscoveryResponse findbyResourceIdAndType(String transactionId, - String requestId, - String resourceType, - List<String> resourceIds, - String notificationURL, - String notificationAuthorization, - ONAPLogAdapter adapter) throws ApplicationException { + public NetworkDiscoveryResponse findbyResourceIdAndType(String transactionId, String requestId, String resourceType, + List<String> resourceIds, String notificationURL, String notificationAuthorization, ONAPLogAdapter adapter) + throws ApplicationException { NetworkDiscoveryResponse response = new NetworkDiscoveryResponse(); response.setRequestId(requestId); - String enricherURL = this.enricherTypeURLs.get(resourceType); + String openstackURL = this.openstackTypeURLs.get(resourceType); // check if resourceType is supported - if (enricherURL == null) { + 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); @@ -118,7 +111,7 @@ public class SpringServiceImpl implements SpringService { // schedule discovery of specified resources Runnable task = new ResourceTask(transactionId, requestId, resourceType, resourceIds, notificationURL, - notificationAuthorization, enricherURL, adapter); + notificationAuthorization, openstackURL, adapter); this.executor.submit(task); response.setCode(Status.ACCEPTED.getStatusCode()); @@ -132,65 +125,29 @@ public class SpringServiceImpl implements SpringService { this.executor.shutdown(); } - private void sendNotification(String url, String authorization, Object notification, ONAPLogAdapter adapter) { + 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 { - // native client marshaller doesn't skip null fields - // so manually marshal notification to json - - if (log.isDebugEnabled()) { - 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<String, List<String>> 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()); - } + adapter.unwrap().info("Posting notfication to url = {} , payload: {}", url, + JsonUtils.toJsonString(Entity.json(notification).getEntity())); Response result = request.post(Entity.json(notification)); - 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"); - debugResponse.append("Result: ").append(result.getStatus()).append("\n"); - String content = result.hasEntity() ? result.getEntity().toString() : null; - if (result.getStatus() >= 300) { - debugResponse.append("Failure Cause: ").append(content).append("\n"); - } else { - debugResponse.append("Payload: ").append(content).append("\n"); - } - if (result.getHeaders() != null) { - debugResponse.append("Headers: "); - for (Entry<String, List<Object>> header : result.getHeaders().entrySet()) { - debugResponse.append("\n\t").append(header.getKey()).append(":"); - for (Object headerEntry : header.getValue()) { - debugResponse.append("\"").append(headerEntry).append("\" "); - } - } - } - log.debug(debugResponse.toString()); - } + adapter.unwrap().info("request at url = {} resulted in http status {} and response: {}", url, + result.getStatus(), result.getEntity()); } catch (Exception x) { log.error("Error during {} operation to endpoint at url = {} with error = {}", "POST", url, - x.getLocalizedMessage()); + x.getLocalizedMessage()); } } @@ -204,14 +161,8 @@ public class SpringServiceImpl implements SpringService { 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) { + 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; @@ -224,19 +175,45 @@ public class SpringServiceImpl implements SpringService { @Override public void run() { + try { + runResourceDiscoveryTask(); + } 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); + } + } + + 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(SpringServiceImpl.this.enricherBaseUrl + this.resourceURL); + MessageFormat format = new MessageFormat(this.resourceURL); MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); - headers.add(ENRICHER_HEADER_APPLICATION, RestService.SERVICE_NAME); - headers.add(ENRICHER_HEADER_TRANSACTION, this.transactionId); + 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.enricherClient.get(url, headers, - MediaType.APPLICATION_XML_TYPE); + OperationResult result = SpringServiceImpl.this.openstackClient.get(url, headers, + MediaType.APPLICATION_JSON_TYPE); + + adapter.unwrap().info("Openstack GET result: {}", result.getResult()); Resource resource = new Resource(); resource.setType(this.resourceType); @@ -248,13 +225,13 @@ public class SpringServiceImpl implements SpringService { resources.add(resource); if (result.wasSuccessful()) { + String transformedOutput = TransformationUtil.transform(result.getResult(), this.resourceType); + + adapter.unwrap().info("Jolt transformed output: {}", transformedOutput); + resource.setDataQuality(DataQuality.ok()); - try { - List<Attribute> attributeList = toAttributeList(result.getResult(), adapter); - resource.setAttributeList(attributeList); - } catch (Exception x) { - resource.setDataQuality(DataQuality.error(x.getMessage())); - } + List<Attribute> attributeList = TransformationUtil.toAttributeList(transformedOutput); + resource.setAttributeList(attributeList); } else { resource.setDataQuality(DataQuality.error(result.getFailureCause())); } @@ -264,37 +241,8 @@ public class SpringServiceImpl implements SpringService { notification.setAckFinalIndicator(true); // call client back with resource details - sendNotification(this.notificationURL, this.notificationAuthorization, notification, adapter); - } - - private List<Attribute> toAttributeList(String xml, ONAPLogAdapter adapter) throws SAXException, IOException { - Logger log = adapter.unwrap(); - Document doc = parser.parse(new InputSource(new StringReader(xml))); - NodeList children = doc.getDocumentElement().getChildNodes(); - List<Attribute> result = new ArrayList<>(); - for (int i = 0; i < children.getLength(); i++) { - Node child = children.item(i); - if (child.getNodeType() == Node.ELEMENT_NODE) { - - // 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; + sendNotification(this.notificationURL, this.transactionId, this.notificationAuthorization, notification, + adapter); } } diff --git a/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/service/rs/RestServiceImpl.java b/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/service/rs/RestServiceImpl.java index 7447e67..0677dcd 100644 --- a/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/service/rs/RestServiceImpl.java +++ b/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/service/rs/RestServiceImpl.java @@ -64,14 +64,13 @@ public class RestServiceImpl implements RestService { String notificationURL) throws ApplicationException { ONAPLogAdapter adapter = new ONAPLogAdapter(log); - adapter.getServiceDescriptor().setServiceName(SERVICE_NAME); - adapter.entering(request); + try { + adapter.getServiceDescriptor().setServiceName(SERVICE_NAME); + adapter.entering(request); if (version == null) { - // only unit tests can pass null here - // url matching will guarantee non-null in real server - version = "v1"; + throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, "version"); } if (authorization == null || !this.basicAuthHeader.equals(authorization)) { @@ -95,7 +94,7 @@ public class RestServiceImpl implements RestService { if (transactionId == null || transactionId.isEmpty()) { transactionId = UUID.randomUUID().toString(); - log.debug("transactionId is missing; using newly generated value: " + transactionId); + log.debug("transactionId is missing; using newly generated value: {}", transactionId); } // just reuse received Authorization header in callback diff --git a/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/service/util/TransformationUtil.java b/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/service/util/TransformationUtil.java new file mode 100644 index 0000000..dc17848 --- /dev/null +++ b/pomba/network-discovery/src/main/java/org/onap/sdnc/apps/pomba/networkdiscovery/service/util/TransformationUtil.java @@ -0,0 +1,99 @@ +/*
+ * Copyright 2014 Bazaarvoice, Inc.
+ *
+ * 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.
+ */
+package org.onap.sdnc.apps.pomba.networkdiscovery.service.util;
+
+import com.bazaarvoice.jolt.Chainr;
+import com.bazaarvoice.jolt.JsonUtils;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParser;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import org.onap.pomba.common.datatypes.DataQuality;
+import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.Attribute;
+
+public class TransformationUtil {
+
+ private static final String CONFIG_JOLT = "config/jolt";
+ private static final String EMPTY_STRING = "";
+
+ public TransformationUtil() {
+ throw new IllegalStateException("Utility class");
+ }
+
+ /**
+ * Transforms the sourceJson using the jolt specification for the given resourceType.
+ *
+ * @param sourceJson
+ * @param resourceType
+ * @return
+ */
+ public static String transform(String sourceJson, String resourceType) {
+
+ Object sourceObject = JsonUtils.jsonToObject(sourceJson);
+
+ List<Object> chainrSpecJSON = JsonUtils.filepathToList(CONFIG_JOLT + File.separator + resourceType + ".json");
+ Chainr chainr = Chainr.fromSpec(chainrSpecJSON);
+ Object output = chainr.transform(sourceObject);
+
+ return JsonUtils.toJsonString(output);
+ }
+
+ /**
+ * Converts the second level of JsonElements from the given json to a list of
+ * Network Discovery Attributes.
+ *
+ * @param json
+ * @return
+ */
+ public static List<Attribute> toAttributeList(String json) {
+
+ JsonParser parser = new JsonParser();
+ JsonElement elem = parser.parse(json);
+
+ Set<Entry<String, JsonElement>> entrySet = elem.getAsJsonObject().entrySet();
+
+ List<Attribute> result = new ArrayList<>();
+
+ Iterator<Entry<String, JsonElement>> iter = entrySet.iterator();
+ while (iter.hasNext()) {
+ Entry<String, JsonElement> next = iter.next();
+
+ JsonElement vserverElem = next.getValue();
+ Set<Entry<String, JsonElement>> vserverEntrySet = vserverElem.getAsJsonObject().entrySet();
+ Iterator<Entry<String, JsonElement>> vserverIter = vserverEntrySet.iterator();
+ while (vserverIter.hasNext()) {
+ Entry<String, JsonElement> vserverNext = vserverIter.next();
+ Attribute attr = new Attribute();
+ attr.setName(vserverNext.getKey());
+ if (vserverNext.getValue().isJsonNull()) {
+ attr.setValue(EMPTY_STRING);
+ } else {
+ attr.setValue(vserverNext.getValue().getAsString());
+ }
+ attr.setDataQuality(DataQuality.ok());
+ result.add(attr);
+ }
+ }
+ return result;
+ }
+
+}
\ No newline at end of file |