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