diff options
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/org/onap/dcae/ApplicationSettings.java | 53 | ||||
-rw-r--r-- | src/main/java/org/onap/dcae/VesApplication.java | 23 | ||||
-rw-r--r-- | src/main/java/org/onap/dcae/common/EventTransformation.java (renamed from src/main/java/org/onap/dcae/common/Event.java) | 5 | ||||
-rw-r--r-- | src/main/java/org/onap/dcae/common/EventUpdater.java | 28 | ||||
-rw-r--r-- | src/main/java/org/onap/dcae/common/HeaderUtils.java | 2 | ||||
-rw-r--r-- | src/main/java/org/onap/dcae/restapi/EventValidator.java | 8 |
6 files changed, 67 insertions, 52 deletions
diff --git a/src/main/java/org/onap/dcae/ApplicationSettings.java b/src/main/java/org/onap/dcae/ApplicationSettings.java index 1e9ae698..8458df8d 100644 --- a/src/main/java/org/onap/dcae/ApplicationSettings.java +++ b/src/main/java/org/onap/dcae/ApplicationSettings.java @@ -3,7 +3,7 @@ * PROJECT * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * Copyright (C) 2018 - 2019 Nokia. All rights reserved.s + * Copyright (C) 2018 - 2020 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. @@ -21,21 +21,28 @@ package org.onap.dcae; +import static java.lang.String.format; + import com.google.common.annotations.VisibleForTesting; +import com.google.common.reflect.TypeToken; +import com.google.gson.Gson; import com.networknt.schema.JsonSchema; import io.vavr.Function1; import io.vavr.collection.HashMap; -import io.vavr.collection.List; import io.vavr.collection.Map; +import java.io.FileReader; +import java.io.IOException; +import java.lang.reflect.Type; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import javax.annotation.Nullable; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; +import org.onap.dcae.common.EventTransformation; import org.onap.dcae.common.configuration.AuthMethodType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.annotation.Nullable; -import java.nio.file.Path; -import java.nio.file.Paths; -import static java.lang.String.format; /** * Abstraction over application configuration. @@ -43,13 +50,16 @@ import static java.lang.String.format; */ public class ApplicationSettings { + private static final String EVENT_TRANSFORM_FILE_PATH = "./etc/eventTransform.json"; + private static final String COULD_NOT_FIND_FILE = "Couldn't find file " + EVENT_TRANSFORM_FILE_PATH; + private static final Logger log = LoggerFactory.getLogger(ApplicationSettings.class); private static final String FALLBACK_VES_VERSION = "v5"; private final String appInvocationDir; private final String configurationFileLocation; private final PropertiesConfiguration properties = new PropertiesConfiguration(); - private final JSonSchemasSupplier jSonSchemasSupplier = new JSonSchemasSupplier(); private final Map<String, JsonSchema> loadedJsonSchemas; + private final List<EventTransformation> eventTransformations; public ApplicationSettings(String[] args, Function1<String[], Map<String, String>> argsParser) { this(args, argsParser, System.getProperty("user.dir")); @@ -64,7 +74,8 @@ public class ApplicationSettings { parsedArgs.filterKeys(k -> !"c".equals(k)).forEach(this::addOrUpdate); String collectorSchemaFile = properties.getString("collector.schema.file", format("{\"%s\":\"etc/CommonEventFormat_28.4.1.json\"}", FALLBACK_VES_VERSION)); - loadedJsonSchemas = jSonSchemasSupplier.loadJsonSchemas(collectorSchemaFile); + loadedJsonSchemas = new JSonSchemasSupplier().loadJsonSchemas(collectorSchemaFile); + eventTransformations = loadEventTransformations(); } public void reloadProperties() { @@ -76,22 +87,22 @@ public class ApplicationSettings { throw new ApplicationException(ex); } } + public Map<String, String> validAuthorizationCredentials() { return prepareUsersMap(properties.getString("header.authlist", null)); } - public Path configurationFileLocation() { return Paths.get(configurationFileLocation); } - public boolean jsonSchemaValidationEnabled() { + public boolean eventSchemaValidationEnabled() { return properties.getInt("collector.schema.checkflag", -1) > 0; } public JsonSchema jsonSchema(String version) { return loadedJsonSchemas.get(version) - .orElse(loadedJsonSchemas.get(FALLBACK_VES_VERSION)) - .getOrElseThrow(() -> new IllegalStateException("No fallback schema present in application.")); + .orElse(loadedJsonSchemas.get(FALLBACK_VES_VERSION)) + .getOrElseThrow(() -> new IllegalStateException("No fallback schema present in application.")); } public boolean isVersionSupported(String version){ @@ -159,6 +170,10 @@ public class ApplicationSettings { } } + public List<EventTransformation> getEventTransformations() { + return eventTransformations; + } + private void loadPropertiesFromFile() { try { properties.load(configurationFileLocation); @@ -182,7 +197,7 @@ public class ApplicationSettings { private Map<String, String> prepareUsersMap(@Nullable String allowedUsers) { return allowedUsers == null ? HashMap.empty() - : List.of(allowedUsers.split("\\|")) + : io.vavr.collection.List.of(allowedUsers.split("\\|")) .map(t->t.split(",")) .toMap(t-> t[0].trim(), t -> t[1].trim()); } @@ -205,6 +220,18 @@ public class ApplicationSettings { return filePath; } + private List<EventTransformation> loadEventTransformations() { + Type EVENT_TRANSFORM_LIST_TYPE = new TypeToken<List<EventTransformation>>() {}.getType(); + + try (FileReader fr = new FileReader(EVENT_TRANSFORM_FILE_PATH)) { + log.info("parse " + EVENT_TRANSFORM_FILE_PATH + " file"); + return new Gson().fromJson(fr, EVENT_TRANSFORM_LIST_TYPE); + } catch (IOException e) { + log.error(COULD_NOT_FIND_FILE, e); + throw new ApplicationException(COULD_NOT_FIND_FILE, e); + } + } + @VisibleForTesting String getStringDirectly(String key) { return properties.getString(key); diff --git a/src/main/java/org/onap/dcae/VesApplication.java b/src/main/java/org/onap/dcae/VesApplication.java index e3340820..bc5b1a8f 100644 --- a/src/main/java/org/onap/dcae/VesApplication.java +++ b/src/main/java/org/onap/dcae/VesApplication.java @@ -3,6 +3,7 @@ * PROJECT * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2020 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. @@ -47,7 +48,7 @@ public class VesApplication { private static final Logger incomingRequestsLogger = LoggerFactory.getLogger("org.onap.dcae.common.input"); private static final Logger oplog = LoggerFactory.getLogger("org.onap.dcae.common.output"); private static final Logger errorLog = LoggerFactory.getLogger("org.onap.dcae.common.error"); - private static ApplicationSettings properties; + private static ApplicationSettings applicationSettings; private static ConfigurableApplicationContext context; private static ConfigLoader configLoader; private static ScheduledThreadPoolExecutor scheduledThreadPoolExecutor; @@ -57,7 +58,7 @@ public class VesApplication { public static void main(String[] args) { app = new SpringApplication(VesApplication.class); - properties = new ApplicationSettings(args, CLIUtils::processCmdLine); + applicationSettings = new ApplicationSettings(args, CLIUtils::processCmdLine); scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1); init(); app.setAddCommandLineProperties(true); @@ -68,7 +69,7 @@ public class VesApplication { public static void restartApplication() { Thread thread = new Thread(() -> { context.close(); - properties.reloadProperties(); + applicationSettings.reloadProperties(); scheduleFeatures.cancel(true); init(); context = SpringApplication.run(VesApplication.class); @@ -89,32 +90,32 @@ public class VesApplication { private static void createSchedulePoolExecutor() { scheduleFeatures = scheduledThreadPoolExecutor.scheduleAtFixedRate(configLoader::updateConfig, - properties.configurationUpdateFrequency(), - properties.configurationUpdateFrequency(), + applicationSettings.configurationUpdateFrequency(), + applicationSettings.configurationUpdateFrequency(), TimeUnit.MINUTES); } private static void createConfigLoader() { configLoader = ConfigLoader.create(getEventPublisher()::reconfigure, - Paths.get(properties.dMaaPConfigurationFileLocation()), - properties.configurationFileLocation()); + Paths.get(applicationSettings.dMaaPConfigurationFileLocation()), + applicationSettings.configurationFileLocation()); } private static EventPublisher getEventPublisher() { return EventPublisher.createPublisher(oplog, DMaaPConfigurationParser - .parseToDomainMapping(Paths.get(properties.dMaaPConfigurationFileLocation())).get()); + .parseToDomainMapping(Paths.get(applicationSettings.dMaaPConfigurationFileLocation())).get()); } private static Map<String, PublisherConfig> getDmapConfig() { return DMaaPConfigurationParser - .parseToDomainMapping(Paths.get(properties.dMaaPConfigurationFileLocation())).get(); + .parseToDomainMapping(Paths.get(applicationSettings.dMaaPConfigurationFileLocation())).get(); } @Bean @Lazy public ApplicationSettings applicationSettings() { - return properties; + return applicationSettings; } @Bean @@ -132,7 +133,7 @@ public class VesApplication { @Bean @Qualifier("eventSender") public EventSender eventSender() { - return new EventSender(eventPublisher,properties); + return new EventSender(eventPublisher, applicationSettings); } } diff --git a/src/main/java/org/onap/dcae/common/Event.java b/src/main/java/org/onap/dcae/common/EventTransformation.java index 1fa8179e..f85fd2de 100644 --- a/src/main/java/org/onap/dcae/common/Event.java +++ b/src/main/java/org/onap/dcae/common/EventTransformation.java @@ -3,6 +3,7 @@ * PROJECT * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2020 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. @@ -23,11 +24,11 @@ import com.google.gson.JsonObject; import java.util.List; -class Event { +public class EventTransformation { final JsonObject filter; final List<Processor> processors; - Event(JsonObject filter, List<Processor> processors) { + EventTransformation(JsonObject filter, List<Processor> processors) { this.filter = filter; this.processors = processors; } diff --git a/src/main/java/org/onap/dcae/common/EventUpdater.java b/src/main/java/org/onap/dcae/common/EventUpdater.java index 1caa4f18..1469d47e 100644 --- a/src/main/java/org/onap/dcae/common/EventUpdater.java +++ b/src/main/java/org/onap/dcae/common/EventUpdater.java @@ -3,7 +3,7 @@ * PROJECT * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * Copyright (C) 2019 Nokia. All rights reserved.s + * Copyright (C) 2020 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. @@ -21,18 +21,12 @@ package org.onap.dcae.common; -import com.google.common.reflect.TypeToken; -import com.google.gson.Gson; -import java.io.FileReader; -import java.io.IOException; -import java.lang.reflect.Type; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.UUID; import org.json.JSONArray; import org.json.JSONObject; -import org.onap.dcae.ApplicationException; import org.onap.dcae.ApplicationSettings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,12 +37,9 @@ public class EventUpdater { private static final String EVENT = "event"; private static final String VES_UNIQUE_ID = "VESuniqueId"; private static final String VES_VERSION = "VESversion"; - private static final String COULD_NOT_FIND_FILE = "Couldn't find file ./etc/eventTransform.json"; - private static final Type EVENT_LIST_TYPE = new TypeToken<List<Event>>() {}.getType(); private static final Logger log = LoggerFactory.getLogger(EventSender.class); private static final String EVENT_LITERAL = "event"; private static final String COMMON_EVENT_HEADER = "commonEventHeader"; - private static final String EVENT_TRANSFORM = "./etc/eventTransform.json"; private ApplicationSettings settings; private final SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, MM dd yyyy hh:mm:ss z"); @@ -87,18 +78,15 @@ public class EventUpdater { private JSONObject overrideEvent(JSONObject event) { JSONObject jsonObject = addCurrentTimeToEvent(event); + if (settings.eventTransformingEnabled()) { - try (FileReader fr = new FileReader(EVENT_TRANSFORM)) { - log.info("parse " + EVENT_TRANSFORM + " file"); - List<Event> events = new Gson().fromJson(fr, EVENT_LIST_TYPE); - parseEventsJson(events, new ConfigProcessorAdapter(new ConfigProcessors(jsonObject))); - } catch (IOException e) { - log.error(COULD_NOT_FIND_FILE, e); - throw new ApplicationException(COULD_NOT_FIND_FILE, e); - } + List<EventTransformation> eventTransformations = settings.getEventTransformations(); + applyMatchingTransformations(eventTransformations, new ConfigProcessorAdapter(new ConfigProcessors(jsonObject))); } + if (jsonObject.has(VES_VERSION)) jsonObject.remove(VES_VERSION); + log.debug("Modified event:" + jsonObject); return jsonObject; } @@ -112,8 +100,8 @@ public class EventUpdater { return event; } - private void parseEventsJson(List<Event> eventsTransform, ConfigProcessorAdapter configProcessorAdapter) { - for (Event eventTransform : eventsTransform) { + private void applyMatchingTransformations(List<EventTransformation> eventsTransforms, ConfigProcessorAdapter configProcessorAdapter) { + for (EventTransformation eventTransform : eventsTransforms) { JSONObject filterObj = new JSONObject(eventTransform.filter.toString()); if (configProcessorAdapter.isFilterMet(filterObj)) { callProcessorsMethod(configProcessorAdapter, eventTransform.processors); diff --git a/src/main/java/org/onap/dcae/common/HeaderUtils.java b/src/main/java/org/onap/dcae/common/HeaderUtils.java index a5703091..c046fb4c 100644 --- a/src/main/java/org/onap/dcae/common/HeaderUtils.java +++ b/src/main/java/org/onap/dcae/common/HeaderUtils.java @@ -33,7 +33,7 @@ import org.springframework.stereotype.Component; * @author nil */ @Component -public final class HeaderUtils { +public class HeaderUtils { public String getApiVerFilePath(String fileName) { return Objects.requireNonNull(ClassLoader.getSystemClassLoader().getResource(fileName)) diff --git a/src/main/java/org/onap/dcae/restapi/EventValidator.java b/src/main/java/org/onap/dcae/restapi/EventValidator.java index 009247c0..3261c3b3 100644 --- a/src/main/java/org/onap/dcae/restapi/EventValidator.java +++ b/src/main/java/org/onap/dcae/restapi/EventValidator.java @@ -3,7 +3,7 @@ * PROJECT * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * Copyright (C) 2019 Nokia. All rights reserved.s + * Copyright (C) 2020 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. @@ -20,12 +20,10 @@ */ package org.onap.dcae.restapi; +import java.util.Optional; import org.json.JSONObject; import org.onap.dcae.ApplicationSettings; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.http.ResponseEntity; -import java.util.Optional; public class EventValidator { private final SchemaValidator schemaValidator = new SchemaValidator(); @@ -36,7 +34,7 @@ public class EventValidator { } public Optional<ResponseEntity<String>> validate(JSONObject jsonObject, String type, String version){ - if (applicationSettings.jsonSchemaValidationEnabled()) { + if (applicationSettings.eventSchemaValidationEnabled()) { if (jsonObject.has(type)) { if (!schemaValidator.conformsToSchema(jsonObject, applicationSettings.jsonSchema(version))) { return errorResponse(ApiException.SCHEMA_VALIDATION_FAILED); |