c4d6198f734ad476eed02357304b4f4cbf0a3c35
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.integration
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 org.onap.pnfsimulator.netconfmonitor.netconf.NetconfConfigurationCache;
25 import org.onap.pnfsimulator.netconfmonitor.netconf.NetconfConfigurationReader;
26 import org.onap.pnfsimulator.netconfmonitor.netconf.NetconfConfigurationWriter;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Service;
29
30 import javax.annotation.PostConstruct;
31 import java.io.IOException;
32 import java.util.Timer;
33
34 @Service
35 public class NetconfMonitorService {
36     private static final long timePeriod = 1000L;
37     private static final long startDelay = 0;
38
39     private Timer timer;
40     private NetconfConfigurationReader reader;
41     private NetconfConfigurationWriter writer;
42     private NetconfConfigurationCache cache;
43
44     @Autowired
45     public NetconfMonitorService(Timer timer,
46         NetconfConfigurationReader reader,
47         NetconfConfigurationWriter writer,
48         NetconfConfigurationCache cache) {
49         this.timer = timer;
50         this.reader = reader;
51         this.writer = writer;
52         this.cache = cache;
53     }
54
55     @PostConstruct
56     public void start() throws IOException, JNCException {
57         setStartConfiguration();
58         NetconfConfigurationCheckingTask task =  new NetconfConfigurationCheckingTask(reader, writer, cache);
59         timer.scheduleAtFixedRate(task, startDelay, timePeriod);
60     }
61
62     private void setStartConfiguration() throws IOException, JNCException {
63         String configuration = reader.read();
64         writer.writeToFile(configuration);
65         cache.update(configuration);
66     }
67 }