68bbf5366508d7ce763c86bb43e4ca08798be720
[u/mrichter/AliRoot.git] / SHUTTLE / AliShuttleConfig.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /*
17 $Log$
18 Revision 1.7  2006/07/20 09:54:40  jgrosseo
19 introducing status management: The processing per subdetector is divided into several steps,
20 after each step the status is stored on disk. If the system crashes in any of the steps the Shuttle
21 can keep track of the number of failures and skips further processing after a certain threshold is
22 exceeded. These thresholds can be configured in LDAP.
23
24 Revision 1.6  2006/07/19 10:09:55  jgrosseo
25 new configuration, accesst to DAQ FES (Alberto)
26
27 Revision 1.5  2006/07/10 13:01:41  jgrosseo
28 enhanced storing of last sucessfully processed run (alberto)
29
30 Revision 1.4  2006/06/12 09:11:16  jgrosseo
31 coding conventions (Alberto)
32
33 Revision 1.3  2006/06/06 14:26:40  jgrosseo
34 o) removed files that were moved to STEER
35 o) shuttle updated to follow the new interface (Alberto)
36
37 Revision 1.7  2006/05/12 09:07:16  colla
38 12/05/06
39 New configuration complete
40
41 Revision 1.2  2006/03/07 07:52:34  hristov
42 New version (B.Yordanov)
43
44 Revision 1.4  2005/11/19 14:20:31  byordano
45 logbook config added to AliShuttleConfig
46
47 Revision 1.3  2005/11/17 19:24:25  byordano
48 TList changed to TObjArray in AliShuttleConfig
49
50 Revision 1.2  2005/11/17 14:43:23  byordano
51 import to local CVS
52
53 Revision 1.1.1.1  2005/10/28 07:33:58  hristov
54 Initial import as subdirectory in AliRoot
55
56 Revision 1.1.1.1  2005/09/12 22:11:40  byordano
57 SHUTTLE package
58
59 Revision 1.3  2005/08/30 09:13:02  byordano
60 some docs added
61
62 */
63
64
65 //
66 // This class keeps the AliShuttle configuration.
67 // It reads the configuration for LDAP server.
68 // For every child entry in basedn which has schema type 'shuttleConfig'
69 // it creates a detector configuration. This configuration includes:
70 // DCS server host and port and the set of aliases for which data from
71 // will be retrieved (used by AliShuttle).
72 //
73
74
75 #include "AliShuttleConfig.h"
76 #include "AliShuttleInterface.h"
77
78 #include "AliLog.h"
79
80 #include <TSystem.h>
81 #include <TObjString.h>
82 #include <TLDAPResult.h>
83 #include <TLDAPEntry.h>
84 #include <TLDAPAttribute.h>
85
86
87 AliShuttleConfig::AliShuttleConfigHolder::AliShuttleConfigHolder(const TLDAPEntry* entry):
88 fDetector(""),
89 fDCSHost(""),
90 fDCSPort(0),
91 fDCSAliases(),
92 fIsValid(kFALSE),
93 fSkipDCSQuery(kFALSE)
94 {
95 // constructor of the shuttle configuration holder
96
97         TLDAPAttribute* anAttribute;
98
99         anAttribute = entry->GetAttribute("det"); // MUST
100         fDetector = anAttribute->GetValue();
101
102         anAttribute = entry->GetAttribute("DCSHost"); // MAY
103         if (!anAttribute) {
104                 AliWarning(
105                         Form("%s has not DCS host entry - Shuttle will skip DCS data query!",
106                                 fDetector.Data()));
107                 fIsValid = kTRUE;
108                 fSkipDCSQuery = kTRUE;
109                 return;
110         }
111
112         fDCSHost = anAttribute->GetValue();
113
114         anAttribute = entry->GetAttribute("DCSPort"); // MAY
115         if (!anAttribute) {
116                 AliError(Form("Invalid configuration! %s has DCS Host but no port number!",
117                                 fDetector.Data()));
118                 return;
119         }
120         TString portStr = anAttribute->GetValue();
121         fDCSPort = portStr.Atoi();
122
123         anAttribute = entry->GetAttribute("DCSAlias"); // MAY
124         if (!anAttribute) {
125                 AliError(Form("Invalid configuration! %s has DCS host settings but no DCSAlias entries!",
126                                 fDetector.Data()));
127                 return;
128         }
129
130         const char* anAlias;
131         while ((anAlias = anAttribute->GetValue())) {
132                 fDCSAliases.AddLast(new TObjString(anAlias));
133         }
134
135         fIsValid = kTRUE;
136
137
138 }
139
140 //______________________________________________________________________________________________
141 AliShuttleConfig::AliShuttleConfigHolder::~AliShuttleConfigHolder()
142 {
143 // destructor of the shuttle configuration holder
144
145         fDCSAliases.Delete();
146 }
147
148 ClassImp(AliShuttleConfig)
149
150 //______________________________________________________________________________________________
151 AliShuttleConfig::AliShuttleConfig(const char* host, Int_t port,
152         const char* binddn, const char* password, const char* basedn):
153         fIsValid(kFALSE),
154         fDAQlbHost(""), fDAQlbUser(""), fDAQlbPass(""),
155         fMaxPPRetries(0), fMaxRetries(0),
156         fDetectorMap(), fDetectorList(),
157         fShuttleInstanceHost(""), fProcessedDetectors(),
158         fProcessAll(kFALSE)
159 {
160         //
161         // host: ldap server host
162         // port: ldap server port
163         // binddn: binddn used for ldap binding (simple bind is used!).
164         // password: password for binddn
165         // basedn: this is basedn whose childeren entries which have
166         // (objectClass=shuttleConfig) will be used as detector configurations.
167         //
168
169         TLDAPServer aServer(host, port, binddn, password, 3);
170
171         if (!aServer.IsConnected()) {
172                 AliError(Form("Can't connect to ldap server %s:%d", 
173                                 host, port));
174                 return;
175         }
176
177         // reads configuration for the shuttle running on this machine
178         
179         fShuttleInstanceHost = gSystem->HostName();
180         TString queryFilter = "(ShuttleHost=";
181         queryFilter += fShuttleInstanceHost;
182         queryFilter += ")";     
183         
184         TLDAPResult* aResult = aServer.Search(basedn, LDAP_SCOPE_ONELEVEL,
185                         queryFilter.Data());
186
187         if (!aResult) {
188                 AliError(Form("Can't find configuration with base DN: %s",
189                                 basedn));
190                 return;
191         }
192
193         if (aResult->GetCount() == 0) {
194                 AliError(Form("No Shuttle instance for host = %s!",
195                                         fShuttleInstanceHost.Data()));
196                 AliError(Form("All detectors will be processed."));
197                 fProcessAll=kTRUE;
198         }
199
200         if (aResult->GetCount() > 1) {
201                 AliError(Form("More than one Shuttle instance for host %s!",
202                                         fShuttleInstanceHost.Data()));
203                 return;
204         }
205
206         TLDAPEntry* anEntry;
207         TLDAPAttribute* anAttribute;
208
209         if(!fProcessAll){
210                 anEntry = aResult->GetNext();
211                 anAttribute = anEntry->GetAttribute("detectors");
212                 const char *detName;
213                 while((detName = anAttribute->GetValue())){
214                         TObjString *objDet= new TObjString(detName);
215                         fProcessedDetectors.Add(objDet);
216                 }
217         }
218
219         // Detector configuration (DCS Archive DB settings)
220
221         aResult = aServer.Search(basedn, LDAP_SCOPE_ONELEVEL,
222                         "(objectClass=AliShuttleDetector)");
223         if (!aResult) {
224                 AliError(Form("Can't find configuration with base DN: %s",
225                                 basedn));
226                 return;
227         }
228
229
230         while ((anEntry = aResult->GetNext())) {
231                 AliShuttleConfigHolder* aHolder = new AliShuttleConfigHolder(anEntry);
232                 delete anEntry;
233
234                 if (!aHolder->IsValid()) {
235                         AliError("Detector configuration error!");
236                         delete aHolder;
237                         return;
238                 }
239
240                 TObjString* detStr = new TObjString(aHolder->GetDetector());
241                 fDetectorMap.Add(detStr, aHolder);
242                 fDetectorList.AddLast(detStr);
243         }
244
245         delete aResult;
246
247         // Global configuration (DAQ logbook)
248
249         aResult = aServer.Search(basedn, LDAP_SCOPE_ONELEVEL,
250                         "(objectClass=AliShuttleGlobalConfig)");
251         if (!aResult) {
252                 AliError(Form("Can't find configuration with base DN: %s",
253                                 basedn));
254                 return;
255         }
256
257         if (aResult->GetCount() == 0) {
258                 AliError("Can't find DAQ logbook configuration!");
259                 return;
260         }
261
262         if (aResult->GetCount() > 1) {
263                 AliError("More than one DAQ logbook configuration found!");
264                 return;
265         }
266
267         anEntry = aResult->GetNext();
268
269         anAttribute = anEntry->GetAttribute("DAQLogbookHost");
270         if (!anAttribute) {
271                 AliError("Can't find DAQLogbookHost attribute!");
272                 return;
273         }
274         fDAQlbHost = anAttribute->GetValue();
275
276         anAttribute = anEntry->GetAttribute("DAQLogbookUser");
277         if (!anAttribute) {
278                 AliError("Can't find DAQLogbookUser attribute!");
279                 return;
280         }
281         fDAQlbUser = anAttribute->GetValue();
282
283         anAttribute = anEntry->GetAttribute("DAQLogbookPassword");
284         if (!anAttribute) {
285                 AliError("Can't find DAQLogbookPassword attribute!");
286                 return;
287         }
288         fDAQlbPass = anAttribute->GetValue();
289
290         anAttribute = anEntry->GetAttribute("MaxPPRetries");
291         if (!anAttribute) {
292                 AliError("Can't find MaxPPRetries attribute!");
293                 return;
294         }
295         TString tmpStr = anAttribute->GetValue();
296   fMaxPPRetries = tmpStr.Atoi();
297
298         anAttribute = anEntry->GetAttribute("MaxRetries");
299         if (!anAttribute) {
300                 AliError("Can't find MaxRetries attribute!");
301                 return;
302         }
303         tmpStr = anAttribute->GetValue();
304   fMaxRetries = tmpStr.Atoi();
305
306   delete anEntry;
307         delete aResult;
308
309         // FES configuration (FES logbook and hosts)
310
311
312         for(int iSys=0;iSys<3;iSys++){
313                 queryFilter = Form("(system=%s)", AliShuttleInterface::fkSystemNames[iSys]);
314                 aResult = aServer.Search(basedn, LDAP_SCOPE_ONELEVEL, queryFilter.Data());
315                 if (!aResult) {
316                         AliError(Form("Can't find configuration for system: %s",
317                                         AliShuttleInterface::fkSystemNames[iSys]));
318                         return;
319                 }
320
321                 if (aResult->GetCount() != 1 ) {
322                         AliError("Error in FES configuration!");
323                         return;
324                 }
325
326                 anEntry = aResult->GetNext();
327
328                 anAttribute = anEntry->GetAttribute("LogbookHost");
329                 if (!anAttribute) {
330                         AliError(Form ("Can't find LogbookHost attribute for %s!!",
331                                                 AliShuttleInterface::fkSystemNames[iSys]));
332                         return;
333                 }
334                 fFESlbHost[iSys] = anAttribute->GetValue();
335
336                 anAttribute = anEntry->GetAttribute("LogbookUser");
337                 if (!anAttribute) {
338                         AliError(Form ("Can't find LogbookUser attribute for %s!!",
339                                                 AliShuttleInterface::fkSystemNames[iSys]));
340                         return;
341                 }
342                 fFESlbUser[iSys] = anAttribute->GetValue();
343
344                 anAttribute = anEntry->GetAttribute("LogbookPassword");
345                 if (!anAttribute) {
346                         AliError(Form ("Can't find LogbookPassword attribute for %s!!",
347                                                 AliShuttleInterface::fkSystemNames[iSys]));
348                         return;
349                 }
350                 fFESlbPass[iSys] = anAttribute->GetValue();
351
352                 anAttribute = anEntry->GetAttribute("FSHost");
353                 if (!anAttribute) {
354                         AliError(Form ("Can't find FSHost attribute for %s!!",
355                                                 AliShuttleInterface::fkSystemNames[iSys]));
356                         return;
357                 }
358                 fFESHost[iSys] = anAttribute->GetValue();
359
360                 anAttribute = anEntry->GetAttribute("FSUser");
361                 if (!anAttribute) {
362                         AliError(Form ("Can't find FSUser attribute for %s!!",
363                                                 AliShuttleInterface::fkSystemNames[iSys]));
364                         return;
365                 }
366                 fFESUser[iSys] = anAttribute->GetValue();
367
368                 anAttribute = anEntry->GetAttribute("FSPassword");
369                 if (anAttribute) fFESPass[iSys] = anAttribute->GetValue();
370
371                 delete anEntry;
372                 delete aResult;
373         }
374
375         fIsValid = kTRUE;
376 }
377
378 //______________________________________________________________________________________________
379 AliShuttleConfig::~AliShuttleConfig()
380 {
381 // destructor
382
383         fDetectorMap.DeleteAll();
384 }
385
386 //______________________________________________________________________________________________
387 const TObjArray* AliShuttleConfig::GetDetectors() const
388 {
389         //
390         // returns collection of TObjString which contains the name
391         // of every detector which is in the configuration.
392         //
393
394         return &fDetectorList;
395 }
396
397 //______________________________________________________________________________________________
398 Bool_t AliShuttleConfig::HasDetector(const char* detector) const
399 {
400         //
401         // checks for paricular detector in the configuration.
402         //
403         return fDetectorMap.GetValue(detector) != NULL;
404 }
405
406 //______________________________________________________________________________________________
407 const char* AliShuttleConfig::GetDCSHost(const char* detector) const
408 {
409         //
410         // returns DCS server host used by particular detector
411         //
412         
413         AliShuttleConfigHolder* aHolder = (AliShuttleConfigHolder*) fDetectorMap.GetValue(detector);
414         if (!aHolder) {
415                 AliError(Form("There isn't configuration for detector: %s",
416                         detector));
417                 return NULL;
418         }
419
420         return aHolder->GetDCSHost();
421 }
422
423 //______________________________________________________________________________________________
424 Int_t AliShuttleConfig::GetDCSPort(const char* detector) const
425 {
426         //
427         // returns DCS server port used by particular detector
428         //
429
430
431         AliShuttleConfigHolder* aHolder = (AliShuttleConfigHolder*) fDetectorMap.GetValue(detector);
432         if (!aHolder) {
433                 AliError(Form("There isn't configuration for detector: %s",
434                         detector));
435                 return 0;
436         }
437
438         return aHolder->GetDCSPort();
439 }
440
441 //______________________________________________________________________________________________
442 const TObjArray* AliShuttleConfig::GetDCSAliases(const char* detector) const
443 {
444         //
445         // returns collection of TObjString which represents the set of aliases
446         // which used for data retrieval for particular detector
447         //
448
449         AliShuttleConfigHolder* aHolder = (AliShuttleConfigHolder*) fDetectorMap.GetValue(detector);
450         if (!aHolder) {
451                 AliError(Form("There isn't configuration for detector: %s",
452                         detector));
453                 return NULL;
454         }
455
456         return aHolder->GetDCSAliases();
457 }
458
459 //______________________________________________________________________________________________
460 Bool_t AliShuttleConfig::HostProcessDetector(const char* detector) const
461 {
462         // return TRUE if detector is handled by host or if fProcessAll is TRUE
463
464         if(fProcessAll) return kTRUE;
465         TIter iter(&fProcessedDetectors);
466         TObjString* detName;
467         while((detName = (TObjString*) iter.Next())){
468                 if(detName->String() == detector) return kTRUE;
469         }
470         return kFALSE;
471 }
472
473 //______________________________________________________________________________________________
474 void AliShuttleConfig::Print(Option_t* /*option*/) const
475 {
476 // print configuration
477         
478         TString result;
479         result += '\n';
480
481         result += Form("\nShuttle running on %s \n\n", fShuttleInstanceHost.Data());
482
483         if(fProcessAll) {
484                 result += Form("All detectors will be processed! \n\n");
485         } else {
486                 result += "Detectors processed by this host: ";
487                 TIter it(&fProcessedDetectors);
488                 TObjString* aDet;
489                 while ((aDet = (TObjString*) it.Next())) {
490                         result += Form("%s ", aDet->String().Data());
491                 }
492                 result += "\n\n";
493         }
494
495         result += Form("DAQ Logbook Configuration \n \tHost: %s - User: %s - ",
496                 fDAQlbHost.Data(), fDAQlbUser.Data());
497
498         result += "Password: ";
499         result.Append('*', fDAQlbPass.Length());
500         result += "\n\n";
501
502         for(int iSys=0;iSys<3;iSys++){
503                 result += Form("FES Configuration for %s system\n", AliShuttleInterface::fkSystemNames[iSys]);
504                 result += Form("\tLogbook host: \t%s - \tUser: %s\n",
505                                                 fFESlbHost[iSys].Data(), fFESlbUser[iSys].Data());
506                 // result += Form("Logbook Password:",fFESlbPass[iSys].Data());
507                 result += Form("\tFES host: \t%s - \tUser: %s\n\n", fFESHost[iSys].Data(), fFESUser[iSys].Data());
508                 // result += Form("FES Password:",fFESPass[iSys].Data());
509         }
510
511         TIter iter(fDetectorMap.GetTable());
512         TPair* aPair;
513         while ((aPair = (TPair*) iter.Next())) {
514                 AliShuttleConfigHolder* aHolder = (AliShuttleConfigHolder*) aPair->Value();
515                 if(aHolder->SkipDCSQuery()) continue;
516                 result += Form("DCS archive DB configuration for *** %s *** \n", aHolder->GetDetector());
517                 result += Form("\tAmanda server: %s:%d \n", aHolder->GetDCSHost(), aHolder->GetDCSPort());
518
519                 result += "\tDCS Aliases: ";
520                 const TObjArray* aliases = aHolder->GetDCSAliases();
521                 TIter it(aliases);
522                 TObjString* anAlias;
523                 while ((anAlias = (TObjString*) it.Next())) {
524                         result += Form("%s ", anAlias->String().Data());
525                 }
526
527                 result += "\n\n";
528
529         }
530
531         if(!fIsValid) result += "\n\n********** !!!!! Configuration is INVALID !!!!! **********\n";
532
533         AliInfo(result);
534 }