0e7ab257d5e56320b036f362616e5b1d9d0343c9
[integration.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA Intellectual Property. 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.netconfmonitor;
22
23 import com.tailf.jnc.JNCException;
24 import java.io.IOException;
25 import java.util.Timer;
26 import javax.annotation.PostConstruct;
27 import org.onap.pnfsimulator.netconfmonitor.netconf.NetconfConfigurationCache;
28 import org.onap.pnfsimulator.netconfmonitor.netconf.NetconfConfigurationReader;
29 import org.onap.pnfsimulator.netconfmonitor.netconf.NetconfConfigurationWriter;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Service;
34
35 @Service
36 public class NetconfMonitorService {
37     private static final Logger LOGGER = LoggerFactory.getLogger(NetconfMonitorService.class);
38     private static final long timePeriod = 1000L;
39     private static final long startDelay = 0;
40
41     private Timer timer;
42     private NetconfConfigurationReader reader;
43     private NetconfConfigurationWriter writer;
44     private NetconfConfigurationCache cache;
45
46     @Autowired
47     public NetconfMonitorService(Timer timer,
48         NetconfConfigurationReader reader,
49         NetconfConfigurationWriter writer,
50         NetconfConfigurationCache cache) {
51         this.timer = timer;
52         this.reader = reader;
53         this.writer = writer;
54         this.cache = cache;
55     }
56
57     @PostConstruct
58     public void start() {
59         setStartConfiguration();
60         NetconfConfigurationCheckingTask task = new NetconfConfigurationCheckingTask(reader, writer, cache);
61         timer.scheduleAtFixedRate(task, startDelay, timePeriod);
62     }
63
64     private void setStartConfiguration() {
65         try {
66             String configuration = reader.read();
67             writer.writeToFile(configuration);
68             cache.update(configuration);
69         } catch (IOException | JNCException e) {
70             LOGGER.warn("Error during configuration reading: {}", e.getMessage());
71         }
72     }
73 }