diff options
author | wasala <przemyslaw.wasala@nokia.com> | 2018-06-05 12:15:44 +0200 |
---|---|---|
committer | wasala <przemyslaw.wasala@nokia.com> | 2018-06-05 12:15:44 +0200 |
commit | e30c7d54b34daf855303f32035827ee19555b536 (patch) | |
tree | 5c6c1c0043df03bd8f9200352fe3ae7625d0abf5 /prh-app-server/src/main | |
parent | 53e33ec68a6eb0a06ea130215e03a7725da9d22d (diff) |
Correction for DmaapJsonParser
*Added cases fo uTests
*Changed schedule tasks to 5s
*Changed batching message to 1
Change-Id: I8daa4f143699488c8a593634ad67452b4a194437
Issue-ID: DCAEGEN2-540
Signed-off-by: wasala <przemyslaw.wasala@nokia.com>
Diffstat (limited to 'prh-app-server/src/main')
3 files changed, 27 insertions, 22 deletions
diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/ScheduleController.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/ScheduleController.java index 96b69b7b..dbac6a53 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/ScheduleController.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/controllers/ScheduleController.java @@ -45,7 +45,7 @@ import java.util.concurrent.ScheduledFuture; public class ScheduleController { private static final Logger logger = LoggerFactory.getLogger(ScheduleController.class); - private static final int SCHEDULING_DELAY = 20000; + private static final int SCHEDULING_DELAY = 5000; private static volatile List<ScheduledFuture> scheduledFutureList = new ArrayList<>(); private final TaskScheduler taskScheduler; diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapConsumerJsonParser.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapConsumerJsonParser.java index ba0ec0e8..3bac7c36 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapConsumerJsonParser.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapConsumerJsonParser.java @@ -22,13 +22,12 @@ package org.onap.dcaegen2.services.prh.service; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import java.util.Optional; +import java.util.stream.StreamSupport; import org.onap.dcaegen2.services.prh.exceptions.DmaapNotFoundException; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.model.ImmutableConsumerDmaapModel; -import java.util.Optional; -import java.util.stream.StreamSupport; - /** * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 5/8/18 */ @@ -41,23 +40,22 @@ public class DmaapConsumerJsonParser { private static final String PNF_VENDOR_NAME = "pnfVendorName"; private static final String PNF_SERIAL_NUMBER = "pnfSerialNumber"; - - private DmaapConsumerJsonParser() {} - - - public static ConsumerDmaapModel getJsonObject(String message) throws DmaapNotFoundException { + public ConsumerDmaapModel getJsonObject(String message) throws DmaapNotFoundException { JsonElement jsonElement = new JsonParser().parse(message); + if (jsonElement.isJsonObject()) { + return create(jsonElement.getAsJsonObject()); + } else { + return create(StreamSupport.stream(jsonElement.getAsJsonArray().spliterator(), false).findFirst() + .flatMap(this::getJsonObjectFromAnArray) + .orElseThrow(() -> new DmaapNotFoundException("Json object not found in json array"))); + } + } - JsonObject jsonObject = jsonElement.isJsonObject() ? jsonElement.getAsJsonObject() : - StreamSupport.stream(jsonElement.getAsJsonArray().spliterator(), false).findFirst() - .flatMap(element -> Optional.of(new JsonParser().parse(element.toString()).getAsJsonObject())) - .orElseThrow(() -> new DmaapNotFoundException("Json object not found in json array")) - .getAsJsonObject(); - - return create(jsonObject); + public Optional<JsonObject> getJsonObjectFromAnArray(JsonElement element) { + return Optional.of(new JsonParser().parse(element.getAsString()).getAsJsonObject()); } - private static ConsumerDmaapModel create(JsonObject jsonObject) throws DmaapNotFoundException { + private ConsumerDmaapModel create(JsonObject jsonObject) throws DmaapNotFoundException { if (containsHeader(jsonObject)) { jsonObject = jsonObject.getAsJsonObject(EVENT).getAsJsonObject(OTHER_FIELDS); String pnfVendorName = getValueFromJson(jsonObject, PNF_VENDOR_NAME); @@ -76,21 +74,21 @@ public class DmaapConsumerJsonParser { throw new DmaapNotFoundException("Incorrect JsonObject - missing header"); } - private static String getValueFromJson(JsonObject jsonObject, String jsonKey) { + private String getValueFromJson(JsonObject jsonObject, String jsonKey) { return jsonObject.has(jsonKey) ? jsonObject.get(jsonKey).getAsString() : ""; } - private static boolean isVendorAndSerialNotEmpty(String pnfSerialNumber, String pnfVendorName) { + private boolean isVendorAndSerialNotEmpty(String pnfSerialNumber, String pnfVendorName) { return ((pnfSerialNumber != null && !pnfSerialNumber.isEmpty()) && (pnfVendorName != null && !pnfVendorName .isEmpty())); } - private static boolean isIpPropertiesNotEmpty(String ipv4, String ipv6) { + private boolean isIpPropertiesNotEmpty(String ipv4, String ipv6) { return (ipv4 != null && !ipv4.isEmpty()) || (ipv6 != null && !ipv6.isEmpty()); } - private static boolean containsHeader(JsonObject jsonObject) { + private boolean containsHeader(JsonObject jsonObject) { return jsonObject.has(EVENT) && jsonObject.getAsJsonObject(EVENT).has(OTHER_FIELDS); } diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java index a78d33b2..01b85a9d 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java @@ -44,16 +44,23 @@ public class DmaapConsumerTaskImpl extends private static final Logger logger = LoggerFactory.getLogger(DmaapConsumerTaskImpl.class); private final Config prhAppConfig; private ExtendedDmaapConsumerHttpClientImpl extendedDmaapConsumerHttpClient; + private DmaapConsumerJsonParser dmaapConsumerJsonParser; @Autowired public DmaapConsumerTaskImpl(AppConfig prhAppConfig) { this.prhAppConfig = prhAppConfig; + this.dmaapConsumerJsonParser = new DmaapConsumerJsonParser(); + } + + DmaapConsumerTaskImpl(AppConfig prhAppConfig, DmaapConsumerJsonParser dmaapConsumerJsonParser) { + this.prhAppConfig = prhAppConfig; + this.dmaapConsumerJsonParser = dmaapConsumerJsonParser; } @Override ConsumerDmaapModel consume(String message) throws DmaapNotFoundException { logger.trace("Method called with arg {}", message); - return DmaapConsumerJsonParser.getJsonObject(message); + return dmaapConsumerJsonParser.getJsonObject(message); } @Override |