--- /dev/null
+/*
+ * ============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);
+ }
+}
+
--- /dev/null
+/*
+ * ============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);
+ }
+}
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.cmdLine.NsaCommandLineUtil;\r
-import com.att.nsa.drumlin.service.framework.DrumlinServlet;\r
-import com.att.nsa.drumlin.till.nv.impl.nvPropertiesFile;\r
-import com.att.nsa.drumlin.till.nv.impl.nvReadableStack;\r
-import com.att.nsa.drumlin.till.nv.impl.nvReadableTable;\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.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.report.ProcessingReport;\r
import com.github.fge.jsonschema.main.JsonSchema;\r
import com.github.fge.jsonschema.main.JsonSchemaFactory;\r
-import java.io.IOException;\r
-import java.net.URL;\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.Map;\r
-import java.util.concurrent.ExecutorService;\r
-import java.util.concurrent.Executors;\r
-import java.util.concurrent.LinkedBlockingQueue;\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 String KCONFIG = "c";\r
- private static final String KSETTING_PORT = "collector.service.port";\r
- private static final int KDEFAULT_PORT = 8080;\r
- private static final String KSETTING_SECUREPORT = "collector.service.secure.port";\r
- private static final int KDEFAULT_SECUREPORT = -1;\r
- private static final String KSETTING_KEYSTOREPASSFILE = "collector.keystore.passwordfile";\r
- private static final String KDEFAULT_KEYSTOREPASSFILE = "../etc/passwordfile";\r
- private static final String KSETTING_KEYSTOREFILE = "collector.keystore.file.location";\r
- private static final String KDEFAULT_KEYSTOREFILE = "../etc/keystore";\r
- private static final String KSETTING_KEYALIAS = "collector.keystore.alias";\r
- private static final String KDEFAULT_KEYALIAS = "tomcat";\r
- private static final String KSETTING_DMAAPCONFIGS = "collector.dmaapfile";\r
- private static final String[] KDEFAULT_DMAAPCONFIGS = new String[]{"/etc/DmaapConfig.json"};\r
- private static final String KSETTING_SCHEMAVALIDATOR = "collector.schema.checkflag";\r
- private static final int KDEFAULT_SCHEMAVALIDATOR = -1;\r
- private static final String KSETTING_SCHEMAFILE = "collector.schema.file";\r
- private static final String KDEFAULT_SCHEMAFILE = "{\"v5\":\"./etc/CommonEventFormat_28.3.json\"}";\r
- private static final String KSETTING_DMAAPSTREAMID = "collector.dmaap.streamid";\r
- private static final String KSETTING_AUTHFLAG = "header.authflag";\r
- private static final int KDEFAULT_AUTHFLAG = 0;\r
- private static final String KSETTING_EVENTTRANSFORMFLAG = "event.transform.flag";\r
- private static final int KDEFAULT_EVENTTRANSFORMFLAG = 1;\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
- public static final String KSETTING_AUTHLIST = "header.authlist";\r
- static final int KDEFAULT_MAXQUEUEDEVENTS = 1024 * 4;\r
- public static int schemaValidatorflag = -1;\r
- public static int authflag = 1;\r
- static int eventTransformFlag = 1;\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 String streamID;\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(rrNvReadable settings) throws loadException, IOException, rrNvReadable.missingReqdSetting {\r
+ private CommonStartup(ApplicationSettings settings) throws loadException, IOException, rrNvReadable.missingReqdSetting {\r
final List<ApiServerConnector> connectors = new LinkedList<>();\r
\r
- if (settings.getInt(KSETTING_PORT, KDEFAULT_PORT) > 0) {\r
- connectors.add(new ApiServerConnector.Builder(settings.getInt(KSETTING_PORT, KDEFAULT_PORT)).secure(false)\r
- .build());\r
+ if (!settings.authorizationEnabled()) {\r
+ connectors.add(new ApiServerConnector.Builder(settings.httpPort()).secure(false).build());\r
}\r
\r
- final int securePort = settings.getInt(KSETTING_SECUREPORT, KDEFAULT_SECUREPORT);\r
- final String keystoreFile = settings.getString(KSETTING_KEYSTOREFILE, KDEFAULT_KEYSTOREFILE);\r
- final String keystorePasswordFile = settings.getString(KSETTING_KEYSTOREPASSFILE, KDEFAULT_KEYSTOREPASSFILE);\r
- final String keyAlias = settings.getString(KSETTING_KEYALIAS, KDEFAULT_KEYALIAS);\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 (securePort > 0) {\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
+ .keystorePassword(keystorePassword).keystoreFile(keystoreFile).keyAlias(keyAlias).build());\r
\r
}\r
\r
- schemaValidatorflag = settings.getInt(KSETTING_SCHEMAVALIDATOR, KDEFAULT_SCHEMAVALIDATOR);\r
- if (schemaValidatorflag > 0) {\r
- String schemaFile = settings.getString(KSETTING_SCHEMAFILE, KDEFAULT_SCHEMAFILE);\r
- schemaFileJson = new JSONObject(schemaFile);\r
+ schemaValidatorflag = settings.jsonSchemaValidationEnabled();\r
+ maxQueueEvent = settings.maximumAllowedQueuedEvents();\r
+ if (schemaValidatorflag) {\r
+ schemaFileJson = settings.jsonSchema();\r
\r
}\r
- authflag = settings.getInt(CommonStartup.KSETTING_AUTHFLAG, CommonStartup.KDEFAULT_AUTHFLAG);\r
- String[] currentConfigFile = settings.getStrings(KSETTING_DMAAPCONFIGS, KDEFAULT_DMAAPCONFIGS);\r
- cambriaConfigFile = currentConfigFile[0];\r
- streamID = settings.getString(KSETTING_DMAAPSTREAMID, null);\r
- eventTransformFlag = settings.getInt(KSETTING_EVENTTRANSFORMFLAG, KDEFAULT_EVENTTRANSFORMFLAG);\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
public static void main(String[] args) {\r
try {\r
- final Map<String, String> argMap = NsaCommandLineUtil.processCmdLine(args, true);\r
- final String config = NsaCommandLineUtil.getSetting(argMap, KCONFIG, "collector.properties");\r
- final URL settingStream = DrumlinServlet.findStream(config, CommonStartup.class);\r
-\r
- final nvReadableStack settings = new nvReadableStack();\r
- settings.push(new nvPropertiesFile(settingStream));\r
- settings.push(new nvReadableTable(argMap));\r
\r
- fProcessingInputQueue = new LinkedBlockingQueue<>(CommonStartup.KDEFAULT_MAXQUEUEDEVENTS);\r
+ fProcessingInputQueue = new LinkedBlockingQueue<>(CommonStartup.maxQueueEvent);\r
\r
VESLogger.setUpEcompLogging();\r
\r
- CommonStartup cs = new CommonStartup(settings);\r
+ CommonStartup cs = new CommonStartup(new ApplicationSettings(args, CLIUtils::processCmdLine));\r
\r
Thread commonStartupThread = new Thread(cs);\r
commonStartupThread.start();\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
+ *\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
import com.att.nsa.logging.log4j.EcompFields;\r
import com.google.common.reflect.TypeToken;\r
import com.google.gson.Gson;\r
-import java.util.Map;\r
import org.json.JSONObject;\r
import org.onap.dcae.commonFunction.event.publishing.EventPublisher;\r
import org.slf4j.Logger;\r
import java.util.Date;\r
import java.util.HashMap;\r
import java.util.List;\r
-\r
+import java.util.Map;\r
\r
class EventProcessor implements Runnable {\r
\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>>() {\r
- }.getType();\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
+ static Map<String, String[]> streamidHash = new HashMap<>();\r
+ public JSONObject event;\r
private EventPublisher eventPublisher;\r
\r
public EventProcessor(EventPublisher eventPublisher) {\r
this.eventPublisher = eventPublisher;\r
- streamidHash = parseStreamIdToStreamHashMapping(CommonStartup.streamID);\r
- }\r
-\r
- private Map<String, String[]> parseStreamIdToStreamHashMapping(String streamId) {\r
- Map<String, String[]> streamidHash = new HashMap<>();\r
- String[] list = streamId.split("\\|");\r
- for (String aList : list) {\r
- String domain = aList.split("=")[0];\r
- String[] streamIdList = aList.substring(aList.indexOf('=') + 1).split(",");\r
- streamidHash.put(domain, streamIdList);\r
- }\r
- return streamidHash;\r
+ streamidHash = CommonStartup.streamID.toJavaMap();\r
}\r
\r
-\r
@Override\r
public void run() {\r
try {\r
log.error("EventProcessor InterruptedException" + e.getMessage());\r
Thread.currentThread().interrupt();\r
}\r
-\r
}\r
\r
public void overrideEvent() {\r
// Set collector timestamp in event payload before publish\r
addCurrentTimeToEvent(event);\r
\r
- if (CommonStartup.eventTransformFlag == 1) {\r
+ if (CommonStartup.eventTransformFlag) {\r
// read the mapping json file\r
try (FileReader fr = new FileReader("./etc/eventTransform.json")) {\r
log.info("parse eventTransform.json");\r
log.error("Couldn't find file ./etc/eventTransform.json" + e.toString());\r
}\r
}\r
- // Remove VESversion from event. This field is for internal use and must\r
- // be removed after use.\r
+ // Remove VESversion from event. This field is for internal use and must be removed after use.\r
if (event.has("VESversion"))\r
event.remove("VESversion");\r
\r
log.debug("Modified event:" + event);\r
-\r
}\r
\r
private void sendEventsToStreams(String[] streamIdList) {\r
log.info("Invoking publisher for streamId:" + aStreamIdList);\r
this.overrideEvent();\r
eventPublisher.sendEvent(event, aStreamIdList);\r
-\r
}\r
}\r
\r
}\r
\r
void parseEventsJson(List<Event> eventsTransform, ConfigProcessorAdapter configProcessorAdapter) {\r
-\r
// load VESProcessors class at runtime\r
for (Event eventTransform : eventsTransform) {\r
JSONObject filterObj = new JSONObject(eventTransform.filter.toString());\r
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) {
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
\r
private static String authCredentialsList;\r
\r
- public RestfulCollectorServlet ( rrNvReadable settings ) throws loadException, missingReqdSetting\r
+ public RestfulCollectorServlet ( ApplicationSettings settings ) throws loadException, missingReqdSetting\r
{\r
- super ( settings, "collector", false );\r
- authCredentialsList = settings.getString(CommonStartup.KSETTING_AUTHLIST, null);\r
+ super ( settings.torrNvReadable(), "collector", false );\r
+ authCredentialsList = settings.validAuthorizationCredentials();\r
}\r
\r
\r
final DrumlinPlayishRoutingFileSource drs = new DrumlinPlayishRoutingFileSource ( routes );\r
drr.addRouteSource ( drs );\r
\r
- if (CommonStartup.authflag > 0) {\r
+ if (CommonStartup.authflag) {\r
NsaAuthenticator<NsaSimpleApiKey> NsaAuth;\r
NsaAuth = createAuthenticator(authCredentialsList);\r
\r
}\r
\r
try {\r
- if (CommonStartup.authflag == 1) {\r
+ if (CommonStartup.authflag) {\r
userId = getUser (ctx);\r
retkey = NsaBaseEndpoint.getAuthenticatedUser(ctx);\r
}\r
JSONArray jsonArrayMod = new JSONArray();\r
JSONObject event;\r
FileReader fr;\r
- if (retkey != null || CommonStartup.authflag == 0) {\r
- if (CommonStartup.schemaValidatorflag > 0) {\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
--- /dev/null
+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
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() {
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);
@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;
@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";
// then
assertEquals(authentic.getSecret(), password1UnHashed);
}
-
-
}
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;
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;
@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
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;
/**
* @author Pawel Szalapski (pawel.szalapski@nokia.com)
*/
-final class TestingUtilities {
+public final class TestingUtilities {
private TestingUtilities() {
// utility class, no objects allowed
});
}
+ 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)));
}