diff options
-rw-r--r-- | src/main/java/org/onap/dcae/ApplicationSettings.java | 176 | ||||
-rw-r--r-- | src/main/java/org/onap/dcae/CLIUtils.java | 60 | ||||
-rw-r--r-- | src/main/java/org/onap/dcae/commonFunction/CommonStartup.java | 107 | ||||
-rw-r--r-- | src/main/java/org/onap/dcae/commonFunction/EventProcessor.java | 37 | ||||
-rw-r--r-- | src/main/java/org/onap/dcae/commonFunction/event/publishing/DMaaPConfigurationParser.java | 3 | ||||
-rw-r--r-- | src/main/java/org/onap/dcae/restapi/RestfulCollectorServlet.java | 9 | ||||
-rw-r--r-- | src/main/java/org/onap/dcae/restapi/endpoints/EventReceipt.java | 6 | ||||
-rw-r--r-- | src/test/java/org/onap/dcae/ApplicationSettingsTest.java | 417 | ||||
-rw-r--r-- | src/test/java/org/onap/dcae/commonFunction/CommonStartupTest.java (renamed from src/test/java/org/onap/dcae/commonFunction/TestCommonStartup.java) | 16 | ||||
-rw-r--r-- | src/test/java/org/onap/dcae/commonFunction/EventProcessorTest.java | 6 | ||||
-rw-r--r-- | src/test/java/org/onap/dcae/vestest/TestingUtilities.java | 15 |
11 files changed, 733 insertions, 119 deletions
diff --git a/src/main/java/org/onap/dcae/ApplicationSettings.java b/src/main/java/org/onap/dcae/ApplicationSettings.java new file mode 100644 index 00000000..0ebd1e90 --- /dev/null +++ b/src/main/java/org/onap/dcae/ApplicationSettings.java @@ -0,0 +1,176 @@ +/* + * ============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.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.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 java.io.File; +import java.nio.file.Paths; + +/** + * Abstraction over application configuration. + * Its job is to provide easily discoverable (by method names lookup) and type safe access to configuration properties. + */ +public class ApplicationSettings { + + private static final Logger inlog = LoggerFactory.getLogger(ApplicationSettings.class); + private static final String COLLECTOR_PROPERTIES = "etc/collector.properties"; + private final PropertiesConfiguration properties = new PropertiesConfiguration(); + + public ApplicationSettings(String[] args, Function1<String[], Map<String, String>> argsParser) { + 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(); + loadProperties(e); + }); + } + + private void loadProperties(String property){ + try { + properties.load(property); + } catch (ConfigurationException ex) { + inlog.error("Cannot load properties cause:", ex); + throw new RuntimeException(ex); + } + } + + public String validAuthorizationCredentials() { + return properties.getString("header.authlist", null); + } + + public int maximumAllowedQueuedEvents() { + return properties.getInt("collector.inputQueue.maxPending", 1024 * 4); + } + + public boolean jsonSchemaValidationEnabled() { + return properties.getInt("collector.schema.checkflag", -1) > 0; + } + + public boolean authorizationEnabled() { + return properties.getInt("header.authflag", 0) > 0; + } + + public JSONObject jsonSchema() { + return new JSONObject( + properties.getString("collector.schema.file", "{\"v5\":\"./etc/CommonEventFormat_28.3.json\"}")); + } + + public int httpPort() { + return properties.getInt("collector.service.port", 8080); + } + + public int httpsPort() { + return properties.getInt("collector.service.secure.port", 8443); + } + + public boolean httpsEnabled() { + return httpsPort() > 0; + } + + public boolean eventTransformingEnabled() { + return properties.getInt("event.transform.flag", 1) > 0; + } + + public String keystorePasswordFileLocation() { + return properties.getString("collector.keystore.passwordfile", "./etc/passwordfile"); + } + + public String keystoreFileLocation() { + return properties.getString("collector.keystore.file.location", "../etc/keystore"); + } + + public String keystoreAlias() { + return properties.getString("collector.keystore.alias", "tomcat"); + } + + public String exceptionConfigFileLocation() { + return properties.getString("exceptionConfig", null); + } + + public String cambriaConfigurationFileLocation() { + return properties.getString("collector.dmaapfile", "./etc/DmaapConfig.json"); + } + + public Map<String, String[]> dMaaPStreamsMapping() { + String streamIdsProperty = properties.getString("collector.dmaap.streamid", null); + if (streamIdsProperty == null) { + return HashMap.empty(); + } else { + return convertDMaaPStreamsPropertyToMap(streamIdsProperty); + } + } + + /* + * 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("\\|"); + for (String t : topics) { + String domain = t.split("=")[0]; + String[] streamIds = t.split("=")[1].split(","); + domainToStreamIdsMapping.put(domain, streamIds); + } + return HashMap.ofAll(domainToStreamIdsMapping); + } + + private void updateProperty(String key, String value) { + if (properties.containsKey(key)) { + properties.setProperty(key, value); + } else { + properties.addProperty(key, value); + } + } + + @VisibleForTesting + String getStringDirectly(String key) { + return properties.getString(key); + } +} + diff --git a/src/main/java/org/onap/dcae/CLIUtils.java b/src/main/java/org/onap/dcae/CLIUtils.java new file mode 100644 index 00000000..6450d2e5 --- /dev/null +++ b/src/main/java/org/onap/dcae/CLIUtils.java @@ -0,0 +1,60 @@ +/* + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * 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; + +import java.util.HashMap; + +/** + * 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> (); + + String lastKey = null; + 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 ); + } + lastKey = null; + } + } + if ( lastKey != null ) + { + map.put ( lastKey.substring(1), "" ); + } + return io.vavr.collection.HashMap.ofAll(map); + } +} diff --git a/src/main/java/org/onap/dcae/commonFunction/CommonStartup.java b/src/main/java/org/onap/dcae/commonFunction/CommonStartup.java index 3469531e..36713aa4 100644 --- a/src/main/java/org/onap/dcae/commonFunction/CommonStartup.java +++ b/src/main/java/org/onap/dcae/commonFunction/CommonStartup.java @@ -23,14 +23,8 @@ package org.onap.dcae.commonFunction; import com.att.nsa.apiServer.ApiServer;
import com.att.nsa.apiServer.ApiServerConnector;
import com.att.nsa.apiServer.endpoints.NsaBaseEndpoint;
-import com.att.nsa.cmdLine.NsaCommandLineUtil;
-import com.att.nsa.drumlin.service.framework.DrumlinServlet;
-import com.att.nsa.drumlin.till.nv.impl.nvPropertiesFile;
-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.att.nsa.drumlin.till.nv.rrNvReadable.loadException;
-import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
@@ -39,99 +33,75 @@ 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 java.io.IOException;
-import java.net.URL;
-import java.nio.file.Files;
-import java.nio.file.Paths;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.LinkedBlockingQueue;
import org.apache.catalina.LifecycleException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
+import org.onap.dcae.ApplicationSettings;
+import org.onap.dcae.CLIUtils;
import org.onap.dcae.commonFunction.event.publishing.DMaaPConfigurationParser;
import org.onap.dcae.commonFunction.event.publishing.EventPublisher;
import org.onap.dcae.restapi.RestfulCollectorServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+
public class CommonStartup extends NsaBaseEndpoint implements Runnable {
- private static final String KCONFIG = "c";
- private static final String KSETTING_PORT = "collector.service.port";
- private static final int KDEFAULT_PORT = 8080;
- private static final String KSETTING_SECUREPORT = "collector.service.secure.port";
- private static final int KDEFAULT_SECUREPORT = -1;
- private static final String KSETTING_KEYSTOREPASSFILE = "collector.keystore.passwordfile";
- private static final String KDEFAULT_KEYSTOREPASSFILE = "../etc/passwordfile";
- private static final String KSETTING_KEYSTOREFILE = "collector.keystore.file.location";
- private static final String KDEFAULT_KEYSTOREFILE = "../etc/keystore";
- private static final String KSETTING_KEYALIAS = "collector.keystore.alias";
- private static final String KDEFAULT_KEYALIAS = "tomcat";
- private static final String KSETTING_DMAAPCONFIGS = "collector.dmaapfile";
- private static final String[] KDEFAULT_DMAAPCONFIGS = new String[]{"/etc/DmaapConfig.json"};
- private static final String KSETTING_SCHEMAVALIDATOR = "collector.schema.checkflag";
- private static final int KDEFAULT_SCHEMAVALIDATOR = -1;
- private static final String KSETTING_SCHEMAFILE = "collector.schema.file";
- private static final String KDEFAULT_SCHEMAFILE = "{\"v5\":\"./etc/CommonEventFormat_28.3.json\"}";
- private static final String KSETTING_DMAAPSTREAMID = "collector.dmaap.streamid";
- private static final String KSETTING_AUTHFLAG = "header.authflag";
- private static final int KDEFAULT_AUTHFLAG = 0;
- private static final String KSETTING_EVENTTRANSFORMFLAG = "event.transform.flag";
- private static final int KDEFAULT_EVENTTRANSFORMFLAG = 1;
private static final Logger metriclog = LoggerFactory.getLogger("com.att.ecomp.metrics");
public static final Logger inlog = LoggerFactory.getLogger("org.onap.dcae.commonFunction.input");
static final Logger oplog = LoggerFactory.getLogger("org.onap.dcae.commonFunction.output");
public static final Logger eplog = LoggerFactory.getLogger("org.onap.dcae.commonFunction.error");
- public static final String KSETTING_AUTHLIST = "header.authlist";
- static final int KDEFAULT_MAXQUEUEDEVENTS = 1024 * 4;
- public static int schemaValidatorflag = -1;
- public static int authflag = 1;
- static int eventTransformFlag = 1;
+ static int maxQueueEvent = 1024 * 4;
+ public static boolean schemaValidatorflag = false;
+ public static boolean authflag = false;
+ static boolean eventTransformFlag = true;
public static JSONObject schemaFileJson;
static String cambriaConfigFile;
- public static String streamID;
+ public static io.vavr.collection.Map<String , String [] > streamID;
static LinkedBlockingQueue<JSONObject> fProcessingInputQueue;
private static ApiServer fTomcatServer = null;
private static final Logger log = LoggerFactory.getLogger(CommonStartup.class);
- private CommonStartup(rrNvReadable settings) throws loadException, IOException, rrNvReadable.missingReqdSetting {
+ private CommonStartup(ApplicationSettings settings) throws loadException, IOException, rrNvReadable.missingReqdSetting {
final List<ApiServerConnector> connectors = new LinkedList<>();
- if (settings.getInt(KSETTING_PORT, KDEFAULT_PORT) > 0) {
- connectors.add(new ApiServerConnector.Builder(settings.getInt(KSETTING_PORT, KDEFAULT_PORT)).secure(false)
- .build());
+ if (!settings.authorizationEnabled()) {
+ connectors.add(new ApiServerConnector.Builder(settings.httpPort()).secure(false).build());
}
- final int securePort = settings.getInt(KSETTING_SECUREPORT, KDEFAULT_SECUREPORT);
- final String keystoreFile = settings.getString(KSETTING_KEYSTOREFILE, KDEFAULT_KEYSTOREFILE);
- final String keystorePasswordFile = settings.getString(KSETTING_KEYSTOREPASSFILE, KDEFAULT_KEYSTOREPASSFILE);
- final String keyAlias = settings.getString(KSETTING_KEYALIAS, KDEFAULT_KEYALIAS);
+ final int securePort = settings.httpsPort();
+ final String keystoreFile = settings.keystoreFileLocation();
+ final String keystorePasswordFile = settings.keystorePasswordFileLocation();
+ final String keyAlias = settings.keystoreAlias();
- if (securePort > 0) {
+ if (settings.authorizationEnabled()) {
String keystorePassword = readFile(keystorePasswordFile);
connectors.add(new ApiServerConnector.Builder(securePort).secure(true)
- .keystorePassword(keystorePassword).keystoreFile(keystoreFile).keyAlias(keyAlias).build());
+ .keystorePassword(keystorePassword).keystoreFile(keystoreFile).keyAlias(keyAlias).build());
}
- schemaValidatorflag = settings.getInt(KSETTING_SCHEMAVALIDATOR, KDEFAULT_SCHEMAVALIDATOR);
- if (schemaValidatorflag > 0) {
- String schemaFile = settings.getString(KSETTING_SCHEMAFILE, KDEFAULT_SCHEMAFILE);
- schemaFileJson = new JSONObject(schemaFile);
+ schemaValidatorflag = settings.jsonSchemaValidationEnabled();
+ maxQueueEvent = settings.maximumAllowedQueuedEvents();
+ if (schemaValidatorflag) {
+ schemaFileJson = settings.jsonSchema();
}
- authflag = settings.getInt(CommonStartup.KSETTING_AUTHFLAG, CommonStartup.KDEFAULT_AUTHFLAG);
- String[] currentConfigFile = settings.getStrings(KSETTING_DMAAPCONFIGS, KDEFAULT_DMAAPCONFIGS);
- cambriaConfigFile = currentConfigFile[0];
- streamID = settings.getString(KSETTING_DMAAPSTREAMID, null);
- eventTransformFlag = settings.getInt(KSETTING_EVENTTRANSFORMFLAG, KDEFAULT_EVENTTRANSFORMFLAG);
+ authflag = settings.authorizationEnabled();
+ cambriaConfigFile = settings.cambriaConfigurationFileLocation();
+ streamID = settings.dMaaPStreamsMapping();
+ eventTransformFlag = settings.eventTransformingEnabled();
fTomcatServer = new ApiServer.Builder(connectors, new RestfulCollectorServlet(settings)).encodeSlashes(true)
.name("collector").build();
@@ -139,19 +109,12 @@ public class CommonStartup extends NsaBaseEndpoint implements Runnable { public static void main(String[] args) {
try {
- final Map<String, String> argMap = NsaCommandLineUtil.processCmdLine(args, true);
- final String config = NsaCommandLineUtil.getSetting(argMap, KCONFIG, "collector.properties");
- final URL settingStream = DrumlinServlet.findStream(config, CommonStartup.class);
-
- final nvReadableStack settings = new nvReadableStack();
- settings.push(new nvPropertiesFile(settingStream));
- settings.push(new nvReadableTable(argMap));
- fProcessingInputQueue = new LinkedBlockingQueue<>(CommonStartup.KDEFAULT_MAXQUEUEDEVENTS);
+ fProcessingInputQueue = new LinkedBlockingQueue<>(CommonStartup.maxQueueEvent);
VESLogger.setUpEcompLogging();
- CommonStartup cs = new CommonStartup(settings);
+ CommonStartup cs = new CommonStartup(new ApplicationSettings(args, CLIUtils::processCmdLine));
Thread commonStartupThread = new Thread(cs);
commonStartupThread.start();
diff --git a/src/main/java/org/onap/dcae/commonFunction/EventProcessor.java b/src/main/java/org/onap/dcae/commonFunction/EventProcessor.java index 9d6ad360..a57ea3f0 100644 --- a/src/main/java/org/onap/dcae/commonFunction/EventProcessor.java +++ b/src/main/java/org/onap/dcae/commonFunction/EventProcessor.java @@ -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.
@@ -25,7 +25,6 @@ import com.att.nsa.logging.LoggingContext; import com.att.nsa.logging.log4j.EcompFields;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
-import java.util.Map;
import org.json.JSONObject;
import org.onap.dcae.commonFunction.event.publishing.EventPublisher;
import org.slf4j.Logger;
@@ -40,38 +39,25 @@ import java.util.Arrays; import java.util.Date;
import java.util.HashMap;
import java.util.List;
-
+import java.util.Map;
class EventProcessor implements Runnable {
private static final Logger log = LoggerFactory.getLogger(EventProcessor.class);
private static final String EVENT_LITERAL = "event";
private static final String COMMON_EVENT_HEADER = "commonEventHeader";
- static final Type EVENT_LIST_TYPE = new TypeToken<List<Event>>() {
- }.getType();
+ static final Type EVENT_LIST_TYPE = new TypeToken<List<Event>>() {}.getType();
private final SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, MM dd yyyy hh:mm:ss z");
- static Map<String, String[]> streamidHash = new HashMap<>();
- public JSONObject event;
+ static Map<String, String[]> streamidHash = new HashMap<>();
+ public JSONObject event;
private EventPublisher eventPublisher;
public EventProcessor(EventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
- streamidHash = parseStreamIdToStreamHashMapping(CommonStartup.streamID);
- }
-
- private Map<String, String[]> parseStreamIdToStreamHashMapping(String streamId) {
- Map<String, String[]> streamidHash = new HashMap<>();
- String[] list = streamId.split("\\|");
- for (String aList : list) {
- String domain = aList.split("=")[0];
- String[] streamIdList = aList.substring(aList.indexOf('=') + 1).split(",");
- streamidHash.put(domain, streamIdList);
- }
- return streamidHash;
+ streamidHash = CommonStartup.streamID.toJavaMap();
}
-
@Override
public void run() {
try {
@@ -101,14 +87,13 @@ class EventProcessor implements Runnable { log.error("EventProcessor InterruptedException" + e.getMessage());
Thread.currentThread().interrupt();
}
-
}
public void overrideEvent() {
// Set collector timestamp in event payload before publish
addCurrentTimeToEvent(event);
- if (CommonStartup.eventTransformFlag == 1) {
+ if (CommonStartup.eventTransformFlag) {
// read the mapping json file
try (FileReader fr = new FileReader("./etc/eventTransform.json")) {
log.info("parse eventTransform.json");
@@ -118,13 +103,11 @@ class EventProcessor implements Runnable { log.error("Couldn't find file ./etc/eventTransform.json" + e.toString());
}
}
- // Remove VESversion from event. This field is for internal use and must
- // be removed after use.
+ // Remove VESversion from event. This field is for internal use and must be removed after use.
if (event.has("VESversion"))
event.remove("VESversion");
log.debug("Modified event:" + event);
-
}
private void sendEventsToStreams(String[] streamIdList) {
@@ -132,7 +115,6 @@ class EventProcessor implements Runnable { log.info("Invoking publisher for streamId:" + aStreamIdList);
this.overrideEvent();
eventPublisher.sendEvent(event, aStreamIdList);
-
}
}
@@ -145,7 +127,6 @@ class EventProcessor implements Runnable { }
void parseEventsJson(List<Event> eventsTransform, ConfigProcessorAdapter configProcessorAdapter) {
-
// load VESProcessors class at runtime
for (Event eventTransform : eventsTransform) {
JSONObject filterObj = new JSONObject(eventTransform.filter.toString());
diff --git a/src/main/java/org/onap/dcae/commonFunction/event/publishing/DMaaPConfigurationParser.java b/src/main/java/org/onap/dcae/commonFunction/event/publishing/DMaaPConfigurationParser.java index bef14448..5865b12c 100644 --- a/src/main/java/org/onap/dcae/commonFunction/event/publishing/DMaaPConfigurationParser.java +++ b/src/main/java/org/onap/dcae/commonFunction/event/publishing/DMaaPConfigurationParser.java @@ -59,8 +59,7 @@ public final class DMaaPConfigurationParser { 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) { diff --git a/src/main/java/org/onap/dcae/restapi/RestfulCollectorServlet.java b/src/main/java/org/onap/dcae/restapi/RestfulCollectorServlet.java index 0d9df155..e5a29e9f 100644 --- a/src/main/java/org/onap/dcae/restapi/RestfulCollectorServlet.java +++ b/src/main/java/org/onap/dcae/restapi/RestfulCollectorServlet.java @@ -27,6 +27,7 @@ import java.net.URL; import javax.servlet.ServletException;
import org.apache.tomcat.util.codec.binary.Base64;
+import org.onap.dcae.ApplicationSettings;
import org.onap.dcae.commonFunction.CommonStartup;
import org.onap.dcae.commonFunction.VESLogger;
import org.slf4j.Logger;
@@ -53,10 +54,10 @@ public class RestfulCollectorServlet extends CommonServlet private static String authCredentialsList;
- public RestfulCollectorServlet ( rrNvReadable settings ) throws loadException, missingReqdSetting
+ public RestfulCollectorServlet ( ApplicationSettings settings ) throws loadException, missingReqdSetting
{
- super ( settings, "collector", false );
- authCredentialsList = settings.getString(CommonStartup.KSETTING_AUTHLIST, null);
+ super ( settings.torrNvReadable(), "collector", false );
+ authCredentialsList = settings.validAuthorizationCredentials();
}
@@ -91,7 +92,7 @@ public class RestfulCollectorServlet extends CommonServlet final DrumlinPlayishRoutingFileSource drs = new DrumlinPlayishRoutingFileSource ( routes );
drr.addRouteSource ( drs );
- if (CommonStartup.authflag > 0) {
+ if (CommonStartup.authflag) {
NsaAuthenticator<NsaSimpleApiKey> NsaAuth;
NsaAuth = createAuthenticator(authCredentialsList);
diff --git a/src/main/java/org/onap/dcae/restapi/endpoints/EventReceipt.java b/src/main/java/org/onap/dcae/restapi/endpoints/EventReceipt.java index d028a957..d60e2a11 100644 --- a/src/main/java/org/onap/dcae/restapi/endpoints/EventReceipt.java +++ b/src/main/java/org/onap/dcae/restapi/endpoints/EventReceipt.java @@ -100,7 +100,7 @@ public class EventReceipt extends NsaBaseEndpoint { }
try {
- if (CommonStartup.authflag == 1) {
+ if (CommonStartup.authflag) {
userId = getUser (ctx);
retkey = NsaBaseEndpoint.getAuthenticatedUser(ctx);
}
@@ -160,8 +160,8 @@ public class EventReceipt extends NsaBaseEndpoint { JSONArray jsonArrayMod = new JSONArray();
JSONObject event;
FileReader fr;
- if (retkey != null || CommonStartup.authflag == 0) {
- if (CommonStartup.schemaValidatorflag > 0) {
+ if (retkey != null || !CommonStartup.authflag) {
+ if (CommonStartup.schemaValidatorflag) {
if ((arrayFlag == 1) && (jsonObject.has("eventList") && (!jsonObject.has("event")))
|| ((arrayFlag == 0) && (!jsonObject.has("eventList") && (jsonObject.has("event"))))) {
fr = new FileReader(schemaFileVersion(vesVersion));
diff --git a/src/test/java/org/onap/dcae/ApplicationSettingsTest.java b/src/test/java/org/onap/dcae/ApplicationSettingsTest.java new file mode 100644 index 00000000..b162cef2 --- /dev/null +++ b/src/test/java/org/onap/dcae/ApplicationSettingsTest.java @@ -0,0 +1,417 @@ +package org.onap.dcae; + +/*- + * ============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========================================================= + */ + +import io.vavr.collection.HashMap; +import io.vavr.collection.Map; +import org.json.JSONObject; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.Objects; + +import static java.util.Collections.singletonList; +import static org.junit.Assert.*; +import static org.onap.dcae.CLIUtils.processCmdLine; + +public class ApplicationSettingsTest { + + @Test + public void shouldMakeApplicationSettingsOutOfCLIArguments() { + // given + String[] cliArguments = {"-param1", "param1value", "-param2", "param2value"}; + + // when + ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine); + String param1value = configurationAccessor.getStringDirectly("param1"); + String param2value = configurationAccessor.getStringDirectly("param2"); + + // then + assertEquals("param1value", param1value); + assertEquals("param2value", param2value); + } + + @Test + public void shouldMakeApplicationSettingsOutOfCLIArgumentsAndAConfigurationFile() + throws IOException { + // given + File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter"); + Files.write(tempConfFile.toPath(), Arrays.asList("section.subSection1=abc", "section.subSection2=zxc")); + tempConfFile.deleteOnExit(); + String[] cliArguments = {"-param1", "param1value", "-param2", "param2value", "-c", tempConfFile.toString()}; + + // when + ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine); + String param1value = configurationAccessor.getStringDirectly("param1"); + String param2value = configurationAccessor.getStringDirectly("param2"); + String fromFileParam1Value = configurationAccessor.getStringDirectly("section.subSection1"); + String fromFileParam2Value = configurationAccessor.getStringDirectly("section.subSection2"); + + // then + assertEquals("param1value", param1value); + assertEquals("param2value", param2value); + assertEquals("abc", fromFileParam1Value); + assertEquals("zxc", fromFileParam2Value); + } + + @Test + public void shouldCLIArgumentsOverrideConfigFileParameters() throws IOException { + // given + String[] cliArguments = {"-section.subSection1", "abc"}; + File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter"); + Files.write(tempConfFile.toPath(), singletonList("section.subSection1=zxc")); + tempConfFile.deleteOnExit(); + + // when + ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine); + String actuallyOverridenByCLIParam = configurationAccessor.getStringDirectly("section.subSection1"); + + // then + assertEquals("abc", actuallyOverridenByCLIParam); + } + + @Test + public void shouldReturnHTTPPort() throws IOException { + // when + int applicationPort = fromTemporaryConfiguration("collector.service.port=8090") + .httpPort(); + + // then + assertEquals(8090, applicationPort); + } + + @Test + public void shouldReturnDefaultHTTPPort() throws IOException { + // when + int applicationPort = fromTemporaryConfiguration().httpPort(); + + // then + assertEquals(8080, applicationPort); + } + + @Test + public void shouldReturnIfHTTPSIsEnabled() throws IOException { + // when + boolean httpsEnabled = fromTemporaryConfiguration("collector.service.secure.port=8443") + .httpsEnabled(); + + // then + assertTrue(httpsEnabled); + } + + @Test + public void shouldReturnIfHTTPIsEnabled() throws IOException { + // when + boolean httpsEnabled = fromTemporaryConfiguration("collector.service.port=8080").httpsEnabled(); + // then + assertTrue(httpsEnabled); + } + + @Test + public void shouldByDefaultHTTPSBeDisabled() throws IOException { + // when + boolean httpsEnabled = fromTemporaryConfiguration().httpsEnabled(); + + // then + assertTrue(httpsEnabled); + } + + @Test + public void shouldReturnHTTPSPort() throws IOException { + // when + int httpsPort = fromTemporaryConfiguration("collector.service.secure.port=8443") + .httpsPort(); + + // then + assertEquals(8443, httpsPort); + } + + @Test + public void shouldReturnLocationOfThePasswordFile() throws IOException { + // when + String passwordFileLocation = fromTemporaryConfiguration("collector.keystore.passwordfile=/somewhere/password").keystorePasswordFileLocation(); + + // then + assertEquals("/somewhere/password", passwordFileLocation); + } + + @Test + public void shouldReturnDefaultLocationOfThePasswordFile() throws IOException { + // when + String passwordFileLocation = fromTemporaryConfiguration().keystorePasswordFileLocation(); + + // then + assertEquals("./etc/passwordfile", passwordFileLocation); + } + + @Test + public void shouldReturnLocationOfTheKeystoreFile() throws IOException { + // when + String keystoreFileLocation = fromTemporaryConfiguration("collector.keystore.file.location=/somewhere/keystore") + .keystoreFileLocation(); + + // then + assertEquals("/somewhere/keystore", keystoreFileLocation); + } + + @Test + public void shouldReturnLocationOfTheDefaultKeystoreFile() throws IOException { + // when + String keystoreFileLocation = fromTemporaryConfiguration().keystoreFileLocation(); + + // then + assertEquals("../etc/keystore", keystoreFileLocation); + } + + + @Test + public void shouldReturnKeystoreAlias() throws IOException { + // when + String keystoreAlias = fromTemporaryConfiguration("collector.keystore.alias=alias").keystoreAlias(); + + // then + assertEquals("alias", keystoreAlias); + } + + @Test + public void shouldReturnDefaultKeystoreAlias() throws IOException { + // when + String keystoreAlias = fromTemporaryConfiguration().keystoreAlias(); + + // then + assertEquals("tomcat", keystoreAlias); + } + + @Test + public void shouldReturnDMAAPConfigFileLocation() throws IOException { + // when + String dmaapConfigFileLocation = fromTemporaryConfiguration("collector.dmaapfile=/somewhere/dmaapFile").cambriaConfigurationFileLocation(); + + // then + assertEquals("/somewhere/dmaapFile", dmaapConfigFileLocation); + } + + @Test + public void shouldReturnDefaultDMAAPConfigFileLocation() throws IOException { + // when + String dmaapConfigFileLocation = fromTemporaryConfiguration().cambriaConfigurationFileLocation(); + + // then + assertEquals("./etc/DmaapConfig.json", dmaapConfigFileLocation); + } + + @Test + public void shouldReturnMaximumAllowedQueuedEvents() throws IOException { + // when + int maximumAllowedQueuedEvents = fromTemporaryConfiguration("collector.inputQueue.maxPending=10000") + .maximumAllowedQueuedEvents(); + + // then + assertEquals(10000, maximumAllowedQueuedEvents); + } + + @Test + public void shouldReturnDefaultMaximumAllowedQueuedEvents() throws IOException { + // when + int maximumAllowedQueuedEvents = fromTemporaryConfiguration().maximumAllowedQueuedEvents(); + + // then + assertEquals(1024 * 4, maximumAllowedQueuedEvents); + } + + @Test + public void shouldTellIfSchemaValidationIsEnabled() throws IOException { + // when + boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration("collector.schema.checkflag=1") + .jsonSchemaValidationEnabled(); + + // then + assertTrue(jsonSchemaValidationEnabled); + } + + @Test + public void shouldByDefaultSchemaValidationBeDisabled() throws IOException { + // when + boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration().jsonSchemaValidationEnabled(); + + // then + assertFalse(jsonSchemaValidationEnabled); + } + + @Test + public void shouldReturnJSONSchema() throws IOException { + // when + JSONObject jsonSchema = fromTemporaryConfiguration("collector.schema.file={\"v1\": {}}") + .jsonSchema(); + + // then + assertEquals(new JSONObject("{\"v1\": {}}").toMap(), jsonSchema.toMap()); + } + + @Test + public void shouldReturnDefaultJSONSchema() throws IOException { + // when + JSONObject jsonSchema = fromTemporaryConfiguration().jsonSchema(); + + // then + assertEquals(new JSONObject("{\"v5\":\"./etc/CommonEventFormat_28.3.json\"}").toMap(), jsonSchema.toMap()); + } + + @Test + public void shouldReturnExceptionConfigFileLocation() throws IOException { + // when + String exceptionConfigFileLocation = fromTemporaryConfiguration("exceptionConfig=/somewhere/exceptionFile") + .exceptionConfigFileLocation(); + + // then + assertEquals("/somewhere/exceptionFile", exceptionConfigFileLocation); + } + + @Test + public void shouldReturnDefaultExceptionConfigFileLocation() throws IOException { + // when + String exceptionConfigFileLocation = fromTemporaryConfiguration().exceptionConfigFileLocation(); + + // then + assertNull(exceptionConfigFileLocation); + } + + + @Test + public void shouldReturnDMAAPStreamId() throws IOException { + // given + Map<String, String[]> expected = HashMap.of( + "s", new String[]{"something", "something2"}, + "s2", new String[]{"something3"} + ); + + // when + Map<String, String[]> dmaapStreamID = fromTemporaryConfiguration("collector.dmaap.streamid=s=something,something2|s2=something3") + .dMaaPStreamsMapping(); + + // then + assertArrayEquals(expected.get("s").get(), Objects.requireNonNull(dmaapStreamID).get("s").get()); + assertArrayEquals(expected.get("s2").get(), Objects.requireNonNull(dmaapStreamID).get("s2").get()); + assertEquals(expected.keySet(), dmaapStreamID.keySet()); + } + + @Test + public void shouldReturnDefaultDMAAPStreamId() throws IOException { + // when + Map<String, String[]> dmaapStreamID = fromTemporaryConfiguration().dMaaPStreamsMapping(); + + // then + assertEquals(dmaapStreamID, HashMap.empty()); + } + + @Test + public void shouldReturnIfAuthorizationIsEnabled() throws IOException { + // when + boolean authorizationEnabled = fromTemporaryConfiguration("header.authflag=1") + .authorizationEnabled(); + + // then + assertTrue(authorizationEnabled); + } + + @Test + public void shouldAuthorizationBeDisabledByDefault() throws IOException { + // when + boolean authorizationEnabled = fromTemporaryConfiguration().authorizationEnabled(); + + // then + assertFalse(authorizationEnabled); + } + + @Test + public void shouldReturnValidCredentials() throws IOException { + // when + String userToBase64PasswordDelimitedByCommaSeparatedByPipes = fromTemporaryConfiguration( + "header.authlist=pasza,123jsad1|someoneelse,12asd31" + ).validAuthorizationCredentials(); + + // then + assertEquals("pasza,123jsad1|someoneelse,12asd31", userToBase64PasswordDelimitedByCommaSeparatedByPipes); + } + + @Test + public void shouldbyDefaultThereShouldBeNoValidCredentials() throws IOException { + // when + String userToBase64PasswordDelimitedByCommaSeparatedByPipes = fromTemporaryConfiguration(). + validAuthorizationCredentials(); + + // then + assertNull(userToBase64PasswordDelimitedByCommaSeparatedByPipes); + } + + + @Test + public void shouldReturnIfEventTransformingIsEnabled() throws IOException { + // when + boolean isEventTransformingEnabled = fromTemporaryConfiguration("event.transform.flag=0") + .eventTransformingEnabled(); + + // then + assertFalse(isEventTransformingEnabled); + } + + @Test + public void shouldEventTransformingBeEnabledByDefault() throws IOException { + // when + boolean isEventTransformingEnabled = fromTemporaryConfiguration().eventTransformingEnabled(); + + // then + assertTrue(isEventTransformingEnabled); + } + + @Test + public void shouldReturnCambriaConfigurationFileLocation() throws IOException { + // when + String cambriaConfigurationFileLocation = fromTemporaryConfiguration("collector.dmaapfile=/somewhere/dmaapConfig") + .cambriaConfigurationFileLocation(); + + // then + assertEquals("/somewhere/dmaapConfig", cambriaConfigurationFileLocation); + } + + @Test + public void shouldReturnDefaultCambriaConfigurationFileLocation() throws IOException { + // when + String cambriaConfigurationFileLocation = fromTemporaryConfiguration() + .cambriaConfigurationFileLocation(); + + // then + assertEquals("./etc/DmaapConfig.json", cambriaConfigurationFileLocation); + } + + private static ApplicationSettings fromTemporaryConfiguration(String... fileLines) + throws IOException { + 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)); + } + + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dcae/commonFunction/TestCommonStartup.java b/src/test/java/org/onap/dcae/commonFunction/CommonStartupTest.java index 12428024..5a171484 100644 --- a/src/test/java/org/onap/dcae/commonFunction/TestCommonStartup.java +++ b/src/test/java/org/onap/dcae/commonFunction/CommonStartupTest.java @@ -39,16 +39,20 @@ 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 TestCommonStartup { +public class CommonStartupTest { @Test public void testParseCLIArguments() { @@ -67,7 +71,7 @@ public class TestCommonStartup { public void shouldPutValidVESEventOnProcessingQueueWithoutExceptions() throws IOException, QueueFullException { // given CommonStartup.fProcessingInputQueue = new LinkedBlockingQueue<>( - CommonStartup.KDEFAULT_MAXQUEUEDEVENTS); + 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); @@ -80,9 +84,9 @@ public class TestCommonStartup { @Test public void testParseStreamIdToStreamHashMapping() { // given - CommonStartup.streamID = "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)); + 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; @@ -94,7 +98,7 @@ public class TestCommonStartup { @Test public void testAuthListHandler() throws loadException, missingReqdSetting { // given - final nvReadableStack settings = new nvReadableStack(); + ApplicationSettings settings = new ApplicationSettings(new String[]{}, CLIUtils::processCmdLine); String user1 = "secureid"; String password1Hashed = "IWRjYWVSb2FkbTEyMyEt"; @@ -118,8 +122,6 @@ public class TestCommonStartup { // then assertEquals(authentic.getSecret(), password1UnHashed); } - - } diff --git a/src/test/java/org/onap/dcae/commonFunction/EventProcessorTest.java b/src/test/java/org/onap/dcae/commonFunction/EventProcessorTest.java index e211c12a..77ef005f 100644 --- a/src/test/java/org/onap/dcae/commonFunction/EventProcessorTest.java +++ b/src/test/java/org/onap/dcae/commonFunction/EventProcessorTest.java @@ -22,6 +22,8 @@ package org.onap.dcae.commonFunction; import com.google.gson.Gson; import java.util.concurrent.atomic.AtomicReference; + +import io.vavr.collection.HashMap; import org.json.JSONObject; import org.junit.Before; import org.junit.Test; @@ -29,6 +31,7 @@ import org.mockito.ArgumentCaptor; import java.util.List; import org.onap.dcae.commonFunction.event.publishing.EventPublisher; +import org.onap.dcae.vestest.TestingUtilities; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertTrue; @@ -45,8 +48,7 @@ public class EventProcessorTest { @Before public void setUp() { - CommonStartup.streamID = "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"; - CommonStartup.eventTransformFlag = 1; + 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"); } @Test diff --git a/src/test/java/org/onap/dcae/vestest/TestingUtilities.java b/src/test/java/org/onap/dcae/vestest/TestingUtilities.java index 7c7c09dc..eff31f6d 100644 --- a/src/test/java/org/onap/dcae/vestest/TestingUtilities.java +++ b/src/test/java/org/onap/dcae/vestest/TestingUtilities.java @@ -23,6 +23,8 @@ import static java.nio.file.Files.readAllBytes; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import io.vavr.collection.HashMap; + import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -30,7 +32,7 @@ import java.nio.file.Paths; /** * @author Pawel Szalapski (pawel.szalapski@nokia.com) */ -final class TestingUtilities { +public final class TestingUtilities { private TestingUtilities() { // utility class, no objects allowed @@ -50,6 +52,17 @@ final class TestingUtilities { }); } + public static HashMap<String, String[]> convertDMaaPStreamsPropertyToMap(String streamIdsProperty) { + java.util.HashMap<String, String[]> domainToStreamIdsMapping = new java.util.HashMap<>(); + String[] topics = streamIdsProperty.split("\\|"); + for (String t : topics) { + String domain = t.split("=")[0]; + String[] streamIds = t.split("=")[1].split(","); + domainToStreamIdsMapping.put(domain, streamIds); + } + return HashMap.ofAll(domainToStreamIdsMapping); + } + private static Path createFile(String path) { return rethrow(() -> Files.createFile(Paths.get(path))); } |