Update groupId to org.onap.ccsdk.sli
[ccsdk/sli/core.git] / sli / recording / src / main / java / org / onap / ccsdk / sli / core / sli / recording / FileRecorder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 ONAP
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.onap.ccsdk.sli.core.sli.recording;
22
23 import java.io.File;
24 import java.io.FileWriter;
25 import java.io.PrintWriter;
26 import java.text.DateFormat;
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29 import java.util.Map;
30 import java.util.TimeZone;
31
32 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicRecorder;
35
36
37 public class FileRecorder implements SvcLogicRecorder {
38
39         @Override
40         public void record(Map<String, String> parmMap) throws SvcLogicException {
41                 
42                 String fileName = parmMap.get("file");
43                 if (fileName == null)
44                 {
45                         throw new ConfigurationException("No file parameter specified");
46                 }
47                 
48                 String record = parmMap.get("record");
49                 if (record == null)
50                 {
51                         String delimiter = parmMap.get("delimiter");
52                         if (delimiter == null)
53                         {
54                                 delimiter = "|";
55                         }
56                         
57                         int idx = 1;
58                         boolean moreFields = true;
59                         while (moreFields)
60                         {
61                                 String curField = parmMap.get("field"+idx++);
62                                 if (curField == null)
63                                 {
64                                         moreFields = false;
65                                 }
66                                 else
67                                 {
68                                         if (record == null)
69                                         {
70                                                 record = delimiter;
71                                         }
72                                         record = record + curField + delimiter;
73                                 }
74                         }
75                 }
76                 
77                 if (record == null)
78                 {
79                         throw new ConfigurationException("No record/fields passed in record node");
80                 }
81                 
82                 File recordFile = new File(fileName);
83                 PrintWriter recPrinter = null;
84                 Date now = new Date();
85
86                 TimeZone tz = TimeZone.getTimeZone("UTC");
87                 DateFormat dateFmt = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss:SS'+00:00'");
88                 dateFmt.setTimeZone(tz);
89                 if (record.indexOf("__TIMESTAMP__") != -1)
90                 {
91                         record = record.replaceFirst("__TIMESTAMP__", dateFmt.format(now));
92                 }
93                 
94                 try
95                 {
96                 
97                         recPrinter = new PrintWriter(new FileWriter(recordFile, true));
98                         recPrinter.println(record);
99                 }
100                 catch (Exception e)
101                 {
102                         throw new SvcLogicException("Cannot write record to file", e);
103                 }
104                 finally
105                 {
106                         if (recPrinter != null)
107                         {
108                                 recPrinter.close();
109                         }
110                 }
111                 
112                 
113         }
114
115 }
116