blob: c65bf3f97030617a0530657a7419002341eff13d [file] [log] [blame]
Patrick Benavoli68a91282011-08-31 11:23:23 +02001/* <auto_header>
2 * <FILENAME>
3 *
4 * INTEL CONFIDENTIAL
5 * Copyright © 2011 Intel
6 * Corporation All Rights Reserved.
7 *
8 * The source code contained or described herein and all documents related to
9 * the source code ("Material") are owned by Intel Corporation or its suppliers
10 * or licensors. Title to the Material remains with Intel Corporation or its
11 * suppliers and licensors. The Material contains trade secrets and proprietary
12 * and confidential information of Intel or its suppliers and licensors. The
13 * Material is protected by worldwide copyright and trade secret laws and
14 * treaty provisions. No part of the Material may be used, copied, reproduced,
15 * modified, published, uploaded, posted, transmitted, distributed, or
16 * disclosed in any way without Intel’s prior express written permission.
17 *
18 * No license under any patent, copyright, trade secret or other intellectual
19 * property right is granted to or conferred upon you by disclosure or delivery
20 * of the Materials, either expressly, by implication, inducement, estoppel or
21 * otherwise. Any license under such intellectual property rights must be
22 * express and approved by Intel in writing.
23 *
24 * AUTHOR: Patrick Benavoli (patrickx.benavoli@intel.com)
25 * CREATED: 2011-06-01
26 * UPDATED: 2011-07-27
27 *
28 *
29 * </auto_header>
30 */
31#include "ConfigurableDomains.h"
32#include "ConfigurableDomain.h"
33#include "ConfigurableElement.h"
34#include "BinaryStream.h"
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020035#include "AutoLog.h"
Patrick Benavoli68a91282011-08-31 11:23:23 +020036
37#define base CBinarySerializableElement
38
39CConfigurableDomains::CConfigurableDomains(const string& strSystemClassName) : base(strSystemClassName)
40{
41}
42
43string CConfigurableDomains::getKind() const
44{
45 return "ConfigurableDomains";
46}
47
48bool CConfigurableDomains::childrenAreDynamic() const
49{
50 return true;
51}
52
53// Ensure validity on whole domains from main blackboard
54void CConfigurableDomains::validate(const CParameterBlackboard* pMainBlackboard)
55{
56 // Delegate to domains
57 uint32_t uiChild;
58 uint32_t uiNbConfigurableDomains = getNbChildren();
59
60 for (uiChild = 0; uiChild < uiNbConfigurableDomains; uiChild++) {
61
62 CConfigurableDomain* pChildConfigurableDomain = static_cast<CConfigurableDomain*>(getChild(uiChild));
63
64 pChildConfigurableDomain->validate(pMainBlackboard);
65 }
66}
67
68// Configuration application if required
Patrick Benavoli63499d42011-10-24 18:50:03 +020069bool CConfigurableDomains::apply(CParameterBlackboard* pParameterBlackboard, bool bForce, string& strError) const
Patrick Benavoli68a91282011-08-31 11:23:23 +020070{
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020071 CAutoLog autoLog(this, "Applying configurations");
Patrick Benavoli68a91282011-08-31 11:23:23 +020072
73 // Syncer set
74 CSyncerSet syncerSet;
75
76 // Delegate to domains
Patrick Benavoli63499d42011-10-24 18:50:03 +020077
78 // Start with sequence unaware domains
Patrick Benavoli68a91282011-08-31 11:23:23 +020079 uint32_t uiChild;
80 uint32_t uiNbConfigurableDomains = getNbChildren();
81
82 for (uiChild = 0; uiChild < uiNbConfigurableDomains; uiChild++) {
83
Patrick Benavoli63499d42011-10-24 18:50:03 +020084 const CConfigurableDomain* pChildConfigurableDomain = static_cast<const CConfigurableDomain*>(getChild(uiChild));
Patrick Benavoli68a91282011-08-31 11:23:23 +020085
Patrick Benavoli63499d42011-10-24 18:50:03 +020086 if (!pChildConfigurableDomain->getSequenceAwareness() && !pChildConfigurableDomain->apply(pParameterBlackboard, syncerSet, bForce, strError)) {
87
88 return false;
89 }
Patrick Benavoli68a91282011-08-31 11:23:23 +020090 }
91 // Synchronize
Patrick Benavoli63499d42011-10-24 18:50:03 +020092 if (!syncerSet.sync(*pParameterBlackboard, false, strError)) {
93
94 return false;
95 }
96
97 // Then deal with sequence aware domains
98 for (uiChild = 0; uiChild < uiNbConfigurableDomains; uiChild++) {
99
100 const CConfigurableDomain* pChildConfigurableDomain = static_cast<const CConfigurableDomain*>(getChild(uiChild));
101
102 if (pChildConfigurableDomain->getSequenceAwareness() && !pChildConfigurableDomain->apply(pParameterBlackboard, syncerSet, bForce, strError)) {
103
104 return false;
105 }
106 }
107
108 return true;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200109}
110
111// From IXmlSource
112void CConfigurableDomains::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const
113{
114 // Set attribute
115 xmlElement.setAttributeString("SystemClassName", getName());
116
117 base::toXml(xmlElement, serializingContext);
118}
119
120// Configuration/Domains handling
121/// Domains
122bool CConfigurableDomains::createDomain(const string& strName, string& strError)
123{
124 // Already exists?
125 if (findChild(strName)) {
126
127 strError = "Already existing configurable domain";
128
129 return false;
130 }
131
132 log("Creating configurable domain \"%s\"", strName.c_str());
133
134 // Creation/Hierarchy
135 addChild(new CConfigurableDomain(strName));
136
137 return true;
138}
139
140bool CConfigurableDomains::deleteDomain(const string& strName, string& strError)
141{
142 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strName));
143
144 if (!pConfigurableDomain) {
145
146 strError = "Configurable domain not found";
147
148 return false;
149 }
150
151 // Check domain has no rule (prevent accidental loss of data)
152 if (pConfigurableDomain->hasRules()) {
153
154 strError = "Deletion of domain containing configurations with application rules is not supported to prevent any accitental loss of data.\nPlease consider a direct modification of the XML file.";
155
156 return false;
157 }
158
159 log("Deleting configurable domain \"%s\"", strName.c_str());
160
161 // Hierarchy
162 removeChild(pConfigurableDomain);
163
164 // Destroy
165 delete pConfigurableDomain;
166
167 return true;
168}
169
170bool CConfigurableDomains::renameDomain(const string& strName, const string& strNewName, string& strError)
171{
172 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strName));
173
174 if (!pConfigurableDomain) {
175
176 strError = "Configurable domain not found";
177
178 return false;
179 }
180
181 log("Renaming configurable domain \"%s\" to \"%s\"", strName.c_str(), strNewName.c_str());
182
183 // Rename
184 return pConfigurableDomain->rename(strNewName, strError);
185}
186
Patrick Benavoli63499d42011-10-24 18:50:03 +0200187bool CConfigurableDomains::setSequenceAwareness(const string& strDomain, bool bSequenceAware, string& strError)
188{
189 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
190
191 if (!pConfigurableDomain) {
192
193 strError = "Configurable domain not found";
194
195 return false;
196 }
197
198 pConfigurableDomain->setSequenceAwareness(bSequenceAware);
199
200 return true;
201}
202
203bool CConfigurableDomains::getSequenceAwareness(const string& strDomain, bool& bSequenceAware, string& strError) const
204{
205 const CConfigurableDomain* pConfigurableDomain = static_cast<const CConfigurableDomain*>(findChild(strDomain));
206
207 if (!pConfigurableDomain) {
208
209 strError = "Configurable domain not found";
210
211 return false;
212 }
213
214 bSequenceAware = pConfigurableDomain->getSequenceAwareness();
215
216 return true;
217}
218
Patrick Benavoli68a91282011-08-31 11:23:23 +0200219/// Configurations
220bool CConfigurableDomains::listConfigurations(const string& strDomain, string& strResult) const
221{
222 const CElement* pConfigurableDomain = findChild(strDomain);
223
224 if (!pConfigurableDomain) {
225
226 strResult = "Configurable domain not found";
227
228 return false;
229 }
230 // delegate
231 pConfigurableDomain->listChildren(strResult);
232
233 return true;
234}
235
236bool CConfigurableDomains::createConfiguration(const string& strDomain, const string& strConfiguration, const CParameterBlackboard* pMainBlackboard, string& strError)
237{
238 // Find domain
239 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
240
241 if (!pConfigurableDomain) {
242
243 strError = "Configurable domain " + strDomain + " not found";
244
245 return false;
246 }
247 // Delegate
248 return pConfigurableDomain->createConfiguration(strConfiguration, pMainBlackboard, strError);
249}
250
251bool CConfigurableDomains::deleteConfiguration(const string& strDomain, const string& strConfiguration, string& strError)
252{
253 // Find domain
254 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
255
256 if (!pConfigurableDomain) {
257
258 strError = "Configurable domain " + strDomain + " not found";
259
260 return false;
261 }
262 // Delegate
263 return pConfigurableDomain->deleteConfiguration(strConfiguration, strError);
264}
265
266bool CConfigurableDomains::renameConfiguration(const string& strDomain, const string& strConfigurationName, const string& strNewConfigurationName, string& strError)
267{
268 // Find domain
269 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
270
271 if (!pConfigurableDomain) {
272
273 strError = "Configurable domain " + strDomain + " not found";
274
275 return false;
276 }
277 // Delegate
278 return pConfigurableDomain->renameConfiguration(strConfigurationName, strNewConfigurationName, strError);
279}
280
281bool CConfigurableDomains::listDomainElements(const string& strDomain, string& strResult) const
282{
283 // Find domain
284 const CConfigurableDomain* pConfigurableDomain = static_cast<const CConfigurableDomain*>(findChild(strDomain));
285
286 if (!pConfigurableDomain) {
287
288 strResult = "Configurable domain " + strDomain + " not found";
289
290 return false;
291 }
292 // Delegate
293 pConfigurableDomain->listAssociatedToElements(strResult);
294
295 return true;
296}
297
298bool CConfigurableDomains::split(const string& strDomain, CConfigurableElement* pConfigurableElement, string& strError)
299{
300 // Find domain
301 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
302
303 if (!pConfigurableDomain) {
304
305 strError = "Configurable domain " + strDomain + " not found";
306
307 return false;
308 }
309 // Delegate
310 pConfigurableDomain->split(pConfigurableElement, strError);
311
312 return true;
313}
314
315void CConfigurableDomains::listAssociatedElements(string& strResult) const
316{
317 strResult = "\n";
318
319 set<const CConfigurableElement*> configurableElementSet;
320
321 // Get all owned configurable elements
322 gatherAllOwnedConfigurableElements(configurableElementSet);
323
324 // Fill result
325 set<const CConfigurableElement*>::const_iterator it;
326
327 for (it = configurableElementSet.begin(); it != configurableElementSet.end(); ++it) {
328
329 const CConfigurableElement* pConfigurableElement = *it;
330
331 string strAssociatedDomainList;
332
333 pConfigurableElement->listAssociatedDomains(strAssociatedDomainList, false);
334
335 strResult += pConfigurableElement->getPath() + " [" + strAssociatedDomainList + "]\n";
336 }
337}
338
339void CConfigurableDomains::listConflictingElements(string& strResult) const
340{
341 strResult = "\n";
342
343 set<const CConfigurableElement*> configurableElementSet;
344
345 // Get all owned configurable elements
346 gatherAllOwnedConfigurableElements(configurableElementSet);
347
348 // Fill result
349 set<const CConfigurableElement*>::const_iterator it;
350
351 for (it = configurableElementSet.begin(); it != configurableElementSet.end(); ++it) {
352
353 const CConfigurableElement* pConfigurableElement = *it;
354
355 if (pConfigurableElement->getBelongingDomainCount() > 1) {
356
357 string strBelongingDomainList;
358
359 pConfigurableElement->listBelongingDomains(strBelongingDomainList, false);
360
361 strResult += pConfigurableElement->getPath() + " contained in multiple domains: " + strBelongingDomainList + "\n";
362 }
363 }
364}
365
Patrick Benavoli63499d42011-10-24 18:50:03 +0200366void CConfigurableDomains::listDomains(string& strResult) const
367{
368 strResult = "\n";
369
370 // List domains
371 uint32_t uiChild;
372 uint32_t uiNbConfigurableDomains = getNbChildren();
373
374 for (uiChild = 0; uiChild < uiNbConfigurableDomains; uiChild++) {
375
376 const CConfigurableDomain* pChildConfigurableDomain = static_cast<const CConfigurableDomain*>(getChild(uiChild));
377
378 // Name
379 strResult += pChildConfigurableDomain->getName();
380
381 // Sequence awareness
382 if (pChildConfigurableDomain->getSequenceAwareness()) {
383
384 strResult += " [sequence aware]\n";
385 }
386 }
387}
388
Patrick Benavoli68a91282011-08-31 11:23:23 +0200389// Gather configurable elements owned by any domain
390void CConfigurableDomains::gatherAllOwnedConfigurableElements(set<const CConfigurableElement*>& configurableElementSet) const
391{
392 // Delegate to domains
393 uint32_t uiChild;
394 uint32_t uiNbConfigurableDomains = getNbChildren();
395
396 for (uiChild = 0; uiChild < uiNbConfigurableDomains; uiChild++) {
397
398 const CConfigurableDomain* pChildConfigurableDomain = static_cast<const CConfigurableDomain*>(getChild(uiChild));
399
400 pChildConfigurableDomain->gatherConfigurableElements(configurableElementSet);
401 }
402}
403
404// Config restore
405bool CConfigurableDomains::restoreConfiguration(const string& strDomain, const string& strConfiguration, CParameterBlackboard* pMainBlackboard, bool bAutoSync, string& strError)
406{
407 // Find domain
408 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
409
410 if (!pConfigurableDomain) {
411
412 strError = "Configurable domain " + strDomain + " not found";
413
414 return false;
415 }
416 // Delegate
417 return pConfigurableDomain->restoreConfiguration(strConfiguration, pMainBlackboard, bAutoSync, strError);
418}
419
420// Config save
421bool CConfigurableDomains::saveConfiguration(const string& strDomain, const string& strConfiguration, const CParameterBlackboard* pMainBlackboard, string& strError)
422{
423 // Find domain
424 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
425
426 if (!pConfigurableDomain) {
427
428 strError = "Configurable domain " + strDomain + " not found";
429
430 return false;
431 }
432 // Delegate
433 return pConfigurableDomain->saveConfiguration(strConfiguration, pMainBlackboard, strError);
434}
435
Patrick Benavoli63499d42011-10-24 18:50:03 +0200436bool CConfigurableDomains::setElementSequence(const string& strDomain, const string& strConfiguration, const vector<string>& astrNewElementSequence, string& strError)
437{
438 // Find domain
439 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
440
441 if (!pConfigurableDomain) {
442
443 strError = "Configurable domain " + strDomain + " not found";
444
445 return false;
446 }
447
448 // Delegate to domain
449 return pConfigurableDomain->setElementSequence(strConfiguration, astrNewElementSequence, strError);
450}
451
452bool CConfigurableDomains::getElementSequence(const string& strDomain, const string& strConfiguration, string& strResult) const
453{
454 // Find domain
455 const CConfigurableDomain* pConfigurableDomain = static_cast<const CConfigurableDomain*>(findChild(strDomain));
456
457 if (!pConfigurableDomain) {
458
459 strResult = "Configurable domain " + strDomain + " not found";
460
461 return false;
462 }
463 // Delegate to domain
464 return pConfigurableDomain->getElementSequence(strConfiguration, strResult);
465}
466
Patrick Benavoli68a91282011-08-31 11:23:23 +0200467// Last applied configurations
468void CConfigurableDomains::listLastAppliedConfigurations(string& strResult) const
469{
Patrick Benavoli68a91282011-08-31 11:23:23 +0200470 // Browse domains
471 uint32_t uiChild;
472 uint32_t uiNbConfigurableDomains = getNbChildren();
473
474 for (uiChild = 0; uiChild < uiNbConfigurableDomains; uiChild++) {
475
476 const CConfigurableDomain* pChildConfigurableDomain = static_cast<const CConfigurableDomain*>(getChild(uiChild));
477
478 strResult += pChildConfigurableDomain->getName() + ": " + pChildConfigurableDomain->getLastAppliedConfigurationName() + "\n";
479 }
480}
481
482// Configurable element - domain association
483bool CConfigurableDomains::addConfigurableElementToDomain(const string& strDomain, CConfigurableElement* pConfigurableElement, const CParameterBlackboard* pMainBlackboard, string& strError)
484{
485 // Find domain
486 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
487
488 if (!pConfigurableDomain) {
489
490 strError = "Configurable domain " + strDomain + " not found";
491
492 return false;
493 }
494 // Delegate
495 return pConfigurableDomain->addConfigurableElement(pConfigurableElement, pMainBlackboard, strError);
496}
497
498bool CConfigurableDomains::removeConfigurableElementFromDomain(const string& strDomain, CConfigurableElement* pConfigurableElement, string& strError)
499{
500 // Find domain
501 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
502
503 if (!pConfigurableDomain) {
504
505 strError = "Configurable domain " + strDomain + " not found";
506
507 return false;
508 }
509 // Delegate
510 return pConfigurableDomain->removeConfigurableElement(pConfigurableElement, strError);
511}
512
513// Binary settings load/store
514bool CConfigurableDomains::serializeSettings(const string& strBinarySettingsFilePath, bool bOut, uint8_t uiStructureChecksum, string& strError)
515{
516 // Instantiate byte stream
517 CBinaryStream binarySettingsStream(strBinarySettingsFilePath, bOut, getDataSize(), uiStructureChecksum);
518
519 // Open file
520 if (!binarySettingsStream.open(strError)) {
521
522 strError = "Unable to open binary settings file " + strBinarySettingsFilePath + ": " + strError;
523
524 return false;
525 }
526
527 // Serialize
528 binarySerialize(binarySettingsStream);
529
530 // Close stream
531 binarySettingsStream.close();
532
533 return true;
534}