2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Intel Corp. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.distribution.reception.handling.sdc;
23 import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
26 import java.nio.file.FileSystems;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.nio.file.WatchEvent;
30 import java.nio.file.WatchKey;
31 import java.nio.file.WatchService;
33 import org.onap.policy.common.logging.flexlogger.FlexLogger;
34 import org.onap.policy.common.logging.flexlogger.Logger;
36 import org.onap.policy.common.parameters.ParameterService;
37 import org.onap.policy.distribution.model.Csar;
38 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
39 import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler;
42 * Handles reception of inputs from File System which can be used to decode policies.
44 public class FileSystemReceptionHandler extends AbstractReceptionHandler {
45 private boolean running = true;
46 private static final Logger LOGGER = FlexLogger.getLogger(FileSystemReceptionHandler.class);
49 protected void initializeReception(final String parameterGroupName) {
50 LOGGER.debug("FileSystemReceptionHandler init...");
52 final FileSystemReceptionHandlerConfigurationParameterGroup handlerParameters =
53 ParameterService.get(parameterGroupName);
54 main(handlerParameters.getWatchPath());
55 } catch (final PolicyDecodingException ex) {
61 public void destroy() {
62 // Tear down subscription etc
69 * @param watchPath Path to watch
70 * @throws PolicyDecodingException Decoding exception
72 @SuppressWarnings("unchecked")
73 public void main(String watchPath) throws PolicyDecodingException {
74 try (final WatchService watcher = FileSystems.getDefault().newWatchService()) {
75 final Path dir = Paths.get(watchPath);
77 dir.register(watcher, ENTRY_CREATE);
78 LOGGER.debug("Watch Service registered for dir: " + dir.getFileName());
83 } catch (final InterruptedException ex) {
85 Thread.currentThread().interrupt();
88 for (final WatchEvent<?> event : key.pollEvents()) {
89 final WatchEvent.Kind<?> kind = event.kind();
90 final WatchEvent<Path> ev = (WatchEvent<Path>) event;
91 final Path fileName = ev.context();
92 LOGGER.debug("new CSAR found: " + kind.name() + ": " + fileName);
93 createPolicyInputAndCallHandler(dir.toString() + File.separator + fileName.toString());
94 LOGGER.debug("CSAR complete: " + kind.name() + ": " + fileName);
96 final boolean valid = key.reset();
101 } catch (final Exception ex) {
106 private void createPolicyInputAndCallHandler(final String fileName) throws PolicyDecodingException {
107 final Csar csarObject = new Csar(fileName);
108 inputReceived(csarObject);