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.filesystem;
23 import java.io.IOException;
24 import java.nio.charset.StandardCharsets;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.StandardWatchEventKinds;
28 import java.nio.file.WatchEvent;
29 import java.nio.file.WatchEvent.Kind;
30 import java.time.Instant;
31 import java.util.Arrays;
32 import java.util.Optional;
33 import java.util.stream.Collectors;
34 import java.util.stream.Stream;
36 import lombok.extern.slf4j.Slf4j;
37 import org.bson.json.JsonParseException;
38 import org.onap.pnfsimulator.db.Storage;
39 import org.onap.pnfsimulator.template.Template;
40 import org.bson.Document;
43 public enum WatcherEventProcessor {
44 CREATED(StandardWatchEventKinds.ENTRY_CREATE) {
46 public void processEvent(Path path, Storage<Template> storage) throws IOException {
47 String content = getContent(path);
48 String fileName = path.getFileName().toString();
49 Document documentsContent = Document.parse(content);
50 storage.persist(new Template(fileName, documentsContent, Instant.now().getNano()));
51 log.info("DB record created for template: " + fileName);
54 MODIFIED(StandardWatchEventKinds.ENTRY_MODIFY) {
56 public void processEvent(Path path, Storage<Template> storage) throws IOException {
57 String fileName = path.getFileName().toString();
58 String content = getContent(path);
59 Document documentsContent = Document.parse(content);
60 Template template = storage.get(fileName).orElse(new Template(fileName, documentsContent, Instant.now().getNano()));
61 template.setContent(documentsContent);
62 storage.persist(template);
63 log.info("DB record modified for template: " + fileName);
66 DELETED(StandardWatchEventKinds.ENTRY_DELETE) {
68 public void processEvent(Path path, Storage<Template> storage) {
69 String fileName = path.getFileName().toString();
70 storage.delete(fileName);
71 log.info("DB record deleted for template: " + fileName);
75 private final Kind<Path> pathKind;
77 String getContent(Path path) throws IOException {
78 try (Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8)) {
79 return lines.collect(Collectors.joining(System.lineSeparator()));
80 } catch (IOException e) {
81 log.error("Could not get content due to: " + e.getMessage() + " " + e.getCause(), e);
86 WatcherEventProcessor(Kind<Path> pathKind) {
87 this.pathKind = pathKind;
90 public abstract void processEvent(Path templateName, Storage<Template> storage) throws IOException;
92 static void process(WatchEvent<?> event, Storage<Template> storage, Path templatesDir) {
93 Optional<WatcherEventProcessor> watcherEventProcessor = getWatcherEventProcessor(event);
94 watcherEventProcessor.ifPresent(processor -> {
96 final Path templatePath = templatesDir.resolve((Path) event.context());
97 processor.processEvent(templatePath, storage);
98 } catch (IOException e) {
99 log.error("Error during processing DB record for template.", e);
100 } catch (JsonParseException e) {
101 log.error("Invalid JSON format provided for template.", e);
106 private static Optional<WatcherEventProcessor> getWatcherEventProcessor(WatchEvent<?> event) {
107 return Arrays.stream(values()).filter(value -> value.pathKind.equals(event.kind())).findFirst();