Replace nsaCore library with Spring 69/58169/3
authorPawelSzalapski <pawel.szalapski@nokia.com>
Tue, 31 Jul 2018 06:18:03 +0000 (08:18 +0200)
committerPawelSzalapski <pawel.szalapski@nokia.com>
Wed, 1 Aug 2018 07:56:00 +0000 (09:56 +0200)
Change-Id: I2227939a67a2cbba2d392136d49ef4419600d186
Issue-ID: DCAEGEN2-602
Signed-off-by: PawelSzalapski <pawel.szalapski@nokia.com>
35 files changed:
etc/collector.properties
etc/passwordfile
pom.xml
src/main/java/org/onap/dcae/ApplicationSettings.java
src/main/java/org/onap/dcae/CLIUtils.java
src/main/java/org/onap/dcae/CollectorSchemas.java [new file with mode: 0644]
src/main/java/org/onap/dcae/SchemaValidator.java [new file with mode: 0644]
src/main/java/org/onap/dcae/VesApplication.java [new file with mode: 0644]
src/main/java/org/onap/dcae/commonFunction/AnyNode.java
src/main/java/org/onap/dcae/commonFunction/CommonStartup.java [deleted file]
src/main/java/org/onap/dcae/commonFunction/ConfigProcessors.java
src/main/java/org/onap/dcae/commonFunction/EventProcessor.java
src/main/java/org/onap/dcae/commonFunction/VESLogger.java
src/main/java/org/onap/dcae/commonFunction/event/publishing/DMaaPConfigurationParser.java
src/main/java/org/onap/dcae/commonFunction/event/publishing/DMaaPEventPublisher.java
src/main/java/org/onap/dcae/commonFunction/event/publishing/DMaaPPublishersBuilder.java
src/main/java/org/onap/dcae/commonFunction/event/publishing/DMaaPPublishersCache.java
src/main/java/org/onap/dcae/commonFunction/event/publishing/PublisherConfig.java
src/main/java/org/onap/dcae/commonFunction/event/publishing/VavrUtils.java
src/main/java/org/onap/dcae/controller/FetchDynamicConfig.java
src/main/java/org/onap/dcae/controller/LoadDynamicConfig.java
src/main/java/org/onap/dcae/restapi/ApiAuthInterceptor.java [new file with mode: 0644]
src/main/java/org/onap/dcae/restapi/ApiConfiguration.java [moved from src/main/java/org/onap/dcae/restapi/endpoints/Ui.java with 50% similarity]
src/main/java/org/onap/dcae/restapi/ApiException.java
src/main/java/org/onap/dcae/restapi/RestfulCollectorServlet.java [deleted file]
src/main/java/org/onap/dcae/restapi/ServletConfig.java [new file with mode: 0644]
src/main/java/org/onap/dcae/restapi/VesRestController.java [new file with mode: 0644]
src/main/java/org/onap/dcae/restapi/endpoints/EventReceipt.java [deleted file]
src/main/scripts/VESrestfulCollector.sh
src/test/java/org/onap/dcae/ApplicationSettingsTest.java
src/test/java/org/onap/dcae/commonFunction/CommonStartupTest.java [deleted file]
src/test/java/org/onap/dcae/commonFunction/EventProcessorTest.java
src/test/java/org/onap/dcae/vestest/TestConfigProcessor.java
src/test/java/org/onap/dcae/vestest/TestEventReceipt.java [deleted file]
src/test/java/org/onap/dcae/vestest/TestJsonSchemaValidation.java

index 67c6d39..4354eb6 100755 (executable)
@@ -24,8 +24,8 @@ collector.service.port=8080
 collector.service.secure.port=8443\r
 \r
 ## The keystore must be setup per installation when secure port is configured\r
-collector.keystore.file.location=../etc/keystore\r
-collector.keystore.passwordfile=./etc/passwordfile\r
+collector.keystore.file.location=etc/keystore\r
+collector.keystore.passwordfile=etc/passwordfile\r
 collector.keystore.alias=tomcat\r
 \r
 \r
index 702a4cb..25acfbf 100644 (file)
@@ -1 +1 @@
-collector
+collector
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index c87a9d2..150d9b3 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -63,6 +63,18 @@ limitations under the License.
     <build>
         <pluginManagement>
             <plugins>
+                <plugin>
+                    <groupId>org.springframework.boot</groupId>
+                    <artifactId>spring-boot-maven-plugin</artifactId>
+                    <version>2.0.2.RELEASE</version>
+                    <executions>
+                        <execution>
+                            <goals>
+                                <goal>repackage</goal>
+                            </goals>
+                        </execution>
+                    </executions>
+                </plugin>
                 <plugin>
                     <artifactId>maven-compiler-plugin</artifactId>
                     <version>3.7.0</version>
@@ -318,6 +330,19 @@ limitations under the License.
         </plugins>
     </reporting>
 
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <!-- Import dependency management from Spring Boot -->
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-dependencies</artifactId>
+                <version>2.0.3.RELEASE</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
     <dependencies>
         <!-- JSON RELATED -->
         <dependency>
@@ -347,55 +372,12 @@ limitations under the License.
         </dependency>
 
         <!-- REST API RELATED -->
-        <dependency>
-            <groupId>org.apache.tomcat.embed</groupId>
-            <artifactId>tomcat-embed-core</artifactId>
-            <version>8.5.23</version>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.tomcat</groupId>
-            <artifactId>tomcat-catalina</artifactId>
-            <version>8.5.23</version>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.tomcat</groupId>
-            <artifactId>tomcat-coyote</artifactId>
-            <version>8.5.23</version>
-        </dependency>
         <dependency>
             <groupId>com.att.nsa</groupId>
-            <artifactId>nsaServerLibrary</artifactId>
-            <version>1.0.10</version>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.httpcomponents</groupId>
-            <artifactId>httpclient</artifactId>
-            <version>4.5.3</version>
-        </dependency>
-        <dependency>
-            <groupId>com.fasterxml.jackson.core</groupId>
-            <artifactId>jackson-databind</artifactId>
-            <version>2.8.11</version>
+            <artifactId>cambriaClient</artifactId>
+            <version>0.0.1</version>
         </dependency>
 
-        <!-- LOGGING RELATED -->
-        <dependency>
-            <groupId>org.slf4j</groupId>
-            <artifactId>slf4j-log4j12</artifactId>
-            <version>1.7.21</version>
-        </dependency>
-        <dependency>
-            <groupId>log4j</groupId>
-            <artifactId>apache-log4j-extras</artifactId>
-            <version>1.2.17</version>
-        </dependency>
-
-        <!-- MISCELLANEOUS -->
-        <dependency>
-            <groupId>com.google.guava</groupId>
-            <artifactId>guava</artifactId>
-            <version>18.0</version>
-        </dependency>
         <dependency>
             <groupId>commons-collections</groupId>
             <artifactId>commons-collections</artifactId>
@@ -406,17 +388,34 @@ limitations under the License.
             <artifactId>commons-configuration</artifactId>
             <version>1.10</version>
         </dependency>
-        <dependency>
-            <groupId>javax.mail</groupId>
-            <artifactId>mail</artifactId>
-            <version>1.4.7</version>
-        </dependency>
         <dependency>
             <groupId>io.vavr</groupId>
             <artifactId>vavr</artifactId>
             <version>0.9.2</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-security</artifactId>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.springframework.boot</groupId>
+                    <artifactId>spring-boot-starter-logging</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.security</groupId>
+            <artifactId>spring-security-config</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-log4j2</artifactId>
+        </dependency>
 
         <!-- TESTING -->
         <dependency>
@@ -425,6 +424,12 @@ limitations under the License.
             <version>2.18.0</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.12</version>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.assertj</groupId>
             <artifactId>assertj-core</artifactId>
index 0ebd1e9..9063faa 100644 (file)
 
 package org.onap.dcae;
 
-import com.att.nsa.drumlin.till.nv.impl.nvReadableStack;
-import com.att.nsa.drumlin.till.nv.impl.nvReadableTable;
-import com.att.nsa.drumlin.till.nv.rrNvReadable;
 import com.google.common.annotations.VisibleForTesting;
 import io.vavr.Function1;
 import io.vavr.collection.HashMap;
+import io.vavr.collection.List;
 import io.vavr.collection.Map;
-import org.apache.commons.configuration.ConfigurationConverter;
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.PropertiesConfiguration;
 import org.json.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
 
+import javax.annotation.Nullable;
 import java.io.File;
 import java.nio.file.Paths;
+import java.util.Base64;
+
+import static java.util.Arrays.stream;
 
 /**
  * Abstraction over application configuration.
  * Its job is to provide easily discoverable (by method names lookup) and type safe access to configuration properties.
  */
+@Component
 public class ApplicationSettings {
 
     private static final Logger inlog = LoggerFactory.getLogger(ApplicationSettings.class);
     private static final String COLLECTOR_PROPERTIES = "etc/collector.properties";
+
+    private final String appInvocationDir;
     private final PropertiesConfiguration properties = new PropertiesConfiguration();
 
     public ApplicationSettings(String[] args, Function1<String[], Map<String, String>> argsParser) {
+        this(args, argsParser, System.getProperty("user.dir"));
+    }
+
+    public ApplicationSettings(String[] args, Function1<String[], Map<String, String>> argsParser, String appInvocationDir) {
+        this.appInvocationDir = appInvocationDir;
         properties.setDelimiterParsingDisabled(true);
         Map<String, String> parsedArgs = argsParser.apply(args);
         loadProperties(Paths.get(new File(COLLECTOR_PROPERTIES).getAbsolutePath()).toString());
         loadCommandLineProperties(parsedArgs);
         parsedArgs.filterKeys(k -> !k.equals("c")).forEach(this::updateProperty);
     }
-
     private void loadCommandLineProperties(Map<String, String> parsedArgs) {
         parsedArgs.get("c").forEach(e -> {
             properties.clear();
@@ -63,7 +72,7 @@ public class ApplicationSettings {
         });
     }
 
-    private void loadProperties(String property){
+    private void loadProperties(String property) {
         try {
             properties.load(property);
         } catch (ConfigurationException ex) {
@@ -72,8 +81,13 @@ public class ApplicationSettings {
         }
     }
 
-    public String validAuthorizationCredentials() {
-        return properties.getString("header.authlist", null);
+    public Map<String, String> validAuthorizationCredentials() {
+        return prepareUsersMap(properties.getString("header.authlist", null));
+    }
+
+    private Map<String, String> prepareUsersMap(@Nullable String allowedUsers) {
+        return allowedUsers == null ? HashMap.empty() : List.ofAll(stream(allowedUsers.split("\\|")))
+                .toMap(t -> t.split(",")[0].trim(), t -> new String(Base64.getDecoder().decode(t.split(",")[1])).trim());
     }
 
     public int maximumAllowedQueuedEvents() {
@@ -110,11 +124,11 @@ public class ApplicationSettings {
     }
 
     public String keystorePasswordFileLocation() {
-        return properties.getString("collector.keystore.passwordfile", "./etc/passwordfile");
+        return prependWithUserDirOnRelative(properties.getString("collector.keystore.passwordfile", "etc/passwordfile"));
     }
 
     public String keystoreFileLocation() {
-        return properties.getString("collector.keystore.file.location", "../etc/keystore");
+        return prependWithUserDirOnRelative(properties.getString("collector.keystore.file.location", "etc/keystore"));
     }
 
     public String keystoreAlias() {
@@ -126,7 +140,7 @@ public class ApplicationSettings {
     }
 
     public String cambriaConfigurationFileLocation() {
-        return properties.getString("collector.dmaapfile", "./etc/DmaapConfig.json");
+        return prependWithUserDirOnRelative(properties.getString("collector.dmaapfile", "etc/DmaapConfig.json"));
     }
 
     public Map<String, String[]> dMaaPStreamsMapping() {
@@ -138,17 +152,6 @@ public class ApplicationSettings {
         }
     }
 
-    /*
-     * Kept back here for backward compatibility.
-     * RestfulCollectorServlet upon its initialization requires options to be represented
-     * as object represented by rrNvReadable interface, so we define a a handy transformation function here.
-     */
-    public rrNvReadable torrNvReadable() {
-        final nvReadableStack settings = new nvReadableStack();
-        settings.push(new nvReadableTable(ConfigurationConverter.getProperties(properties)));
-        return settings;
-    }
-
     private Map<String, String[]> convertDMaaPStreamsPropertyToMap(String streamIdsProperty) {
         java.util.HashMap<String, String[]> domainToStreamIdsMapping = new java.util.HashMap<>();
         String[] topics = streamIdsProperty.split("\\|");
@@ -168,6 +171,13 @@ public class ApplicationSettings {
         }
     }
 
+    public String prependWithUserDirOnRelative(String filePath) {
+        if (!Paths.get(filePath).isAbsolute()) {
+            filePath = Paths.get(appInvocationDir, filePath).toString();
+        }
+        return filePath;
+    }
+
     @VisibleForTesting
     String getStringDirectly(String key) {
         return properties.getString(key);
index 6450d2e..6764d5b 100644 (file)
@@ -24,36 +24,29 @@ package org.onap.dcae;
 import java.util.HashMap;
 
 /**
- *  CLIUtils extracted from nsaServerLibrary this implementation will be removed once we switch to different API library
+ * CLIUtils extracted from nsaServerLibrary this implementation will be removed once we switch to different API library
  */
 public class CLIUtils {
 
-    public static io.vavr.collection.HashMap<String, String>  processCmdLine (String[] args) {
-        final HashMap<String,String> map = new HashMap<String,String> ();
+    public static io.vavr.collection.HashMap<String, String> processCmdLine(String[] args) {
+        final HashMap<String, String> map = new HashMap<String, String>();
 
         String lastKey = null;
-        for ( String arg : args )
-        {
-            if ( arg.startsWith ( "-" ) )
-            {
-                if ( lastKey != null )
-                {
-                    map.put ( lastKey.substring(1), "" );
+        for (String arg : args) {
+            if (arg.startsWith("-")) {
+                if (lastKey != null) {
+                    map.put(lastKey.substring(1), "");
                 }
                 lastKey = arg;
-            }
-            else
-            {
-                if ( lastKey != null )
-                {
-                    map.put ( lastKey.substring(1), arg );
+            } else {
+                if (lastKey != null) {
+                    map.put(lastKey.substring(1), arg);
                 }
                 lastKey = null;
             }
         }
-        if ( lastKey != null )
-        {
-            map.put ( lastKey.substring(1), "" );
+        if (lastKey != null) {
+            map.put(lastKey.substring(1), "");
         }
         return io.vavr.collection.HashMap.ofAll(map);
     }
diff --git a/src/main/java/org/onap/dcae/CollectorSchemas.java b/src/main/java/org/onap/dcae/CollectorSchemas.java
new file mode 100644 (file)
index 0000000..fc12b1f
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * ============LICENSE_START=======================================================
+ * PROJECT
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018 Nokia. All rights reserved.s
+ * ================================================================================
+ * 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.dcae;
+
+import static java.nio.file.Files.readAllBytes;
+import static java.util.stream.Collectors.toMap;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.github.fge.jackson.JsonLoader;
+import com.github.fge.jsonschema.core.exceptions.ProcessingException;
+import com.github.fge.jsonschema.main.JsonSchema;
+import com.github.fge.jsonschema.main.JsonSchemaFactory;
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.AbstractMap;
+import java.util.Map;
+import org.json.JSONObject;
+import org.onap.dcae.restapi.VesRestController;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+public class CollectorSchemas {
+
+  private static final Logger LOG = (Logger) LoggerFactory.getLogger(VesRestController.class);
+
+  @Autowired
+  private ApplicationSettings collectorProperties;
+
+  //refactor is needed in next iteration
+  public Map<String, JsonSchema> getJSONSchemasMap(String version) {
+    JSONObject jsonObject = collectorProperties.jsonSchema();
+    Map<String, JsonSchema> schemas = jsonObject.toMap().entrySet().stream().map(
+        versionToFilePath -> {
+          try {
+            String schemaContent = new String(
+                readAllBytes(Paths.get(versionToFilePath.getValue().toString())));
+            JsonNode schemaNode = JsonLoader.fromString(schemaContent);
+            JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema(schemaNode);
+            return new AbstractMap.SimpleEntry<>(versionToFilePath.getKey(), schema);
+          } catch (IOException | ProcessingException e) {
+            LOG.error("Could not read schema from path: " + versionToFilePath.getValue(), e);
+            throw new RuntimeException(
+                "Could not read schema from path: " + versionToFilePath.getValue(), e);
+          }
+        }
+    ).collect(toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
+    if (schemas.get(version) == null && collectorProperties.eventTransformingEnabled()) {
+      LOG.error(String.format("Missing necessary %s JSON schema", version));
+      throw new RuntimeException(String.format("Missing necessary %s JSON schema", version));
+    }
+    return schemas;
+  }
+}
\ No newline at end of file
diff --git a/src/main/java/org/onap/dcae/SchemaValidator.java b/src/main/java/org/onap/dcae/SchemaValidator.java
new file mode 100644 (file)
index 0000000..e4b52cf
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * ============LICENSE_START=======================================================
+ * PROJECT
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018 Nokia. All rights reserved.s
+ * ================================================================================
+ * 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.dcae;
+
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.github.fge.jackson.JsonLoader;
+import com.github.fge.jsonschema.core.exceptions.ProcessingException;
+import com.github.fge.jsonschema.core.report.ProcessingMessage;
+import com.github.fge.jsonschema.core.report.ProcessingReport;
+import com.github.fge.jsonschema.main.JsonSchema;
+import com.github.fge.jsonschema.main.JsonSchemaFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+public class SchemaValidator {
+
+    private static final Logger log = LoggerFactory.getLogger(SchemaValidator.class);
+
+    //refactor in next iteration
+    public static String validateAgainstSchema(String jsonData, String jsonSchema) {
+        ProcessingReport report;
+        String result = "false";
+
+        try {
+            log.trace("Schema validation for event:" + jsonData);
+            JsonNode schemaNode = JsonLoader.fromString(jsonSchema);
+            JsonNode data = JsonLoader.fromString(jsonData);
+            JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
+            JsonSchema schema = factory.getJsonSchema(schemaNode);
+            report = schema.validate(data);
+        } catch (JsonParseException e) {
+            log.error("validateAgainstSchema:JsonParseException for event:" + jsonData);
+            return e.getMessage();
+        } catch (ProcessingException e) {
+            log.error("validateAgainstSchema:Processing exception for event:" + jsonData);
+            return e.getMessage();
+        } catch (IOException e) {
+            log.error(
+                    "validateAgainstSchema:IO exception; something went wrong trying to read json data for event:" + jsonData);
+            return e.getMessage();
+        }
+        if (report != null) {
+            for (ProcessingMessage pm : report) {
+                log.trace("Processing Message: " + pm.getMessage());
+            }
+            result = String.valueOf(report.isSuccess());
+        }
+        try {
+            log.debug("Validation Result:" + result + " Validation report:" + report);
+        } catch (NullPointerException e) {
+            log.error("validateAgainstSchema:NullpointerException on report");
+        }
+        return result;
+    }
+}
diff --git a/src/main/java/org/onap/dcae/VesApplication.java b/src/main/java/org/onap/dcae/VesApplication.java
new file mode 100644 (file)
index 0000000..86b8ccb
--- /dev/null
@@ -0,0 +1,110 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * PROJECT
+ * ================================================================================
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.dcae;
+
+import io.vavr.collection.Map;
+import org.json.JSONObject;
+import org.onap.dcae.commonFunction.EventProcessor;
+import org.onap.dcae.commonFunction.event.publishing.DMaaPConfigurationParser;
+import org.onap.dcae.commonFunction.event.publishing.EventPublisher;
+import org.onap.dcae.commonFunction.event.publishing.PublisherConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
+import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Lazy;
+
+import java.nio.file.Paths;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+
+@SpringBootApplication
+@EnableAutoConfiguration(exclude = {GsonAutoConfiguration.class, SecurityAutoConfiguration.class})
+public class VesApplication {
+
+    private static final Logger metriclog = LoggerFactory.getLogger("com.att.ecomp.metrics");
+    private static final Logger incomingRequestsLogger = LoggerFactory.getLogger("org.onap.dcae.commonFunction.input");
+    private static final Logger oplog = LoggerFactory.getLogger("org.onap.dcae.commonFunction.output");
+    private static final Logger errorLog = LoggerFactory.getLogger("org.onap.dcae.commonFunction.error");
+    private static final int MAX_THREADS = 20;
+    public static LinkedBlockingQueue<JSONObject> fProcessingInputQueue;
+    private static ApplicationSettings properties;
+
+    public static void main(String[] args) {
+        SpringApplication app = new SpringApplication(VesApplication.class);
+
+        properties = new ApplicationSettings(args, CLIUtils::processCmdLine);
+
+        fProcessingInputQueue = new LinkedBlockingQueue<>(properties.maximumAllowedQueuedEvents());
+
+        app.setAddCommandLineProperties(true);
+        app.run();
+
+        EventProcessor ep = new EventProcessor(EventPublisher.createPublisher(oplog, getDmapConfig()), properties);
+
+        ExecutorService executor = Executors.newFixedThreadPool(MAX_THREADS);
+        for (int i = 0; i < MAX_THREADS; ++i) {
+            executor.execute(ep);
+        }
+    }
+
+
+    private static Map<String, PublisherConfig> getDmapConfig() {
+        return DMaaPConfigurationParser.
+                parseToDomainMapping(Paths.get(properties.cambriaConfigurationFileLocation())).get();
+    }
+
+    @Bean
+    @Lazy
+    public ApplicationSettings applicationSettings() {
+        return properties;
+    }
+
+    @Bean
+    @Qualifier("incomingRequestsLogger")
+    public Logger incomingRequestsLogger() {
+        return incomingRequestsLogger;
+    }
+
+    @Bean
+    @Qualifier("metriclog")
+    public Logger incomingRequestsMetricsLogger() {
+        return metriclog;
+    }
+
+    @Bean
+    @Qualifier("errorLog")
+    public Logger errorLogger() {
+        return errorLog;
+    }
+
+    @Bean
+    public LinkedBlockingQueue<JSONObject> inputQueue() {
+        return fProcessingInputQueue;
+    }
+
+}
index 97d73dd..7be45b0 100644 (file)
  */
 package org.onap.dcae.commonFunction;
 
-import static io.vavr.API.Set;
-
 import io.vavr.collection.List;
 import io.vavr.collection.Set;
 import io.vavr.control.Option;
-import java.util.stream.StreamSupport;
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
+import java.util.stream.StreamSupport;
+
+import static io.vavr.API.Set;
+
 /**
  * This class is a wrapper for 2 most used entities of org.json lib: JSONArray and JSONObject and comprises utility
  * methods for fast access of json structures without need to explicitly coerce between them. While using this, bear in
@@ -109,5 +110,4 @@ public class AnyNode {
         return (JSONObject) this.obj;
     }
 
-
 }
diff --git a/src/main/java/org/onap/dcae/commonFunction/CommonStartup.java b/src/main/java/org/onap/dcae/commonFunction/CommonStartup.java
deleted file mode 100644 (file)
index 36713aa..0000000
+++ /dev/null
@@ -1,204 +0,0 @@
-/*-\r
- * ============LICENSE_START=======================================================\r
- * PROJECT\r
- * ================================================================================\r
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.\r
- * ================================================================================\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *      http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ============LICENSE_END=========================================================\r
- */\r
-\r
-package org.onap.dcae.commonFunction;\r
-\r
-import com.att.nsa.apiServer.ApiServer;\r
-import com.att.nsa.apiServer.ApiServerConnector;\r
-import com.att.nsa.apiServer.endpoints.NsaBaseEndpoint;\r
-import com.att.nsa.drumlin.till.nv.rrNvReadable;\r
-import com.att.nsa.drumlin.till.nv.rrNvReadable.loadException;\r
-import com.fasterxml.jackson.core.JsonParseException;\r
-import com.fasterxml.jackson.databind.JsonNode;\r
-import com.github.fge.jackson.JsonLoader;\r
-import com.github.fge.jsonschema.core.exceptions.ProcessingException;\r
-import com.github.fge.jsonschema.core.report.ProcessingMessage;\r
-import com.github.fge.jsonschema.core.report.ProcessingReport;\r
-import com.github.fge.jsonschema.main.JsonSchema;\r
-import com.github.fge.jsonschema.main.JsonSchemaFactory;\r
-import org.apache.catalina.LifecycleException;\r
-import org.json.JSONArray;\r
-import org.json.JSONException;\r
-import org.json.JSONObject;\r
-import org.onap.dcae.ApplicationSettings;\r
-import org.onap.dcae.CLIUtils;\r
-import org.onap.dcae.commonFunction.event.publishing.DMaaPConfigurationParser;\r
-import org.onap.dcae.commonFunction.event.publishing.EventPublisher;\r
-import org.onap.dcae.restapi.RestfulCollectorServlet;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-import java.io.IOException;\r
-import java.nio.file.Files;\r
-import java.nio.file.Paths;\r
-import java.util.LinkedList;\r
-import java.util.List;\r
-import java.util.concurrent.ExecutorService;\r
-import java.util.concurrent.Executors;\r
-import java.util.concurrent.LinkedBlockingQueue;\r
-\r
-public class CommonStartup extends NsaBaseEndpoint implements Runnable {\r
-\r
-    private static final Logger metriclog = LoggerFactory.getLogger("com.att.ecomp.metrics");\r
-    public static final Logger inlog = LoggerFactory.getLogger("org.onap.dcae.commonFunction.input");\r
-    static final Logger oplog = LoggerFactory.getLogger("org.onap.dcae.commonFunction.output");\r
-    public static final Logger eplog = LoggerFactory.getLogger("org.onap.dcae.commonFunction.error");\r
-\r
-    static int maxQueueEvent = 1024 * 4;\r
-    public static boolean schemaValidatorflag = false;\r
-    public static boolean authflag = false;\r
-    static boolean eventTransformFlag = true;\r
-    public static JSONObject schemaFileJson;\r
-    static String cambriaConfigFile;\r
-    public static io.vavr.collection.Map<String , String [] > streamID;\r
-\r
-    static LinkedBlockingQueue<JSONObject> fProcessingInputQueue;\r
-    private static ApiServer fTomcatServer = null;\r
-    private static final Logger log = LoggerFactory.getLogger(CommonStartup.class);\r
-\r
-    private CommonStartup(ApplicationSettings settings) throws loadException, IOException, rrNvReadable.missingReqdSetting {\r
-        final List<ApiServerConnector> connectors = new LinkedList<>();\r
-\r
-        if (!settings.authorizationEnabled()) {\r
-            connectors.add(new ApiServerConnector.Builder(settings.httpPort()).secure(false).build());\r
-        }\r
-\r
-        final int securePort = settings.httpsPort();\r
-        final String keystoreFile = settings.keystoreFileLocation();\r
-        final String keystorePasswordFile = settings.keystorePasswordFileLocation();\r
-        final String keyAlias = settings.keystoreAlias();\r
-\r
-        if (settings.authorizationEnabled()) {\r
-            String keystorePassword = readFile(keystorePasswordFile);\r
-            connectors.add(new ApiServerConnector.Builder(securePort).secure(true)\r
-                    .keystorePassword(keystorePassword).keystoreFile(keystoreFile).keyAlias(keyAlias).build());\r
-\r
-        }\r
-\r
-        schemaValidatorflag = settings.jsonSchemaValidationEnabled();\r
-        maxQueueEvent = settings.maximumAllowedQueuedEvents();\r
-        if (schemaValidatorflag) {\r
-            schemaFileJson = settings.jsonSchema();\r
-\r
-        }\r
-        authflag = settings.authorizationEnabled();\r
-        cambriaConfigFile = settings.cambriaConfigurationFileLocation();\r
-        streamID = settings.dMaaPStreamsMapping();\r
-        eventTransformFlag = settings.eventTransformingEnabled();\r
-\r
-        fTomcatServer = new ApiServer.Builder(connectors, new RestfulCollectorServlet(settings)).encodeSlashes(true)\r
-            .name("collector").build();\r
-    }\r
-\r
-    public static void main(String[] args) {\r
-        try {\r
-\r
-            fProcessingInputQueue = new LinkedBlockingQueue<>(CommonStartup.maxQueueEvent);\r
-\r
-            VESLogger.setUpEcompLogging();\r
-\r
-            CommonStartup cs = new CommonStartup(new ApplicationSettings(args, CLIUtils::processCmdLine));\r
-\r
-            Thread commonStartupThread = new Thread(cs);\r
-            commonStartupThread.start();\r
-\r
-            EventProcessor ep = new EventProcessor(EventPublisher.createPublisher(oplog,\r
-                                                                                  DMaaPConfigurationParser\r
-                                                                                      .parseToDomainMapping(Paths.get(cambriaConfigFile))\r
-                                                                                      .get()));\r
-            ExecutorService executor = Executors.newFixedThreadPool(20);\r
-            for (int i = 0; i < 20; ++i) {\r
-                executor.execute(ep);\r
-            }\r
-        } catch (Exception e) {\r
-            CommonStartup.eplog.error("Fatal error during application startup", e);\r
-            throw new RuntimeException(e);\r
-        }\r
-    }\r
-\r
-    public void run() {\r
-        try {\r
-            fTomcatServer.start();\r
-            fTomcatServer.await();\r
-        } catch (LifecycleException | IOException e) {\r
-            throw new RuntimeException(e);\r
-        }\r
-    }\r
-\r
-    public static class QueueFullException extends Exception {\r
-\r
-        private static final long serialVersionUID = 1L;\r
-    }\r
-\r
-    public static void handleEvents(JSONArray a) throws QueueFullException, JSONException {\r
-        CommonStartup.metriclog.info("EVENT_PUBLISH_START");\r
-        for (int i = 0; i < a.length(); i++) {\r
-            if (!fProcessingInputQueue.offer(a.getJSONObject(i))) {\r
-                throw new QueueFullException();\r
-            }\r
-        }\r
-        log.debug("CommonStartup.handleEvents:EVENTS has been published successfully!");\r
-        CommonStartup.metriclog.info("EVENT_PUBLISH_END");\r
-    }\r
-\r
-    private static String readFile(String path) throws IOException {\r
-        byte[] encoded = Files.readAllBytes(Paths.get(path));\r
-        String pwd = new String(encoded);\r
-        return pwd.substring(0, pwd.length() - 1);\r
-    }\r
-\r
-    public static String validateAgainstSchema(String jsonData, String jsonSchema) {\r
-        ProcessingReport report;\r
-        String result = "false";\r
-\r
-        try {\r
-            log.trace("Schema validation for event:" + jsonData);\r
-            JsonNode schemaNode = JsonLoader.fromString(jsonSchema);\r
-            JsonNode data = JsonLoader.fromString(jsonData);\r
-            JsonSchemaFactory factory = JsonSchemaFactory.byDefault();\r
-            JsonSchema schema = factory.getJsonSchema(schemaNode);\r
-            report = schema.validate(data);\r
-        } catch (JsonParseException e) {\r
-            log.error("validateAgainstSchema:JsonParseException for event:" + jsonData);\r
-            return e.getMessage();\r
-        } catch (ProcessingException e) {\r
-            log.error("validateAgainstSchema:Processing exception for event:" + jsonData);\r
-            return e.getMessage();\r
-        } catch (IOException e) {\r
-            log.error(\r
-                "validateAgainstSchema:IO exception; something went wrong trying to read json data for event:" + jsonData);\r
-            return e.getMessage();\r
-        }\r
-        if (report != null) {\r
-            for (ProcessingMessage pm : report) {\r
-                log.trace("Processing Message: " + pm.getMessage());\r
-            }\r
-            result = String.valueOf(report.isSuccess());\r
-        }\r
-        try {\r
-            log.debug("Validation Result:" + result + " Validation report:" + report);\r
-        } catch (NullPointerException e) {\r
-            log.error("validateAgainstSchema:NullpointerException on report");\r
-        }\r
-        return result;\r
-    }\r
-\r
-\r
-}\r
index a6de0fc..e3d5909 100644 (file)
@@ -8,9 +8,9 @@
  * 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.
 
 package org.onap.dcae.commonFunction;
 
-import java.text.DecimalFormat;
 import org.json.JSONArray;
 import org.json.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class ConfigProcessors {
-
-       private static final Logger log = LoggerFactory.getLogger(ConfigProcessors.class);
-       private static final String FIELD = "field";
-       private static final String OLD_FIELD = "oldField";
-       private static final String FILTER = "filter";
-       private static final String VALUE = "value";
-       private static final String REGEX = "\\[\\]";
-       private static final String OBJECT_NOT_FOUND = "ObjectNotFound";
-       private static final String FILTER_NOT_MET = "Filter not met";
-       private static final String COMP_FALSE = "==false";
-
-       private final JSONObject event;
-
-       public ConfigProcessors(JSONObject eventJson) {
-               event = eventJson;
-       }
-
-       public void getValue(JSONObject jsonObject) {
-
-               final String field = jsonObject.getString(FIELD);
-               final JSONObject filter = jsonObject.optJSONObject(FILTER);
-
-               if (filter == null || isFilterMet(filter)) {
-                       getEventObjectVal(field);
-               } else
-                       log.info(FILTER_NOT_MET);
-       }
+import java.text.DecimalFormat;
 
+public class ConfigProcessors {
 
-       public void setValue(JSONObject jsonObject) {
-               final String field = jsonObject.getString(FIELD);
-               final String value = jsonObject.getString(VALUE);
-               final JSONObject filter = jsonObject.optJSONObject(FILTER);
-               if (filter == null || isFilterMet(filter)) {
-                       setEventObjectVal(field, value);
-               } else
-                       log.info(FILTER_NOT_MET);
-       }
+    private static final Logger log = LoggerFactory.getLogger(ConfigProcessors.class);
+    private static final String FIELD = "field";
+    private static final String OLD_FIELD = "oldField";
+    private static final String FILTER = "filter";
+    private static final String VALUE = "value";
+    private static final String REGEX = "\\[\\]";
+    private static final String OBJECT_NOT_FOUND = "ObjectNotFound";
+    private static final String FILTER_NOT_MET = "Filter not met";
+    private static final String COMP_FALSE = "==false";
 
+    private final JSONObject event;
 
+    public ConfigProcessors(JSONObject eventJson) {
+        event = eventJson;
+    }
 
-       private String evaluate(String str) {
-               String value = str;
-               if (str.startsWith("$")) {
-                       value = (String) getEventObjectVal(str.substring(1));
+    public void getValue(JSONObject jsonObject) {
 
-               }
-               return value;
-       }
+        final String field = jsonObject.getString(FIELD);
+        final JSONObject filter = jsonObject.optJSONObject(FILTER);
 
+        if (filter == null || isFilterMet(filter)) {
+            getEventObjectVal(field);
+        } else
+            log.info(FILTER_NOT_MET);
+    }
 
-       public void suppressEvent(JSONObject jsonObject) {
-               final JSONObject filter = jsonObject.optJSONObject(FILTER);
 
-               if (filter == null || isFilterMet(filter)) {
-                       setEventObjectVal("suppressEvent", "true");
-               } else
-                       log.info(FILTER_NOT_MET);
-       }
+    public void setValue(JSONObject jsonObject) {
+        final String field = jsonObject.getString(FIELD);
+        final String value = jsonObject.getString(VALUE);
+        final JSONObject filter = jsonObject.optJSONObject(FILTER);
+        if (filter == null || isFilterMet(filter)) {
+            setEventObjectVal(field, value);
+        } else
+            log.info(FILTER_NOT_MET);
+    }
 
 
-       public void addAttribute(JSONObject jsonObject) {
+    private String evaluate(String str) {
+        String value = str;
+        if (str.startsWith("$")) {
+            value = (String) getEventObjectVal(str.substring(1));
+
+        }
+        return value;
+    }
 
-               final String field = jsonObject.getString(FIELD);
-               final String value = evaluate(jsonObject.getString(VALUE));
-               final JSONObject filter = jsonObject.optJSONObject(FILTER);
-               final String fieldType = jsonObject.optString("fieldType", "string").toLowerCase();
 
-               if (filter == null || isFilterMet(filter)) {
-                       setEventObjectVal(field, value, fieldType);
-               } else
-                       log.info(FILTER_NOT_MET);
-       }
+    public void suppressEvent(JSONObject jsonObject) {
+        final JSONObject filter = jsonObject.optJSONObject(FILTER);
 
+        if (filter == null || isFilterMet(filter)) {
+            setEventObjectVal("suppressEvent", "true");
+        } else
+            log.info(FILTER_NOT_MET);
+    }
 
-       public void updateAttribute(JSONObject jsonObject) {
 
-               final String field = jsonObject.getString(FIELD);
-               final String value = evaluate(jsonObject.getString(VALUE));
-               final JSONObject filter = jsonObject.optJSONObject(FILTER);
-               if (filter == null || isFilterMet(filter)) {
-                       setEventObjectVal(field, value);
-               } else
-                       log.info(FILTER_NOT_MET);
-       }
+    public void addAttribute(JSONObject jsonObject) {
+
+        final String field = jsonObject.getString(FIELD);
+        final String value = evaluate(jsonObject.getString(VALUE));
+        final JSONObject filter = jsonObject.optJSONObject(FILTER);
+        final String fieldType = jsonObject.optString("fieldType", "string").toLowerCase();
+
+        if (filter == null || isFilterMet(filter)) {
+            setEventObjectVal(field, value, fieldType);
+        } else
+            log.info(FILTER_NOT_MET);
+    }
 
 
-       public void removeAttribute(JSONObject jsonObject) {
-
-               final String field = jsonObject.getString(FIELD);
-               final JSONObject filter = jsonObject.optJSONObject(FILTER);
-
-               if (filter == null || isFilterMet(filter)) {
-                       removeEventKey(field);
-               } else
-                       log.info(FILTER_NOT_MET);
-       }
-
-
-       private void renameArrayInArray(JSONObject jsonObject) // map
-       {
-               log.info("renameArrayInArray");
-               final String field = jsonObject.getString(FIELD);
-               final String oldField = jsonObject.getString(OLD_FIELD);
-               final JSONObject filter = jsonObject.optJSONObject(FILTER);
-
-               if (filter == null || isFilterMet(filter)) {
-
-                       final String[] fsplit = field.split(REGEX, field.length());
-                       final String[] oldfsplit = oldField.split(REGEX, oldField.length());
-
-                       final String oldValue = getEventObjectVal(oldfsplit[0]).toString();
-                       if (!oldValue.equals(OBJECT_NOT_FOUND)) {
-                               final String oldArrayName = oldfsplit[1].substring(1);
-                               final String newArrayName = fsplit[1].substring(1);
-                               final String value = oldValue.replaceAll(oldArrayName, newArrayName);
-
-                               log.info("oldValue ==" + oldValue);
-                               log.info("value ==" + value);
-                               JSONArray ja = new JSONArray(value);
-                               removeEventKey(oldfsplit[0]);
-                               setEventObjectVal(fsplit[0], ja);
-                       }
-               } else
-                       log.info(FILTER_NOT_MET);
-       }
-
-
-       public void map(JSONObject jsonObject) {
-
-               final String field = jsonObject.getString(FIELD);
-               if (field.contains("[]")) {
-                       if (field.matches(".*\\[\\]\\..*\\[\\]"))
-                               renameArrayInArray(jsonObject);
-                       else
-                               mapToJArray(jsonObject);
-               } else
-                       mapAttribute(jsonObject);
-       }
-
-       private String performOperation(String operation, String value) {
-               log.info("performOperation");
-               if ("convertMBtoKB".equals(operation)) {
-                       float kbValue = Float.parseFloat(value) * 1024;
-                       value = String.valueOf(kbValue);
-               }
-               return value;
-       }
-
-
-       public void mapAttribute(JSONObject jsonObject) {
-
-               final String field = jsonObject.getString(FIELD);
-               final String oldField = jsonObject.getString(OLD_FIELD);
-               final JSONObject filter = jsonObject.optJSONObject(FILTER);
-               final String operation = jsonObject.optString("operation");
-               String value;
-               if (filter == null || isFilterMet(filter)) {
-
-                       value = getEventObjectVal(oldField).toString();
-                       if (!value.equals(OBJECT_NOT_FOUND)) {
-                               if (operation != null && !operation.isEmpty())
-                                       value = performOperation(operation, value);
-
-                               setEventObjectVal(field, value);
-
-                               removeEventKey(oldField);
-                       }
-               } else
-                       log.info(FILTER_NOT_MET);
-       }
-
-
-       private void mapToJArray(JSONObject jsonObject) {
-               log.info("mapToJArray");
-               String field = jsonObject.getString(FIELD);
-               String oldField = jsonObject.getString(OLD_FIELD);
-               final JSONObject filter = jsonObject.optJSONObject(FILTER);
-               final JSONObject attrMap = jsonObject.optJSONObject("attrMap");
-               oldField = oldField.replaceAll(REGEX, "");
-               field = field.replaceAll(REGEX, "");
-
-               if (filter == null || isFilterMet(filter)) {
-
-                       String value = getEventObjectVal(oldField).toString();
-                       if (!value.equals(OBJECT_NOT_FOUND)) {
-                               log.info("old value ==" + value);
-                               // update old value based on attrMap
-                               if (attrMap != null) {
-                                       // loop thru attrMap and update attribute name to new name
-                                       for (String key : attrMap.keySet()) {
-                                               value = value.replaceAll(key, attrMap.getString(key));
-                                       }
-                               }
-
-                               log.info("new value ==" + value);
-                               char c = value.charAt(0);
-                               if (c != '[') {
-                                       // oldfield is JsonObject
-                                       JSONObject valueJO = new JSONObject(value);
-                                       // if the array already exists
-                                       String existingValue = getEventObjectVal(field).toString();
-                                       if (!existingValue.equals(OBJECT_NOT_FOUND)) {
-                                               JSONArray ja = new JSONArray(existingValue);
-                                               JSONObject jo = ja.optJSONObject(0);
-                                               if (jo != null) {
-                                                       for (String key : valueJO.keySet()) {
-                                                               jo.put(key, valueJO.get(key));
-
-                                                       }
-                                                       ja.put(0, jo);
-
-                                                       setEventObjectVal(field, ja);
-                                               }
-                                       } else // if new array
-                                               setEventObjectVal(field + "[0]", new JSONObject(value), "JArray");
-                               } else // oldfield is jsonArray
-                                       setEventObjectVal(field, new JSONArray(value));
-
-                               removeEventKey(oldField);
-                       }
-               } else
-                       log.info(FILTER_NOT_MET);
-       }
-
-       /**
-        * example - { "functionName": "concatenateValue", "args":{ "filter":
-        * {"event.commonEventHeader.event":"heartbeat"},
-        * FIELD:"event.commonEventHeader.eventName", "concatenate":
-        * ["event.commonEventHeader.domain","event.commonEventHeader.eventType","event.commonEventHeader.alarmCondition"],
-        * "delimiter":"_" } }
-        **/
-       public void concatenateValue(JSONObject jsonObject) {
-
-               final String field = jsonObject.getString(FIELD);
-               final String delimiter = jsonObject.getString("delimiter");
-               final JSONArray values = jsonObject.getJSONArray("concatenate");
-               final JSONObject filter = jsonObject.optJSONObject(FILTER);
-               if (filter == null || isFilterMet(filter)) {
-                       StringBuilder value = new StringBuilder();
-                       for (int i = 0; i < values.length(); i++) {
-
-                               String tempVal = evaluate(values.getString(i));
-                               if (!tempVal.equals(OBJECT_NOT_FOUND)) {
-                                       if (i == 0)
-                                               value.append(tempVal);
-                                       else
-                                               value.append(delimiter).append(tempVal);
-                               }
-                       }
-
-                       setEventObjectVal(field, value.toString());
-               } else
-                       log.info(FILTER_NOT_MET);
-       }
-
-       public void subtractValue(JSONObject jsonObject) {
-
-               final String field = jsonObject.getString(FIELD);
-               final JSONArray values = jsonObject.getJSONArray("subtract");
-               final JSONObject filter = jsonObject.optJSONObject(FILTER);
-               if (filter == null || isFilterMet(filter)) {
-                       float value = 0;
-                       for (int i = 0; i < values.length(); i++) {
-                               log.info(values.getString(i));
-                               String tempVal = evaluate(values.getString(i));
-                               log.info("tempVal==" + tempVal);
-                               if (!tempVal.equals(OBJECT_NOT_FOUND)) {
-                                       if (i == 0)
-                                               value = value + Float.valueOf(tempVal);
-                                       else
-                                               value = value - Float.valueOf(tempVal);
-                               }
-                       }
-                       log.info("value ==" + value);
-                       setEventObjectVal(field, value, "number");
-               } else
-                       log.info(FILTER_NOT_MET);
-       }
-
-
-       private void removeEventKey(String field) {
-               String[] keySet = field.split("\\.", field.length());
-               JSONObject keySeries = event;
-               for (int i = 0; i < (keySet.length - 1); i++) {
-
-                       keySeries = keySeries.getJSONObject(keySet[i]);
-               }
-
-               keySeries.remove(keySet[keySet.length - 1]);
-       }
-
-
-       private boolean checkFilter(JSONObject jo, String key, String logicKey) {
-               String filterValue = jo.getString(key);
-               if (filterValue.contains(":")) {
-                       String[] splitVal = filterValue.split(":");
-                       if ("matches".equals(splitVal[0])) {
-                               if ("not".equals(logicKey)) {
-                                       if (getEventObjectVal(key).toString().matches(splitVal[1])) {
-                                               log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + COMP_FALSE);
-                                               return false;
-                                       }
-                               } else {
-                                       if (!(getEventObjectVal(key).toString().matches(splitVal[1]))) {
-                                               log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + COMP_FALSE);
-                                               return false;
-                                       }
-                               }
-
-                       }
-                       if ("contains".equals(splitVal[0])) {
-                               if ("not".equals(logicKey)) {
-                                       if (getEventObjectVal(key).toString().contains(splitVal[1])) {
-                                               log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + COMP_FALSE);
-                                               return false;
-                                       }
-                               } else {
-                                       if (!(getEventObjectVal(key).toString().contains(splitVal[1]))) {
-                                               log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + COMP_FALSE);
-                                               return false;
-                                       }
-                               }
-
-                       }
-               } else {
-                       if ("not".equals(logicKey)) {
-                               if (getEventObjectVal(key).toString().equals(filterValue)) {
-                                       log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + COMP_FALSE);
-                                       return false;
-                               }
-                       } else {
-                               if (!(getEventObjectVal(key).toString().equals(filterValue))) {
-                                       log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + COMP_FALSE);
-                                       return false;
-                               }
-                       }
-               }
-               return true;
-       }
-
-
-       public boolean isFilterMet(JSONObject jo) {
-               for (String key : jo.keySet()) {
-                       if ("not".equals(key)) {
-                               JSONObject njo = jo.getJSONObject(key);
-                               for (String njoKey : njo.keySet()) {
-                                       if (!checkFilter(njo, njoKey, key))
-                                               return false;
-                               }
-                       } else {
-                               if (!checkFilter(jo, key, key))
-                                       return false;
-                       }
-               }
-               return true;
-       }
-
-       /**
-        * returns a string or JSONObject or JSONArray
-        **/
-       public Object getEventObjectVal(String keySeriesStr) {
-               keySeriesStr = keySeriesStr.replaceAll("\\[", ".");
-               keySeriesStr = keySeriesStr.replaceAll("\\]", ".");
-               if (keySeriesStr.contains("..")) {
-                       keySeriesStr = keySeriesStr.replaceAll("\\.\\.", ".");
-               }
-
-               if (keySeriesStr.lastIndexOf(".") == keySeriesStr.length() - 1)
-                       keySeriesStr = keySeriesStr.substring(0, keySeriesStr.length() - 1);
-               String[] keySet = keySeriesStr.split("\\.", keySeriesStr.length());
-               Object keySeriesObj = event;
-               for (String aKeySet : keySet) {
-                       if (keySeriesObj != null) {
-                               if (keySeriesObj instanceof String) {
-
-                                       log.info("STRING==" + keySeriesObj);
-                               } else if (keySeriesObj instanceof JSONArray) {
-                                       keySeriesObj = ((JSONArray) keySeriesObj).optJSONObject(Integer.parseInt(aKeySet));
-
-                               } else if (keySeriesObj instanceof JSONObject) {
-                                       keySeriesObj = ((JSONObject) keySeriesObj).opt(aKeySet);
-
-                               } else {
-                                       log.info("unknown object==" + keySeriesObj);
-                               }
-                       }
-               }
-
-               if (keySeriesObj == null)
-                       return OBJECT_NOT_FOUND;
-               return keySeriesObj;
-       }
-
-       public void setEventObjectVal(String keySeriesStr, Object value) {
-               setEventObjectVal(keySeriesStr, value, "string");
-       }
-
-       /**
-        * returns a string or JSONObject or JSONArray
-        **/
-       public void setEventObjectVal(String keySeriesStr, Object value, String fieldType) {
-               keySeriesStr = keySeriesStr.replaceAll("\\[", ".");
-               keySeriesStr = keySeriesStr.replaceAll("\\]", ".");
-               if (keySeriesStr.contains("..")) {
-                       keySeriesStr = keySeriesStr.replaceAll("\\.\\.", ".");
-               }
-               log.info("fieldType==" + fieldType);
-
-               if (keySeriesStr.lastIndexOf(".") == keySeriesStr.length() - 1)
-                       keySeriesStr = keySeriesStr.substring(0, keySeriesStr.length() - 1);
-               String[] keySet = keySeriesStr.split("\\.", keySeriesStr.length());
-               Object keySeriesObj = event;
-               for (int i = 0; i < (keySet.length - 1); i++) {
-
-                       if (keySeriesObj instanceof JSONArray) {
-
-                               if (((JSONArray) keySeriesObj).optJSONObject(Integer.parseInt(keySet[i])) == null) // if
-                                                                                                                                                                                                       // the
-                                                                                                                                                                                                       // object
-                                                                                                                                                                                                       // is
-                                                                                                                                                                                                       // not
-                                                                                                                                                                                                       // there
-                                                                                                                                                                                                       // then
-                                                                                                                                                                                                       // add
-                                                                                                                                                                                                       // it
-                               {
-                                       log.info("Object is null, must add it");
-                                       if (keySet[i + 1].matches("[0-9]*")) // if index then array
-                                               ((JSONArray) keySeriesObj).put(Integer.parseInt(keySet[i]), new JSONArray());
-                                       else
-                                               ((JSONArray) keySeriesObj).put(Integer.parseInt(keySet[i]), new JSONObject());
-                               }
-                               keySeriesObj = ((JSONArray) keySeriesObj).optJSONObject(Integer.parseInt(keySet[i]));
-
-                       } else if (keySeriesObj instanceof JSONObject) {
-                               if (((JSONObject) keySeriesObj).opt(keySet[i]) == null) // if
-                                                                                                                                               // the
-                                                                                                                                               // object
-                                                                                                                                               // is
-                                                                                                                                               // not
-                                                                                                                                               // there
-                                                                                                                                               // then
-                                                                                                                                               // add
-                                                                                                                                               // it
-                               {
-                                       if (keySet[i + 1].matches("[0-9]*")) // if index then array
-                                               ((JSONObject) keySeriesObj).put(keySet[i], new JSONArray());
-                                       else
-                                               ((JSONObject) keySeriesObj).put(keySet[i], new JSONObject());
-                                       log.info("Object is null, must add it");
-                               }
-                               keySeriesObj = ((JSONObject) keySeriesObj).opt(keySet[i]);
-                       } else {
-                               log.info("unknown object==" + keySeriesObj);
-                       }
-               }
-               if ("number".equals(fieldType)) {
-                       DecimalFormat df = new DecimalFormat("#.0");
-                       if (value instanceof String)
-                               ((JSONObject) keySeriesObj).put(keySet[keySet.length - 1],
-                                               Float.valueOf(df.format(Float.valueOf((String) value))));
-                       else
-                               ((JSONObject) keySeriesObj).put(keySet[keySet.length - 1], Float.valueOf(df.format(value)));
-               } else if ("integer".equals(fieldType) && value instanceof String)
-                       ((JSONObject) keySeriesObj).put(keySet[keySet.length - 1], Integer.valueOf((String) value));
-               else if ("JArray".equals(fieldType))
-                       ((JSONArray) keySeriesObj).put(value);
-               else
-                       ((JSONObject) keySeriesObj).put(keySet[keySet.length - 1], value);
-
-       }
+    public void updateAttribute(JSONObject jsonObject) {
+
+        final String field = jsonObject.getString(FIELD);
+        final String value = evaluate(jsonObject.getString(VALUE));
+        final JSONObject filter = jsonObject.optJSONObject(FILTER);
+        if (filter == null || isFilterMet(filter)) {
+            setEventObjectVal(field, value);
+        } else
+            log.info(FILTER_NOT_MET);
+    }
+
+
+    public void removeAttribute(JSONObject jsonObject) {
+
+        final String field = jsonObject.getString(FIELD);
+        final JSONObject filter = jsonObject.optJSONObject(FILTER);
+
+        if (filter == null || isFilterMet(filter)) {
+            removeEventKey(field);
+        } else
+            log.info(FILTER_NOT_MET);
+    }
+
+
+    private void renameArrayInArray(JSONObject jsonObject) // map
+    {
+        log.info("renameArrayInArray");
+        final String field = jsonObject.getString(FIELD);
+        final String oldField = jsonObject.getString(OLD_FIELD);
+        final JSONObject filter = jsonObject.optJSONObject(FILTER);
+
+        if (filter == null || isFilterMet(filter)) {
+
+            final String[] fsplit = field.split(REGEX, field.length());
+            final String[] oldfsplit = oldField.split(REGEX, oldField.length());
+
+            final String oldValue = getEventObjectVal(oldfsplit[0]).toString();
+            if (!oldValue.equals(OBJECT_NOT_FOUND)) {
+                final String oldArrayName = oldfsplit[1].substring(1);
+                final String newArrayName = fsplit[1].substring(1);
+                final String value = oldValue.replaceAll(oldArrayName, newArrayName);
+
+                log.info("oldValue ==" + oldValue);
+                log.info("value ==" + value);
+                JSONArray ja = new JSONArray(value);
+                removeEventKey(oldfsplit[0]);
+                setEventObjectVal(fsplit[0], ja);
+            }
+        } else
+            log.info(FILTER_NOT_MET);
+    }
+
+
+    public void map(JSONObject jsonObject) {
+
+        final String field = jsonObject.getString(FIELD);
+        if (field.contains("[]")) {
+            if (field.matches(".*\\[\\]\\..*\\[\\]"))
+                renameArrayInArray(jsonObject);
+            else
+                mapToJArray(jsonObject);
+        } else
+            mapAttribute(jsonObject);
+    }
+
+    private String performOperation(String operation, String value) {
+        log.info("performOperation");
+        if ("convertMBtoKB".equals(operation)) {
+            float kbValue = Float.parseFloat(value) * 1024;
+            value = String.valueOf(kbValue);
+        }
+        return value;
+    }
+
+
+    public void mapAttribute(JSONObject jsonObject) {
+
+        final String field = jsonObject.getString(FIELD);
+        final String oldField = jsonObject.getString(OLD_FIELD);
+        final JSONObject filter = jsonObject.optJSONObject(FILTER);
+        final String operation = jsonObject.optString("operation");
+        String value;
+        if (filter == null || isFilterMet(filter)) {
+
+            value = getEventObjectVal(oldField).toString();
+            if (!value.equals(OBJECT_NOT_FOUND)) {
+                if (operation != null && !operation.isEmpty())
+                    value = performOperation(operation, value);
+
+                setEventObjectVal(field, value);
+
+                removeEventKey(oldField);
+            }
+        } else
+            log.info(FILTER_NOT_MET);
+    }
+
+
+    private void mapToJArray(JSONObject jsonObject) {
+        log.info("mapToJArray");
+        String field = jsonObject.getString(FIELD);
+        String oldField = jsonObject.getString(OLD_FIELD);
+        final JSONObject filter = jsonObject.optJSONObject(FILTER);
+        final JSONObject attrMap = jsonObject.optJSONObject("attrMap");
+        oldField = oldField.replaceAll(REGEX, "");
+        field = field.replaceAll(REGEX, "");
+
+        if (filter == null || isFilterMet(filter)) {
+
+            String value = getEventObjectVal(oldField).toString();
+            if (!value.equals(OBJECT_NOT_FOUND)) {
+                log.info("old value ==" + value);
+                // update old value based on attrMap
+                if (attrMap != null) {
+                    // loop thru attrMap and update attribute name to new name
+                    for (String key : attrMap.keySet()) {
+                        value = value.replaceAll(key, attrMap.getString(key));
+                    }
+                }
+
+                log.info("new value ==" + value);
+                char c = value.charAt(0);
+                if (c != '[') {
+                    // oldfield is JsonObject
+                    JSONObject valueJO = new JSONObject(value);
+                    // if the array already exists
+                    String existingValue = getEventObjectVal(field).toString();
+                    if (!existingValue.equals(OBJECT_NOT_FOUND)) {
+                        JSONArray ja = new JSONArray(existingValue);
+                        JSONObject jo = ja.optJSONObject(0);
+                        if (jo != null) {
+                            for (String key : valueJO.keySet()) {
+                                jo.put(key, valueJO.get(key));
+
+                            }
+                            ja.put(0, jo);
+
+                            setEventObjectVal(field, ja);
+                        }
+                    } else // if new array
+                        setEventObjectVal(field + "[0]", new JSONObject(value), "JArray");
+                } else // oldfield is jsonArray
+                    setEventObjectVal(field, new JSONArray(value));
+
+                removeEventKey(oldField);
+            }
+        } else
+            log.info(FILTER_NOT_MET);
+    }
+
+    /**
+     * example - { "functionName": "concatenateValue", "args":{ "filter":
+     * {"event.commonEventHeader.event":"heartbeat"},
+     * FIELD:"event.commonEventHeader.eventName", "concatenate":
+     * ["event.commonEventHeader.domain","event.commonEventHeader.eventType","event.commonEventHeader.alarmCondition"],
+     * "delimiter":"_" } }
+     **/
+    public void concatenateValue(JSONObject jsonObject) {
+
+        final String field = jsonObject.getString(FIELD);
+        final String delimiter = jsonObject.getString("delimiter");
+        final JSONArray values = jsonObject.getJSONArray("concatenate");
+        final JSONObject filter = jsonObject.optJSONObject(FILTER);
+        if (filter == null || isFilterMet(filter)) {
+            StringBuilder value = new StringBuilder();
+            for (int i = 0; i < values.length(); i++) {
+
+                String tempVal = evaluate(values.getString(i));
+                if (!tempVal.equals(OBJECT_NOT_FOUND)) {
+                    if (i == 0)
+                        value.append(tempVal);
+                    else
+                        value.append(delimiter).append(tempVal);
+                }
+            }
+
+            setEventObjectVal(field, value.toString());
+        } else
+            log.info(FILTER_NOT_MET);
+    }
+
+    public void subtractValue(JSONObject jsonObject) {
+
+        final String field = jsonObject.getString(FIELD);
+        final JSONArray values = jsonObject.getJSONArray("subtract");
+        final JSONObject filter = jsonObject.optJSONObject(FILTER);
+        if (filter == null || isFilterMet(filter)) {
+            float value = 0;
+            for (int i = 0; i < values.length(); i++) {
+                log.info(values.getString(i));
+                String tempVal = evaluate(values.getString(i));
+                log.info("tempVal==" + tempVal);
+                if (!tempVal.equals(OBJECT_NOT_FOUND)) {
+                    if (i == 0)
+                        value = value + Float.valueOf(tempVal);
+                    else
+                        value = value - Float.valueOf(tempVal);
+                }
+            }
+            log.info("value ==" + value);
+            setEventObjectVal(field, value, "number");
+        } else
+            log.info(FILTER_NOT_MET);
+    }
+
+
+    private void removeEventKey(String field) {
+        String[] keySet = field.split("\\.", field.length());
+        JSONObject keySeries = event;
+        for (int i = 0; i < (keySet.length - 1); i++) {
+
+            keySeries = keySeries.getJSONObject(keySet[i]);
+        }
+
+        keySeries.remove(keySet[keySet.length - 1]);
+    }
+
+
+    private boolean checkFilter(JSONObject jo, String key, String logicKey) {
+        String filterValue = jo.getString(key);
+        if (filterValue.contains(":")) {
+            String[] splitVal = filterValue.split(":");
+            if ("matches".equals(splitVal[0])) {
+                if ("not".equals(logicKey)) {
+                    if (getEventObjectVal(key).toString().matches(splitVal[1])) {
+                        log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + COMP_FALSE);
+                        return false;
+                    }
+                } else {
+                    if (!(getEventObjectVal(key).toString().matches(splitVal[1]))) {
+                        log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + COMP_FALSE);
+                        return false;
+                    }
+                }
+
+            }
+            if ("contains".equals(splitVal[0])) {
+                if ("not".equals(logicKey)) {
+                    if (getEventObjectVal(key).toString().contains(splitVal[1])) {
+                        log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + COMP_FALSE);
+                        return false;
+                    }
+                } else {
+                    if (!(getEventObjectVal(key).toString().contains(splitVal[1]))) {
+                        log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + COMP_FALSE);
+                        return false;
+                    }
+                }
+
+            }
+        } else {
+            if ("not".equals(logicKey)) {
+                if (getEventObjectVal(key).toString().equals(filterValue)) {
+                    log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + COMP_FALSE);
+                    return false;
+                }
+            } else {
+                if (!(getEventObjectVal(key).toString().equals(filterValue))) {
+                    log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + COMP_FALSE);
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
+
+    public boolean isFilterMet(JSONObject jo) {
+        for (String key : jo.keySet()) {
+            if ("not".equals(key)) {
+                JSONObject njo = jo.getJSONObject(key);
+                for (String njoKey : njo.keySet()) {
+                    if (!checkFilter(njo, njoKey, key))
+                        return false;
+                }
+            } else {
+                if (!checkFilter(jo, key, key))
+                    return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * returns a string or JSONObject or JSONArray
+     **/
+    public Object getEventObjectVal(String keySeriesStr) {
+        keySeriesStr = keySeriesStr.replaceAll("\\[", ".");
+        keySeriesStr = keySeriesStr.replaceAll("\\]", ".");
+        if (keySeriesStr.contains("..")) {
+            keySeriesStr = keySeriesStr.replaceAll("\\.\\.", ".");
+        }
+
+        if (keySeriesStr.lastIndexOf(".") == keySeriesStr.length() - 1)
+            keySeriesStr = keySeriesStr.substring(0, keySeriesStr.length() - 1);
+        String[] keySet = keySeriesStr.split("\\.", keySeriesStr.length());
+        Object keySeriesObj = event;
+        for (String aKeySet : keySet) {
+            if (keySeriesObj != null) {
+                if (keySeriesObj instanceof String) {
+
+                    log.info("STRING==" + keySeriesObj);
+                } else if (keySeriesObj instanceof JSONArray) {
+                    keySeriesObj = ((JSONArray) keySeriesObj).optJSONObject(Integer.parseInt(aKeySet));
+
+                } else if (keySeriesObj instanceof JSONObject) {
+                    keySeriesObj = ((JSONObject) keySeriesObj).opt(aKeySet);
+
+                } else {
+                    log.info("unknown object==" + keySeriesObj);
+                }
+            }
+        }
+
+        if (keySeriesObj == null)
+            return OBJECT_NOT_FOUND;
+        return keySeriesObj;
+    }
+
+    public void setEventObjectVal(String keySeriesStr, Object value) {
+        setEventObjectVal(keySeriesStr, value, "string");
+    }
+
+    /**
+     * returns a string or JSONObject or JSONArray
+     **/
+    public void setEventObjectVal(String keySeriesStr, Object value, String fieldType) {
+        keySeriesStr = keySeriesStr.replaceAll("\\[", ".");
+        keySeriesStr = keySeriesStr.replaceAll("\\]", ".");
+        if (keySeriesStr.contains("..")) {
+            keySeriesStr = keySeriesStr.replaceAll("\\.\\.", ".");
+        }
+        log.info("fieldType==" + fieldType);
+
+        if (keySeriesStr.lastIndexOf(".") == keySeriesStr.length() - 1)
+            keySeriesStr = keySeriesStr.substring(0, keySeriesStr.length() - 1);
+        String[] keySet = keySeriesStr.split("\\.", keySeriesStr.length());
+        Object keySeriesObj = event;
+        for (int i = 0; i < (keySet.length - 1); i++) {
+
+            if (keySeriesObj instanceof JSONArray) {
+
+                if (((JSONArray) keySeriesObj).optJSONObject(Integer.parseInt(keySet[i])) == null) // if
+                // the
+                // object
+                // is
+                // not
+                // there
+                // then
+                // add
+                // it
+                {
+                    log.info("Object is null, must add it");
+                    if (keySet[i + 1].matches("[0-9]*")) // if index then array
+                        ((JSONArray) keySeriesObj).put(Integer.parseInt(keySet[i]), new JSONArray());
+                    else
+                        ((JSONArray) keySeriesObj).put(Integer.parseInt(keySet[i]), new JSONObject());
+                }
+                keySeriesObj = ((JSONArray) keySeriesObj).optJSONObject(Integer.parseInt(keySet[i]));
+
+            } else if (keySeriesObj instanceof JSONObject) {
+                if (((JSONObject) keySeriesObj).opt(keySet[i]) == null) // if
+                // the
+                // object
+                // is
+                // not
+                // there
+                // then
+                // add
+                // it
+                {
+                    if (keySet[i + 1].matches("[0-9]*")) // if index then array
+                        ((JSONObject) keySeriesObj).put(keySet[i], new JSONArray());
+                    else
+                        ((JSONObject) keySeriesObj).put(keySet[i], new JSONObject());
+                    log.info("Object is null, must add it");
+                }
+                keySeriesObj = ((JSONObject) keySeriesObj).opt(keySet[i]);
+            } else {
+                log.info("unknown object==" + keySeriesObj);
+            }
+        }
+        if ("number".equals(fieldType)) {
+            DecimalFormat df = new DecimalFormat("#.0");
+            if (value instanceof String)
+                ((JSONObject) keySeriesObj).put(keySet[keySet.length - 1],
+                        Float.valueOf(df.format(Float.valueOf((String) value))));
+            else
+                ((JSONObject) keySeriesObj).put(keySet[keySet.length - 1], Float.valueOf(df.format(value)));
+        } else if ("integer".equals(fieldType) && value instanceof String)
+            ((JSONObject) keySeriesObj).put(keySet[keySet.length - 1], Integer.valueOf((String) value));
+        else if ("JArray".equals(fieldType))
+            ((JSONArray) keySeriesObj).put(value);
+        else
+            ((JSONObject) keySeriesObj).put(keySet[keySet.length - 1], value);
+
+    }
 }
index a57ea3f..7d27399 100644 (file)
@@ -25,7 +25,10 @@ import com.att.nsa.logging.LoggingContext;
 import com.att.nsa.logging.log4j.EcompFields;\r
 import com.google.common.reflect.TypeToken;\r
 import com.google.gson.Gson;\r
+import io.vavr.collection.Map;\r
 import org.json.JSONObject;\r
+import org.onap.dcae.ApplicationSettings;\r
+import org.onap.dcae.VesApplication;\r
 import org.onap.dcae.commonFunction.event.publishing.EventPublisher;\r
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
@@ -35,37 +38,38 @@ import java.io.IOException;
 import java.lang.reflect.Method;\r
 import java.lang.reflect.Type;\r
 import java.text.SimpleDateFormat;\r
-import java.util.Arrays;\r
 import java.util.Date;\r
-import java.util.HashMap;\r
 import java.util.List;\r
-import java.util.Map;\r
 \r
-class EventProcessor implements Runnable {\r
+public class EventProcessor implements Runnable {\r
 \r
+    static final Type EVENT_LIST_TYPE = new TypeToken<List<Event>>() {\r
+    }.getType();\r
     private static final Logger log = LoggerFactory.getLogger(EventProcessor.class);\r
     private static final String EVENT_LITERAL = "event";\r
     private static final String COMMON_EVENT_HEADER = "commonEventHeader";\r
-    static final Type EVENT_LIST_TYPE = new TypeToken<List<Event>>() {}.getType();\r
     private final SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, MM dd yyyy hh:mm:ss z");\r
 \r
-    static Map<String, String[]> streamidHash = new HashMap<>();\r
     public JSONObject event;\r
     private EventPublisher eventPublisher;\r
+    private Map<String, String[]> streamidHash;\r
+    private ApplicationSettings properties;\r
 \r
-    public EventProcessor(EventPublisher eventPublisher) {\r
+\r
+    public EventProcessor(EventPublisher eventPublisher, ApplicationSettings properties) {\r
         this.eventPublisher = eventPublisher;\r
-        streamidHash = CommonStartup.streamID.toJavaMap();\r
+        this.properties = properties;\r
+        this.streamidHash = properties.dMaaPStreamsMapping();\r
     }\r
 \r
     @Override\r
     public void run() {\r
         try {\r
             while (true) {\r
-                event = CommonStartup.fProcessingInputQueue.take();\r
+                event = VesApplication.fProcessingInputQueue.take();\r
                 // As long as the producer is running we remove elements from\r
                 // the queue.\r
-                log.info("QueueSize:" + CommonStartup.fProcessingInputQueue.size() + "\tEventProcessor\tRemoving element: " + event);\r
+                log.info("QueueSize:" + VesApplication.fProcessingInputQueue.size() + "\tEventProcessor\tRemoving element: " + event);\r
 \r
                 String uuid = event.get("VESuniqueId").toString();\r
                 LoggingContext localLC = VESLogger.getLoggingContextForThread(uuid);\r
@@ -73,14 +77,12 @@ class EventProcessor implements Runnable {
 \r
                 String domain = event.getJSONObject(EVENT_LITERAL).getJSONObject(COMMON_EVENT_HEADER).getString("domain");\r
                 log.debug("event.VESuniqueId" + event.get("VESuniqueId") + "event.commonEventHeader.domain:" + domain);\r
-                String[] streamIdList = streamidHash.get(domain);\r
-                log.debug("streamIdList:" + Arrays.toString(streamIdList));\r
-\r
-                if (streamIdList.length == 0) {\r
-                    log.error("No StreamID defined for publish - Message dropped" + event);\r
-                } else {\r
-                    sendEventsToStreams(streamIdList);\r
-                }\r
+                streamidHash.get(domain)\r
+                        .onEmpty(() -> {\r
+                            log.error("No StreamID defined for publish - Message dropped" + event);\r
+                        }).forEach(streamIds -> {\r
+                    sendEventsToStreams(streamIds);\r
+                });\r
                 log.debug("Message published" + event);\r
             }\r
         } catch (InterruptedException e) {\r
@@ -93,7 +95,7 @@ class EventProcessor implements Runnable {
         // Set collector timestamp in event payload before publish\r
         addCurrentTimeToEvent(event);\r
 \r
-        if (CommonStartup.eventTransformFlag) {\r
+        if (properties.eventTransformingEnabled()) {\r
             // read the mapping json file\r
             try (FileReader fr = new FileReader("./etc/eventTransform.json")) {\r
                 log.info("parse eventTransform.json");\r
@@ -168,5 +170,4 @@ class EventProcessor implements Runnable {
             method.invoke(configProcessors, parameter);\r
         }\r
     }\r
-}\r
-\r
+}
\ No newline at end of file
index a967327..2a392e8 100644 (file)
@@ -8,9 +8,9 @@
  * 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.
@@ -35,128 +35,126 @@ import java.util.UUID;
 
 public class VESLogger {
 
-       public static final String VES_AGENT = "VES_AGENT";
-       public static final String REQUEST_ID = "requestId";
-       private static final String IP_ADDRESS = "127.0.0.1";
-       private static final String HOST_NAME = "localhost";
-
-       public static Logger auditLog;
-       public static Logger metricsLog;
-       public static Logger errorLog;
-       public static Logger debugLog;
-
-       // Common LoggingContext
-       private static LoggingContext commonLC;
-       // Thread-specific LoggingContext
-       private static LoggingContext threadLC;
-       public LoggingContext lc;
-
-       /**
-        * Returns the common LoggingContext instance that is the base context for
-        * all subsequent instances.
-        *
-        * @return the common LoggingContext
-        */
-       public static LoggingContext getCommonLoggingContext() {
-               if (commonLC == null) {
-                       commonLC = new LoggingContextFactory.Builder().build();
-                       final UUID uuid = UUID.randomUUID();
-
-                       commonLC.put(REQUEST_ID, uuid.toString());
-               }
-               return commonLC;
-       }
-
-       /**
-        * Get a logging context for the current thread that's based on the common
-        * logging context. Populate the context with context-specific values.
-        *
-        * @param aUuid
-        *            uuid for request id
-        * @return a LoggingContext for the current thread
-        */
-       public static LoggingContext getLoggingContextForThread(UUID aUuid) {
-               // note that this operation requires everything from the common context
-               // to be (re)copied into the target context. That seems slow, but it
-               // actually
-               // helps prevent the thread from overwriting supposedly common data. It
-               // also
-               // should be fairly quick compared with the overhead of handling the
-               // actual
-               // service call.
-
-               threadLC = new LoggingContextFactory.Builder().withBaseContext(getCommonLoggingContext()).build();
-               // Establish the request-specific UUID, as long as we are here...
-               threadLC.put(REQUEST_ID, aUuid.toString());
-               threadLC.put(EcompFields.kEndTimestamp, SaClock.now());
-
-               return threadLC;
-       }
-
-       /**
-        * Get a logging context for the current thread that's based on the common
-        * logging context. Populate the context with context-specific values.
-        *
-        * @param aUuid
-        *            uuid for request id
-        * @return a LoggingContext for the current thread
-        */
-       public static LoggingContext getLoggingContextForThread(String aUuid) {
-               // note that this operation requires everything from the common context
-               // to be (re)copied into the target context. That seems slow, but it
-               // actually
-               // helps prevent the thread from overwriting supposedly common data. It
-               // also
-               // should be fairly quick compared with the overhead of handling the
-               // actual
-               // service call.
-
-               threadLC = new LoggingContextFactory.Builder().withBaseContext(getCommonLoggingContext()).build();
-               // Establish the request-specific UUID, as long as we are here...
-               threadLC.put(REQUEST_ID, aUuid);
-               threadLC.put("statusCode", "COMPLETE");
-               threadLC.put(EcompFields.kEndTimestamp, SaClock.now());
-               return threadLC;
-       }
-
-       public static void setUpEcompLogging() {
-
-               // Create ECOMP Logger instances
-               auditLog = LoggerFactory.getLogger("com.att.ecomp.audit");
-               metricsLog = LoggerFactory.getLogger("com.att.ecomp.metrics");
-               debugLog = LoggerFactory.getLogger("com.att.ecomp.debug");
-               errorLog = LoggerFactory.getLogger("com.att.ecomp.error");
-
-               final LoggingContext lc = getCommonLoggingContext();
-
-               String ipAddr = IP_ADDRESS;
-               String hostname = HOST_NAME;
-               try {
-                       final InetAddress ip = InetAddress.getLocalHost();
-                       hostname = ip.getCanonicalHostName();
-                       ipAddr = ip.getHostAddress();
-               } catch (UnknownHostException x) {
-                       Log.debug(x.getMessage());
-               }
-
-               lc.put("serverName", hostname);
-               lc.put("serviceName", "VESCollecor");
-               lc.put("statusCode", "RUNNING");
-               lc.put("targetEntity", "NULL");
-               lc.put("targetServiceName", "NULL");
-               lc.put("server", hostname);
-               lc.put("serverIpAddress", ipAddr);
-
-               // instance UUID is meaningless here, so we just create a new one each
-               // time the
-               // server starts. One could argue each new instantiation of the service
-               // should
-               // have a new instance ID.
-               lc.put("instanceUuid", "");
-               lc.put("severity", "");
-               lc.put(EcompFields.kEndTimestamp, SaClock.now());
-               lc.put("EndTimestamp", SaClock.now());
-               lc.put("partnerName", "NA");
-       }
+    public static final String VES_AGENT = "VES_AGENT";
+    public static final String REQUEST_ID = "requestId";
+    private static final String IP_ADDRESS = "127.0.0.1";
+    private static final String HOST_NAME = "localhost";
+
+    public static Logger auditLog;
+    public static Logger metricsLog;
+    public static Logger errorLog;
+    public static Logger debugLog;
+
+    // Common LoggingContext
+    private static LoggingContext commonLC;
+    // Thread-specific LoggingContext
+    private static LoggingContext threadLC;
+    public LoggingContext lc;
+
+    /**
+     * Returns the common LoggingContext instance that is the base context for
+     * all subsequent instances.
+     *
+     * @return the common LoggingContext
+     */
+    public static LoggingContext getCommonLoggingContext() {
+        if (commonLC == null) {
+            commonLC = new LoggingContextFactory.Builder().build();
+            final UUID uuid = UUID.randomUUID();
+
+            commonLC.put(REQUEST_ID, uuid.toString());
+        }
+        return commonLC;
+    }
+
+    /**
+     * Get a logging context for the current thread that's based on the common
+     * logging context. Populate the context with context-specific values.
+     *
+     * @param aUuid uuid for request id
+     * @return a LoggingContext for the current thread
+     */
+    public static LoggingContext getLoggingContextForThread(UUID aUuid) {
+        // note that this operation requires everything from the common context
+        // to be (re)copied into the target context. That seems slow, but it
+        // actually
+        // helps prevent the thread from overwriting supposedly common data. It
+        // also
+        // should be fairly quick compared with the overhead of handling the
+        // actual
+        // service call.
+
+        threadLC = new LoggingContextFactory.Builder().withBaseContext(getCommonLoggingContext()).build();
+        // Establish the request-specific UUID, as long as we are here...
+        threadLC.put(REQUEST_ID, aUuid.toString());
+        threadLC.put(EcompFields.kEndTimestamp, SaClock.now());
+
+        return threadLC;
+    }
+
+    /**
+     * Get a logging context for the current thread that's based on the common
+     * logging context. Populate the context with context-specific values.
+     *
+     * @param aUuid uuid for request id
+     * @return a LoggingContext for the current thread
+     */
+    public static LoggingContext getLoggingContextForThread(String aUuid) {
+        // note that this operation requires everything from the common context
+        // to be (re)copied into the target context. That seems slow, but it
+        // actually
+        // helps prevent the thread from overwriting supposedly common data. It
+        // also
+        // should be fairly quick compared with the overhead of handling the
+        // actual
+        // service call.
+
+        threadLC = new LoggingContextFactory.Builder().withBaseContext(getCommonLoggingContext()).build();
+        // Establish the request-specific UUID, as long as we are here...
+        threadLC.put(REQUEST_ID, aUuid);
+        threadLC.put("statusCode", "COMPLETE");
+        threadLC.put(EcompFields.kEndTimestamp, SaClock.now());
+        return threadLC;
+    }
+
+    public static void setUpEcompLogging() {
+
+        // Create ECOMP Logger instances
+        auditLog = LoggerFactory.getLogger("com.att.ecomp.audit");
+        metricsLog = LoggerFactory.getLogger("com.att.ecomp.metrics");
+        debugLog = LoggerFactory.getLogger("com.att.ecomp.debug");
+        errorLog = LoggerFactory.getLogger("com.att.ecomp.error");
+
+        final LoggingContext lc = getCommonLoggingContext();
+
+        String ipAddr = IP_ADDRESS;
+        String hostname = HOST_NAME;
+        try {
+            final InetAddress ip = InetAddress.getLocalHost();
+            hostname = ip.getCanonicalHostName();
+            ipAddr = ip.getHostAddress();
+        } catch (UnknownHostException x) {
+            Log.debug(x.getMessage());
+        }
+
+        lc.put("serverName", hostname);
+        lc.put("serviceName", "VESCollecor");
+        lc.put("statusCode", "RUNNING");
+        lc.put("targetEntity", "NULL");
+        lc.put("targetServiceName", "NULL");
+        lc.put("server", hostname);
+        lc.put("serverIpAddress", ipAddr);
+
+        // instance UUID is meaningless here, so we just create a new one each
+        // time the
+        // server starts. One could argue each new instantiation of the service
+        // should
+        // have a new instance ID.
+        lc.put("instanceUuid", "");
+        lc.put("severity", "");
+        lc.put(EcompFields.kEndTimestamp, SaClock.now());
+        lc.put("EndTimestamp", SaClock.now());
+        lc.put("partnerName", "NA");
+    }
 
 }
index 5865b12..179e882 100644 (file)
  */
 package org.onap.dcae.commonFunction.event.publishing;
 
-import static io.vavr.API.List;
-import static io.vavr.API.Try;
-import static io.vavr.API.Tuple;
-import static io.vavr.API.unchecked;
-import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.enhanceError;
-import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.f;
-
 import io.vavr.collection.List;
 import io.vavr.collection.Map;
 import io.vavr.control.Option;
 import io.vavr.control.Try;
+import org.onap.dcae.commonFunction.AnyNode;
+
 import java.net.URL;
 import java.nio.file.Files;
 import java.nio.file.Path;
-import org.onap.dcae.commonFunction.AnyNode;
+
+import static io.vavr.API.*;
+import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.enhanceError;
+import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.f;
 
 /**
  * @author Pawel Szalapski (pawel.szalapski@nokia.com)
@@ -43,23 +41,23 @@ public final class DMaaPConfigurationParser {
 
     public static Try<Map<String, PublisherConfig>> parseToDomainMapping(Path configLocation) {
         return readFromFile(configLocation)
-            .flatMap(DMaaPConfigurationParser::toJSON)
-            .flatMap(DMaaPConfigurationParser::toConfigMap);
+                .flatMap(DMaaPConfigurationParser::toJSON)
+                .flatMap(DMaaPConfigurationParser::toConfigMap);
     }
 
     private static Try<String> readFromFile(Path configLocation) {
         return Try(() -> new String(Files.readAllBytes(configLocation)))
-            .mapFailure(enhanceError(f("Could not read DMaaP configuration from location: '%s'", configLocation)));
+                .mapFailure(enhanceError(f("Could not read DMaaP configuration from location: '%s'", configLocation)));
     }
 
     private static Try<AnyNode> toJSON(String config) {
         return Try(() -> AnyNode.fromString(config))
-            .mapFailure(enhanceError(f("DMaaP configuration '%s' is not a valid JSON document", config)));
+                .mapFailure(enhanceError(f("DMaaP configuration '%s' is not a valid JSON document", config)));
     }
 
     private static Try<Map<String, PublisherConfig>> toConfigMap(AnyNode config) {
         return Try(() -> usesLegacyFormat(config) ? parseLegacyFormat(config) : parseNewFormat(config))
-            .mapFailure(enhanceError(f("Parsing DMaaP configuration: '%s' failed, probably it is in unexpected format", config)));
+                .mapFailure(enhanceError(f("Parsing DMaaP configuration: '%s' failed, probably it is in unexpected format", config)));
     }
 
     private static boolean usesLegacyFormat(AnyNode dMaaPConfig) {
@@ -68,40 +66,40 @@ public final class DMaaPConfigurationParser {
 
     private static Map<String, PublisherConfig> parseLegacyFormat(AnyNode root) {
         return root.get("channels").toList().toMap(
-            channel -> channel.get("name").toString(),
-            channel -> {
-                String destinationsStr = channel.getAsOption("cambria.url")
-                    .getOrElse(channel.getAsOption("cambria.hosts").get())
-                    .toString();
-                String topic = channel.get("cambria.topic").toString();
-                Option<String> maybeUser = channel.getAsOption("basicAuthUsername").map(AnyNode::toString);
-                Option<String> maybePassword = channel.getAsOption("basicAuthPassword").map(AnyNode::toString);
-                List<String> destinations = List(destinationsStr.split(","));
-                return buildBasedOnAuth(maybeUser, maybePassword, topic, destinations);
-            });
+                channel -> channel.get("name").toString(),
+                channel -> {
+                    String destinationsStr = channel.getAsOption("cambria.url")
+                            .getOrElse(channel.getAsOption("cambria.hosts").get())
+                            .toString();
+                    String topic = channel.get("cambria.topic").toString();
+                    Option<String> maybeUser = channel.getAsOption("basicAuthUsername").map(AnyNode::toString);
+                    Option<String> maybePassword = channel.getAsOption("basicAuthPassword").map(AnyNode::toString);
+                    List<String> destinations = List(destinationsStr.split(","));
+                    return buildBasedOnAuth(maybeUser, maybePassword, topic, destinations);
+                });
     }
 
     private static Map<String, PublisherConfig> parseNewFormat(AnyNode root) {
         return root.keys().toMap(
-            channelName -> channelName,
-            channelName -> {
-                AnyNode channelConfig = root.get(channelName);
-                Option<String> maybeUser = channelConfig.getAsOption("aaf_username").map(AnyNode::toString);
-                Option<String> maybePassword = channelConfig.getAsOption("aaf_password").map(AnyNode::toString);
-                URL topicURL = unchecked(
-                    () -> new URL(channelConfig.get("dmaap_info").get("topic_url").toString())).apply();
-                String[] pathSegments = topicURL.getPath().substring(1).split("/");
-                String topic = pathSegments[1];
-                String destination = "events".equals(pathSegments[0]) ? topicURL.getAuthority() : topicURL.getHost();
-                List<String> destinations = List(destination);
-                return buildBasedOnAuth(maybeUser, maybePassword, topic, destinations);
-            });
+                channelName -> channelName,
+                channelName -> {
+                    AnyNode channelConfig = root.get(channelName);
+                    Option<String> maybeUser = channelConfig.getAsOption("aaf_username").map(AnyNode::toString);
+                    Option<String> maybePassword = channelConfig.getAsOption("aaf_password").map(AnyNode::toString);
+                    URL topicURL = unchecked(
+                            () -> new URL(channelConfig.get("dmaap_info").get("topic_url").toString())).apply();
+                    String[] pathSegments = topicURL.getPath().substring(1).split("/");
+                    String topic = pathSegments[1];
+                    String destination = "events".equals(pathSegments[0]) ? topicURL.getAuthority() : topicURL.getHost();
+                    List<String> destinations = List(destination);
+                    return buildBasedOnAuth(maybeUser, maybePassword, topic, destinations);
+                });
     }
 
     private static PublisherConfig buildBasedOnAuth(Option<String> maybeUser, Option<String> maybePassword,
                                                     String topic, List<String> destinations) {
         return maybeUser.flatMap(user -> maybePassword.map(password -> Tuple(user, password)))
-            .map(credentials -> new PublisherConfig(destinations, topic, credentials._1, credentials._2))
-            .getOrElse(new PublisherConfig(destinations, topic));
+                .map(credentials -> new PublisherConfig(destinations, topic, credentials._1, credentials._2))
+                .getOrElse(new PublisherConfig(destinations, topic));
     }
 }
index fd9b3ae..a0ee3bf 100644 (file)
 
 package org.onap.dcae.commonFunction.event.publishing;
 
-import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.f;
-
 import com.att.nsa.cambria.client.CambriaBatchingPublisher;
 import com.att.nsa.clock.SaClock;
 import com.att.nsa.logging.LoggingContext;
 import com.att.nsa.logging.log4j.EcompFields;
 import io.vavr.collection.Map;
 import io.vavr.control.Try;
-import java.io.IOException;
 import org.json.JSONObject;
 import org.onap.dcae.commonFunction.VESLogger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.IOException;
+
+import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.f;
+
 /**
  * @author Pawel Szalapski (pawel.szalapski@nokia.com)
  */
@@ -55,9 +56,9 @@ class DMaaPEventPublisher implements EventPublisher {
     public void sendEvent(JSONObject event, String domain) {
         clearVesUniqueIdFromEvent(event);
         publishersCache.getPublisher(domain)
-            .onEmpty(() ->
-                log.warn(f("Could not find event publisher for domain: '%s', dropping message: '%s'", domain, event)))
-            .forEach(publisher -> sendEvent(event, domain, publisher));
+                .onEmpty(() ->
+                        log.warn(f("Could not find event publisher for domain: '%s', dropping message: '%s'", domain, event)))
+                .forEach(publisher -> sendEvent(event, domain, publisher));
     }
 
     @Override
@@ -67,11 +68,11 @@ class DMaaPEventPublisher implements EventPublisher {
 
     private void sendEvent(JSONObject event, String domain, CambriaBatchingPublisher publisher) {
         Try.run(() -> uncheckedSendEvent(event, domain, publisher))
-            .onFailure(exc -> closePublisher(event, domain, exc));
+                .onFailure(exc -> closePublisher(event, domain, exc));
     }
 
     private void uncheckedSendEvent(JSONObject event, String domain, CambriaBatchingPublisher publisher)
-        throws IOException {
+            throws IOException {
         int pendingMsgs = publisher.send("MyPartitionKey", event.toString());
         if (pendingMsgs > PENDING_MESSAGE_LOG_THRESHOLD) {
             log.info("Pending messages count: " + pendingMsgs);
@@ -83,7 +84,7 @@ class DMaaPEventPublisher implements EventPublisher {
 
     private void closePublisher(JSONObject event, String domain, Throwable e) {
         log.error(f("Unable to schedule event: '%s' on domain: '%s'. Closing publisher and dropping message.",
-            event, domain), e);
+                event, domain), e);
         publishersCache.closePublisherFor(domain);
     }
 
index a7865a4..489fcbf 100644 (file)
  */
 package org.onap.dcae.commonFunction.event.publishing;
 
-import static io.vavr.API.Try;
-import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.enhanceError;
-import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.f;
-
 import com.att.nsa.cambria.client.CambriaBatchingPublisher;
 import com.att.nsa.cambria.client.CambriaClientBuilders;
 import com.att.nsa.cambria.client.CambriaClientBuilders.PublisherBuilder;
 import io.vavr.control.Try;
 
+import static io.vavr.API.Try;
+import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.enhanceError;
+import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.f;
+
 /**
  * @author Pawel Szalapski (pawel.szalapski@nokia.com)
  */
@@ -36,7 +36,7 @@ final class DMaaPPublishersBuilder {
     @SuppressWarnings("mapFailure takes a generic varargs, unchecked because of Javas type system limitation, actually safe to do")
     static Try<CambriaBatchingPublisher> buildPublisher(PublisherConfig config) {
         return Try(() -> builder(config).build())
-            .mapFailure(enhanceError(f("DMaaP client builder throws exception for this configuration: '%s'", config)));
+                .mapFailure(enhanceError(f("DMaaP client builder throws exception for this configuration: '%s'", config)));
     }
 
     private static PublisherBuilder builder(PublisherConfig config) {
@@ -49,14 +49,14 @@ final class DMaaPPublishersBuilder {
 
     private static PublisherBuilder authenticatedBuilder(PublisherConfig config) {
         return unAuthenticatedBuilder(config)
-            .usingHttps()
-            .authenticatedByHttp(config.userName().get(), config.password().get());
+                .usingHttps()
+                .authenticatedByHttp(config.userName().get(), config.password().get());
     }
 
     private static PublisherBuilder unAuthenticatedBuilder(PublisherConfig config) {
         return new CambriaClientBuilders.PublisherBuilder()
-            .usingHosts(config.destinations().mkString(","))
-            .onTopic(config.topic())
-            .logSendFailuresAfter(5);
+                .usingHosts(config.destinations().mkString(","))
+                .onTopic(config.topic())
+                .logSendFailuresAfter(5);
     }
 }
index 102d277..4cdf92d 100644 (file)
  */
 package org.onap.dcae.commonFunction.event.publishing;
 
-import static io.vavr.API.Option;
-import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.f;
-
 import com.att.nsa.cambria.client.CambriaBatchingPublisher;
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
-import com.google.common.cache.RemovalListener;
-import com.google.common.cache.RemovalNotification;
+import com.google.common.cache.*;
 import io.vavr.collection.Map;
 import io.vavr.control.Option;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
 import java.io.IOException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicReference;
-import javax.annotation.Nonnull;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+
+import static io.vavr.API.Option;
+import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.f;
 
 /**
  * @author Pawel Szalapski (pawel.szalapski@nokia.com)
@@ -51,8 +47,8 @@ class DMaaPPublishersCache {
     DMaaPPublishersCache(Map<String, PublisherConfig> dMaaPConfiguration) {
         this.dMaaPConfiguration = new AtomicReference<>(dMaaPConfiguration);
         this.publishersCache = CacheBuilder.newBuilder()
-            .removalListener(new OnPublisherRemovalListener())
-            .build(new CambriaPublishersCacheLoader());
+                .removalListener(new OnPublisherRemovalListener())
+                .build(new CambriaPublishersCacheLoader());
     }
 
     DMaaPPublishersCache(CambriaPublishersCacheLoader dMaaPPublishersCacheLoader,
@@ -60,8 +56,8 @@ class DMaaPPublishersCache {
                          Map<String, PublisherConfig> dMaaPConfiguration) {
         this.dMaaPConfiguration = new AtomicReference<>(dMaaPConfiguration);
         this.publishersCache = CacheBuilder.newBuilder()
-            .removalListener(onPublisherRemovalListener)
-            .build(dMaaPPublishersCacheLoader);
+                .removalListener(onPublisherRemovalListener)
+                .build(dMaaPPublishersCacheLoader);
     }
 
     Option<CambriaBatchingPublisher> getPublisher(String streamID) {
@@ -80,9 +76,9 @@ class DMaaPPublishersCache {
     synchronized void reconfigure(Map<String, PublisherConfig> newConfig) {
         Map<String, PublisherConfig> currentConfig = dMaaPConfiguration.get();
         Map<String, PublisherConfig> removedConfigurations = currentConfig
-            .filterKeys(domain -> !newConfig.containsKey(domain));
+                .filterKeys(domain -> !newConfig.containsKey(domain));
         Map<String, PublisherConfig> changedConfigurations = newConfig
-            .filterKeys(e -> currentConfig.containsKey(e) && !currentConfig.get(e).equals(newConfig.get(e)));
+                .filterKeys(e -> currentConfig.containsKey(e) && !currentConfig.get(e).equals(newConfig.get(e)));
         dMaaPConfiguration.set(newConfig);
         removedConfigurations.merge(changedConfigurations).forEach(e -> publishersCache.invalidate(e._1));
     }
@@ -99,7 +95,7 @@ class DMaaPPublishersCache {
                     java.util.List<?> stuck = publisher.close(timeout, unit);
                     if (!stuck.isEmpty()) {
                         log.error(f("Publisher got stuck and did not manage to close in '%s' '%s', "
-                            + "%s messages were dropped", stuck.size(), timeout, unit));
+                                + "%s messages were dropped", stuck.size(), timeout, unit));
                     }
                 } catch (InterruptedException | IOException e) {
                     log.error("Could not close Cambria publisher, some messages might have been dropped", e);
@@ -113,11 +109,11 @@ class DMaaPPublishersCache {
         @Override
         public CambriaBatchingPublisher load(@Nonnull String domain) {
             return dMaaPConfiguration.get()
-                .get(domain)
-                .toTry(() -> new RuntimeException(
-                    f("DMaaP configuration contains no configuration for domain: '%s'", domain)))
-                .flatMap(DMaaPPublishersBuilder::buildPublisher)
-                .get();
+                    .get(domain)
+                    .toTry(() -> new RuntimeException(
+                            f("DMaaP configuration contains no configuration for domain: '%s'", domain)))
+                    .flatMap(DMaaPPublishersBuilder::buildPublisher)
+                    .get();
         }
     }
 
index 4a05677..f1cbb8e 100644 (file)
@@ -21,6 +21,7 @@ package org.onap.dcae.commonFunction.event.publishing;
 
 import io.vavr.collection.List;
 import io.vavr.control.Option;
+
 import java.util.Objects;
 
 /**
@@ -76,9 +77,9 @@ public final class PublisherConfig {
         }
         PublisherConfig that = (PublisherConfig) o;
         return Objects.equals(destinations, that.destinations) &&
-            Objects.equals(topic, that.topic) &&
-            Objects.equals(userName, that.userName) &&
-            Objects.equals(password, that.password);
+                Objects.equals(topic, that.topic) &&
+                Objects.equals(userName, that.userName) &&
+                Objects.equals(password, that.password);
     }
 
     @Override
@@ -89,10 +90,10 @@ public final class PublisherConfig {
     @Override
     public String toString() {
         return "PublisherConfig{" +
-            "destinations=" + destinations +
-            ", topic='" + topic + '\'' +
-            ", userName='" + userName + '\'' +
-            ", password='" + password + '\'' +
-            '}';
+                "destinations=" + destinations +
+                ", topic='" + topic + '\'' +
+                ", userName='" + userName + '\'' +
+                ", password='" + password + '\'' +
+                '}';
     }
 }
index 9bf3ef8..78f34ff 100644 (file)
  */
 package org.onap.dcae.commonFunction.event.publishing;
 
-import static io.vavr.API.$;
-
 import io.vavr.API;
 import io.vavr.API.Match.Case;
 
+import static io.vavr.API.$;
+
 /**
  * @author Pawel Szalapski (pawel.szalapski@nokia.com)
  */
index 99e269c..ed42a5a 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  * You may obtain a copy of the License at\r
- * \r
+ *\r
  *      http://www.apache.org/licenses/LICENSE-2.0\r
- * \r
+ *\r
  * Unless required by applicable law or agreed to in writing, software\r
  * distributed under the License is distributed on an "AS IS" BASIS,\r
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
 \r
 package org.onap.dcae.controller;\r
 \r
+import com.fasterxml.jackson.databind.JsonNode;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
 import org.json.JSONArray;\r
 import org.json.JSONObject;\r
 import org.json.JSONTokener;\r
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
 \r
-import com.fasterxml.jackson.databind.JsonNode;\r
-import com.fasterxml.jackson.databind.ObjectMapper;\r
-import java.io.BufferedReader;\r
-import java.io.File;\r
-import java.io.FileWriter;\r
-import java.io.IOException;\r
-import java.io.InputStreamReader;\r
+import java.io.*;\r
 import java.util.Map;\r
 \r
 public class FetchDynamicConfig {\r
 \r
-       private static final Logger log = LoggerFactory.getLogger(FetchDynamicConfig.class);\r
-\r
-       public static String configFile = "/opt/app/KV-Configuration.json";\r
-       private static String url;\r
-       public static String retString;\r
-       public static String retCBSString;\r
-       private static Map<String, String> env;\r
+    private static final Logger log = LoggerFactory.getLogger(FetchDynamicConfig.class);\r
+\r
+    public static String configFile = "/opt/app/KV-Configuration.json";\r
+    public static String retString;\r
+    public static String retCBSString;\r
+    private static String url;\r
+    private static Map<String, String> env;\r
+\r
+    public FetchDynamicConfig() {\r
+    }\r
+\r
+    public static void main(String[] args) {\r
+        Boolean areEqual;\r
+        // Call consul api and identify the CBS Service address and port\r
+        getconsul();\r
+        // Construct and invoke CBS API to get application Configuration\r
+        getCBS();\r
+        // Verify if data has changed\r
+        areEqual = verifyConfigChange();\r
+        // If new config then write data returned into configFile for\r
+        // LoadDynamicConfig process\r
+        if (!areEqual) {\r
+            FetchDynamicConfig fc = new FetchDynamicConfig();\r
+            fc.writefile(retCBSString);\r
+        } else {\r
+            log.info("New config pull results identical -  " + configFile + " NOT refreshed");\r
+        }\r
+    }\r
+\r
+    private static void getconsul() {\r
+\r
+        env = System.getenv();\r
+        for (Map.Entry<String, String> entry : env.entrySet()) {\r
+            log.info(entry.getKey() + ":" + entry.getValue());\r
+        }\r
+\r
+        if (env.containsKey("CONSUL_HOST") && env.containsKey("CONFIG_BINDING_SERVICE")) {\r
+            // && env.containsKey("HOSTNAME")) {\r
+            log.info(">>>Dynamic configuration to be fetched from ConfigBindingService");\r
+            url = env.get("CONSUL_HOST") + ":8500/v1/catalog/service/" + env.get("CONFIG_BINDING_SERVICE");\r
+\r
+            retString = executecurl(url);\r
+\r
+        } else {\r
+            log.info(">>>Static configuration to be used");\r
+        }\r
+\r
+    }\r
+\r
+    public static boolean verifyConfigChange() {\r
+\r
+        boolean areEqual = false;\r
+        // Read current data\r
+        try {\r
+            File f = new File(configFile);\r
+            if (f.exists() && !f.isDirectory()) {\r
+\r
+                String jsonData = LoadDynamicConfig.readFile(configFile);\r
+                JSONObject jsonObject = new JSONObject(jsonData);\r
+\r
+                ObjectMapper mapper = new ObjectMapper();\r
+\r
+                JsonNode tree1 = mapper.readTree(jsonObject.toString());\r
+                JsonNode tree2 = mapper.readTree(retCBSString);\r
+                areEqual = tree1.equals(tree2);\r
+                log.info("Comparison value:" + areEqual);\r
+            } else {\r
+                log.info("First time config file read: " + configFile);\r
+            }\r
+\r
+        } catch (IOException e) {\r
+            log.error("Comparison with new fetched data failed" + e.getMessage());\r
+\r
+        }\r
+\r
+        return areEqual;\r
+\r
+    }\r
+\r
+    public static void getCBS() {\r
+\r
+        env = System.getenv();\r
+        // consul return as array\r
+        JSONTokener temp = new JSONTokener(retString);\r
+        JSONObject cbsjobj = (JSONObject) new JSONArray(temp).get(0);\r
+\r
+        String urlPart1 = null;\r
+        if (cbsjobj.has("ServiceAddress") && cbsjobj.has("ServicePort")) {\r
+            urlPart1 = cbsjobj.getString("ServiceAddress") + ":" + cbsjobj.getInt("ServicePort");\r
+        }\r
+\r
+        log.info("CONFIG_BINDING_SERVICE DNS RESOLVED:" + urlPart1);\r
+\r
+        if (env.containsKey("HOSTNAME")) {\r
+            url = urlPart1 + "/service_component/" + env.get("HOSTNAME");\r
+            retCBSString = executecurl(url);\r
+        } else if (env.containsKey("SERVICE_NAME")) {\r
+            url = urlPart1 + "/service_component/" + env.get("SERVICE_NAME");\r
+            retCBSString = executecurl(url);\r
+        } else {\r
+            log.error("Service name environment variable - HOSTNAME/SERVICE_NAME not found within container ");\r
+        }\r
+\r
+    }\r
+\r
+    private static String executecurl(String url) {\r
+\r
+        String[] command = {"curl", "-v", url};\r
+        ProcessBuilder process = new ProcessBuilder(command);\r
+        Process p;\r
+        String result = null;\r
+        try {\r
+            p = process.start();\r
+            InputStreamReader ipr = new InputStreamReader(p.getInputStream());\r
+            BufferedReader reader = new BufferedReader(ipr);\r
+            StringBuilder builder = new StringBuilder();\r
+            String line;\r
+\r
+            while ((line = reader.readLine()) != null) {\r
+                builder.append(line);\r
+            }\r
+            result = builder.toString();\r
+            log.info(result);\r
 \r
-       public FetchDynamicConfig() {\r
-       }\r
+            reader.close();\r
+            ipr.close();\r
+        } catch (IOException e) {\r
+            log.error("error", e);\r
+            e.printStackTrace();\r
+        }\r
+        return result;\r
 \r
-       public static void main(String[] args) {\r
-               Boolean areEqual;\r
-               // Call consul api and identify the CBS Service address and port\r
-               getconsul();\r
-               // Construct and invoke CBS API to get application Configuration\r
-               getCBS();\r
-               // Verify if data has changed\r
-               areEqual = verifyConfigChange();\r
-               // If new config then write data returned into configFile for\r
-               // LoadDynamicConfig process\r
-               if (! areEqual) {\r
-                       FetchDynamicConfig fc = new FetchDynamicConfig();\r
-                       fc.writefile(retCBSString);\r
-               } else {\r
-                       log.info("New config pull results identical -  " + configFile + " NOT refreshed");\r
-               }\r
-       }\r
+    }\r
 \r
-       private static void getconsul() {\r
-\r
-               env = System.getenv();\r
-               for (Map.Entry<String, String> entry : env.entrySet()) {\r
-                       log.info(entry.getKey() + ":" + entry.getValue());\r
-               }\r
-\r
-               if (env.containsKey("CONSUL_HOST") && env.containsKey("CONFIG_BINDING_SERVICE")) {\r
-                       // && env.containsKey("HOSTNAME")) {\r
-                       log.info(">>>Dynamic configuration to be fetched from ConfigBindingService");\r
-                       url = env.get("CONSUL_HOST") + ":8500/v1/catalog/service/" + env.get("CONFIG_BINDING_SERVICE");\r
-\r
-                       retString = executecurl(url);\r
-\r
-               } else {\r
-                       log.info(">>>Static configuration to be used");\r
-               }\r
-\r
-       }\r
-\r
-       public static boolean verifyConfigChange() {\r
-\r
-               boolean areEqual = false;\r
-               // Read current data\r
-               try {\r
-                       File f = new File(configFile);\r
-                       if (f.exists() && !f.isDirectory()) {\r
-\r
-                               String jsonData = LoadDynamicConfig.readFile(configFile);\r
-                               JSONObject jsonObject = new JSONObject(jsonData);\r
-\r
-                               ObjectMapper mapper = new ObjectMapper();\r
-\r
-                               JsonNode tree1 = mapper.readTree(jsonObject.toString());\r
-                               JsonNode tree2 = mapper.readTree(retCBSString);\r
-                               areEqual = tree1.equals(tree2);\r
-                               log.info("Comparison value:" + areEqual);\r
-                       } else {\r
-                               log.info("First time config file read: " + configFile);\r
-                       }\r
-\r
-               } catch (IOException e) {\r
-                       log.error("Comparison with new fetched data failed" + e.getMessage());\r
-\r
-               }\r
-\r
-               return areEqual;\r
-\r
-       }\r
-\r
-       public static void getCBS() {\r
-\r
-               env = System.getenv();\r
-               // consul return as array\r
-               JSONTokener temp = new JSONTokener(retString);\r
-               JSONObject cbsjobj = (JSONObject) new JSONArray(temp).get(0);\r
-\r
-               String urlPart1 = null;\r
-               if (cbsjobj.has("ServiceAddress") && cbsjobj.has("ServicePort")) {\r
-                       urlPart1 = cbsjobj.getString("ServiceAddress") + ":" + cbsjobj.getInt("ServicePort");\r
-               }\r
-\r
-               log.info("CONFIG_BINDING_SERVICE DNS RESOLVED:" + urlPart1);\r
-\r
-               if (env.containsKey("HOSTNAME")) {\r
-                       url = urlPart1 + "/service_component/" + env.get("HOSTNAME");\r
-                       retCBSString = executecurl(url);\r
-               } else if (env.containsKey("SERVICE_NAME")) {\r
-                       url = urlPart1 + "/service_component/" + env.get("SERVICE_NAME");\r
-                       retCBSString = executecurl(url);\r
-               } else {\r
-                       log.error("Service name environment variable - HOSTNAME/SERVICE_NAME not found within container ");\r
-               }\r
-\r
-       }\r
-\r
-       public void writefile(String retCBSString) {\r
-               log.info("URL to fetch configuration:" + url + " Return String:" + retCBSString);\r
-\r
-               String indentedretstring = (new JSONObject(retCBSString)).toString(4);\r
-\r
-               try (FileWriter file = new FileWriter(FetchDynamicConfig.configFile)) {\r
-                       file.write(indentedretstring);\r
-\r
-                       log.info("Successfully Copied JSON Object to file " + configFile);\r
-               } catch (IOException e) {\r
-                       log.error("Error in writing configuration into file " + configFile + retString + e.getMessage());\r
-                       e.printStackTrace();\r
-               }\r
-\r
-       }\r
-\r
-       private static String executecurl(String url) {\r
+    public void writefile(String retCBSString) {\r
+        log.info("URL to fetch configuration:" + url + " Return String:" + retCBSString);\r
 \r
-               String[] command = { "curl", "-v", url };\r
-               ProcessBuilder process = new ProcessBuilder(command);\r
-               Process p;\r
-               String result = null;\r
-               try {\r
-                       p = process.start();\r
-                       InputStreamReader ipr = new InputStreamReader(p.getInputStream());\r
-                       BufferedReader reader = new BufferedReader(ipr);\r
-                       StringBuilder builder = new StringBuilder();\r
-                       String line;\r
+        String indentedretstring = (new JSONObject(retCBSString)).toString(4);\r
 \r
-                       while ((line = reader.readLine()) != null) {\r
-                               builder.append(line);\r
-                       }\r
-                       result = builder.toString();\r
-                       log.info(result);\r
+        try (FileWriter file = new FileWriter(FetchDynamicConfig.configFile)) {\r
+            file.write(indentedretstring);\r
 \r
-                       reader.close();\r
-                       ipr.close();\r
-               } catch (IOException e) {\r
-                       log.error("error", e);\r
-                       e.printStackTrace();\r
-               }\r
-               return result;\r
+            log.info("Successfully Copied JSON Object to file " + configFile);\r
+        } catch (IOException e) {\r
+            log.error("Error in writing configuration into file " + configFile + retString + e.getMessage());\r
+            e.printStackTrace();\r
+        }\r
 \r
-       }\r
+    }\r
 \r
 }\r
index a8ecaba..c1ab80c 100644 (file)
@@ -7,9 +7,9 @@
  * 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.
@@ -35,95 +35,94 @@ import java.util.Map;
 
 public class LoadDynamicConfig {
 
-       private static final Logger log = LoggerFactory.getLogger(LoadDynamicConfig.class);
-
-       public String propFile = "collector.properties";
-       public String configFile = "/opt/app/KV-Configuration.json";
-       public String dMaaPOutputFile = "./etc/DmaapConfig.json";
-
-       public LoadDynamicConfig() {
-
-       }
-
-       public static void main(String[] args) {
-               Map<String, String> env = System.getenv();
-
-               // Check again to ensure new controller deployment related config
-               if (env.containsKey("CONSUL_HOST") && env.containsKey("CONFIG_BINDING_SERVICE")
-                               && env.containsKey("HOSTNAME")) {
-
-                       try {
-
-                               LoadDynamicConfig lc = new LoadDynamicConfig();
-                               String jsonData = readFile(lc.configFile);
-                               JSONObject jsonObject = new JSONObject(jsonData);
-                               lc.writeconfig(jsonObject);
-               
-
-                       } catch (Exception e) {
-                               log.error(e.getLocalizedMessage(), e);
-                               e.printStackTrace();
-
-                       }
-
-               } else {
-                       log.info(">>>Static configuration to be used");
-               }
-
-       }
-       
-       public void writeconfig (JSONObject jsonObject)
-       {
-               
-               PropertiesConfiguration conf;
-               try {
-                       conf = new PropertiesConfiguration(propFile);
-       
-               conf.setEncoding(null);
-
-               // update properties based on consul dynamic configuration
-               Iterator<?> keys = jsonObject.keys();
-
-               while (keys.hasNext()) {
-                       String key = (String) keys.next();
-                       // check if any configuration is related to dmaap
-                       // and write into dmaapconfig.json
-                       if (key.startsWith("streams_publishes")) {
-                               // VESCollector only have publish streams
-                               try (FileWriter file = new FileWriter(dMaaPOutputFile)) {
-                                       String indentedretstring=(new JSONObject(jsonObject.get(key).toString())).toString(4);
-                                       file.write(indentedretstring);
-                                       log.info("Successfully written JSON Object to DmaapConfig.json");
-                               } catch (IOException e) {
-                                       log.info("Error in writing dmaap configuration into DmaapConfig.json", e);
-                               }
-                       } else {
-                               conf.setProperty(key, jsonObject.get(key).toString());
-                       }
-
-               }
-               conf.save();
-               } catch (ConfigurationException e) {
-                       log.error(e.getLocalizedMessage(), e);
-                       e.printStackTrace();
-               }
-       }
-
-       public static String readFile(String filename) {
-               String result = "";
-               try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
-                       StringBuilder sb = new StringBuilder();
-                       String line = br.readLine();
-                       while (line != null) {
-                               sb.append(line);
-                               line = br.readLine();
-                       }
-                       result = sb.toString();
-               } catch (Exception e) {
-                       log.error(e.getLocalizedMessage(), e);
-                       e.printStackTrace();
-               }
-               return result;
-       }
+    private static final Logger log = LoggerFactory.getLogger(LoadDynamicConfig.class);
+
+    public String propFile = "collector.properties";
+    public String configFile = "/opt/app/KV-Configuration.json";
+    public String dMaaPOutputFile = "./etc/DmaapConfig.json";
+
+    public LoadDynamicConfig() {
+
+    }
+
+    public static void main(String[] args) {
+        Map<String, String> env = System.getenv();
+
+        // Check again to ensure new controller deployment related config
+        if (env.containsKey("CONSUL_HOST") && env.containsKey("CONFIG_BINDING_SERVICE")
+                && env.containsKey("HOSTNAME")) {
+
+            try {
+
+                LoadDynamicConfig lc = new LoadDynamicConfig();
+                String jsonData = readFile(lc.configFile);
+                JSONObject jsonObject = new JSONObject(jsonData);
+                lc.writeconfig(jsonObject);
+
+
+            } catch (Exception e) {
+                log.error(e.getLocalizedMessage(), e);
+                e.printStackTrace();
+
+            }
+
+        } else {
+            log.info(">>>Static configuration to be used");
+        }
+
+    }
+
+    public static String readFile(String filename) {
+        String result = "";
+        try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
+            StringBuilder sb = new StringBuilder();
+            String line = br.readLine();
+            while (line != null) {
+                sb.append(line);
+                line = br.readLine();
+            }
+            result = sb.toString();
+        } catch (Exception e) {
+            log.error(e.getLocalizedMessage(), e);
+            e.printStackTrace();
+        }
+        return result;
+    }
+
+    public void writeconfig(JSONObject jsonObject) {
+
+        PropertiesConfiguration conf;
+        try {
+            conf = new PropertiesConfiguration(propFile);
+
+            conf.setEncoding(null);
+
+            // update properties based on consul dynamic configuration
+            Iterator<?> keys = jsonObject.keys();
+
+            while (keys.hasNext()) {
+                String key = (String) keys.next();
+                // check if any configuration is related to dmaap
+                // and write into dmaapconfig.json
+                if (key.startsWith("streams_publishes")) {
+                    // VESCollector only have publish streams
+                    try (FileWriter file = new FileWriter(dMaaPOutputFile)) {
+                        String indentedretstring = (new JSONObject(jsonObject.get(key).toString())).toString(4);
+                        file.write(indentedretstring);
+                        log.info("Successfully written JSON Object to DmaapConfig.json");
+                    } catch (IOException e) {
+                        log.info("Error in writing dmaap configuration into DmaapConfig.json", e);
+                    }
+                } else {
+                    conf.setProperty(key, jsonObject.get(key).toString());
+                }
+
+            }
+            conf.save();
+        } catch (ConfigurationException e) {
+            log.error(e.getLocalizedMessage(), e);
+            e.printStackTrace();
+        }
+    }
 
 }
diff --git a/src/main/java/org/onap/dcae/restapi/ApiAuthInterceptor.java b/src/main/java/org/onap/dcae/restapi/ApiAuthInterceptor.java
new file mode 100644 (file)
index 0000000..864a16d
--- /dev/null
@@ -0,0 +1,78 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dcaegen2.collectors.ves
+ * ================================================================================
+ * Copyright (C) 2018 Nokia. All rights reserved.
+ * ================================================================================
+ * 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.dcae.restapi;
+
+import io.vavr.control.Option;
+import org.onap.dcae.ApplicationSettings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.Base64;
+
+final class ApiAuthInterceptor extends HandlerInterceptorAdapter {
+
+    private static final Logger LOG = LoggerFactory.getLogger(ApiAuthInterceptor.class);
+    private final ApplicationSettings applicationSettings;
+
+    private Logger errorLog;
+
+    ApiAuthInterceptor(ApplicationSettings applicationSettings, Logger errorLog) {
+        this.applicationSettings = applicationSettings;
+        this.errorLog = errorLog;
+    }
+
+    @Override
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
+                             Object handler) throws IOException {
+        if (applicationSettings.authorizationEnabled()) {
+            String authorizationHeader = request.getHeader("Authorization");
+            if (authorizationHeader == null || !isAuthorized(authorizationHeader)) {
+                response.setStatus(400);
+                errorLog.error("EVENT_RECEIPT_FAILURE: Unauthorized user");
+                response.getWriter().write(ApiException.UNAUTHORIZED_USER.toJSON().toString());
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private boolean isAuthorized(String authorizationHeader) {
+        try  {
+            String encodedData = authorizationHeader.split(" ")[1];
+            String decodedData = new String(Base64.getDecoder().decode(encodedData));
+            String providedUser = decodedData.split(":")[0].trim();
+            String providedPassword = decodedData.split(":")[1].trim();
+            Option<String> maybeSavedPassword = applicationSettings.validAuthorizationCredentials().get(providedUser);
+            boolean userRegistered = maybeSavedPassword.isDefined();
+            return userRegistered && maybeSavedPassword.get().equals(providedPassword);
+        } catch (Exception e) {
+            LOG.warn(String.format("Could not check if user is authorized (header: '%s')), probably malformed header.",
+                    authorizationHeader), e);
+            return false;
+        }
+    }
+}
@@ -1,15 +1,16 @@
-/*-
+/*
  * ============LICENSE_START=======================================================
  * PROJECT
  * ================================================================================
  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018 Nokia. All rights reserved.s
  * ================================================================================
  * 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.
  * ============LICENSE_END=========================================================
  */
 
-package org.onap.dcae.restapi.endpoints;
+package org.onap.dcae.restapi;
 
-import com.att.nsa.apiServer.endpoints.NsaBaseEndpoint;
-import com.att.nsa.drumlin.service.framework.context.DrumlinRequestContext;
+import org.onap.dcae.ApplicationSettings;
+import org.slf4j.Logger;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
-public class Ui extends NsaBaseEndpoint {
+@Configuration
+public class ApiConfiguration implements WebMvcConfigurer {
+    private final ApplicationSettings applicationSettings;
+    private Logger errorLogger;
 
-    public static void hello(DrumlinRequestContext ctx) {
-        ctx.renderer().renderTemplate("templates/hello.html");
+    @Autowired
+    ApiConfiguration(ApplicationSettings applicationSettings, Logger errorLogger) {
+        this.applicationSettings = applicationSettings;
+        this.errorLogger = errorLogger;
+    }
+
+    @Override
+    public void addInterceptors(InterceptorRegistry registry) {
+        registry.addInterceptor(new ApiAuthInterceptor(applicationSettings, errorLogger));
     }
 }
index 0f92267..53895ff 100644 (file)
@@ -33,10 +33,10 @@ public enum ApiException {
     UNAUTHORIZED_USER(ExceptionType.POLICY_EXCEPTION, "POL2000", "Unauthorized user", 401),
     NO_SERVER_RESOURCES(ExceptionType.SERVICE_EXCEPTION, "SVC1000", "No server resources (internal processing queue full)", 503);
 
+    public final int httpStatusCode;
     private final ExceptionType type;
     private final String code;
     private final String details;
-    public final int httpStatusCode;
 
     ApiException(ExceptionType type, String code, String details, int httpStatusCode) {
         this.type = type;
@@ -45,18 +45,9 @@ public enum ApiException {
         this.httpStatusCode = httpStatusCode;
     }
 
-    public enum ExceptionType {
-        SERVICE_EXCEPTION, POLICY_EXCEPTION;
-
-        @Override
-        public String toString() {
-            return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, this.name());
-        }
-    }
-
     public JSONObject toJSON() {
         JSONObject exceptionTypeNode = new JSONObject();
-        exceptionTypeNode.put("messageId", code );
+        exceptionTypeNode.put("messageId", code);
         exceptionTypeNode.put("text", details);
 
         JSONObject requestErrorNode = new JSONObject();
@@ -67,4 +58,13 @@ public enum ApiException {
         return rootNode;
     }
 
+    public enum ExceptionType {
+        SERVICE_EXCEPTION, POLICY_EXCEPTION;
+
+        @Override
+        public String toString() {
+            return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, this.name());
+        }
+    }
+
 }
diff --git a/src/main/java/org/onap/dcae/restapi/RestfulCollectorServlet.java b/src/main/java/org/onap/dcae/restapi/RestfulCollectorServlet.java
deleted file mode 100644 (file)
index e5a29e9..0000000
+++ /dev/null
@@ -1,127 +0,0 @@
-\r
-/*\r
- * ============LICENSE_START=======================================================\r
- * PROJECT\r
- * ================================================================================\r
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
- * ================================================================================\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- * \r
- *      http://www.apache.org/licenses/LICENSE-2.0\r
- * \r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ============LICENSE_END=========================================================\r
- */\r
-\r
-package org.onap.dcae.restapi;\r
-\r
-import java.io.IOException;\r
-import java.net.URL;\r
-\r
-import javax.servlet.ServletException;\r
-\r
-import org.apache.tomcat.util.codec.binary.Base64;\r
-import org.onap.dcae.ApplicationSettings;\r
-import org.onap.dcae.commonFunction.CommonStartup;\r
-import org.onap.dcae.commonFunction.VESLogger;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-import com.att.nsa.apiServer.CommonServlet;\r
-import com.att.nsa.configs.ConfigDbException;\r
-import com.att.nsa.drumlin.service.framework.routing.DrumlinRequestRouter;\r
-import com.att.nsa.drumlin.service.framework.routing.playish.DrumlinPlayishRoutingFileSource;\r
-import com.att.nsa.drumlin.service.standards.HttpStatusCodes;\r
-import com.att.nsa.drumlin.till.nv.rrNvReadable;\r
-import com.att.nsa.drumlin.till.nv.rrNvReadable.loadException;\r
-import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;\r
-import com.att.nsa.security.NsaAuthenticator;\r
-\r
-import com.att.nsa.security.authenticators.SimpleAuthenticator;\r
-import com.att.nsa.security.db.simple.NsaSimpleApiKey;\r
-\r
-public class RestfulCollectorServlet extends CommonServlet\r
-{\r
-\r
-       private static final long serialVersionUID = 1L;\r
-       private static final Logger log = LoggerFactory.getLogger ( RestfulCollectorServlet.class );\r
-\r
-       private static String authCredentialsList;\r
-\r
-       public RestfulCollectorServlet ( ApplicationSettings settings ) throws loadException, missingReqdSetting\r
-       {\r
-               super ( settings.torrNvReadable(), "collector", false );\r
-               authCredentialsList = settings.validAuthorizationCredentials();\r
-       }\r
-\r
-\r
-\r
-\r
-       /**\r
-        * This is called once at server start. Use it to init any shared objects and setup the route mapping.\r
-        */\r
-       @Override\r
-       protected void servletSetup () throws rrNvReadable.missingReqdSetting, rrNvReadable.invalidSettingValue, ServletException\r
-       {\r
-               super.servletSetup ();\r
-\r
-               try {\r
-                       // the base class provides a bunch of things like API authentication and ECOMP compliant\r
-                       // logging. The Restful Collector likely doesn't need API authentication, so for now,\r
-                       // we init the base class services with an in-memory (and empty!) config DB.\r
-                       commonServletSetup ( ConfigDbType.MEMORY );\r
-\r
-                       VESLogger.setUpEcompLogging();\r
-\r
-                       // setup the servlet routing and error handling\r
-                       final DrumlinRequestRouter drr = getRequestRouter ();\r
-\r
-                       // you can tell the request router what to do when a particular kind of exception is thrown.\r
-                       drr.setHandlerForException(IllegalArgumentException.class,\r
-                                                                          (ctx, cause) -> sendJsonReply (ctx, HttpStatusCodes.k400_badRequest, cause.getMessage() ));\r
-\r
-                       // load the routes from the config file\r
-                       final URL routes = findStream ( "routes.conf" );\r
-                       if ( routes == null ) throw new rrNvReadable.missingReqdSetting ( "No routing configuration." );\r
-                       final DrumlinPlayishRoutingFileSource drs = new DrumlinPlayishRoutingFileSource ( routes );\r
-                       drr.addRouteSource ( drs );\r
-\r
-                       if (CommonStartup.authflag) {\r
-                               NsaAuthenticator<NsaSimpleApiKey> NsaAuth;\r
-                               NsaAuth = createAuthenticator(authCredentialsList);\r
-\r
-                               this.getSecurityManager().addAuthenticator(NsaAuth);\r
-                       }\r
-\r
-                       log.info ( "Restful Collector Servlet is up." );\r
-               }\r
-               catch ( SecurityException | IOException | ConfigDbException e ) {\r
-                       throw new ServletException ( e );\r
-               }\r
-       }\r
-\r
-       public NsaAuthenticator<NsaSimpleApiKey> createAuthenticator(String authCredentials) {\r
-               NsaAuthenticator<NsaSimpleApiKey> authenticator = new SimpleAuthenticator();\r
-               if (authCredentials != null) {\r
-                       String authpair[] = authCredentials.split("\\|");\r
-                       for (String pair : authpair) {\r
-                               String lineid[] = pair.split(",");\r
-                               String listauthid = lineid[0];\r
-                               String listauthpwd = new String(Base64.decodeBase64(lineid[1]));\r
-                               ((SimpleAuthenticator) authenticator).add(listauthid, listauthpwd);\r
-                       }\r
-\r
-               } else {\r
-                       ((SimpleAuthenticator) authenticator).add("admin", "collectorpasscode");\r
-               }\r
-               return authenticator;\r
-       }\r
-\r
-}\r
-\r
diff --git a/src/main/java/org/onap/dcae/restapi/ServletConfig.java b/src/main/java/org/onap/dcae/restapi/ServletConfig.java
new file mode 100644 (file)
index 0000000..e8efa37
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * ============LICENSE_START=======================================================
+ * PROJECT
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018 Nokia. All rights reserved.s
+ * ================================================================================
+ * 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.dcae.restapi;
+
+import org.onap.dcae.ApplicationSettings;
+import org.onap.dcae.SchemaValidator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.web.server.Ssl;
+import org.springframework.boot.web.server.WebServerFactoryCustomizer;
+import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+import java.nio.file.Paths;
+
+import static java.nio.file.Files.readAllBytes;
+
+@Component
+public class ServletConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
+
+    private static final Logger log = LoggerFactory.getLogger(SchemaValidator.class);
+
+    @Autowired
+    private ApplicationSettings properties;
+
+    @Override
+    public void customize(ConfigurableServletWebServerFactory container) {
+        if (properties.authorizationEnabled()) {
+            container.setSsl(createSSL());
+            container.setPort(properties.httpsPort());
+        } else {
+            container.setPort(properties.httpPort());
+        }
+    }
+
+    private Ssl createSSL() {
+        log.info("Enabling SSL");
+        Ssl ssl = new Ssl();
+        ssl.setEnabled(true);
+        String keyStore = Paths.get(properties.keystoreFileLocation()).toAbsolutePath().toString();
+        log.info("Using keyStore path: " + keyStore);
+        ssl.setKeyStore(keyStore);
+        String keyPasswordFileLocation = Paths.get(properties.keystorePasswordFileLocation()).toAbsolutePath().toString();
+        log.info("Using keyStore password from: " + keyPasswordFileLocation);
+        ssl.setKeyPassword(getKeyStorePassword(keyPasswordFileLocation));
+        ssl.setKeyAlias(properties.keystoreAlias());
+        return ssl;
+    }
+
+    private String getKeyStorePassword(String location) {
+        try {
+            return new String(readAllBytes(Paths.get(location)));
+        } catch (IOException e) {
+            log.error("Could not read keystore password from: '" + location + "'.", e);
+            throw new RuntimeException(e);
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/org/onap/dcae/restapi/VesRestController.java b/src/main/java/org/onap/dcae/restapi/VesRestController.java
new file mode 100644 (file)
index 0000000..b7fc5f3
--- /dev/null
@@ -0,0 +1,214 @@
+/*
+ * ============LICENSE_START=======================================================
+ * PROJECT
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018 Nokia. All rights reserved.s
+ * ================================================================================
+ * 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.dcae.restapi;
+
+import static java.util.Optional.ofNullable;
+import static java.util.stream.StreamSupport.stream;
+import static org.springframework.http.ResponseEntity.accepted;
+import static org.springframework.http.ResponseEntity.ok;
+
+import com.att.nsa.clock.SaClock;
+import com.att.nsa.logging.LoggingContext;
+import com.att.nsa.logging.log4j.EcompFields;
+import com.github.fge.jackson.JsonLoader;
+import com.github.fge.jsonschema.core.report.ProcessingReport;
+import com.github.fge.jsonschema.main.JsonSchema;
+
+import java.util.UUID;
+import java.util.concurrent.LinkedBlockingQueue;
+import javax.servlet.http.HttpServletRequest;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.onap.dcae.ApplicationSettings;
+import org.onap.dcae.CollectorSchemas;
+import org.onap.dcae.commonFunction.VESLogger;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class VesRestController {
+
+    private static final Logger LOG = LoggerFactory.getLogger(VesRestController.class);
+
+    private static final String FALLBACK_VES_VERSION = "v5";
+
+    @Autowired
+    private ApplicationSettings collectorProperties;
+
+    @Autowired
+    private CollectorSchemas schemas;
+
+    @Autowired
+    @Qualifier("metriclog")
+    private Logger metriclog;
+
+    @Autowired
+    @Qualifier("incomingRequestsLogger")
+    private Logger incomingRequestsLogger;
+
+    @Autowired
+    @Qualifier("errorLog")
+    private Logger errorLog;
+
+    private LinkedBlockingQueue<JSONObject> inputQueue;
+    private String version;
+
+    @Autowired
+    VesRestController(@Qualifier("incomingRequestsLogger") Logger incomingRequestsLogger,
+                      @Qualifier("inputQueue") LinkedBlockingQueue<JSONObject> inputQueue) {
+        this.incomingRequestsLogger = incomingRequestsLogger;
+        this.inputQueue = inputQueue;
+    }
+
+    @GetMapping("/")
+    String mainPage() {
+        return "Welcome to VESCollector";
+    }
+
+    //refactor in next iteration
+    @PostMapping(value = {"/eventListener/v1",
+            "/eventListener/v1/eventBatch",
+            "/eventListener/v2",
+            "/eventListener/v2/eventBatch",
+            "/eventListener/v3",
+            "/eventListener/v3/eventBatch",
+            "/eventListener/v4",
+            "/eventListener/v4/eventBatch",
+            "/eventListener/v5",
+            "/eventListener/v5/eventBatch"}, consumes = "application/json")
+    ResponseEntity<String> receiveEvent(@RequestBody String jsonPayload, HttpServletRequest httpServletRequest) {
+        String request = httpServletRequest.getRequestURI();
+        extractVersion(request);
+
+        JSONObject jsonObject;
+        try {
+            jsonObject = new JSONObject(jsonPayload);
+        } catch (Exception e) {
+            return ResponseEntity.badRequest().body(ApiException.INVALID_JSON_INPUT.toJSON().toString());
+        }
+
+        String uuid = setUpECOMPLoggingForRequest();
+        incomingRequestsLogger.info(String.format(
+                "Received a VESEvent '%s', marked with unique identifier '%s', on api version '%s', from host: '%s'",
+                jsonObject, uuid, version, httpServletRequest.getRemoteHost()));
+
+        if (collectorProperties.jsonSchemaValidationEnabled()) {
+            if (isBatchRequest(request) && (jsonObject.has("eventList") && (!jsonObject.has("event")))) {
+                if (!conformsToSchema(jsonObject, version)) {
+                    return errorResponse(ApiException.SCHEMA_VALIDATION_FAILED);
+                }
+            } else if (!isBatchRequest(request) && (!jsonObject.has("eventList") && (jsonObject.has("event")))) {
+                if (!conformsToSchema(jsonObject, version)) {
+                    return errorResponse(ApiException.SCHEMA_VALIDATION_FAILED);
+                }
+            } else {
+                return errorResponse(ApiException.INVALID_JSON_INPUT);
+            }
+        }
+
+        JSONArray commonlyFormatted = convertToJSONArrayCommonFormat(jsonObject, request, uuid, version);
+
+        if (!putEventsOnProcessingQueue(commonlyFormatted)) {
+            errorLog.error("EVENT_RECEIPT_FAILURE: QueueFull " + ApiException.NO_SERVER_RESOURCES);
+            return errorResponse(ApiException.NO_SERVER_RESOURCES);
+        }
+        return ok().contentType(MediaType.APPLICATION_JSON).body("Message Accepted");
+    }
+
+    private void extractVersion(String httpServletRequest) {
+        version = httpServletRequest.split("/")[2];
+    }
+
+    private ResponseEntity<String> errorResponse(ApiException noServerResources) {
+        return ResponseEntity.status(noServerResources.httpStatusCode)
+                .body(noServerResources.toJSON().toString());
+    }
+
+    private boolean putEventsOnProcessingQueue(JSONArray arrayOfEvents) {
+        for (int i = 0; i < arrayOfEvents.length(); i++) {
+            metriclog.info("EVENT_PUBLISH_START");
+            if (!inputQueue.offer((JSONObject) arrayOfEvents.get(i))) {
+                return false;
+            }
+        }
+        LOG.debug("CommonStartup.handleEvents:EVENTS has been published successfully!");
+        metriclog.info("EVENT_PUBLISH_END");
+        return true;
+    }
+
+    private boolean conformsToSchema(JSONObject payload, String version) {
+        try {
+            JsonSchema schema = ofNullable(schemas.getJSONSchemasMap(version).get(version))
+                    .orElse(schemas.getJSONSchemasMap(version).get(FALLBACK_VES_VERSION));
+            ProcessingReport report = schema.validate(JsonLoader.fromString(payload.toString()));
+            if (!report.isSuccess()) {
+                LOG.warn("Schema validation failed for event: " + payload);
+                stream(report.spliterator(), false).forEach(e -> LOG.warn(e.getMessage()));
+                return false;
+            }
+            return report.isSuccess();
+        } catch (Exception e) {
+            throw new RuntimeException("Unable to validate against schema", e);
+        }
+    }
+
+    private static JSONArray convertToJSONArrayCommonFormat(JSONObject jsonObject, String request,
+                                                            String uuid, String version) {
+        JSONArray asArrayEvents = new JSONArray();
+        String vesUniqueIdKey = "VESuniqueId";
+        String vesVersionKey = "VESversion";
+        if (isBatchRequest(request)) {
+            JSONArray events = jsonObject.getJSONArray("eventList");
+            for (int i = 0; i < events.length(); i++) {
+                JSONObject event = new JSONObject().put("event", events.getJSONObject(i));
+                event.put(vesUniqueIdKey, uuid + "-" + i);
+                event.put(vesVersionKey, version);
+                asArrayEvents.put(event);
+            }
+        } else {
+            jsonObject.put(vesUniqueIdKey, uuid);
+            jsonObject.put(vesVersionKey, version);
+            asArrayEvents = new JSONArray().put(jsonObject);
+        }
+        return asArrayEvents;
+    }
+
+    private static String setUpECOMPLoggingForRequest() {
+        final UUID uuid = UUID.randomUUID();
+        LoggingContext localLC = VESLogger.getLoggingContextForThread(uuid);
+        localLC.put(EcompFields.kBeginTimestampMs, SaClock.now());
+        return uuid.toString();
+    }
+
+    private static boolean isBatchRequest(String request) {
+        return request.contains("eventBatch");
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/org/onap/dcae/restapi/endpoints/EventReceipt.java b/src/main/java/org/onap/dcae/restapi/endpoints/EventReceipt.java
deleted file mode 100644 (file)
index d60e2a1..0000000
+++ /dev/null
@@ -1,247 +0,0 @@
-/*-\r
- * ============LICENSE_START=======================================================\r
- * PROJECT\r
- * ================================================================================\r
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
- * ================================================================================\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *      http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ============LICENSE_END=========================================================\r
- */\r
-\r
-package org.onap.dcae.restapi.endpoints;\r
-\r
-import com.att.nsa.apiServer.endpoints.NsaBaseEndpoint;\r
-import com.att.nsa.clock.SaClock;\r
-import com.att.nsa.drumlin.service.framework.context.DrumlinRequestContext;\r
-import com.att.nsa.drumlin.service.standards.HttpStatusCodes;\r
-import com.att.nsa.drumlin.service.standards.MimeTypes;\r
-import com.att.nsa.logging.LoggingContext;\r
-import com.att.nsa.logging.log4j.EcompFields;\r
-import com.att.nsa.security.db.simple.NsaSimpleApiKey;\r
-import com.google.gson.JsonParser;\r
-import java.io.FileReader;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.nio.charset.Charset;\r
-import java.util.Base64;\r
-import java.util.UUID;\r
-import java.util.regex.Matcher;\r
-import java.util.regex.Pattern;\r
-import org.json.JSONArray;\r
-import org.json.JSONException;\r
-import org.json.JSONObject;\r
-import org.json.JSONTokener;\r
-import org.onap.dcae.commonFunction.CommonStartup;\r
-import org.onap.dcae.commonFunction.CommonStartup.QueueFullException;\r
-import org.onap.dcae.commonFunction.VESLogger;\r
-import org.onap.dcae.restapi.ApiException;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-public class EventReceipt extends NsaBaseEndpoint {\r
-\r
-       private static final Logger log = LoggerFactory.getLogger(EventReceipt.class);\r
-       private static final String MESSAGE = " Message:";\r
-\r
-       public static void receiveVESEvent(DrumlinRequestContext ctx) {\r
-               // the request body carries events. assume for now it's an array\r
-               // of json objects that fits in memory. (See cambria's parsing for\r
-               // handling large messages)\r
-\r
-               NsaSimpleApiKey retkey = null;\r
-\r
-\r
-               JSONObject jsonObject;\r
-               InputStream istr = null;\r
-               int arrayFlag = 0;\r
-               String vesVersion = null;\r
-               String userId=null;\r
-\r
-               try {\r
-\r
-\r
-                       istr = ctx.request().getBodyStream();\r
-                       jsonObject = new JSONObject(new JSONTokener(istr));\r
-\r
-                       log.info("ctx getPathInContext: " + ctx.request().getPathInContext());\r
-                       Pattern p = Pattern.compile("(v\\d+)");\r
-                       Matcher m = p.matcher(ctx.request().getPathInContext());\r
-\r
-                       if (m.find()) {\r
-                               log.info("VES version:" + m.group());\r
-                               vesVersion = m.group();\r
-                       }\r
-                       \r
-                       final UUID uuid = UUID.randomUUID();\r
-                       LoggingContext localLC = VESLogger.getLoggingContextForThread(uuid);\r
-                       localLC.put(EcompFields.kBeginTimestampMs, SaClock.now());\r
-                       \r
-                       if (ctx.request().getPathInContext().contains("eventBatch")) {\r
-                               CommonStartup.inlog.info(ctx.request().getRemoteAddress() + "VESUniqueID-Prefix:" + uuid\r
-                                               + " VES Batch Input Messsage: " + jsonObject);\r
-                               log.info(ctx.request().getRemoteAddress() + "VESUniqueID-Prefix:" + uuid + " VES Batch Input Messsage: "\r
-                                               + jsonObject);\r
-                               arrayFlag = 1;\r
-                       } else {\r
-                               CommonStartup.inlog.info(\r
-                                               ctx.request().getRemoteAddress() + "VESUniqueID:" + uuid + " Input Messsage: " + jsonObject);\r
-                               log.info(ctx.request().getRemoteAddress() + "VESUniqueID:" + uuid + " Input Messsage: " + jsonObject);\r
-\r
-                       }\r
-\r
-                       try {\r
-                               if (CommonStartup.authflag) {\r
-                                       userId = getUser (ctx);\r
-                                       retkey = NsaBaseEndpoint.getAuthenticatedUser(ctx);\r
-                               }\r
-                       } catch (NullPointerException x) {\r
-                               //log.info("Invalid user request :" + userId + " FROM " + ctx.request().getRemoteAddress() + " " +  ctx.request().getContentType() + MESSAGE + jsonObject);\r
-                               log.info(String.format("Unauthorized request %s FROM %s %s %s %s", getUser(ctx), ctx.request().getRemoteAddress(), ctx.request().getContentType(), MESSAGE,     jsonObject));\r
-                               CommonStartup.eplog.info("EVENT_RECEIPT_FAILURE: Unauthorized user" + userId +  x);\r
-                               respondWithCustomMsginJson(ctx, ApiException.UNAUTHORIZED_USER);\r
-                               return;\r
-                       }\r
-                       \r
-                       if (schemaCheck(retkey, arrayFlag, jsonObject, vesVersion, ctx, uuid)) {\r
-                               return;\r
-                       }\r
-\r
-               } catch (JSONException | NullPointerException | IOException x) {\r
-                       log.error(String.format("Couldn't parse JSON Array - HttpStatusCodes.k400_badRequest%d%s%s",\r
-                                       HttpStatusCodes.k400_badRequest, MESSAGE, x.getMessage()));\r
-                       CommonStartup.eplog.info("EVENT_RECEIPT_FAILURE: Invalid user request " + x);\r
-                       respondWithCustomMsginJson(ctx, ApiException.INVALID_JSON_INPUT);\r
-                       return;\r
-               } catch (QueueFullException e) {\r
-                       log.error("Collector internal queue full  :" + e.getMessage(), e);\r
-                       CommonStartup.eplog.info("EVENT_RECEIPT_FAILURE: QueueFull" + e);\r
-                       respondWithCustomMsginJson(ctx, ApiException.NO_SERVER_RESOURCES);\r
-                       return;\r
-               } finally {\r
-                       if (istr != null) {\r
-                               safeClose(istr);\r
-                       }\r
-               }\r
-               log.info("MessageAccepted and k200_ok to be sent");\r
-               ctx.response().sendErrorAndBody(HttpStatusCodes.k200_ok, "Message Accepted", MimeTypes.kAppJson);\r
-       }\r
-       \r
-       \r
-       private static String getUser(DrumlinRequestContext ctx){\r
-               String authorization = ctx.request().getFirstHeader("Authorization");\r
-               if (authorization != null && authorization.startsWith("Basic")) {\r
-                       String base64Credentials = authorization.substring("Basic".length()).trim();\r
-                       String credentials = new String(Base64.getDecoder().decode(base64Credentials),\r
-                               Charset.forName("UTF-8"));\r
-                       final String[] values = credentials.split(":",2);\r
-                       log.debug("User:" + values[0] + " Pwd:" + values[1]);\r
-                       return values[0];\r
-                }\r
-                return null;\r
-               \r
-       }\r
-\r
-       private static Boolean schemaCheck(NsaSimpleApiKey retkey, int arrayFlag,\r
-                                                                          JSONObject jsonObject, String vesVersion,\r
-                                                                          DrumlinRequestContext ctx, UUID uuid)\r
-               throws JSONException, QueueFullException, IOException {\r
-\r
-               JSONArray jsonArray;\r
-               JSONArray jsonArrayMod = new JSONArray();\r
-               JSONObject event;\r
-               FileReader fr;\r
-               if (retkey != null || !CommonStartup.authflag) {\r
-                       if (CommonStartup.schemaValidatorflag) {\r
-                               if ((arrayFlag == 1) && (jsonObject.has("eventList") && (!jsonObject.has("event")))\r
-                                               || ((arrayFlag == 0) && (!jsonObject.has("eventList") && (jsonObject.has("event"))))) {\r
-                                       fr = new FileReader(schemaFileVersion(vesVersion));\r
-                                       String schema = new JsonParser().parse(fr).toString();\r
-\r
-                                       String valresult = CommonStartup.validateAgainstSchema(jsonObject.toString(), schema);\r
-                                       switch (valresult) {\r
-                                               case "true":\r
-                                                       log.info("Validation successful");\r
-                                                       break;\r
-                                               case "false":\r
-                                                       log.info("Validation failed");\r
-                                                       respondWithCustomMsginJson(ctx, ApiException.SCHEMA_VALIDATION_FAILED);\r
-                                                       return true;\r
-                                               default:\r
-                                                       log.error("Validation errored" + valresult);\r
-                                                       respondWithCustomMsginJson(ctx, ApiException.INVALID_JSON_INPUT);\r
-                                                       return true;\r
-                                       }\r
-                               } else {\r
-                                       log.info("Validation failed");\r
-                                       respondWithCustomMsginJson(ctx, ApiException.SCHEMA_VALIDATION_FAILED);\r
-                                       return true;\r
-                               }\r
-                               if (arrayFlag == 1) {\r
-                                       jsonArray = jsonObject.getJSONArray("eventList");\r
-                                       log.info("Validation successful for all events in batch");\r
-                                       for (int i = 0; i < jsonArray.length(); i++) {\r
-                                               event = new JSONObject().put("event", jsonArray.getJSONObject(i));\r
-                                               event.put("VESuniqueId", uuid + "-" + i);\r
-                                               event.put("VESversion", vesVersion);\r
-                                               jsonArrayMod.put(event);\r
-                                       }\r
-                                       log.info("Modified jsonarray:" + jsonArrayMod.toString());\r
-                               } else {\r
-                                       jsonObject.put("VESuniqueId", uuid);\r
-                                       jsonObject.put("VESversion", vesVersion);\r
-                                       jsonArrayMod = new JSONArray().put(jsonObject);\r
-                               }\r
-                       }\r
-\r
-                       // reject anything that's not JSON\r
-                       if (!ctx.request().getContentType().equalsIgnoreCase("application/json")) {\r
-                               log.info(String.format("Rejecting request with content type %s Message:%s",\r
-                                               ctx.request().getContentType(), jsonObject));\r
-                               respondWithCustomMsginJson(ctx, ApiException.INVALID_CONTENT_TYPE);\r
-                               return true;\r
-                       }\r
-\r
-                       CommonStartup.handleEvents(jsonArrayMod);\r
-               } else {\r
-                       log.info(String.format("Unauthorized request %s FROM %s %s %s %s", getUser(ctx), ctx.request().getRemoteAddress(), ctx.request().getContentType(), MESSAGE,\r
-                                       jsonObject));\r
-                       respondWithCustomMsginJson(ctx, ApiException.UNAUTHORIZED_USER);\r
-                       return true;\r
-               }\r
-               return false;\r
-       }\r
-\r
-       private static void respondWithCustomMsginJson(DrumlinRequestContext ctx, ApiException apiException) {\r
-               ctx.response()\r
-                       .sendErrorAndBody(apiException.httpStatusCode,\r
-                               apiException.toJSON().toString(), MimeTypes.kAppJson);\r
-       }\r
-\r
-       private static void safeClose(InputStream is) {\r
-               if (is != null) {\r
-                       try {\r
-                               is.close();\r
-                       } catch (IOException e) {\r
-                               log.error("Error closing Input stream : " + e);\r
-                       }\r
-               }\r
-\r
-       }\r
-\r
-       public static String schemaFileVersion(String version) {\r
-               return CommonStartup.schemaFileJson.has(version) ?\r
-                       CommonStartup.schemaFileJson.getString(version) : CommonStartup.schemaFileJson.getString("v5");\r
-       }\r
-\r
-}\r
-\r
index 8462f4e..7f6d17c 100644 (file)
@@ -36,7 +36,7 @@ start() {
       -XX:ErrorFile=logs/java_error%p.log \\r
       -XX:+HeapDumpOnOutOfMemoryError \\r
       -Dhttps.protocols=TLSv1.1,TLSv1.2 \\r
-      org.onap.dcae.commonFunction.CommonStartup $* & &>> logs/collector.log\r
+      org.onap.dcae.VesApplication $* & &>> logs/collector.log\r
 }\r
 \r
 stop() {\r
index b162cef..b483bcb 100644 (file)
@@ -28,6 +28,7 @@ import org.junit.Test;
 import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.Arrays;
 import java.util.Objects;
 
@@ -153,7 +154,7 @@ public class ApplicationSettingsTest {
         String passwordFileLocation = fromTemporaryConfiguration("collector.keystore.passwordfile=/somewhere/password").keystorePasswordFileLocation();
 
         // then
-        assertEquals("/somewhere/password", passwordFileLocation);
+        assertEquals(sanitizePath("/somewhere/password"), passwordFileLocation);
     }
 
     @Test
@@ -162,7 +163,7 @@ public class ApplicationSettingsTest {
         String passwordFileLocation = fromTemporaryConfiguration().keystorePasswordFileLocation();
 
         // then
-        assertEquals("./etc/passwordfile", passwordFileLocation);
+        assertEquals(sanitizePath("etc/passwordfile"), passwordFileLocation);
     }
 
     @Test
@@ -172,7 +173,7 @@ public class ApplicationSettingsTest {
                 .keystoreFileLocation();
 
         // then
-        assertEquals("/somewhere/keystore", keystoreFileLocation);
+        assertEquals(sanitizePath("/somewhere/keystore"), keystoreFileLocation);
     }
 
     @Test
@@ -181,7 +182,7 @@ public class ApplicationSettingsTest {
         String keystoreFileLocation = fromTemporaryConfiguration().keystoreFileLocation();
 
         // then
-        assertEquals("../etc/keystore", keystoreFileLocation);
+        assertEquals(sanitizePath("etc/keystore"), keystoreFileLocation);
     }
 
 
@@ -209,7 +210,7 @@ public class ApplicationSettingsTest {
         String dmaapConfigFileLocation = fromTemporaryConfiguration("collector.dmaapfile=/somewhere/dmaapFile").cambriaConfigurationFileLocation();
 
         // then
-        assertEquals("/somewhere/dmaapFile", dmaapConfigFileLocation);
+        assertEquals(sanitizePath("/somewhere/dmaapFile"), dmaapConfigFileLocation);
     }
 
     @Test
@@ -218,7 +219,7 @@ public class ApplicationSettingsTest {
         String dmaapConfigFileLocation = fromTemporaryConfiguration().cambriaConfigurationFileLocation();
 
         // then
-        assertEquals("./etc/DmaapConfig.json", dmaapConfigFileLocation);
+        assertEquals(sanitizePath("etc/DmaapConfig.json"), dmaapConfigFileLocation);
     }
 
     @Test
@@ -347,25 +348,25 @@ public class ApplicationSettingsTest {
     @Test
     public void shouldReturnValidCredentials() throws IOException {
         // when
-        String userToBase64PasswordDelimitedByCommaSeparatedByPipes = fromTemporaryConfiguration(
-                "header.authlist=pasza,123jsad1|someoneelse,12asd31"
+        Map<String, String> allowedUsers = fromTemporaryConfiguration(
+                "header.authlist=pasza,c2ltcGxlcGFzc3dvcmQNCg==|someoneelse,c2ltcGxlcGFzc3dvcmQNCg=="
         ).validAuthorizationCredentials();
 
         // then
-        assertEquals("pasza,123jsad1|someoneelse,12asd31", userToBase64PasswordDelimitedByCommaSeparatedByPipes);
+        assertEquals(allowedUsers.get("pasza").get(), "simplepassword");
+        assertEquals(allowedUsers.get("someoneelse").get(), "simplepassword");
     }
 
     @Test
     public void shouldbyDefaultThereShouldBeNoValidCredentials() throws IOException {
         // when
-        String userToBase64PasswordDelimitedByCommaSeparatedByPipes = fromTemporaryConfiguration().
+        Map<String, String> userToBase64PasswordDelimitedByCommaSeparatedByPipes = fromTemporaryConfiguration().
                 validAuthorizationCredentials();
 
         // then
-        assertNull(userToBase64PasswordDelimitedByCommaSeparatedByPipes);
+        assertTrue(userToBase64PasswordDelimitedByCommaSeparatedByPipes.isEmpty());
     }
 
-
     @Test
     public void shouldReturnIfEventTransformingIsEnabled() throws IOException {
         // when
@@ -392,7 +393,7 @@ public class ApplicationSettingsTest {
                 .cambriaConfigurationFileLocation();
 
         // then
-        assertEquals("/somewhere/dmaapConfig", cambriaConfigurationFileLocation);
+        assertEquals(sanitizePath("/somewhere/dmaapConfig"), cambriaConfigurationFileLocation);
     }
 
     @Test
@@ -402,7 +403,7 @@ public class ApplicationSettingsTest {
                 .cambriaConfigurationFileLocation();
 
         // then
-        assertEquals("./etc/DmaapConfig.json", cambriaConfigurationFileLocation);
+        assertEquals(sanitizePath("etc/DmaapConfig.json"), cambriaConfigurationFileLocation);
     }
 
     private static ApplicationSettings fromTemporaryConfiguration(String... fileLines)
@@ -410,8 +411,10 @@ public class ApplicationSettingsTest {
         File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter");
         Files.write(tempConfFile.toPath(), Arrays.asList(fileLines));
         tempConfFile.deleteOnExit();
-        return new ApplicationSettings(new String[]{"-c", tempConfFile.toString()}, args -> processCmdLine(args));
+        return new ApplicationSettings(new String[]{"-c", tempConfFile.toString()}, args -> processCmdLine(args), "");
     }
 
-
+    private String sanitizePath(String path) {
+        return Paths.get(path).toString();
+    }
 }
\ No newline at end of file
diff --git a/src/test/java/org/onap/dcae/commonFunction/CommonStartupTest.java b/src/test/java/org/onap/dcae/commonFunction/CommonStartupTest.java
deleted file mode 100644 (file)
index 5a17148..0000000
+++ /dev/null
@@ -1,127 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * PROJECT
- * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * 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.dcae.commonFunction;
-
-import static java.util.Base64.getDecoder;
-import static java.util.Base64.getEncoder;
-import static org.junit.Assert.assertEquals;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import com.att.nsa.cmdLine.NsaCommandLineUtil;
-import com.att.nsa.drumlin.service.framework.context.DrumlinRequest;
-import com.att.nsa.drumlin.till.nv.impl.nvReadableStack;
-import com.att.nsa.drumlin.till.nv.impl.nvReadableTable;
-import com.att.nsa.drumlin.till.nv.rrNvReadable.loadException;
-import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
-import com.att.nsa.security.authenticators.SimpleAuthenticator;
-import com.att.nsa.security.db.simple.NsaSimpleApiKey;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonParser;
-import java.io.FileReader;
-import java.io.IOException;
-import java.util.Map;
-import java.util.concurrent.LinkedBlockingQueue;
-
-import org.json.JSONArray;
-import org.json.JSONObject;
-import org.junit.Test;
-import org.mockito.Mockito;
-import org.onap.dcae.ApplicationSettings;
-import org.onap.dcae.CLIUtils;
-import org.onap.dcae.commonFunction.CommonStartup.QueueFullException;
-import org.onap.dcae.commonFunction.event.publishing.EventPublisher;
-import org.onap.dcae.restapi.RestfulCollectorServlet;
-import org.onap.dcae.vestest.TestingUtilities;
-
-
-public class CommonStartupTest {
-
-    @Test
-    public void testParseCLIArguments() {
-        // given
-        String args[] = {"-a", "aa"};
-        Map<String, String> argMap = NsaCommandLineUtil.processCmdLine(args, true);
-        // when
-        nvReadableStack settings = new nvReadableStack();
-        settings.push(new nvReadableTable(argMap));
-
-        // then
-        assertEquals(settings.getString("a", "default"), "aa");
-    }
-
-    @Test
-    public void shouldPutValidVESEventOnProcessingQueueWithoutExceptions() throws IOException, QueueFullException {
-        // given
-        CommonStartup.fProcessingInputQueue = new LinkedBlockingQueue<>(
-            CommonStartup.maxQueueEvent);
-        JsonElement vesEvent = new JsonParser().parse(new FileReader("src/test/resources/VES_valid.txt"));
-        JSONObject validVESEvent = new JSONObject(vesEvent.toString());
-        JSONArray jsonArrayMod = new JSONArray().put(validVESEvent);
-
-        // then
-        CommonStartup.handleEvents(jsonArrayMod);
-    }
-
-
-    @Test
-    public void testParseStreamIdToStreamHashMapping() {
-        // given
-
-        CommonStartup.streamID = TestingUtilities.convertDMaaPStreamsPropertyToMap("fault=sec_fault|syslog=sec_syslog|heartbeat=sec_heartbeat|measurementsForVfScaling=sec_measurement|mobileFlow=sec_mobileflow|other=sec_other|stateChange=sec_statechange|thresholdCrossingAlert=sec_thresholdCrossingAlert|voiceQuality=ves_voicequality|sipSignaling=ves_sipsignaling");
-        EventProcessor eventProcessor = new EventProcessor(mock(EventPublisher.class));
-        // when
-        Map<String, String[]> streamHashMapping = EventProcessor.streamidHash;
-
-        // then
-        assertEquals(streamHashMapping.get("fault")[0], "sec_fault");
-        assertEquals(streamHashMapping.get("measurementsForVfScaling")[0], "sec_measurement");
-    }
-
-    @Test
-    public void testAuthListHandler() throws loadException, missingReqdSetting {
-        // given
-        ApplicationSettings settings = new ApplicationSettings(new String[]{}, CLIUtils::processCmdLine);
-
-        String user1 = "secureid";
-        String password1Hashed = "IWRjYWVSb2FkbTEyMyEt";
-        String password1UnHashed = new String(getDecoder().decode("IWRjYWVSb2FkbTEyMyEt"));
-        String user2 = "sample1";
-        String password2Hashed = "c2FtcGxlMQ";
-
-        String authlist = user1 + "," + password1Hashed + "|" + user2 + "," + password2Hashed;
-
-        RestfulCollectorServlet rsv = new RestfulCollectorServlet(settings);
-
-        DrumlinRequest drumlinRequestMock = Mockito.mock(DrumlinRequest.class);
-
-        String basicHeaderForUser1 = "Basic " + getEncoder().encodeToString((user1 + ":" + password1UnHashed).getBytes());
-        when(drumlinRequestMock.getFirstHeader("Authorization")).thenReturn(basicHeaderForUser1);
-
-        // when
-        SimpleAuthenticator simpleAuthenticator = (SimpleAuthenticator) rsv.createAuthenticator(authlist);
-        NsaSimpleApiKey authentic = simpleAuthenticator.isAuthentic(drumlinRequestMock);
-
-        // then
-        assertEquals(authentic.getSecret(), password1UnHashed);
-    }
-}
-
-
index 77ef005..f5c5d5f 100644 (file)
 package org.onap.dcae.commonFunction;
 
 import com.google.gson.Gson;
-import java.util.concurrent.atomic.AtomicReference;
-
-import io.vavr.collection.HashMap;
+import io.vavr.collection.Map;
 import org.json.JSONObject;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
-
-import java.util.List;
+import org.onap.dcae.ApplicationSettings;
+import org.onap.dcae.CLIUtils;
 import org.onap.dcae.commonFunction.event.publishing.EventPublisher;
 import org.onap.dcae.vestest.TestingUtilities;
 
+import java.util.List;
+
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
 import static org.onap.dcae.commonFunction.EventProcessor.EVENT_LIST_TYPE;
 
 public class EventProcessorTest {
 
     private final String ev = "{\"event\": {\"commonEventHeader\": {   \"reportingEntityName\": \"VM name will be provided by ECOMP\", \"startEpochMicrosec\": 1477012779802988,\"lastEpochMicrosec\": 1477012789802988,\"eventId\": \"83\",\"sourceName\": \"Dummy VM name - No Metadata available\",\"sequence\": 83,\"priority\": \"Normal\",\"functionalRole\": \"vFirewall\",\"domain\": \"measurementsForVfScaling\",\"reportingEntityId\": \"VM UUID will be provided by ECOMP\",\"sourceId\": \"Dummy VM UUID - No Metadata available\",\"version\": 1.1},\"measurementsForVfScalingFields\": {\"measurementInterval\": 10,\"measurementsForVfScalingVersion\": 1.1,\"vNicUsageArray\": [{\"multicastPacketsIn\": 0,\"bytesIn\": 3896,\"unicastPacketsIn\": 0, \"multicastPacketsOut\": 0,\"broadcastPacketsOut\": 0,          \"packetsOut\": 28,\"bytesOut\": 12178,\"broadcastPacketsIn\": 0,\"packetsIn\": 58,\"unicastPacketsOut\": 0,\"vNicIdentifier\": \"eth0\"}]}}}";
 
+    Map<String, String[]> streamID;
+    private ApplicationSettings properties;
+
     @Before
     public void setUp() {
-        CommonStartup.streamID = TestingUtilities.convertDMaaPStreamsPropertyToMap("fault=sec_fault|syslog=sec_syslog|heartbeat=sec_heartbeat|measurementsForVfScaling=sec_measurement|mobileFlow=sec_mobileflow|other=sec_other|stateChange=sec_statechange|thresholdCrossingAlert=sec_thresholdCrossingAlert|voiceQuality=ves_voicequality|sipSignaling=ves_sipsignaling");
+        streamID = TestingUtilities.convertDMaaPStreamsPropertyToMap("fault=sec_fault|syslog=sec_syslog|heartbeat=sec_heartbeat|measurementsForVfScaling=sec_measurement|mobileFlow=sec_mobileflow|other=sec_other|stateChange=sec_statechange|thresholdCrossingAlert=sec_thresholdCrossingAlert|voiceQuality=ves_voicequality|sipSignaling=ves_sipsignaling");
+        properties = new ApplicationSettings(new String[]{}, CLIUtils::processCmdLine);
+        streamID = properties.dMaaPStreamsMapping();
     }
 
     @Test
     public void testLoad() {
         //given
-        EventProcessor ec = new EventProcessor(mock(EventPublisher.class));
+        EventProcessor ec = new EventProcessor(mock(EventPublisher.class), properties);
         ec.event = new org.json.JSONObject(ev);
         //when
         ec.overrideEvent();
@@ -67,7 +69,7 @@ public class EventProcessorTest {
     @Test
     public void shouldParseJsonEvents() throws ReflectiveOperationException {
         //given
-        EventProcessor eventProcessor = new EventProcessor(mock(EventPublisher.class));
+        EventProcessor eventProcessor = new EventProcessor(mock(EventPublisher.class), properties);
         String event_json = "[{ \"filter\": {\"event.commonEventHeader.domain\":\"heartbeat\",\"VESversion\":\"v4\"},\"processors\":[" +
                 "{\"functionName\": \"concatenateValue\",\"args\":{\"field\":\"event.commonEventHeader.eventName\",\"concatenate\": [\"$event.commonEventHeader.domain\",\"$event.commonEventHeader.eventType\",\"$event.faultFields.alarmCondition\"], \"delimiter\":\"_\"}}" +
                 ",{\"functionName\": \"addAttribute\",\"args\":{\"field\": \"event.heartbeatFields.heartbeatFieldsVersion\",\"value\": \"1.0\",\"fieldType\": \"number\"}}" +
@@ -86,5 +88,4 @@ public class EventProcessorTest {
         assertThat(stringArgumentCaptor.getAllValues()).contains("concatenateValue", "addAttribute", "map");
     }
 
-}
-
+}
\ No newline at end of file
index 49b53d2..09517ae 100644 (file)
@@ -64,11 +64,11 @@ public class TestConfigProcessor {
 \r
         final JSONObject jsonObject = getFileAsJsonObject();\r
         final String functionRole = (jsonObject.getJSONObject("event")).getJSONObject("commonEventHeader")\r
-            .get("functionalRole").toString();\r
+                .get("functionalRole").toString();\r
         System.out.println("event==" + jsonObject.toString());\r
         System.out.println("functionRole==" + functionRole);\r
         final JSONObject jsonArgs = new JSONObject(\r
-            "{\"field\": \"event.commonEventHeader.nfNamingCode\",\"oldField\": \"event.commonEventHeader.functionalRole\"}");\r
+                "{\"field\": \"event.commonEventHeader.nfNamingCode\",\"oldField\": \"event.commonEventHeader.functionalRole\"}");\r
         ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);\r
         cpEvent.map(jsonArgs);\r
         final String responseData = cpEvent.getEventObjectVal("event.commonEventHeader.nfNamingCode").toString();\r
@@ -82,15 +82,15 @@ public class TestConfigProcessor {
 \r
         final JSONObject jsonObject = getFileAsJsonObject();\r
         final String alarmAdditionalInformation = (jsonObject.getJSONObject("event")).getJSONObject("faultFields")\r
-            .get("alarmAdditionalInformation").toString();\r
+                .get("alarmAdditionalInformation").toString();\r
         System.out.println("event==" + jsonObject.toString());\r
         System.out.println("alarmAdditionalInformation==" + alarmAdditionalInformation);\r
         final JSONObject jsonArgs = new JSONObject(\r
-            "{\"field\": \"event.faultFields.eventAdditionalInformation\",\"oldField\": \"event.faultFields.alarmAdditionalInformation\"}");\r
+                "{\"field\": \"event.faultFields.eventAdditionalInformation\",\"oldField\": \"event.faultFields.alarmAdditionalInformation\"}");\r
         ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);\r
         cpEvent.map(jsonArgs);\r
         final String responseData = cpEvent.getEventObjectVal("event.faultFields.eventAdditionalInformation")\r
-            .toString();\r
+                .toString();\r
         System.out.println("modified event==" + jsonObject.toString());\r
         System.out.println("responseData==" + responseData);\r
         assertEquals(alarmAdditionalInformation, responseData);\r
@@ -104,14 +104,14 @@ public class TestConfigProcessor {
         System.out.println("event==" + jsonObject.toString());\r
         //System.out.println("alarmAdditionalInformation==" + alarmAdditionalInformation);\r
         final JSONObject jsonArgs = new JSONObject(\r
-            "{\"field\": \"event.faultFields.vNicPerformanceArray[]\",\"oldField\": \"event.faultFields.errors\",\"attrMap\":{\"receiveDiscards\":\"receivedDiscardedPacketsAccumulated\"}}");\r
+                "{\"field\": \"event.faultFields.vNicPerformanceArray[]\",\"oldField\": \"event.faultFields.errors\",\"attrMap\":{\"receiveDiscards\":\"receivedDiscardedPacketsAccumulated\"}}");\r
         ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);\r
         final String receiveDiscards = cpEvent.getEventObjectVal("event.faultFields.errors.receiveDiscards").toString();\r
         System.out.println("receiveDiscards==" + receiveDiscards);\r
         cpEvent.map(jsonArgs);\r
         final String responseData = cpEvent\r
-            .getEventObjectVal("event.faultFields.vNicPerformanceArray[0].receivedDiscardedPacketsAccumulated")\r
-            .toString();\r
+                .getEventObjectVal("event.faultFields.vNicPerformanceArray[0].receivedDiscardedPacketsAccumulated")\r
+                .toString();\r
         System.out.println("modified event==" + jsonObject.toString());\r
         System.out.println("responseData==" + responseData);\r
         assertEquals(receiveDiscards, responseData);\r
@@ -125,7 +125,7 @@ public class TestConfigProcessor {
         System.out.println("event==" + jsonObject.toString());\r
         //System.out.println("functionRole==" + functionRole);\r
         final JSONObject jsonArgs = new JSONObject(\r
-            "{\"field\": \"event.faultFields.version\",\"value\": \"2.0\",\"fieldType\": \"number\"}");\r
+                "{\"field\": \"event.faultFields.version\",\"value\": \"2.0\",\"fieldType\": \"number\"}");\r
         ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);\r
         cpEvent.addAttribute(jsonArgs);\r
         final String responseData = cpEvent.getEventObjectVal("event.faultFields.version").toString();\r
@@ -142,7 +142,7 @@ public class TestConfigProcessor {
         System.out.println("event==" + jsonObject.toString());\r
         //System.out.println("functionRole==" + functionRole);\r
         final JSONObject jsonArgs = new JSONObject(\r
-            "{\"field\": \"event.faultFields.version\",\"value\": \"2.0\",\"fieldType\": \"number\"}");\r
+                "{\"field\": \"event.faultFields.version\",\"value\": \"2.0\",\"fieldType\": \"number\"}");\r
         ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);\r
         cpEvent.updateAttribute(jsonArgs);\r
         final String responseData = cpEvent.getEventObjectVal("event.faultFields.version").toString();\r
@@ -156,16 +156,16 @@ public class TestConfigProcessor {
 \r
         final JSONObject jsonObject = getFileAsJsonObject();\r
         final String eventType = (jsonObject.getJSONObject("event")).getJSONObject("commonEventHeader").get("eventType")\r
-            .toString();\r
+                .toString();\r
         final String domain = (jsonObject.getJSONObject("event")).getJSONObject("commonEventHeader").get("domain")\r
-            .toString();\r
+                .toString();\r
         final String alarmCondition = (jsonObject.getJSONObject("event")).getJSONObject("faultFields")\r
-            .get("alarmCondition").toString();\r
+                .get("alarmCondition").toString();\r
         System.out.println("event==" + jsonObject.toString());\r
         final String eventName = domain + "_" + eventType + "_" + alarmCondition;\r
         System.out.println("eventName==" + eventName);\r
         final JSONObject jsonArgs = new JSONObject(\r
-            "{\"field\":\"event.commonEventHeader.eventName\",\"concatenate\": [\"$event.commonEventHeader.domain\",\"$event.commonEventHeader.eventType\",\"$event.faultFields.alarmCondition\"],\"delimiter\":\"_\"}");\r
+                "{\"field\":\"event.commonEventHeader.eventName\",\"concatenate\": [\"$event.commonEventHeader.domain\",\"$event.commonEventHeader.eventType\",\"$event.faultFields.alarmCondition\"],\"delimiter\":\"_\"}");\r
         ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);\r
         cpEvent.concatenateValue(jsonArgs);\r
         final String responseData = cpEvent.getEventObjectVal("event.commonEventHeader.eventName").toString();\r
@@ -179,14 +179,14 @@ public class TestConfigProcessor {
 \r
         final JSONObject jsonObject = getFileAsJsonObject();\r
         final String memoryConfigured = (jsonObject.getJSONObject("event")).getJSONObject("faultFields")\r
-            .get("memoryConfigured").toString();\r
+                .get("memoryConfigured").toString();\r
         final String memoryUsed = (jsonObject.getJSONObject("event")).getJSONObject("faultFields").get("memoryUsed")\r
-            .toString();\r
+                .toString();\r
         System.out.println("event==" + jsonObject.toString());\r
         System.out.println("memoryConfigured==" + memoryConfigured);\r
         System.out.println("memoryUsed==" + memoryUsed);\r
         final JSONObject jsonArgs = new JSONObject(\r
-            "{\"field\": \"event.faultFields.memoryFree\",\"subtract\": [\"$event.faultFields.memoryConfigured\",\"$event.faultFields.memoryUsed\"]}");\r
+                "{\"field\": \"event.faultFields.memoryFree\",\"subtract\": [\"$event.faultFields.memoryConfigured\",\"$event.faultFields.memoryUsed\"]}");\r
         ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);\r
         cpEvent.subtractValue(jsonArgs);\r
         final String responseData = cpEvent.getEventObjectVal("event.faultFields.memoryFree").toString();\r
@@ -202,7 +202,7 @@ public class TestConfigProcessor {
         System.out.println("event==" + jsonObject.toString());\r
         System.out.println("Testing SetValue");\r
         final JSONObject jsonArgs = new JSONObject(\r
-            "{\"field\": \"event.faultFields.version\",\"value\": \"2.0\",\"fieldType\": \"number\"}");\r
+                "{\"field\": \"event.faultFields.version\",\"value\": \"2.0\",\"fieldType\": \"number\"}");\r
         ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);\r
         cpEvent.setValue(jsonArgs);\r
         final String responseData = cpEvent.getEventObjectVal("event.faultFields.version").toString();\r
diff --git a/src/test/java/org/onap/dcae/vestest/TestEventReceipt.java b/src/test/java/org/onap/dcae/vestest/TestEventReceipt.java
deleted file mode 100644 (file)
index a3893ea..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/*-\r
- * ============LICENSE_START=======================================================\r
- * PROJECT\r
- * ================================================================================\r
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.\r
- * ================================================================================\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *      http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ============LICENSE_END=========================================================\r
- */\r
-package org.onap.dcae.vestest;\r
-\r
-import static org.junit.Assert.assertEquals;\r
-\r
-import org.json.JSONObject;\r
-import org.junit.Test;\r
-import org.onap.dcae.commonFunction.CommonStartup;\r
-import org.onap.dcae.restapi.endpoints.EventReceipt;\r
-\r
-public class TestEventReceipt {\r
-\r
-\r
-    @Test\r
-    public void shouldGetSchemaFileLocationBasedOnVersion() {\r
-        CommonStartup.schemaFileJson = new JSONObject("{\"v1\":\"filePath1\", \"v5\":\"filePath2\"}");\r
-        String schemaFilePath = EventReceipt.schemaFileVersion("v5");\r
-        assertEquals(schemaFilePath, "filePath2");\r
-    }\r
-\r
-    @Test\r
-    public void shouldByDefaultReturnV5SchemaFileLocation() {\r
-        CommonStartup.schemaFileJson = new JSONObject("{\"v1\":\"filePath1\", \"v5\":\"filePath2\"}");\r
-        String schemaFilePath = EventReceipt.schemaFileVersion("v2");\r
-        assertEquals(schemaFilePath, "filePath2");\r
-    }\r
-\r
-}\r
index 0489811..c39fb01 100644 (file)
@@ -28,24 +28,24 @@ import com.google.gson.JsonParser;
 import java.io.IOException;
 import java.nio.file.Paths;
 import org.junit.Test;
-import org.onap.dcae.commonFunction.CommonStartup;
+import org.onap.dcae.SchemaValidator;
 
 public class TestJsonSchemaValidation {
 
     @Test
     public void shouldValidEventPassSchema_27_2() throws IOException {
-        String result = CommonStartup.validateAgainstSchema(
-            readJSONFromFile("src/test/resources/VES_valid.txt").toString(),
-            readJSONFromFile("etc/CommonEventFormat_27.2.json").toString());
+        String result = SchemaValidator.validateAgainstSchema(
+                readJSONFromFile("src/test/resources/VES_valid.txt").toString(),
+                readJSONFromFile("etc/CommonEventFormat_27.2.json").toString());
         assertEquals(result, "true");
     }
 
 
     @Test
     public void shouldInvalidEventDoesNotPassSchema_27_2() throws IOException {
-        String result = CommonStartup.validateAgainstSchema(
-            readJSONFromFile("src/test/resources/VES_invalid.txt").toString(),
-            readJSONFromFile("etc/CommonEventFormat_27.2.json").toString());
+        String result = SchemaValidator.validateAgainstSchema(
+                readJSONFromFile("src/test/resources/VES_invalid.txt").toString(),
+                readJSONFromFile("etc/CommonEventFormat_27.2.json").toString());
         assertEquals(result, "false");
     }