Update ves-agent dependency
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / vfc / nfvo / emsdriver / commons / utils / VarExprParser.java
1 /*
2  * Copyright 2017 BOCO Corporation.  CMCC 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.emsdriver.commons.utils;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import java.text.SimpleDateFormat;
23 import java.util.Calendar;
24 import java.util.Locale;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28     public  final class VarExprParser {
29     private static Logger log = LoggerFactory.getLogger(VarExprParser.class);
30     
31     private static Pattern varPattern = Pattern.compile("(\\$\\{([^\\}]+)\\})",
32             Pattern.CASE_INSENSITIVE);
33
34     public static final String replaceVar(String str, long scanStartTime, long scanStopTime) {
35         if (str.indexOf("${") == -1)
36             return str;
37
38         // support original system variable
39         str = str.replace("${s_year}", "${SCAN_START_TIME,yyyy}");
40         str = str.replace("${s_mon}", "${SCAN_START_TIME,MM}");
41         str = str.replace("${s_day}", "${SCAN_START_TIME,dd}");
42         str = str.replace("${s_hour}", "${SCAN_START_TIME,HH}");
43         str = str.replace("${s_min}", "${SCAN_START_TIME,mm}");
44         str = str.replace("${e_year}", "${SCAN_STOP_TIME,yyyy}");
45         str = str.replace("${e_mon}", "${SCAN_STOP_TIME,MM}");
46         str = str.replace("${e_day}", "${SCAN_STOP_TIME,dd}");
47         str = str.replace("${e_hour}", "${SCAN_STOP_TIME,HH}");
48         str = str.replace("${e_min}", "${SCAN_STOP_TIME,mm}");
49
50         String expr;
51         String varName;
52         String value ;
53         Matcher matcher = varPattern.matcher(str);
54         while (matcher.find()) {
55             value = null;
56             expr = matcher.group(1);
57             varName = matcher.group(2);
58             if (expr.indexOf("${SCAN_START_TIME") != -1) {
59                 value = getTime(scanStartTime, varName, "yyyy-MM-dd HH:mm:ss");
60             } else if (expr.indexOf("${SCAN_STOP_TIME") != -1) {
61                 value = getTime(scanStopTime, varName, "yyyy-MM-dd HH:mm:ss");
62             }
63             if (value == null) {
64                 log.warn(" expr [" + str + "] var["
65                         + expr + "]is fail");
66                 continue;
67             }
68             str = str.replace(expr, value);
69         }
70         return str;
71     }
72
73     private static String getTime(long time, String value, String defaultParam) {
74         String timeStr = null;
75         String formatStr = null;
76         String increaseTime = null;
77         if (value.indexOf(",") == -1) {
78             formatStr = defaultParam;
79             timeStr = value;
80         } else {
81             timeStr = value.split(",")[0];
82             formatStr = value.split(",")[1];
83         }
84
85         if (timeStr.indexOf("+") == -1 && timeStr.indexOf("-") == -1) {
86             SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr, Locale.ENGLISH);
87             return simpleDateFormat.format(time);
88         } else {
89             if (timeStr.indexOf("+") != -1) {
90                 increaseTime = timeStr.substring(timeStr.indexOf("+") + 1, timeStr.length() - 1);
91             }
92             if (timeStr.indexOf("-") != -1) {
93                 increaseTime = timeStr.substring(timeStr.indexOf("-"), timeStr.length() - 1);
94             }
95             if (timeStr.toLowerCase().endsWith("h")) {
96                 Calendar cal = Calendar.getInstance();
97                 cal.setTimeInMillis(time);
98                 cal.add(Calendar.HOUR, Integer.parseInt(increaseTime));
99                 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr, Locale.ENGLISH);
100                 return simpleDateFormat.format(cal.getTimeInMillis());
101             } else if (timeStr.toLowerCase().endsWith("m")) {
102                 Calendar cal = Calendar.getInstance();
103                 cal.setTimeInMillis(time);
104                 cal.add(Calendar.MINUTE, Integer.parseInt(increaseTime));
105                 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr, Locale.ENGLISH);
106                 return simpleDateFormat.format(cal.getTimeInMillis());
107             }
108         }
109         return null;
110     }
111
112     /**
113      * Support two variable substitutions
114      * @param result
115      * @param scanStartTime
116      * @param scanStopTime
117      * @return
118      */
119     public static String replaceTimeVar(String result,String scanStartTime, String scanStopTime) {
120         boolean isReplace = false;
121         if (result.indexOf("${SCAN_ST") != -1) {
122             isReplace = true;
123         }
124         if (isReplace) {
125             if (result.indexOf("${SCAN_START_TIME}") != -1) {
126
127                 result = StringUtils.replace(result, "${SCAN_START_TIME}", scanStartTime);
128             }
129             if (result.indexOf("${SCAN_STOP_TIME") != -1) {
130
131                 result = StringUtils.replace(result, "${SCAN_STOP_TIME}", scanStopTime);
132             }
133         }
134         return result;
135     }
136 }