Add sdnr wt devicemanager
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / base / database / HtDatabaseUpdateFile.java
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  * http://www.apache.org/licenses/LICENSE-2.0
11  * 
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database;
19
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Comparator;
27 import java.util.Enumeration;
28 import java.util.zip.ZipEntry;
29 import java.util.zip.ZipFile;
30
31 import org.json.JSONArray;
32 import org.json.JSONObject;
33 import org.json.JSONTokener;
34
35 public class HtDatabaseUpdateFile extends ZipFile {
36
37         public static final String FILENAME_DEFAULT = "elasticsearch_update.zip";
38
39         public class EsUpdateObject {
40                 public final String Uri;
41                 public final String Method;
42                 public final JSONObject Body;
43
44                 public EsUpdateObject(JSONObject o) {
45                         this.Uri = o.getString("uri");
46                         this.Method = o.getString("method");
47                         this.Body = o.getJSONObject("body");
48                 }
49         }
50
51         public interface FileReadCallback {
52                 void read(EsUpdateObject obj,String filename);
53
54                 void onerror(String filename,IOException e);
55         }
56
57         private static final Comparator<ZipEntry> byfilenameComparator = new Comparator<ZipEntry>() {
58
59                 @Override
60                 public int compare(ZipEntry o1, ZipEntry o2) {
61                         return o1.getName().compareTo(o2.getName());
62                 }
63         };
64
65         public HtDatabaseUpdateFile(String filename) throws IOException {
66                 super(filename);
67         }
68
69         private static String readFile(final InputStream s) throws IOException {
70                 // read file
71                 BufferedReader in = new BufferedReader(new InputStreamReader(s));
72                 StringBuilder sb = new StringBuilder();
73                 String inputLine;
74                 while ((inputLine = in.readLine()) != null) {
75                         sb.append(inputLine);
76                 }
77                 in.close();
78                 s.close();
79                 return sb.toString();
80         }
81
82         public boolean readFiles(FileReadCallback cb) {
83                 boolean r=true;
84                 Enumeration<? extends ZipEntry> entries = this.entries();
85                 ArrayList<? extends ZipEntry> list = Collections.list(entries);
86                 Collections.sort(list, byfilenameComparator);
87                 for (ZipEntry entry : list) {
88                         if (entry.isDirectory())
89                                 continue;
90                         try {
91                                 InputStream stream = this.getInputStream(entry);
92                                 Object data = new JSONTokener(readFile(stream)).nextValue();
93                                 stream.close();
94                                 if(data instanceof JSONArray)
95                                 {
96                                         JSONArray a=(JSONArray)data;
97                                         for(int i=0;i<a.length();i++)
98                                         {
99                                                 cb.read(new EsUpdateObject(a.getJSONObject(i)),entry.getName());
100                                         }
101                                 }
102                                 else if(data instanceof JSONObject)
103                                 {
104                                         cb.read(new EsUpdateObject( (JSONObject)data),entry.getName());
105                                 }
106                         } catch (IOException e) {
107                                 r=false;
108                                 cb.onerror(entry.getName(),e);
109                         }
110                 }
111                 return r;
112         }
113
114 }