Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / base / database / HtDatabaseUpdateFile.java
1 package org.opendaylight.mwtn.base.database;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.Comparator;
10 import java.util.Enumeration;
11 import java.util.zip.ZipEntry;
12 import java.util.zip.ZipFile;
13
14 import org.json.JSONArray;
15 import org.json.JSONObject;
16 import org.json.JSONTokener;
17
18 public class HtDatabaseUpdateFile extends ZipFile {
19
20         public static final String FILENAME_DEFAULT = "elasticsearch_update.zip";
21
22         public class EsUpdateObject {
23                 public final String Uri;
24                 public final String Method;
25                 public final JSONObject Body;
26
27                 public EsUpdateObject(JSONObject o) {
28                         this.Uri = o.getString("uri");
29                         this.Method = o.getString("method");
30                         this.Body = o.getJSONObject("body");
31                 }
32         }
33
34         public interface FileReadCallback {
35                 void read(EsUpdateObject obj,String filename);
36
37                 void onerror(String filename,IOException e);
38         }
39
40         private static final Comparator<ZipEntry> byfilenameComparator = new Comparator<ZipEntry>() {
41
42                 @Override
43                 public int compare(ZipEntry o1, ZipEntry o2) {
44                         return o1.getName().compareTo(o2.getName());
45                 }
46         };
47
48         public HtDatabaseUpdateFile(String filename) throws IOException {
49                 super(filename);
50         }
51
52         private static String readFile(final InputStream s) throws IOException {
53                 // read file
54                 BufferedReader in = new BufferedReader(new InputStreamReader(s));
55                 StringBuilder sb = new StringBuilder();
56                 String inputLine;
57                 while ((inputLine = in.readLine()) != null) {
58                         sb.append(inputLine);
59                 }
60                 in.close();
61                 s.close();
62                 return sb.toString();
63         }
64
65         public boolean readFiles(FileReadCallback cb) {
66                 boolean r=true;
67                 Enumeration<? extends ZipEntry> entries = this.entries();
68                 ArrayList<? extends ZipEntry> list = Collections.list(entries);
69                 Collections.sort(list, byfilenameComparator);
70                 for (ZipEntry entry : list) {
71                         if (entry.isDirectory())
72                                 continue;
73                         try {
74                                 InputStream stream = this.getInputStream(entry);
75                                 Object data = new JSONTokener(readFile(stream)).nextValue();
76                                 stream.close();
77                                 if(data instanceof JSONArray)
78                                 {
79                                         JSONArray a=(JSONArray)data;
80                                         for(int i=0;i<a.length();i++)
81                                         {
82                                                 cb.read(new EsUpdateObject(a.getJSONObject(i)),entry.getName());
83                                         }
84                                 }
85                                 else if(data instanceof JSONObject)
86                                 {
87                                         cb.read(new EsUpdateObject( (JSONObject)data),entry.getName());
88                                 }
89                         } catch (IOException e) {
90                                 r=false;
91                                 cb.onerror(entry.getName(),e);
92                         }
93                 }
94                 return r;
95         }
96
97 }