add support to generate docker image for artifactbroker
[multicloud/framework.git] / artifactbroker / plugins / reception-plugins / src / main / java / org / onap / policy / distribution / reception / handling / sdc / FileSystemReceptionHandler.java
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.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;
33
34 import org.onap.policy.common.logging.flexlogger.FlexLogger;
35 import org.onap.policy.common.logging.flexlogger.Logger;
36
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;
41
42 /**
43  * Handles reception of inputs from File System which can be used to decode policies.
44  */
45 public class FileSystemReceptionHandler extends AbstractReceptionHandler {
46     private boolean running = false;
47     private static final Logger LOGGER = FlexLogger.getLogger(FileSystemReceptionHandler.class);
48
49     @Override
50     protected void initializeReception(final String parameterGroupName) {
51         LOGGER.debug("FileSystemReceptionHandler init...");
52         try {
53             final FileSystemReceptionHandlerConfigurationParameterGroup handlerParameters =
54                                     ParameterService.get(parameterGroupName);
55             main(handlerParameters.getWatchPath());
56         } catch (final Exception ex) {
57             LOGGER.error(ex);
58         }
59         running = false;
60         LOGGER.debug("FileSystemReceptionHandler main loop exited...");
61     }
62
63     @Override
64     public void destroy() {
65         // Tear down subscription etc
66         running = false;
67     }
68
69     public boolean isRunning() {
70         return running;
71     }
72
73     /**
74      * Main entry point.
75      * 
76      * @param watchPath Path to watch
77      */
78     public void main(String watchPath) throws IOException {
79         try (final WatchService watcher = FileSystems.getDefault().newWatchService()) {
80             final Path dir = Paths.get(watchPath);
81             dir.register(watcher, ENTRY_CREATE);
82             LOGGER.debug("Watch Service registered for dir: " + dir.getFileName());
83             startMainLoop(watcher, dir);
84         } catch (final InterruptedException ex) {
85             LOGGER.debug(ex);
86             Thread.currentThread().interrupt();
87         }
88     }
89
90     @SuppressWarnings("unchecked")
91     protected void startMainLoop(WatchService watcher, Path dir) throws InterruptedException {
92         WatchKey key;
93         running = true;
94         while (running) {
95             key = watcher.take();
96
97             for (final WatchEvent<?> event : key.pollEvents()) {
98                 final WatchEvent<Path> ev = (WatchEvent<Path>) event;
99                 final Path fileName = ev.context();
100                 LOGGER.debug("new CSAR found: " + fileName);
101                 createPolicyInputAndCallHandler(dir.toString() + File.separator + fileName.toString());
102                 LOGGER.debug("CSAR complete: " + fileName);
103             }
104             final boolean valid = key.reset();
105             if (!valid) {
106                 LOGGER.error("Watch key no longer valid!");
107                 break;
108             }
109         }
110     }
111
112     protected void createPolicyInputAndCallHandler(final String fileName) {
113         try {
114             final Csar csarObject = new Csar(fileName);
115             inputReceived(csarObject);
116         } catch (final PolicyDecodingException ex) {
117             LOGGER.error(ex);
118         }
119     }
120 }