9ac215503cec56622ca1459a74fe971acb4cd15f
[policy/distribution.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.reception.handling.sdc;
22
23 import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
24
25 import java.io.File;
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;
32
33 import org.onap.policy.common.logging.flexlogger.FlexLogger;
34 import org.onap.policy.common.logging.flexlogger.Logger;
35
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;
40
41 /**
42  * Handles reception of inputs from File System which can be used to decode policies.
43  */
44 public class FileSystemReceptionHandler extends AbstractReceptionHandler {
45     private boolean running = true;
46     private static final Logger LOGGER = FlexLogger.getLogger(FileSystemReceptionHandler.class);
47
48     @Override
49     protected void initializeReception(final String parameterGroupName) {
50         LOGGER.debug("FileSystemReceptionHandler init...");
51         try {
52             final FileSystemReceptionHandlerConfigurationParameterGroup handlerParameters =
53                                     ParameterService.get(parameterGroupName);
54             main(handlerParameters.getWatchPath());
55         } catch (final PolicyDecodingException ex) {
56             LOGGER.debug(ex);
57         }
58     }
59
60     @Override
61     public void destroy() {
62         // Tear down subscription etc
63         running = false;
64     }
65
66     /**
67      * Main entry point.
68      * 
69      * @param watchPath Path to watch
70      * @throws PolicyDecodingException Decoding exception
71      */
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);
76
77             dir.register(watcher, ENTRY_CREATE);
78             LOGGER.debug("Watch Service registered for dir: " + dir.getFileName());
79             while (running) {
80                 WatchKey key;
81                 try {
82                     key = watcher.take();
83                 } catch (final InterruptedException ex) {
84                     LOGGER.debug(ex);
85                     Thread.currentThread().interrupt();
86                     return;
87                 }
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);
95                 }
96                 final boolean valid = key.reset();
97                 if (!valid) {
98                     break;
99                 }
100             }
101         } catch (final Exception ex) {
102             LOGGER.error(ex);
103         }
104     }
105
106     private void createPolicyInputAndCallHandler(final String fileName) throws PolicyDecodingException {
107         final Csar csarObject = new Csar(fileName);
108         inputReceived(csarObject);
109     }
110 }