This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
dbOps.go
618 lines (597 loc) · 16 KB
/
dbOps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*******************************************************************************
* Copyright 2017 Dell Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* @microservice: core-metadata-go service
* @author: Spencer Bull & Ryan Comer, Dell
* @version: 0.5.0
*******************************************************************************/
package main
import (
"time"
enums "github.com/edgexfoundry/core-domain-go/enums"
models "github.com/edgexfoundry/core-domain-go/models"
mgo "gopkg.in/mgo.v2"
bson "gopkg.in/mgo.v2/bson"
)
type DataStore struct {
s *mgo.Session
}
func (ds DataStore) dataStore() *DataStore {
return &DataStore{ds.s.Copy()}
}
// Connect to the database
func dbConnect() bool {
if DATABASE == enums.MONGODB {
mongoDBDialInfo := &mgo.DialInfo{
Addrs: []string{DOCKERMONGO},
Timeout: time.Duration(configuration.MongoDBConnectTimeout) * time.Millisecond,
Database: MONGODATABASE,
Username: DBUSER,
Password: DBPASS,
}
s, err := mgo.DialWithInfo(mongoDBDialInfo)
if err != nil {
return false
}
// Set timeout based on configuration
s.SetSocketTimeout(time.Duration(configuration.MongoDBConnectTimeout) * time.Millisecond)
DS.s = s
return true
}
return false
}
/* ----------------------- Schedule Event ------------------------------*/
func getAllScheduleEvents(se *[]models.ScheduleEvent) error {
if DATABASE == enums.MONGODB {
return mgoGetAllScheduleEvents(se)
}
return nil
}
func addScheduleEvent(se *models.ScheduleEvent) error {
if DATABASE == enums.MONGODB {
return mgoAddScheduleEvent(se)
}
return nil
}
func getScheduleEventByName(se *models.ScheduleEvent, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetScheduleEventByName(se, n)
}
return nil
}
func updateScheduleEvent(se models.ScheduleEvent) error {
if DATABASE == enums.MONGODB {
return mgoUpdateScheduleEvent(se)
}
return nil
}
func getScheduleEventById(se *models.ScheduleEvent, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetScheduleEventById(se, id)
}
return nil
}
func getScheduleEventsByScheduleName(se *[]models.ScheduleEvent, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetScheduleEventsByScheduleName(se, n)
}
return nil
}
func getScheduleEventsByAddressableId(se *[]models.ScheduleEvent, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetScheduleEventsByAddressableId(se, id)
}
return nil
}
func getScheduleEventsByServiceName(se *[]models.ScheduleEvent, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetScheduleEventsByServiceName(se, n)
}
return nil
}
/* -------------------------- Schedule ---------------------------------*/
func getAllSchedules(s *[]models.Schedule) error {
if DATABASE == enums.MONGODB {
return mgoGetAllSchedules(s)
}
return nil
}
func addSchedule(s *models.Schedule) error {
if DATABASE == enums.MONGODB {
return mgoAddSchedule(s)
}
return nil
}
func getScheduleByName(s *models.Schedule, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetScheduleByName(s, n)
}
return nil
}
func updateSchedule(s models.Schedule) error {
if DATABASE == enums.MONGODB {
return mgoUpdateSchedule(s)
}
return nil
}
func getScheduleById(s *models.Schedule, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetScheduleById(s, id)
}
return nil
}
/* ------------------------Device Report -------------------------------*/
func getAllDeviceReports(dr *[]models.DeviceReport) error {
if DATABASE == enums.MONGODB {
return mgoGetAllDeviceReports(dr)
}
return nil
}
func getDeviceReportByDeviceName(dr *[]models.DeviceReport, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceReportByDeviceName(dr, n)
}
return nil
}
func getDeviceReportByName(dr *models.DeviceReport, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceReportByName(dr, n)
}
return nil
}
func getDeviceReportById(dr *models.DeviceReport, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceReportById(dr, id)
}
return nil
}
func addDeviceReport(dr *models.DeviceReport) error {
if DATABASE == enums.MONGODB {
return mgoAddDeviceReport(dr)
}
return nil
}
func updateDeviceReport(dr *models.DeviceReport) error {
if DATABASE == enums.MONGODB {
return mgoUpdateDeviceReport(dr)
}
return nil
}
func getDeviceReportsByScheduleEventName(dr *[]models.DeviceReport, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceReportsByScheduleEventName(dr, n)
}
return nil
}
// ------------------------------------- DEVICE --------------------------------------------
func UpdateDevice(d models.Device) error {
if DATABASE == enums.MONGODB {
return mgoUpdateDevice(d)
}
return nil
}
func getDeviceById(d *models.Device, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceById(d, id)
}
return nil
}
func getDeviceByName(d *models.Device, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceByName(d, n)
}
return nil
}
func getAllDevices(d *[]models.Device) error {
if DATABASE == enums.MONGODB {
return mgoGetAllDevices(d)
}
return nil
}
func getDevicesByProfileId(d *[]models.Device, pid string) error {
if DATABASE == enums.MONGODB {
return mgoGetDevicesByProfileId(d, pid)
}
return nil
}
func getDevicesByProfileName(d *[]models.Device, pn string) error {
if DATABASE == enums.MONGODB {
return mgoGetDevicesByProfileName(d, pn)
}
return nil
}
func getDevicesByServiceId(d *[]models.Device, sid string) error {
if DATABASE == enums.MONGODB {
return mgoGetDevicesByServiceId(d, sid)
}
return nil
}
func getDevicesByServiceName(d *[]models.Device, sn string) error {
if DATABASE == enums.MONGODB {
return mgoGetDevicesByServiceName(d, sn)
}
return nil
}
func getDevicesByAddressableId(d *[]models.Device, aid string) error {
if DATABASE == enums.MONGODB {
return mgoGetDevicesByAddressableId(d, aid)
}
return nil
}
func getDevicesByAddressableName(d *[]models.Device, an string) error {
if DATABASE == enums.MONGODB {
return mgoGetDevicesByAddressableName(d, an)
}
return nil
}
func getDevicesWithLabel(d *[]models.Device, l []string) error {
if DATABASE == enums.MONGODB {
return mgoGetDevicesWithLabel(d, l)
}
return nil
}
func addDevice(d *models.Device) error {
if DATABASE == enums.MONGODB {
return mgoAddNewDevice(d)
}
return nil
}
func updateDeviceProfile(dp *models.DeviceProfile) error {
if DATABASE == enums.MONGODB {
return mgoUpdateDeviceProfile(dp)
}
return nil
}
func addDeviceProfile(d *models.DeviceProfile) error {
if DATABASE == enums.MONGODB {
return mgoAddNewDeviceProfile(d)
}
return nil
}
func getAllDeviceProfiles(d *[]models.DeviceProfile) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceProfiles(d, bson.M{})
}
return nil
}
func getDeviceProfileById(d *models.DeviceProfile, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceProfileById(d, id)
}
return nil
}
func deleteDeviceProfileById(dpid string) error {
if err := deleteById(DPCOL, dpid); err != nil {
return err
}
return nil
}
func deleteDeviceProfileByName(n string) error {
var dp models.DeviceProfile
getDeviceProfileByName(&dp, n)
// Delete all of the commands for the device profile
for i := 0; i < len(dp.Commands); i++ {
// TODO Figure out how to store MONGO ID
if err := deleteById(COMCOL, dp.Commands[i].Id.Hex()); err != nil {
return err
}
}
// Delete the device profile
if err := deleteByName(DPCOL, n); err != nil {
return err
}
return nil
}
//func getDeviceProfilesByCommandName(d *[]models.DeviceProfile, cn string) error {
// if DATABASE == enums.MONGODB {
// return mgoGetDeviceProfilesByCommandName(d, cn)
// }
// return nil
//}
func getDeviceProfilesByModel(dp *[]models.DeviceProfile, m string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceProfilesByModel(dp, m)
}
return nil
}
func getDeviceProfilesWithLabel(dp *[]models.DeviceProfile, l []string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceProfilesWithLabel(dp, l)
}
return nil
}
func getDeviceProfilesByManufacturerModel(dp *[]models.DeviceProfile, man string, mod string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceProfilesByManufacturerModel(dp, man, mod)
}
return nil
}
func getDeviceProfilesByManufacturer(dp *[]models.DeviceProfile, man string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceProfilesByManufacturer(dp, man)
}
return nil
}
func getDeviceProfileByName(dp *models.DeviceProfile, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceProfileByName(dp, n)
}
return nil
}
func updateAddressable(ra *models.Addressable, r *models.Addressable) error {
if DATABASE == enums.MONGODB {
return mgoUpdateAddressable(ra, r)
}
return nil
}
func addAddressable(a *models.Addressable) error {
if DATABASE == enums.MONGODB {
return mgoAddNewAddressable(a)
}
return nil
}
func getAddressableById(a *models.Addressable, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetAddressableById(a, id)
}
return nil
}
func getAddressableByName(a *models.Addressable, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetAddressableByName(a, n)
}
return nil
}
func getAddressablesByTopic(a *[]models.Addressable, t string) error {
if DATABASE == enums.MONGODB {
return mgoGetAddressablesByTopic(a, t)
}
return nil
}
func getAddressablesByPort(a *[]models.Addressable, p int) error {
if DATABASE == enums.MONGODB {
return mgoGetAddressablesByPort(a, p)
}
return nil
}
func getAddressablesByPublisher(a *[]models.Addressable, p string) error {
if DATABASE == enums.MONGODB {
return mgoGetAddressablesByPublisher(a, p)
}
return nil
}
func getAddressablesByAddress(a *[]models.Addressable, add string) error {
if DATABASE == enums.MONGODB {
return mgoGetAddressablesByAddress(a, add)
}
return nil
}
func getAddressable(d *models.Addressable, q bson.M) error {
if DATABASE == enums.MONGODB {
return mgoGetAddressable(d, q)
}
return nil
}
func getAddressables(d *[]models.Addressable, q bson.M) error {
if DATABASE == enums.MONGODB {
return mgoGetAddressables(d, q)
}
return nil
}
func isAddressableAssociatedToDevice(a models.Addressable) (bool, error) {
if DATABASE == enums.MONGODB {
return mgoIsAddressableAssociatedToDevice(a)
}
return false, nil
}
func isAddressableAssociatedToDeviceService(a models.Addressable) (bool, error) {
if DATABASE == enums.MONGODB {
return mgoIsAddressableAssociatedToDeviceService(a)
}
return false, nil
}
// ------------------------ DEVICE SERVICE -----------------------
func updateDeviceService(ds models.DeviceService) error {
if DATABASE == enums.MONGODB {
return mgoUpdateDeviceService(ds)
}
return nil
}
func getDeviceServicesByAddressableName(d *[]models.DeviceService, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceServicesByAddressableName(d, n)
}
return nil
}
func getDeviceServicesByAddressableId(d *[]models.DeviceService, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceServicesByAddressableId(d, id)
}
return nil
}
func getDeviceServicesWithLabel(d *[]models.DeviceService, l []string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceServicesWithLabel(d, l)
}
return nil
}
func getDeviceServiceById(d *models.DeviceService, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceServiceById(d, id)
}
return nil
}
func getDeviceServiceByName(d *models.DeviceService, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceServiceByName(d, n)
}
return nil
}
func getAllDeviceServices(d *[]models.DeviceService) error {
if DATABASE == enums.MONGODB {
return mgoGetAllDeviceServices(d)
}
return nil
}
func addDeviceService(ds *models.DeviceService) error {
if DATABASE == enums.MONGODB {
return mgoAddNewDeviceService(ds)
}
return nil
}
/* -----------------------Provision Watcher ----------------------*/
func getProvisionWatcherById(pw *models.ProvisionWatcher, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetProvisionWatcherById(pw, id)
}
return nil
}
func getAllProvisionWatchers(pw *[]models.ProvisionWatcher) error {
if DATABASE == enums.MONGODB {
return mgoGetAllProvisionWatchers(pw)
}
return nil
}
func getProvisionWatcherByName(pw *models.ProvisionWatcher, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetProvisionWatcherByName(pw, n)
}
return nil
}
func getProvisionWatcherByProfileId(pw *[]models.ProvisionWatcher, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetProvisionWatcherByProfileId(pw, id)
}
return nil
}
func getProvisionWatchersByProfileName(pw *[]models.ProvisionWatcher, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetProvisionWatchersByProfileName(pw, n)
}
return nil
}
func getProvisionWatchersByServiceId(pw *[]models.ProvisionWatcher, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetProvisionWatchersByServiceId(pw, id)
}
return nil
}
func getProvisionWatchersByServiceName(pw *[]models.ProvisionWatcher, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetProvisionWatchersByServiceName(pw, n)
}
return nil
}
func getProvisionWatchersByIdentifier(pw *[]models.ProvisionWatcher, k string, v string) error {
if DATABASE == enums.MONGODB {
return mgoGetProvisionWatchersByIdentifier(pw, k, v)
}
return nil
}
func addProvisionWatcher(pw *models.ProvisionWatcher) error {
if DATABASE == enums.MONGODB {
return mgoAddProvisionWatcher(pw)
}
return nil
}
func updateProvisionWatcher(pw models.ProvisionWatcher) error {
if DATABASE == enums.MONGODB {
return mgoUpdateProvisionWatcher(pw)
}
return nil
}
/* -----------------------COMMAND ----------------------*/
func getCommandById(c *models.Command, id string) error {
if DATABASE == enums.MONGODB {
err := mgoGetCommandById(c, id)
if err == mgo.ErrNotFound {
return ErrNotFound
} else {
return err
}
}
return nil
}
func getCommandByName(d *[]models.Command, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetCommandByName(d, id)
}
return nil
}
func addCommand(c *models.Command) error {
if DATABASE == enums.MONGODB {
return mgoAddCommand(c)
}
return nil
}
func getAllCommands(d *[]models.Command) error {
if DATABASE == enums.MONGODB {
return mgoGetAllCommands(d)
}
return nil
}
func updateCommand(c *models.Command, r *models.Command) error {
if DATABASE == enums.MONGODB {
return mgoUpdateCommand(c, r)
}
return nil
}
func deleteByName(c string, n string) error {
if DATABASE == enums.MONGODB {
return mgoDeleteByName(c, n)
}
return nil
}
func deleteCommandById(id string) error {
if DATABASE == enums.MONGODB {
return mgoDeleteCommandById(id)
}
return nil
}
// Get the device profiles that are using the command
func getDeviceProfilesUsingCommand(dp *[]models.DeviceProfile, c models.Command) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceProfilesUsingCommand(dp, c)
}
return nil
}
func deleteById(c string, did string) error {
if DATABASE == enums.MONGODB {
return mgoDeleteById(c, did)
}
return nil
}
func setByName(c string, n string, pv2 string, p2 string) error {
if DATABASE == enums.MONGODB {
return mgoUpdateByName(c, n, pv2, p2)
}
return nil
}
func setByNameInt(c string, n string, pv2 string, p2 int64) error {
if DATABASE == enums.MONGODB {
return mgoUpdateByNameInt(c, n, pv2, p2)
}
return nil
}
func setById(c string, did string, pv2 string, p2 string) error {
if DATABASE == enums.MONGODB {
return mgoUpdateById(c, did, pv2, p2)
}
return nil
}
func setByIdInt(c string, did string, pv2 string, p2 int64) error {
if DATABASE == enums.MONGODB {
return mgoUpdateByIdInt(c, did, pv2, p2)
}
return nil
}