2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.pnfsimulator.template;
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;
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;
39 public class FsToDbTemplateSynchronizer {
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);
44 private final String templatesDir;
45 private final Storage<Template> storage;
48 public FsToDbTemplateSynchronizer(@Value("${templates.dir}") String templatesDir,
49 Storage<Template> storage) {
50 this.templatesDir = templatesDir;
51 this.storage = storage;
54 public void synchronize() {
56 processTemplatesFolder();
57 } catch (IOException e) {
58 LOGGER.error(CANNOT_SYNC, e);
62 private void processTemplatesFolder() throws IOException {
63 try (Stream<Path> walk = Files.walk(Paths.get(templatesDir))) {
64 walk.filter(Files::isRegularFile).forEach(path -> {
66 WatcherEventProcessor.MODIFIED.processEvent(path, storage);
67 } catch (IOException | JsonParseException e) {
69 .error("Cannot synchronize template: " + path.getFileName().toString(), e);