Update gvnfm-driver .gitreview file
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / gvnfm / jujuvnfmadapter / common / BaseTimeJob.java
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
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 org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common;
17
18 import java.util.Calendar;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.TimeUnit;
22
23 /**
24  * 
25  * Base time job class.<br>
26  * <p>
27  * </p>
28  * 
29  * @author
30  * @version     NFVO 0.5  Sep 12, 2016
31  */
32 public abstract class BaseTimeJob implements Runnable {
33
34     private ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
35
36     private long initialDelay = 1;
37
38     private long period = 1;
39
40     private String startTime = "";
41
42     @Override
43     public abstract void run();
44
45     /**
46      * 
47      * Stop method.<br>
48      * 
49      * @since  NFVO 0.5
50      */
51     public void stop() {
52         service.shutdown();
53     }
54
55     public void setInitialDelay(long initialDelay) {
56         this.initialDelay = initialDelay;
57     }
58
59     public void setPeriod(long period) {
60         this.period = period;
61     }
62
63     /**
64      * 
65      * Start method.<br>
66      * 
67      * @since  NFVO 0.5
68      */
69     public void start() {
70         if(startTime.length() != 0) {
71             String[] vnfTime = startTime.split(":");
72             if(vnfTime.length == 2) {
73                 int minute = Integer.parseInt(vnfTime[0]) * 60 + Integer.parseInt(vnfTime[1]);
74                 Calendar calendar = Calendar.getInstance();
75                 int curMinute = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE);
76                 if(curMinute <= minute) {
77                     initialDelay = (minute - curMinute) * 60L;
78                 } else {
79                     initialDelay = (minute + 24 * 60 - curMinute) * 60L;
80                 }
81             }
82         }
83         service.scheduleAtFixedRate(this, initialDelay, period, TimeUnit.SECONDS);
84     }
85 }