Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

#!/usr/bin/env python 

 

""" 

@file ion/integration/ais/common/metadata_cache.py 

@author David Everett 

@brief Class to cache metadata contained in data sets and data sources.  This 

is just an in-memory cache (non-persistent); it uses a dictionary of 

dictionaries (multi-dimensional dictionary).  The rows are dictionaries of 

either data set metadata for data source metadata; they are indexed by the 

resourceID. 

""" 

 

import ion.util.ionlog 

log = ion.util.ionlog.getLogger(__name__) 

import logging 

from twisted.internet import defer 

 

from decimal import Decimal 

 

from ion.core.object import object_utils 

from ion.core.messaging.message_client import MessageClient 

 

from ion.services.coi.resource_registry.resource_client import ResourceClient, ResourceClientError 

from ion.services.coi.resource_registry.association_client import AssociationClient, AssociationClientError 

 

from ion.services.dm.inventory.association_service import AssociationServiceClient, AssociationServiceError, ASSOCIATION_GET_STAR_MSG_TYPE 

from ion.services.dm.inventory.association_service import PREDICATE_OBJECT_QUERY_TYPE, \ 

    SUBJECT_PREDICATE_QUERY_TYPE, IDREF_TYPE 

from ion.services.coi.datastore_bootstrap.ion_preload_config import TYPE_OF_ID, \ 

    DATASET_RESOURCE_TYPE_ID, DATASOURCE_RESOURCE_TYPE_ID, HAS_A_ID, OWNED_BY_ID 

 

PREDICATE_REFERENCE_TYPE = object_utils.create_type_identifier(object_id=25, version=1) 

 

# 

# Common Metadata Constants 

# 

TYPE         = 'type' 

 

# 

# Data Set Metadata Constants 

# 

DSET         = 'dset' 

KEY          = 'key' 

RESOURCE_ID  = 'ResourceIdentity' 

DSOURCE_ID   = 'DSourceID' 

OWNER_ID     = 'OwnerID' 

TITLE        = 'title' 

INSTITUTION  = 'institution' 

SOURCE       = 'source' 

REFERENCES   = 'references' 

TIME_START   = 'ion_time_coverage_start' 

TIME_END     = 'ion_time_coverage_end' 

SUMMARY      = 'summary' 

COMMENT      = 'comment' 

LAT_MIN      = 'ion_geospatial_lat_min' 

LAT_MAX      = 'ion_geospatial_lat_max' 

LON_MIN      = 'ion_geospatial_lon_min' 

LON_MAX      = 'ion_geospatial_lon_max' 

VERT_MIN     = 'ion_geospatial_vertical_min' 

VERT_MAX     = 'ion_geospatial_vertical_max' 

VERT_POS     = 'ion_geospatial_vertical_positive' 

 

# 

# Data Source Metadata Constants 

# 

DSOURCE      = 'dsource' 

REGISTRATION_TIME = 'registration_datetime_millis' 

PROPERTY = 'property' 

REQUEST_TYPE = 'request_type' 

STATION_ID = 'station_id' 

BASE_URL = 'base_url' 

MAX_INGEST_MILLIS = 'max_ingest_millis' 

ION_TITLE = 'ion_title' 

LCS = 'lcs' 

UPDATE_INTERVAL_SECONDS = 'update_interval_seconds' 

VISUALIZATION_URL = 'visualization_url' 

VISIBILITY = 'visibility' 

 

class MetadataCache(object): 

 

 

    def __init__(self, ais): 

        log.info('MetadataCache.__init__()') 

 

        self.mc = MessageClient(proc = ais) 

        self.asc = AssociationServiceClient(proc = ais) 

        self.rc = ResourceClient(proc = ais) 

        self.ac = AssociationClient(proc = ais) 

 

        self.numDSets    = 0 

        self.numDSources = 0 

 

        self.__metadata = {} 

 

 

        # 

        # A lock to ensure exclusive access to cache when updating 

        # 

        self.cacheLock = {} 

        self.cacheLock = defer.DeferredLock() 

 

    def getNumDatasets(self): 

        return self.numDSets 

 

    def getNumDatasources(self): 

        return self.numDSources 

 

    def getDatasets(self): 

        dSetList = [] 

        for ds in self.__metadata.itervalues(): 

            if (ds[TYPE] is DSET): 

                dSetList.append(ds) 

        return dSetList 

 

 

    def getDataSources(self): 

        dSourceList = [] 

        for ds in self.__metadata.itervalues(): 

            if (ds[TYPE] is DSOURCE): 

                dSourceList.append(ds) 

        return dSourceList 

 

    @defer.inlineCallbacks 

    def loadDataSets(self): 

        """ 

        Find all resources of type DATASET_RESOURCE_TYPE_ID and load their 

        metadata.  The private __loadDSetMetadata method will only load 

        the metadata if the data set is in the Active. 

        """ 

 

        log.debug('loadDataSets()') 

 

        # Get the list of dataset resource IDs 

        dSetResults = yield self.__findResourcesOfType(DATASET_RESOURCE_TYPE_ID) 

        if dSetResults == None: 

            log.error('Error finding dataset resources.') 

            defer.returnValue(False) 

 

        numDSets =  len(dSetResults.idrefs) 

        log.debug('Found ' + str(numDSets) + ' datasets.') 

 

        try: 

            yield self.__lockCache() 

 

            i = 0 

            while (i < numDSets): 

                yield self.__putDSetMetadata(dSetResults.idrefs[i].key) 

                i = i + 1 

 

        finally: 

            self.__unlockCache() 

 

        defer.returnValue(True) 

 

 

    @defer.inlineCallbacks 

    def loadDataSources(self): 

        """ 

        Find all resources of type DATASOURCE_RESOURCE_TYPE_ID and load their 

        metadata.  The private __loadDSetMetadata method will only load 

        the metadata if the data source is in the Active. 

        """ 

 

        log.debug('loadDataSources()') 

 

        # Get the list of datasource resource IDs 

        dSourceResults = yield self.__findResourcesOfType(DATASOURCE_RESOURCE_TYPE_ID) 

        if dSourceResults == None: 

            log.error('Error finding datasource resources.') 

            defer.returnValue(False) 

 

        numDSources =  len(dSourceResults.idrefs) 

        log.debug('Found ' + str(numDSources) + ' datasources.') 

 

        try: 

            yield self.__lockCache() 

 

            i = 0 

            while (i < numDSources): 

                yield self.__putDSourceMetadata(dSourceResults.idrefs[i].key) 

                i = i + 1 

 

        finally: 

            self.__unlockCache() 

 

        defer.returnValue(True) 

 

 

    @defer.inlineCallbacks 

    def getDSet(self, dSetID): 

        """ 

        Get the dictionary entry containing the metadata from the data set 

        represented by the given ResourceID (dSetID); return the dataset 

        object instead of the metadata. 

        """ 

 

        if dSetID is None: 

            log.error('getDSeta: dSetID is None') 

            returnValue = None 

        else: 

            log.debug('getDSet for dSetID %s' %(dSetID)) 

 

            try: 

                yield self.__lockCache() 

 

                metadata = self.__metadata[dSetID] 

                log.debug('Metadata keys for ' + dSetID + ': ' + str(metadata.keys())) 

                returnValue = metadata[DSET] 

            except KeyError: 

                log.error('Metadata not found for datasetID: %s'  %(dSetID)) 

                returnValue = None 

 

            finally: 

                self.__unlockCache() 

 

        defer.returnValue(returnValue) 

 

 

    @defer.inlineCallbacks 

    def getDSetMetadata(self, dSetID): 

        """ 

        Get the dictionary entry containing the metadata from the data set 

        represented by the given ResourceID (dSetID). 

        """ 

 

        if dSetID is None: 

            log.error('getDSetMetadata: dSetID is None') 

            returnValue = None 

        else: 

            log.debug('getDSetMetadata for dSetID %s' %(dSetID)) 

 

            try: 

                yield self.__lockCache() 

 

                metadata = self.__metadata[dSetID] 

                log.debug('Metadata keys for ' + dSetID + ': ' + str(metadata.keys())) 

                returnValue = metadata 

            except KeyError: 

                log.info('Metadata not found for datasetID: ' + dSetID) 

                returnValue = None 

 

            finally: 

                self.__unlockCache() 

 

        defer.returnValue(returnValue) 

 

 

    @defer.inlineCallbacks 

    def putDSetMetadata(self, dSetID): 

        """ 

        Get the instance of the data set represented by the given resource 

        ID (dSetID) and call the private __loadDSetMetadata method with the 

        data set as an argument.  

        """ 

 

        if dSetID is None: 

            log.error('putDSetMetadata: dSetID is None') 

        else: 

            log.debug('putDSetMetadata for dSetID %s' %(dSetID)) 

 

            try: 

                yield self.__lockCache() 

 

                yield self.__putDSetMetadata(dSetID) 

 

            finally: 

                self.__unlockCache() 

 

 

    @defer.inlineCallbacks 

    def deleteDSetMetadata(self, dSetID): 

        """ 

        Delete the dictionary entry for the data set represented by the given 

        resource ID (dSetID). 

        """ 

 

        if dSetID is None: 

            log.error('deleteDSetMetadata: DSetID is None') 

            returnValue = False 

        else: 

            log.debug('deleteDSetMetadata: deleting %s' %(dSetID)) 

 

            try: 

                yield self.__lockCache() 

 

                # 

                # Set the persistent flag to False 

                # 

                dSetMetadata = self.__metadata.pop(dSetID) 

                dSet = dSetMetadata[DSET] 

                dSet.Repository.persistent = False 

 

 

            except KeyError: 

                log.error('deleteDSetMetadata: datasetID ' + dSetID + ' not cached') 

                returnValue = False 

            else: 

                self.numDSets -= 1 

                returnValue = True 

 

            finally: 

 

                self.__unlockCache() 

 

        defer.returnValue(returnValue) 

 

 

    @defer.inlineCallbacks 

    def getDSource(self, dSourceID): 

        """ 

        Get the dictionary entry containing the metadata from the data source 

        represented by the given ResourceID (dSourceID); return the datasource 

        object instead of the metadata 

        """ 

 

        if dSourceID is None: 

            log.error('getDSource: dSourceID is None') 

            returnValue = None 

        else: 

            log.debug('getDSource for %s' %(dSourceID)) 

 

            try: 

                yield self.__lockCache() 

 

                metadata = self.__metadata[dSourceID] 

                log.debug('Metadata keys for ' + dSourceID + ': ' + str(metadata.keys())) 

                returnValue = metadata[DSOURCE] 

            except KeyError: 

                log.info('Metadata not found for datasourceID: ' + dSourceID) 

                returnValue = None 

 

            finally: 

                self.__unlockCache() 

 

        defer.returnValue(returnValue) 

 

 

    @defer.inlineCallbacks 

    def getDSourceMetadata(self, dSourceID): 

        """ 

        Get the dictionary entry containing the metadata from the data source 

        represented by the given ResourceID (dSourceID). 

        """ 

 

        if dSourceID is None: 

            log.error('getDSourceMetadata: dSourceID is None') 

            returnValue = None 

        else: 

            log.debug('getDSourceMetadata for %s' %(dSourceID)) 

 

            try: 

                yield self.__lockCache() 

 

                log.debug('getDSourceMetadata') 

 

                metadata = self.__metadata[dSourceID] 

                log.debug('Metadata keys for ' + dSourceID + ': ' + str(metadata.keys())) 

                returnValue = metadata 

            except KeyError: 

                log.info('Metadata not found for datasourceID: ' + dSourceID) 

                returnValue = None 

 

            finally: 

                self.__unlockCache() 

 

        defer.returnValue(returnValue) 

 

 

    @defer.inlineCallbacks 

    def putDSourceMetadata(self, dSourceID): 

        """ 

        Put the instance of the data source represented by the given resource 

        ID (dSourceID).  

        """ 

 

        if dSourceID is None: 

            log.error('putDSourceMetadata: dSourceID is None') 

        else: 

            log.debug('putDSourceMetadata for dSourceID %s' %(dSourceID)) 

 

            try: 

                yield self.__lockCache() 

 

                yield self.__putDSourceMetadata(dSourceID) 

 

            finally: 

                self.__unlockCache() 

 

 

    @defer.inlineCallbacks 

    def deleteDSourceMetadata(self, dSourceID): 

        """ 

        Delete the dictionary entry for the data source represented by the given 

        resource ID (dSourceID). 

        """ 

 

        if dSourceID is None: 

            log.error('deleteDSourceMetadata: dSourceID is None') 

            returnValue = False 

        else: 

            log.debug('deleteDSourceMetadata for %s' %(dSourceID)) 

 

            try: 

                yield self.__lockCache() 

 

                # 

                # Set the persistent flag to False 

                # 

                dSourceMetadata = self.__metadata.pop(dSourceID) 

                dSource = dSourceMetadata[DSOURCE] 

                dSource.Repository.persistent = False 

 

 

            except KeyError: 

                log.error('deleteDSourceMetadata: datasourceID ' + dSourceID + ' not cached') 

                returnValue = False 

            else: 

                self.numDSources -= 1 

                returnValue = True 

 

            finally: 

                self.__unlockCache() 

 

        defer.returnValue(returnValue) 

 

 

    @defer.inlineCallbacks 

    def getAssociatedSource(self, dSetID): 

        """ 

        Worker class private method to get the data source that associated 

        with a given data set.   

        """ 

 

        if dSetID is None: 

            log.error('getAssociatedSource: dSetID is None') 

            defer.returnValue(None) 

 

        log.debug('getAssociatedSource for dSetID %s' %(dSetID)) 

 

        qmsg = yield self.mc.create_instance(PREDICATE_OBJECT_QUERY_TYPE) 

        pair = qmsg.pairs.add() 

        pair.object = qmsg.CreateObject(IDREF_TYPE) 

        pair.object.key = dSetID 

        pair.predicate = qmsg.CreateObject(PREDICATE_REFERENCE_TYPE) 

        pair.predicate.key = HAS_A_ID 

 

        pair = qmsg.pairs.add() 

        pair.object = qmsg.CreateObject(IDREF_TYPE) 

        pair.object.key = DATASOURCE_RESOURCE_TYPE_ID 

        pair.predicate = qmsg.CreateObject(PREDICATE_REFERENCE_TYPE) 

        pair.predicate.key = TYPE_OF_ID 

        try: 

            results = yield self.asc.get_subjects(qmsg) 

        except: 

            log.exception('Error getting associated data source for Dataset: %s' % dSetID) 

            defer.returnValue(None) 

 

        dsrcs = [str(x.key) for x in results.idrefs] 

 

        # we expect one: 

        if len(dsrcs) != 1: 

            log.error('Expected 1 datasource, got %d' % len(dsrcs)) 

            defer.returnValue(None) 

 

        log.debug('getAssociatedSource() exit: returning: %s' % dsrcs[0]) 

 

        defer.returnValue(dsrcs[0]) 

 

    @defer.inlineCallbacks 

    def getAssociatedDatasets(self, dSource): 

        """ 

        Worker class private method to get the data sets that associated 

        with a given data source.   

        """ 

        log.debug('getAssociatedDatasets() entry') 

 

        dSetList = [] 

 

        if dSource is None: 

            log.error('getAssociatedDatasets: dSource parameter is None') 

            defer.returnValue(dSetList) 

 

        qmsg = yield self.mc.create_instance(ASSOCIATION_GET_STAR_MSG_TYPE) 

        pair = qmsg.subject_pairs.add() 

        pair.subject = qmsg.CreateObject(IDREF_TYPE) 

        pair.subject.key = dSource.ResourceIdentity 

        pair.predicate = qmsg.CreateObject(PREDICATE_REFERENCE_TYPE) 

        pair.predicate.key = HAS_A_ID 

 

        pair = qmsg.object_pairs.add() 

        pair.object = qmsg.CreateObject(IDREF_TYPE) 

        pair.object.key = DATASET_RESOURCE_TYPE_ID 

        pair.predicate = qmsg.CreateObject(PREDICATE_REFERENCE_TYPE) 

        pair.predicate.key = TYPE_OF_ID 

        try: 

            results = yield self.asc.get_star(qmsg) 

        except: 

            log.error('Error getting associated data sets for Datasource: ' + \ 

                      dSource.ResourceIdentity) 

            defer.returnValue([]) 

 

        dsets = [str(x.key) for x in results.idrefs] 

 

        log.info('Datasource %s has %d associated datasets.' %(dSource.ResourceIdentity, len(dsets))) 

        log.debug('getAssociatedDatasets) exit: returning: %s' %(str(dsets))) 

 

        defer.returnValue(dsets) 

 

 

    @defer.inlineCallbacks 

    def __lockCache(self): 

        """ 

        Lock the cache to insure exclusive access while updating 

        """ 

 

        log.debug('__lockCache requesting lock') 

        yield self.cacheLock.acquire() 

        log.debug('__lockCache lock acquired') 

 

 

    def __unlockCache(self): 

        """ 

        Unlock the cache to insure exclusive access while updating 

        """ 

 

        log.debug('__unlockCache') 

        self.cacheLock.release() 

 

 

    @defer.inlineCallbacks 

    def __putDSetMetadata(self, dSetID): 

        """ 

        Get the instance of the data set represented by the given resource 

        ID (dSetID) and call the private __loadDSetMetadata method with the 

        data set as an argument.  

        """ 

 

        log.debug('__putDSetMetadata') 

 

        try: 

            dSet = yield self.rc.get_instance(dSetID) 

            yield self.__loadDSetMetadata(dSet) 

        except ResourceClientError: 

            log.error('get_instance failed for data set ID %s !' %(dSetID)) 

 

 

    @defer.inlineCallbacks 

    def __putDSourceMetadata(self, dSourceID): 

        """ 

        Get the instance of the data source represented by the given resource 

        ID (dSourceID) and call the private __loadDSourceMetadata method with the 

        data source as an argument.  

        """ 

 

        log.debug('__putDSourceMetadata') 

 

        try: 

            dSource = yield self.rc.get_instance(dSourceID) 

            self.__loadDSourceMetadata(dSource) 

        except ResourceClientError: 

            log.error('get_instance failed for data source ID %s !' %(dSourceID)) 

 

 

    @defer.inlineCallbacks 

    def __loadDSetMetadata(self, dSet): 

        """ 

        Create and load a dictionary entry with the metadata from the given 

        data set, and insert the entry into the __metadata dictionary (a 

        dictionary of dictionaries).  Only do this if the data set is Active. 

        """ 

 

        log.debug('__loadDSetMetadata for dSet: %s' %(dSet.ResourceIdentity)) 

 

        # 

        # Only cache the metadata if the data set is in the ACTIVE state. 

        # 

        if (dSet.ResourceLifeCycleState == dSet.ACTIVE): 

            dSetMetadata = {} 

            self.numDSets += 1 

            # 

            # Store the entire dataset now; should be doing only that anyway. 

            # Set persisence to true.  NOTE: remember to set this to false 

            # on delete. 

            # 

            dSetMetadata[TYPE] = DSET 

            dSet.Repository.persistent = True 

            dSetMetadata[DSET] = dSet 

            dSetMetadata[DSOURCE_ID] = yield self.getAssociatedSource(dSet.ResourceIdentity) 

            dSetMetadata[RESOURCE_ID] = dSet.ResourceIdentity 

            dSetMetadata[OWNER_ID] = yield self.__getAssociatedOwner(dSet.ResourceIdentity) 

            for attrib in dSet.root_group.attributes: 

                #log.debug('Root Attribute: %s = %s'  % (str(attrib.name), str(attrib.GetValue()))) 

                if attrib.name == TITLE: 

                    dSetMetadata[TITLE] = attrib.GetValue() 

                elif attrib.name == INSTITUTION: 

                    dSetMetadata[INSTITUTION] = attrib.GetValue() 

                elif attrib.name == SOURCE: 

                    dSetMetadata[SOURCE] = attrib.GetValue() 

                elif attrib.name == REFERENCES: 

                    dSetMetadata[REFERENCES] = attrib.GetValue() 

                elif attrib.name == TIME_START: 

                    dSetMetadata[TIME_START] = attrib.GetValue() 

                elif attrib.name == TIME_END: 

                    dSetMetadata[TIME_END] = attrib.GetValue() 

                elif attrib.name == SUMMARY: 

                    dSetMetadata[SUMMARY] = attrib.GetValue() 

                elif attrib.name == COMMENT: 

                    dSetMetadata[COMMENT] = attrib.GetValue() 

                elif attrib.name == LAT_MIN: 

                    dSetMetadata[LAT_MIN] = Decimal(str(attrib.GetValue())) 

                elif attrib.name == LAT_MAX: 

                    dSetMetadata[LAT_MAX] = Decimal(str(attrib.GetValue())) 

                elif attrib.name == LON_MIN: 

                    dSetMetadata[LON_MIN] = Decimal(str(attrib.GetValue())) 

                elif attrib.name == LON_MAX: 

                    dSetMetadata[LON_MAX] = Decimal(str(attrib.GetValue())) 

                elif attrib.name == VERT_MIN: 

                    dSetMetadata[VERT_MIN] = Decimal(str(attrib.GetValue())) 

                elif attrib.name == VERT_MAX: 

                    dSetMetadata[VERT_MAX] = Decimal(str(attrib.GetValue())) 

                elif attrib.name == VERT_POS: 

                    dSetMetadata[VERT_POS] = attrib.GetValue() 

                dSetMetadata[LCS] = dSet.ResourceLifeCycleState 

 

            log.debug('dSetMetadata keys: ' + str(dSetMetadata.keys())) 

            # 

            # Store this dSetMetadata in the dictionary, indexed by the resourceID 

            # 

            self.__metadata[dSet.ResourceIdentity] = dSetMetadata 

 

            if log.getEffectiveLevel() <= logging.DEBUG: 

                self.__printMetadata('Dataset Metadata', dSet) 

        else: 

            log.info('data set %s is not ACTIVE: Not caching.' %(dSet.ResourceIdentity)) 

 

 

    def __loadDSourceMetadata(self, dSource): 

        """ 

        Create and load a dictionary entry with the metadata from the given 

        data source, and insert the entry into the __metadata dictionary (a 

        dictionary of dictionaries).  Only do this if the data source is Active. 

        """ 

 

        log.debug('__loadDSourceMetadata for dSource: %s' %(dSource.ResourceIdentity)) 

 

        # 

        # Only cache the metadata if the data source is in the ACTIVE state. 

        # 

        if (dSource.ResourceLifeCycleState == dSource.ACTIVE): 

            dSourceMetadata = {} 

            self.numDSources += 1 

            # 

            # Store the entire datasource now; should be doing only that anyway 

            # Set persisence to true.  NOTE: remember to set this to false 

            # on delete. 

            # 

            dSourceMetadata[TYPE] = DSOURCE 

            dSource.Repository.persistent = True 

            dSourceMetadata[DSOURCE] = dSource 

            for property in dSource.property: 

                dSourceMetadata[PROPERTY] = property 

 

            for sid in dSource.station_id: 

                dSourceMetadata[STATION_ID] = sid 

 

            dSourceMetadata[REGISTRATION_TIME] = dSource.registration_datetime_millis 

            dSourceMetadata[REQUEST_TYPE] = dSource.request_type 

            dSourceMetadata[BASE_URL] = dSource.base_url 

            dSourceMetadata[MAX_INGEST_MILLIS] = dSource.max_ingest_millis 

            dSourceMetadata[ION_TITLE] = dSource.ion_title 

            dSourceMetadata[UPDATE_INTERVAL_SECONDS] = dSource.update_interval_seconds 

            dSourceMetadata[LCS] = dSource.ResourceLifeCycleState 

            dSourceMetadata[VISUALIZATION_URL] = dSource.visualization_url 

            dSourceMetadata[VISIBILITY] = dSource.is_public 

 

            log.debug('dSourceMetadata keys: ' + str(dSourceMetadata.keys())) 

            # 

            # Store this dSourceMetadata in the dictionary, indexed by the resourceID 

            # 

            self.__metadata[dSource.ResourceIdentity] = dSourceMetadata 

 

            if log.getEffectiveLevel() <= logging.DEBUG: 

                self.__printMetadata('Datasource Metadata', dSource) 

        else: 

            log.info('data source %s is not ACTIVE: Not caching.' %(dSource.ResourceIdentity)) 

 

 

 

    @defer.inlineCallbacks 

    def __findResourcesOfType(self, resourceType): 

        """ 

        A utility method to find all resources of the given type (resourceType). 

        """ 

 

        request = yield self.mc.create_instance(PREDICATE_OBJECT_QUERY_TYPE) 

 

        # 

        # Set up a resource type search term using: 

        # - TYPE_OF_ID as predicate 

        # - object of type: resourceType parameter as object 

        # 

        pair = request.pairs.add() 

 

        # ..(predicate) 

        pref = request.CreateObject(PREDICATE_REFERENCE_TYPE) 

        pref.key = TYPE_OF_ID 

 

        pair.predicate = pref 

 

        # ..(object) 

        type_ref = request.CreateObject(IDREF_TYPE) 

        type_ref.key = resourceType 

 

        pair.object = type_ref 

 

        try: 

            result = yield self.asc.get_subjects(request) 

 

        except AssociationServiceError: 

            log.error('__findResourcesOfType: association error!') 

            defer.returnValue(None) 

 

        defer.returnValue(result) 

 

 

    @defer.inlineCallbacks 

    def __getAssociatedOwner(self, dsID): 

        """ 

        Worker class method to find the owner associated with a data set. 

        This is a public method because it can be called from the 

        findDataResourceDetail worker class. 

        """ 

        log.debug('getAssociatedOwner() entry') 

 

        request = yield self.mc.create_instance(SUBJECT_PREDICATE_QUERY_TYPE) 

 

        # 

        # Set up an owned_by_id search term using: 

        # - OWNED_BY_ID as predicate 

        # - LCS_REFERENCE_TYPE object set to ACTIVE as object 

        # 

        pair = request.pairs.add() 

 

        # ..(predicate) 

        pref = request.CreateObject(PREDICATE_REFERENCE_TYPE) 

        pref.key = OWNED_BY_ID 

 

        pair.predicate = pref 

 

        # ..(subject) 

        type_ref = request.CreateObject(IDREF_TYPE) 

        type_ref.key = dsID 

 

        pair.subject = type_ref 

 

        log.info('Calling get_objects with dsID: ' + dsID) 

 

        try: 

            result = yield self.asc.get_objects(request) 

 

        except AssociationServiceError: 

            log.error('getAssociatedOwner: association error!') 

            defer.returnValue(None) 

 

        if len(result.idrefs) == 0: 

            log.error('Owner not found!') 

            defer.returnValue('OWNER NOT FOUND!') 

        elif len(result.idrefs) == 1: 

            log.debug('getAssociatedOwner() exit') 

            defer.returnValue(result.idrefs[0].key) 

        else: 

            log.error('More than 1 owner found!') 

            defer.returnValue('MULTIPLE OWNERS!') 

 

 

    def __printMetadata(self, resType, res): 

        log.debug('Metadata for ' + resType + ': ' + res.ResourceIdentity + ':') 

        for key in self.__metadata[res.ResourceIdentity].keys(): 

            log.debug('key: ' + key) 

            # 

            # If this is a datasource object, don't print 

            # 

            if key == 'dset' or key == 'dsource': 

                log.debug('value is dset or dsource (object); not printing') 

            else: 

                log.debug('value: ' + str(self.__metadata[res.ResourceIdentity][key])) 

 

 

 

    def __printObject(self, object): 

        if log.getEffectiveLevel() <= logging.DEBUG: 

            log.debug('Object: ' + str(object))