0c228b8eb18d58798ca7a4871a345283f405288a
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.sdnc.config.generator.tool;
26
27 import java.text.ParseException;
28 import java.text.SimpleDateFormat;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.List;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
34
35 public class LogParserTool {
36     private static final EELFLogger log = EELFManager.getInstance().getLogger(JSONTool.class);
37
38     private String[] singleLines;
39     private List<String> recentErrors = new ArrayList<String>();;
40     private Date todaysDate = new Date();
41     private SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
42     private final int minMilli = 60000;
43     private final int IN_TIME = 0;
44     private final int NOT_IN_TIME = 1;
45     private final int NO_DATE = 2;
46
47     public String parseErrorLog(String data) {
48         singleLines = data.split("\\r?\\n");
49         try {
50             getNearestDates();
51
52         } catch (Exception e) {
53             e.printStackTrace();
54         }
55         if (recentErrors.size() == 0) {
56             recentErrors.clear();
57             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";
58         } else if (recentErrors.size() == 1) {
59             recentErrors.clear();
60             return "Did not find the string ?Error parsing orchestration file? in the log file with timestamp within the last 5 minutes";
61         } else {
62             String error = recentErrors.get(0);
63             recentErrors.clear();
64             return "Error: "
65                     + error.substring(error.indexOf("Error parsing orchestration file:") + 34);
66         }
67     }
68
69     public void getNearestDates() throws ParseException {
70         int result;
71         for (int i = singleLines.length - 1; i >= 0; i--) {
72             if (singleLines[i].contains("Starting orchestration of file backed up to")
73                     || singleLines[i].contains("Error parsing orchestration file:")) {
74                 result = checkDateTime(singleLines[i]);
75                 if (result == IN_TIME)
76                     recentErrors.add(singleLines[i]);
77                 else if (result == NOT_IN_TIME) {
78                     return;
79                 }
80             }
81         }
82     }
83
84     private int checkDateTime(String line) {
85         Date newDate;
86         try {
87             newDate = dFormat.parse(line.substring(0, 19));
88             if ((todaysDate.getTime() - newDate.getTime()) <= 5 * minMilli) {
89                 return IN_TIME;
90             } else
91                 return NOT_IN_TIME;
92         } catch (ParseException e) {
93             e.printStackTrace();
94             return NO_DATE;
95         }
96     }
97
98
99 }