[Simulator] Minor UI changes fixing the IOT-1087.
[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         publishPayloadFormat
128                 .setDeviceName(doc.getString(Constants.RS_DEVICE_NAME));
129         publishPayloadFormat.setDi(doc.getString(Constants.RS_DEVICE_ID));
130         publishPayloadFormat.setBaseUri(doc.getString(Constants.RS_BASE_URI));
131         publishPayloadFormat.setBitmap(doc.getInteger(Constants.RS_BITMAP));
132         publishPayloadFormat.setPort(doc.getInteger(Constants.RS_HOSTING_PORT));
133         publishPayloadFormat.setIns(doc.getInteger(Constants.RS_INS));
134         publishPayloadFormat.setRts(doc.getString(Constants.RS_RTS));
135         publishPayloadFormat.setDrel(doc.getString(Constants.RS_DREL));
136         publishPayloadFormat.setTtl(doc.getInteger(Constants.RS_TTL));
137
138         linksPayloadFormat.setHref(doc.getString(Constants.RS_HREF));
139         linksPayloadFormat
140                 .setRt((ArrayList<String>) doc.get(Constants.RS_RESOURCE_TYPE));
141         linksPayloadFormat
142                 .setItf((ArrayList<String>) doc.get(Constants.RS_INTERFACE));
143         linksPayloadFormat.setRel(doc.getString(Constants.RS_REL));
144         linksPayloadFormat.setObs(doc.getBoolean(Constants.RS_OBS));
145         linksPayloadFormat.setTitle(doc.getString(Constants.RS_TITLE));
146         linksPayloadFormat.setUri(doc.getString(Constants.RS_URI));
147         linksPayloadFormat.setIns(doc.getInteger(Constants.RS_INS));
148         linksPayloadFormat
149                 .setMt((ArrayList<String>) doc.get(Constants.RS_MEDIA_TYPE));
150
151         list.add(linksPayloadFormat);
152         publishPayloadFormat.setLinks(list);
153
154         return publishPayloadFormat;
155     }
156
157     /**
158      * API for storing information of published resources
159      *
160      * @param publishPayloadFormat
161      *            information of published resources to store in collection
162      * @param tablename
163      *            collection name
164      */
165     public void createResource(PublishPayloadFormat publishPayloadFormat,
166             String tablename) {
167         ArrayList<Document> docList = createDocuments(publishPayloadFormat);
168         Iterator<Document> docIter = docList.iterator();
169
170         MongoCollection<Document> collection = db.getCollection(tablename);
171
172         while (docIter.hasNext()) {
173             Document doc = docIter.next();
174
175             if (collection.findOneAndReplace(
176                     Filters.and(Filters.eq(Constants.RS_DEVICE_ID,
177                             doc.get(Constants.RS_DEVICE_ID)),
178                     Filters.eq(Constants.RS_INS, doc.get(Constants.RS_INS))),
179                     doc) == null) {
180
181                 collection.insertOne(doc);
182             }
183         }
184     }
185
186     /**
187      * API for finding resources matched filterValue of filterKey in collection
188      *
189      * @param filterKey
190      *            field name in collection
191      * @param filterValue
192      *            field value about field name
193      * @param tablename
194      *            collection name
195      * @return ArrayList<PublishPayloadFormat> - array list of resource
196      *         information
197      */
198     public ArrayList<PublishPayloadFormat> readResource(String filterKey,
199             String filterValue, String tablename) {
200         MongoCollection<Document> collection = db.getCollection(tablename);
201         ArrayList<PublishPayloadFormat> resourceFormatList = new ArrayList<PublishPayloadFormat>();
202         MongoCursor<Document> cursor = collection
203                 .find(Filters.eq(filterKey, filterValue)).iterator();
204         try {
205             while (cursor.hasNext()) {
206                 Document doc = cursor.next();
207                 resourceFormatList.add(convertDocumentToResourceFormat(doc));
208             }
209         } finally {
210             cursor.close();
211         }
212
213         return resourceFormatList;
214     }
215
216     /**
217      * API for finding resources matched filterValue of filterKey and a
218      * particular device ID in collection
219      *
220      * @param di
221      *            device id
222      * @param filterKey
223      *            field name in collection
224      * @param filterValue
225      *            field value about field name
226      * @param tablename
227      *            collection name
228      * @return ArrayList<PublishPayloadFormat> - array list of resource
229      *         information
230      */
231     public ArrayList<PublishPayloadFormat> readResourceAboutDid(String di,
232             String filterKey, String filterValue, String tablename) {
233         MongoCollection<Document> collection = db.getCollection(tablename);
234         ArrayList<PublishPayloadFormat> resourceFormatList = new ArrayList<PublishPayloadFormat>();
235         MongoCursor<Document> cursor = collection
236                 .find(Filters.and(Filters.eq(Constants.RS_DEVICE_ID, di),
237                         Filters.eq(filterKey, filterValue)))
238                 .iterator();
239         try {
240             while (cursor.hasNext()) {
241                 Document doc = cursor.next();
242                 resourceFormatList.add(convertDocumentToResourceFormat(doc));
243             }
244         } finally {
245             cursor.close();
246         }
247
248         return resourceFormatList;
249     }
250
251     /**
252      * API for deleting resources about a particular device ID in collection
253      *
254      * @param di
255      *            device id
256      * @param tablename
257      *            collection name
258      */
259     public void deleteResourceAboutDid(String di, String tablename) {
260
261         MongoCollection<Document> collection = db.getCollection(tablename);
262
263         collection.findOneAndDelete(Filters.eq(Constants.RS_DEVICE_ID, di));
264     }
265
266     /**
267      * API for deleting resources about a particular device ID and ins in
268      * collection
269      *
270      * @param di
271      *            device id
272      * @param ins
273      *            ins
274      * @param tablename
275      *            collection name
276      */
277     public void deleteResourceAboutDidAndIns(String di, String ins,
278             String tablename) {
279
280         MongoCollection<Document> collection = db.getCollection(tablename);
281
282         collection.findOneAndDelete(
283                 Filters.and(Filters.eq(Constants.RS_DEVICE_ID, di),
284                         Filters.eq(Constants.RS_INS, ins)));
285
286     }
287 }