Honeynode test tool
[transportpce.git] / tests / honeynode / honeynode-plugin-impl / src / test / java / io / fd / honeycomb / transportpce / device / test / DirectoryWatchTest.java
1 /*
2  * Copyright (c) 2018 Orange and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.fd.honeycomb.transportpce.device.test;
17
18 import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
19 import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
20 import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.file.FileSystems;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.nio.file.WatchEvent;
28 import java.nio.file.WatchKey;
29 import java.nio.file.WatchService;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * @author Martial COULIBALY ( mcoulibaly.ext@orange.com ) on behalf of Orange
36  */
37 public class DirectoryWatchTest {
38     private static final Logger LOG = LoggerFactory.getLogger(DirectoryWatchTest.class);
39     private static final String DATA_JSON = "/var/lib/honeycomb/persist/config/data.json";
40
41     public static void main(String[] args) {
42         File data = new File(DATA_JSON);
43         String parent = data.getParent();
44         LOG.info("data.json directory : {}",parent);
45         try {
46             WatchService watcher = FileSystems.getDefault().newWatchService();
47             Path dir = Paths.get(data.getParent());
48             dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
49             LOG.info("Watch Service registered for dir: {}", dir.getFileName());
50             while (true) {
51                 WatchKey key;
52                 try {
53                     key = watcher.take();
54                 } catch (InterruptedException ex) {
55                     return;
56                 }
57                 for (WatchEvent<?> event : key.pollEvents()) {
58                     WatchEvent.Kind<?> kind = event.kind();
59                     @SuppressWarnings("unchecked")
60                     WatchEvent<Path> ev = (WatchEvent<Path>) event;
61                     Path fileName = ev.context();
62                     if (kind == ENTRY_MODIFY && fileName.toString().equals("data.json")) {
63                         LOG.info("My source file has changed ...");
64                         /**
65                          * merge honeycomb-config datastore to  device oper
66                          */
67                     }
68                 }
69                 boolean valid = key.reset();
70                 if (!valid) {
71                     LOG.error("Key has been unregisterede");
72                     break;
73                 }
74             }
75         } catch (IOException ex) {
76             LOG.error("WatchService Error",ex);
77         }
78     }
79
80 }