dc857b2559c6fe0eb34dfb53302b0549c6e5a102
[sdnc/core.git] / sli / recording / src / main / java / org / openecomp / sdnc / sli / recording / FileRecorder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdnc.sli.recording;
23
24 import java.io.File;
25 import java.io.FileWriter;
26 import java.io.PrintWriter;
27 import java.text.DateFormat;
28 import java.text.SimpleDateFormat;
29 import java.util.Date;
30 import java.util.Map;
31 import java.util.TimeZone;
32
33 import org.openecomp.sdnc.sli.ConfigurationException;
34 import org.openecomp.sdnc.sli.SvcLogicException;
35 import org.openecomp.sdnc.sli.SvcLogicRecorder;
36
37
38 public class FileRecorder implements SvcLogicRecorder {
39
40         @Override
41         public void record(Map<String, String> parmMap) throws SvcLogicException {
42                 
43                 String fileName = parmMap.get("file");
44                 if (fileName == null)
45                 {
46                         throw new ConfigurationException("No file parameter specified");
47                 }
48                 
49                 String record = parmMap.get("record");
50                 if (record == null)
51                 {
52                         String delimiter = parmMap.get("delimiter");
53                         if (delimiter == null)
54                         {
55                                 delimiter = "|";
56                         }
57                         
58                         int idx = 1;
59                         boolean moreFields = true;
60                         while (moreFields)
61                         {
62                                 String curField = parmMap.get("field"+idx++);
63                                 if (curField == null)
64                                 {
65                                         moreFields = false;
66                                 }
67                                 else
68                                 {
69                                         if (record == null)
70                                         {
71                                                 record = delimiter;
72                                         }
73                                         record = record + curField + delimiter;
74                                 }
75                         }
76                 }
77                 
78                 if (record == null)
79                 {
80                         throw new ConfigurationException("No record/fields passed in record node");
81                 }
82                 
83                 File recordFile = new File(fileName);
84                 PrintWriter recPrinter = null;
85                 Date now = new Date();
86
87                 TimeZone tz = TimeZone.getTimeZone("UTC");
88                 DateFormat dateFmt = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss:SS'+00:00'");
89                 dateFmt.setTimeZone(tz);
90                 if (record.indexOf("__TIMESTAMP__") != -1)
91                 {
92                         record = record.replaceFirst("__TIMESTAMP__", dateFmt.format(now));
93                 }
94                 
95                 try
96                 {
97                 
98                         recPrinter = new PrintWriter(new FileWriter(recordFile, true));
99                         recPrinter.println(record);
100                 }
101                 catch (Exception e)
102                 {
103                         throw new SvcLogicException("Cannot write record to file", e);
104                 }
105                 finally
106                 {
107                         if (recPrinter != null)
108                         {
109                                 recPrinter.close();
110                         }
111                 }
112                 
113                 
114         }
115
116 }
117