Changed to unmaintained
[appc.git] / appc-config / appc-config-generator / provider / src / main / java / org / onap / sdnc / config / generator / tool / LogParserTool.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.sdnc.config.generator.tool;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import java.text.ParseException;
29 import java.text.SimpleDateFormat;
30 import java.util.ArrayList;
31 import java.util.Date;
32 import java.util.List;
33
34 public class LogParserTool {
35
36     private static final EELFLogger log = EELFManager.getInstance().getLogger(JSONTool.class);
37
38     private String[] singleLines;
39     private List<String> recentErrors = new ArrayList<>();
40     private Date todaysDate = new Date();
41     private SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
42
43     private static final int MIN_MILLI = 60000;
44     private static final int IN_TIME = 0;
45     private static final int NOT_IN_TIME = 1;
46     private static final int NO_DATE = 2;
47
48     public String parseErrorLog(String data) {
49         singleLines = data.split("\\r?\\n");
50         try {
51             getNearestDates();
52
53         } catch (Exception e) {
54             log.error("Failed to get nearest dates", e);
55         }
56         if (recentErrors.isEmpty()) {
57             recentErrors.clear();
58             return "Did not find the string 'Starting orchestration of file backed up to /var/opt/MetaSwitch/orch/orch_conf.json' in the log file with timestamp within the last 5 minutes";
59         } else if (recentErrors.size() == 1) {
60             recentErrors.clear();
61             return "Did not find the string ?Error parsing orchestration file? in the log file with timestamp within the last 5 minutes";
62         } else {
63             String error = recentErrors.get(0);
64             recentErrors.clear();
65             return "Error: "
66                 + error.substring(error.indexOf("Error parsing orchestration file:") + 34);
67         }
68     }
69
70     private void getNearestDates() throws ParseException {
71         int result;
72         for (int i = singleLines.length - 1; i >= 0; i--) {
73             if (singleLines[i].contains("Starting orchestration of file backed up to")
74                 || singleLines[i].contains("Error parsing orchestration file:")) {
75                 result = checkDateTime(singleLines[i]);
76                 if (result == IN_TIME) {
77                     recentErrors.add(singleLines[i]);
78                 } else if (result == NOT_IN_TIME) {
79                     return;
80                 }
81             }
82         }
83     }
84
85     private int checkDateTime(String line) {
86         Date newDate;
87         try {
88             newDate = dFormat.parse(line.substring(0, 19));
89             if ((todaysDate.getTime() - newDate.getTime()) <= 5 * MIN_MILLI) {
90                 return IN_TIME;
91             } else {
92                 return NOT_IN_TIME;
93             }
94         } catch (ParseException e) {
95             log.error("Failed to parse date", e);
96             return NO_DATE;
97         }
98     }
99 }