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