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.io.IOException;
27 import java.nio.file.FileSystems;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.nio.file.WatchEvent;
31 import java.nio.file.WatchKey;
32 import java.nio.file.WatchService;
34 import org.onap.policy.common.logging.flexlogger.FlexLogger;
35 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.common.parameters.ParameterService;
38 import org.onap.policy.distribution.model.Csar;
39 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
40 import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler;
43 * Handles reception of inputs from File System which can be used to decode policies.
45 public class FileSystemReceptionHandler extends AbstractReceptionHandler {
46 private boolean running = true;
47 private static final Logger LOGGER = FlexLogger.getLogger(FileSystemReceptionHandler.class);
50 protected void initializeReception(final String parameterGroupName) {
51 LOGGER.debug("FileSystemReceptionHandler init...");
53 final FileSystemReceptionHandlerConfigurationParameterGroup handlerParameters =
54 ParameterService.get(parameterGroupName);
55 main(handlerParameters.getWatchPath());
56 } catch (final Exception ex) {
59 LOGGER.debug("FileSystemReceptionHandler main loop exited...");
63 public void destroy() {
64 // Tear down subscription etc
71 * @param watchPath Path to watch
73 @SuppressWarnings("unchecked")
74 public void main(String watchPath) {
75 try (final WatchService watcher = FileSystems.getDefault().newWatchService()) {
76 final Path dir = Paths.get(watchPath);
78 dir.register(watcher, ENTRY_CREATE);
79 LOGGER.debug("Watch Service registered for dir: " + dir.getFileName());
84 } catch (final InterruptedException ex) {
86 Thread.currentThread().interrupt();
90 for (final WatchEvent<?> event : key.pollEvents()) {
91 final WatchEvent.Kind<?> kind = event.kind();
92 final WatchEvent<Path> ev = (WatchEvent<Path>) event;
93 final Path fileName = ev.context();
95 LOGGER.debug("new CSAR found: " + kind.name() + ": " + fileName);
96 createPolicyInputAndCallHandler(dir.toString() + File.separator + fileName.toString());
97 LOGGER.debug("CSAR complete: " + kind.name() + ": " + fileName);
98 } catch (final PolicyDecodingException ex) {
102 final boolean valid = key.reset();
104 LOGGER.error("Watch key no longer valid!");
108 } catch (final IOException ex) {
113 protected void createPolicyInputAndCallHandler(final String fileName) throws PolicyDecodingException {
114 final Csar csarObject = new Csar(fileName);
115 inputReceived(csarObject);