186b89c6c6b32aec9b0b6771cae2b625f6bed001
[ccsdk/features.git] /
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 = (o1, o2) -> o1.getName().compareTo(o2.getName());
58
59     public HtDatabaseUpdateFile(String filename) throws IOException {
60         super(filename);
61     }
62
63     private static String readFile(final InputStream s) throws IOException {
64         // read file
65         BufferedReader in = new BufferedReader(new InputStreamReader(s));
66         StringBuilder sb = new StringBuilder();
67         String inputLine;
68         while ((inputLine = in.readLine()) != null) {
69             sb.append(inputLine);
70         }
71         in.close();
72         s.close();
73         return sb.toString();
74     }
75
76     public boolean readFiles(FileReadCallback cb) {
77         boolean r=true;
78         Enumeration<? extends ZipEntry> entries = this.entries();
79         ArrayList<? extends ZipEntry> list = Collections.list(entries);
80         Collections.sort(list, byfilenameComparator);
81         for (ZipEntry entry : list) {
82             if (entry.isDirectory()) {
83                 continue;
84             }
85             try {
86                 InputStream stream = this.getInputStream(entry);
87                 Object data = new JSONTokener(readFile(stream)).nextValue();
88                 stream.close();
89                 if(data instanceof JSONArray)
90                 {
91                     JSONArray a=(JSONArray)data;
92                     for(int i=0;i<a.length();i++)
93                     {
94                         cb.read(new EsUpdateObject(a.getJSONObject(i)),entry.getName());
95                     }
96                 }
97                 else if(data instanceof JSONObject)
98                 {
99                     cb.read(new EsUpdateObject( (JSONObject)data),entry.getName());
100                 }
101             } catch (IOException e) {
102                 r=false;
103                 cb.onerror(entry.getName(),e);
104             }
105         }
106         return r;
107     }
108
109 }