From 6987049afefcf3352143ed6357dc6df49a137e0a Mon Sep 17 00:00:00 2001 From: Lianhao Lu Date: Fri, 9 Nov 2018 14:57:30 +0800 Subject: Make FileSystemReceptionHandler more tolerant Make FileSystemReceptionHandler tolerant of exceptions thrown when parsing tosca template and be able to handle new incoming csar. Also added the unit test for FileSystemReceptionHandler. Change-Id: I0f1647f6f952576a8e61adca4d027990706d1411 Issue-ID: POLICY-837 Signed-off-by: Lianhao Lu (cherry picked from commit 70f9f24e6ed39a6ddb4afef02516af624f50079d) --- .../handling/sdc/FileSystemReceptionHandler.java | 25 ++++++++++++++-------- ...eceptionHandlerConfigurationParameterGroup.java | 10 +++++++-- 2 files changed, 24 insertions(+), 11 deletions(-) (limited to 'plugins/reception-plugins/src/main/java') diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/FileSystemReceptionHandler.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/FileSystemReceptionHandler.java index 9ac21550..b1a95fac 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/FileSystemReceptionHandler.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/FileSystemReceptionHandler.java @@ -23,6 +23,7 @@ package org.onap.policy.distribution.reception.handling.sdc; import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE; import java.io.File; +import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.Paths; @@ -52,9 +53,10 @@ public class FileSystemReceptionHandler extends AbstractReceptionHandler { final FileSystemReceptionHandlerConfigurationParameterGroup handlerParameters = ParameterService.get(parameterGroupName); main(handlerParameters.getWatchPath()); - } catch (final PolicyDecodingException ex) { - LOGGER.debug(ex); + } catch (final Exception ex) { + LOGGER.error(ex); } + LOGGER.debug("FileSystemReceptionHandler main loop exited..."); } @Override @@ -67,10 +69,9 @@ public class FileSystemReceptionHandler extends AbstractReceptionHandler { * Main entry point. * * @param watchPath Path to watch - * @throws PolicyDecodingException Decoding exception */ @SuppressWarnings("unchecked") - public void main(String watchPath) throws PolicyDecodingException { + public void main(String watchPath) { try (final WatchService watcher = FileSystems.getDefault().newWatchService()) { final Path dir = Paths.get(watchPath); @@ -85,25 +86,31 @@ public class FileSystemReceptionHandler extends AbstractReceptionHandler { Thread.currentThread().interrupt(); return; } + for (final WatchEvent event : key.pollEvents()) { final WatchEvent.Kind kind = event.kind(); final WatchEvent ev = (WatchEvent) event; final Path fileName = ev.context(); - LOGGER.debug("new CSAR found: " + kind.name() + ": " + fileName); - createPolicyInputAndCallHandler(dir.toString() + File.separator + fileName.toString()); - LOGGER.debug("CSAR complete: " + kind.name() + ": " + fileName); + try { + LOGGER.debug("new CSAR found: " + kind.name() + ": " + fileName); + createPolicyInputAndCallHandler(dir.toString() + File.separator + fileName.toString()); + LOGGER.debug("CSAR complete: " + kind.name() + ": " + fileName); + } catch (final PolicyDecodingException ex) { + LOGGER.error(ex); + } } final boolean valid = key.reset(); if (!valid) { + LOGGER.error("Watch key no longer valid!"); break; } } - } catch (final Exception ex) { + } catch (final IOException ex) { LOGGER.error(ex); } } - private void createPolicyInputAndCallHandler(final String fileName) throws PolicyDecodingException { + protected void createPolicyInputAndCallHandler(final String fileName) throws PolicyDecodingException { final Csar csarObject = new Csar(fileName); inputReceived(csarObject); } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/FileSystemReceptionHandlerConfigurationParameterGroup.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/FileSystemReceptionHandlerConfigurationParameterGroup.java index 98f3c6a4..457cd5ea 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/FileSystemReceptionHandlerConfigurationParameterGroup.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/FileSystemReceptionHandlerConfigurationParameterGroup.java @@ -68,8 +68,14 @@ public class FileSystemReceptionHandlerConfigurationParameterGroup extends Recep */ private void validatePathElement(final GroupValidationResult validationResult, final String element, final String elementName) { - File file = new File(element); - if (!(file.exists() && file.isDirectory())) { + boolean valid = false; + if (element != null) { + File file = new File(element); + if (file.exists() && file.isDirectory()) { + valid = true; + } + } + if (!valid) { validationResult.setResult(elementName, ValidationStatus.INVALID, elementName + " must be a valid directory"); } -- cgit 1.2.3-korg