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