Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / cloud / resourcedirectory / src / main / java / org / iotivity / cloud / rdserver / MongoDB.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2016 Samsung Electronics All Rights Reserved.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.rdserver;
23
24 import java.util.ArrayList;
25 import java.util.Iterator;
26
27 import org.bson.Document;
28 import org.iotivity.cloud.rdserver.resources.LinksPayloadFormat;
29 import org.iotivity.cloud.rdserver.resources.PublishPayloadFormat;
30
31 import com.mongodb.MongoClient;
32 import com.mongodb.client.MongoCollection;
33 import com.mongodb.client.MongoCursor;
34 import com.mongodb.client.MongoDatabase;
35 import com.mongodb.client.model.Filters;
36
37 /**
38  *
39  * This class provides a set of APIs to use MongoDB APIs.
40  *
41  */
42 public class MongoDB {
43
44     private MongoClient   mongoClient = null;
45     private MongoDatabase db          = null;
46
47     /**
48      * API creating MongoClient and initializing MongoDatabase
49      *
50      * @param dbname
51      *            database name to create MongoDatabase
52      * @throws Exception
53      */
54     public MongoDB(String dbname) throws Exception {
55         mongoClient = new MongoClient();
56         mongoClient.dropDatabase(dbname);
57         db = mongoClient.getDatabase(dbname);
58     }
59
60     /**
61      * API creating collection
62      *
63      * @param tableName
64      *            collection name
65      */
66     public void createTable(String tableName) {
67         db.createCollection(tableName);
68     }
69
70     /**
71      * API deleting collection
72      *
73      * @param tableName
74      *            collection name
75      */
76     public void deleteTable(String tableName) {
77         db.getCollection(tableName).drop();
78     }
79
80     private ArrayList<Document> createDocuments(
81             PublishPayloadFormat publishPayloadFormat) {
82
83         Iterator<LinksPayloadFormat> linksPayloadFormatIter = publishPayloadFormat
84                 .getLinks().iterator();
85         ArrayList<Document> docList = new ArrayList<Document>();
86
87         while (linksPayloadFormatIter.hasNext()) {
88             LinksPayloadFormat links = linksPayloadFormatIter.next();
89             Document doc = new Document(Constants.RS_DEVICE_NAME,
90                     publishPayloadFormat.getDeviceName())
91                             .append(Constants.RS_DEVICE_ID,
92                                     publishPayloadFormat.getDi())
93                             .append(Constants.RS_BASE_URI,
94                                     publishPayloadFormat.getBaseUri())
95                             .append(Constants.RS_BITMAP,
96                                     publishPayloadFormat.getBitmap())
97                             .append(Constants.RS_HOSTING_PORT,
98                                     publishPayloadFormat.getPort())
99                             .append(Constants.RS_INS,
100                                     publishPayloadFormat.getIns())
101                             .append(Constants.RS_RTS,
102                                     publishPayloadFormat.getRts())
103                             .append(Constants.RS_DREL,
104                                     publishPayloadFormat.getDrel())
105                             .append(Constants.RS_TTL,
106                                     publishPayloadFormat.getTtl())
107                             .append(Constants.RS_HREF, links.getHref())
108                             .append(Constants.RS_RESOURCE_TYPE, links.getRt())
109                             .append(Constants.RS_INTERFACE, links.getItf())
110                             .append(Constants.RS_REL, links.getRel())
111                             .append(Constants.RS_OBS, links.isObs())
112                             .append(Constants.RS_TITLE, links.getTitle())
113                             .append(Constants.RS_URI, links.getUri())
114                             .append(Constants.RS_INS, links.getIns())
115                             .append(Constants.RS_MEDIA_TYPE, links.getMt());
116             docList.add(doc);
117         }
118
119         return docList;
120     }
121
122     private PublishPayloadFormat convertDocumentToResourceFormat(Document doc) {
123         PublishPayloadFormat publishPayloadFormat = new PublishPayloadFormat();
124         LinksPayloadFormat linksPayloadFormat = new LinksPayloadFormat();
125         ArrayList<LinksPayloadFormat> list = new ArrayList<LinksPayloadFormat>();
126
127         Object tmp = null;
128         
129         tmp = doc.get(Constants.RS_DEVICE_NAME);
130         if(tmp != null) {
131             publishPayloadFormat
132             .setDeviceName(tmp.toString());
133         }
134         
135         tmp = doc.get(Constants.RS_DEVICE_ID);
136         if(tmp != null) {
137             publishPayloadFormat.setDi(tmp.toString());
138         }
139         
140         tmp = doc.get(Constants.RS_BASE_URI);
141         if(tmp != null) {
142             publishPayloadFormat.setBaseUri(tmp.toString());
143         }
144         
145         tmp = doc.get(Constants.RS_BITMAP);
146         if(tmp != null) {
147             publishPayloadFormat.setBitmap((int)tmp);
148         }
149         
150         tmp = doc.get(Constants.RS_HOSTING_PORT);
151         if(tmp != null) {
152             publishPayloadFormat.setPort((int)tmp);
153         }
154        
155         tmp = doc.get(Constants.RS_INS);
156         if(tmp != null) {
157             publishPayloadFormat.setIns((int)tmp);
158         }
159         
160         tmp = doc.get(Constants.RS_RTS);
161         if(tmp != null) {
162             publishPayloadFormat.setRts(tmp.toString());
163         }
164         
165         tmp = doc.get(Constants.RS_DREL);
166         if(tmp != null) {
167             publishPayloadFormat.setDrel(tmp.toString());
168         }
169         
170         tmp = doc.get(Constants.RS_TTL);
171         if(tmp != null) {
172             publishPayloadFormat.setTtl((int)tmp);
173         }
174         
175         tmp = doc.get(Constants.RS_HREF);
176         if(tmp != null) {
177             linksPayloadFormat.setHref(tmp.toString());
178         }
179         
180         tmp = doc.get(Constants.RS_RESOURCE_TYPE);
181         if(tmp != null) {
182             linksPayloadFormat
183             .setRt((ArrayList<String>) tmp);
184         }
185
186         tmp = doc.get(Constants.RS_INTERFACE);
187         if(tmp != null) {
188             linksPayloadFormat
189             .setItf((ArrayList<String>) tmp);
190         }
191         
192         tmp = doc.get(Constants.RS_REL);
193         if(tmp != null) {
194             linksPayloadFormat.setRel(tmp.toString());
195         }
196         
197         tmp = doc.get(Constants.RS_OBS);
198         if(tmp != null) {
199             linksPayloadFormat.setObs((boolean)tmp);
200         }
201         
202         tmp = doc.get(Constants.RS_TITLE);
203         if(tmp != null) {
204             linksPayloadFormat.setTitle(tmp.toString());
205         }
206         
207         tmp = doc.get(Constants.RS_URI);
208         if(tmp != null) {
209             linksPayloadFormat.setUri(tmp.toString());
210         }
211         
212         tmp = doc.get(Constants.RS_INS);
213         if(tmp != null) {
214             linksPayloadFormat.setIns((int)tmp);
215         }
216         
217         tmp = doc.get(Constants.RS_MEDIA_TYPE);
218         if(tmp != null) {
219             linksPayloadFormat
220             .setMt((ArrayList<String>) tmp);
221         }
222
223         list.add(linksPayloadFormat);
224         publishPayloadFormat.setLinks(list);
225
226         return publishPayloadFormat;
227     }
228
229     /**
230      * API for storing information of published resources
231      *
232      * @param publishPayloadFormat
233      *            information of published resources to store in collection
234      * @param tablename
235      *            collection name
236      */
237     public void createResource(PublishPayloadFormat publishPayloadFormat,
238             String tablename) {
239         ArrayList<Document> docList = createDocuments(publishPayloadFormat);
240         Iterator<Document> docIter = docList.iterator();
241
242         MongoCollection<Document> collection = db.getCollection(tablename);
243
244         while (docIter.hasNext()) {
245             Document doc = docIter.next();
246
247             if (collection.findOneAndReplace(
248                     Filters.and(Filters.eq(Constants.RS_DEVICE_ID,
249                             doc.get(Constants.RS_DEVICE_ID)),
250                     Filters.eq(Constants.RS_INS, doc.get(Constants.RS_INS))),
251                     doc) == null) {
252
253                 collection.insertOne(doc);
254             }
255         }
256     }
257
258     /**
259      * API for finding resources matched filterValue of filterKey in collection
260      *
261      * @param filterKey
262      *            field name in collection
263      * @param filterValue
264      *            field value about field name
265      * @param tablename
266      *            collection name
267      * @return ArrayList<PublishPayloadFormat> - array list of resource
268      *         information
269      */
270     public ArrayList<PublishPayloadFormat> readResource(String filterKey,
271             String filterValue, String tablename) {
272         MongoCollection<Document> collection = db.getCollection(tablename);
273         ArrayList<PublishPayloadFormat> resourceFormatList = new ArrayList<PublishPayloadFormat>();
274         MongoCursor<Document> cursor = collection
275                 .find(Filters.eq(filterKey, filterValue)).iterator();
276         try {
277             while (cursor.hasNext()) {
278                 Document doc = cursor.next();
279                 resourceFormatList.add(convertDocumentToResourceFormat(doc));
280             }
281         } finally {
282             cursor.close();
283         }
284
285         return resourceFormatList;
286     }
287
288     /**
289      * API for finding resources matched filterValue of filterKey and a
290      * particular device ID in collection
291      *
292      * @param di
293      *            device id
294      * @param filterKey
295      *            field name in collection
296      * @param filterValue
297      *            field value about field name
298      * @param tablename
299      *            collection name
300      * @return ArrayList<PublishPayloadFormat> - array list of resource
301      *         information
302      */
303     public ArrayList<PublishPayloadFormat> readResourceAboutDid(String di,
304             String filterKey, String filterValue, String tablename) {
305         MongoCollection<Document> collection = db.getCollection(tablename);
306         ArrayList<PublishPayloadFormat> resourceFormatList = new ArrayList<PublishPayloadFormat>();
307         MongoCursor<Document> cursor = collection
308                 .find(Filters.and(Filters.eq(Constants.RS_DEVICE_ID, di),
309                         Filters.eq(filterKey, filterValue)))
310                 .iterator();
311         try {
312             while (cursor.hasNext()) {
313                 Document doc = cursor.next();
314                 resourceFormatList.add(convertDocumentToResourceFormat(doc));
315             }
316         } finally {
317             cursor.close();
318         }
319
320         return resourceFormatList;
321     }
322
323     /**
324      * API for deleting resources about a particular device ID in collection
325      *
326      * @param di
327      *            device id
328      * @param tablename
329      *            collection name
330      */
331     public void deleteResourceAboutDid(String di, String tablename) {
332
333         MongoCollection<Document> collection = db.getCollection(tablename);
334
335         collection.findOneAndDelete(Filters.eq(Constants.RS_DEVICE_ID, di));
336     }
337
338     /**
339      * API for deleting resources about a particular device ID and ins in
340      * collection
341      *
342      * @param di
343      *            device id
344      * @param ins
345      *            ins
346      * @param tablename
347      *            collection name
348      */
349     public void deleteResourceAboutDidAndIns(String di, String ins,
350             String tablename) {
351
352         MongoCollection<Document> collection = db.getCollection(tablename);
353
354         collection.findOneAndDelete(
355                 Filters.and(Filters.eq(Constants.RS_DEVICE_ID, di),
356                         Filters.eq(Constants.RS_INS, ins)));
357
358     }
359 }