56a569671392b9bf3913044a44f197dee9221a69
[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.filesystem;
22
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;
35
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;
41
42 @Slf4j
43 public enum WatcherEventProcessor {
44     CREATED(StandardWatchEventKinds.ENTRY_CREATE) {
45         @Override
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);
52         }
53     },
54     MODIFIED(StandardWatchEventKinds.ENTRY_MODIFY) {
55         @Override
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);
64         }
65     },
66     DELETED(StandardWatchEventKinds.ENTRY_DELETE) {
67         @Override
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);
72         }
73     };
74
75     private final Kind<Path> pathKind;
76
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);
82             throw e;
83         }
84     }
85
86     WatcherEventProcessor(Kind<Path> pathKind) {
87         this.pathKind = pathKind;
88     }
89
90     public abstract void processEvent(Path templateName, Storage<Template> storage) throws IOException;
91
92     static void process(WatchEvent<?> event, Storage<Template> storage, Path templatesDir) {
93         Optional<WatcherEventProcessor> watcherEventProcessor = getWatcherEventProcessor(event);
94         watcherEventProcessor.ifPresent(processor -> {
95             try {
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);
102             }
103         });
104     }
105
106     private static Optional<WatcherEventProcessor> getWatcherEventProcessor(WatchEvent<?> event) {
107         return Arrays.stream(values()).filter(value -> value.pathKind.equals(event.kind())).findFirst();
108     }
109
110 }