881585bd6e0109b6a811acdabec04d63f62f8dbf
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Simulator
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.pnfsimulator.template;
22
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.util.stream.Stream;
28
29 import org.bson.json.JsonParseException;
30 import org.onap.pnfsimulator.db.Storage;
31 import org.onap.pnfsimulator.filesystem.WatcherEventProcessor;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.stereotype.Service;
37
38 @Service
39 public class FsToDbTemplateSynchronizer {
40
41     private static final String CANNOT_SYNC = "Cannot synchronize templates. Check whether the proper folder exists.";
42     private static final Logger LOGGER = LoggerFactory.getLogger(FsToDbTemplateSynchronizer.class);
43
44     private final String templatesDir;
45     private final Storage<Template> storage;
46
47     @Autowired
48     public FsToDbTemplateSynchronizer(@Value("${templates.dir}") String templatesDir,
49                                       Storage<Template> storage) {
50         this.templatesDir = templatesDir;
51         this.storage = storage;
52     }
53
54     public void synchronize() {
55         try {
56             processTemplatesFolder();
57         } catch (IOException e) {
58             LOGGER.error(CANNOT_SYNC, e);
59         }
60     }
61
62     private void processTemplatesFolder() throws IOException {
63         try (Stream<Path> walk = Files.walk(Paths.get(templatesDir))) {
64             walk.filter(Files::isRegularFile).forEach(path -> {
65                 try {
66                     WatcherEventProcessor.MODIFIED.processEvent(path, storage);
67                 } catch (IOException | JsonParseException e) {
68                     LOGGER
69                             .error("Cannot synchronize template: " + path.getFileName().toString(), e);
70                 }
71             });
72         }
73     }
74 }