blob: fa794b3fd8645619529107f0944bae27f3a32df3 [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
69bool CConfigurableDomains::apply(CParameterBlackboard* pParameterBlackboard, bool bForce, string& strError)
70{
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
77 uint32_t uiChild;
78 uint32_t uiNbConfigurableDomains = getNbChildren();
79
80 for (uiChild = 0; uiChild < uiNbConfigurableDomains; uiChild++) {
81
82 CConfigurableDomain* pChildConfigurableDomain = static_cast<CConfigurableDomain*>(getChild(uiChild));
83
84 pChildConfigurableDomain->apply(pParameterBlackboard, syncerSet, bForce);
85 }
86 // Synchronize
87 return syncerSet.sync(*pParameterBlackboard, false, strError);
88}
89
90// From IXmlSource
91void CConfigurableDomains::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const
92{
93 // Set attribute
94 xmlElement.setAttributeString("SystemClassName", getName());
95
96 base::toXml(xmlElement, serializingContext);
97}
98
99// Configuration/Domains handling
100/// Domains
101bool CConfigurableDomains::createDomain(const string& strName, string& strError)
102{
103 // Already exists?
104 if (findChild(strName)) {
105
106 strError = "Already existing configurable domain";
107
108 return false;
109 }
110
111 log("Creating configurable domain \"%s\"", strName.c_str());
112
113 // Creation/Hierarchy
114 addChild(new CConfigurableDomain(strName));
115
116 return true;
117}
118
119bool CConfigurableDomains::deleteDomain(const string& strName, string& strError)
120{
121 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strName));
122
123 if (!pConfigurableDomain) {
124
125 strError = "Configurable domain not found";
126
127 return false;
128 }
129
130 // Check domain has no rule (prevent accidental loss of data)
131 if (pConfigurableDomain->hasRules()) {
132
133 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.";
134
135 return false;
136 }
137
138 log("Deleting configurable domain \"%s\"", strName.c_str());
139
140 // Hierarchy
141 removeChild(pConfigurableDomain);
142
143 // Destroy
144 delete pConfigurableDomain;
145
146 return true;
147}
148
149bool CConfigurableDomains::renameDomain(const string& strName, const string& strNewName, string& strError)
150{
151 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strName));
152
153 if (!pConfigurableDomain) {
154
155 strError = "Configurable domain not found";
156
157 return false;
158 }
159
160 log("Renaming configurable domain \"%s\" to \"%s\"", strName.c_str(), strNewName.c_str());
161
162 // Rename
163 return pConfigurableDomain->rename(strNewName, strError);
164}
165
166/// Configurations
167bool CConfigurableDomains::listConfigurations(const string& strDomain, string& strResult) const
168{
169 const CElement* pConfigurableDomain = findChild(strDomain);
170
171 if (!pConfigurableDomain) {
172
173 strResult = "Configurable domain not found";
174
175 return false;
176 }
177 // delegate
178 pConfigurableDomain->listChildren(strResult);
179
180 return true;
181}
182
183bool CConfigurableDomains::createConfiguration(const string& strDomain, const string& strConfiguration, const CParameterBlackboard* pMainBlackboard, string& strError)
184{
185 // Find domain
186 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
187
188 if (!pConfigurableDomain) {
189
190 strError = "Configurable domain " + strDomain + " not found";
191
192 return false;
193 }
194 // Delegate
195 return pConfigurableDomain->createConfiguration(strConfiguration, pMainBlackboard, strError);
196}
197
198bool CConfigurableDomains::deleteConfiguration(const string& strDomain, const string& strConfiguration, string& strError)
199{
200 // Find domain
201 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
202
203 if (!pConfigurableDomain) {
204
205 strError = "Configurable domain " + strDomain + " not found";
206
207 return false;
208 }
209 // Delegate
210 return pConfigurableDomain->deleteConfiguration(strConfiguration, strError);
211}
212
213bool CConfigurableDomains::renameConfiguration(const string& strDomain, const string& strConfigurationName, const string& strNewConfigurationName, string& strError)
214{
215 // Find domain
216 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
217
218 if (!pConfigurableDomain) {
219
220 strError = "Configurable domain " + strDomain + " not found";
221
222 return false;
223 }
224 // Delegate
225 return pConfigurableDomain->renameConfiguration(strConfigurationName, strNewConfigurationName, strError);
226}
227
228bool CConfigurableDomains::listDomainElements(const string& strDomain, string& strResult) const
229{
230 // Find domain
231 const CConfigurableDomain* pConfigurableDomain = static_cast<const CConfigurableDomain*>(findChild(strDomain));
232
233 if (!pConfigurableDomain) {
234
235 strResult = "Configurable domain " + strDomain + " not found";
236
237 return false;
238 }
239 // Delegate
240 pConfigurableDomain->listAssociatedToElements(strResult);
241
242 return true;
243}
244
245bool CConfigurableDomains::split(const string& strDomain, CConfigurableElement* pConfigurableElement, string& strError)
246{
247 // Find domain
248 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
249
250 if (!pConfigurableDomain) {
251
252 strError = "Configurable domain " + strDomain + " not found";
253
254 return false;
255 }
256 // Delegate
257 pConfigurableDomain->split(pConfigurableElement, strError);
258
259 return true;
260}
261
262void CConfigurableDomains::listAssociatedElements(string& strResult) const
263{
264 strResult = "\n";
265
266 set<const CConfigurableElement*> configurableElementSet;
267
268 // Get all owned configurable elements
269 gatherAllOwnedConfigurableElements(configurableElementSet);
270
271 // Fill result
272 set<const CConfigurableElement*>::const_iterator it;
273
274 for (it = configurableElementSet.begin(); it != configurableElementSet.end(); ++it) {
275
276 const CConfigurableElement* pConfigurableElement = *it;
277
278 string strAssociatedDomainList;
279
280 pConfigurableElement->listAssociatedDomains(strAssociatedDomainList, false);
281
282 strResult += pConfigurableElement->getPath() + " [" + strAssociatedDomainList + "]\n";
283 }
284}
285
286void CConfigurableDomains::listConflictingElements(string& strResult) const
287{
288 strResult = "\n";
289
290 set<const CConfigurableElement*> configurableElementSet;
291
292 // Get all owned configurable elements
293 gatherAllOwnedConfigurableElements(configurableElementSet);
294
295 // Fill result
296 set<const CConfigurableElement*>::const_iterator it;
297
298 for (it = configurableElementSet.begin(); it != configurableElementSet.end(); ++it) {
299
300 const CConfigurableElement* pConfigurableElement = *it;
301
302 if (pConfigurableElement->getBelongingDomainCount() > 1) {
303
304 string strBelongingDomainList;
305
306 pConfigurableElement->listBelongingDomains(strBelongingDomainList, false);
307
308 strResult += pConfigurableElement->getPath() + " contained in multiple domains: " + strBelongingDomainList + "\n";
309 }
310 }
311}
312
313// Gather configurable elements owned by any domain
314void CConfigurableDomains::gatherAllOwnedConfigurableElements(set<const CConfigurableElement*>& configurableElementSet) const
315{
316 // Delegate to domains
317 uint32_t uiChild;
318 uint32_t uiNbConfigurableDomains = getNbChildren();
319
320 for (uiChild = 0; uiChild < uiNbConfigurableDomains; uiChild++) {
321
322 const CConfigurableDomain* pChildConfigurableDomain = static_cast<const CConfigurableDomain*>(getChild(uiChild));
323
324 pChildConfigurableDomain->gatherConfigurableElements(configurableElementSet);
325 }
326}
327
328// Config restore
329bool CConfigurableDomains::restoreConfiguration(const string& strDomain, const string& strConfiguration, CParameterBlackboard* pMainBlackboard, bool bAutoSync, string& strError)
330{
331 // Find domain
332 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
333
334 if (!pConfigurableDomain) {
335
336 strError = "Configurable domain " + strDomain + " not found";
337
338 return false;
339 }
340 // Delegate
341 return pConfigurableDomain->restoreConfiguration(strConfiguration, pMainBlackboard, bAutoSync, strError);
342}
343
344// Config save
345bool CConfigurableDomains::saveConfiguration(const string& strDomain, const string& strConfiguration, const CParameterBlackboard* pMainBlackboard, string& strError)
346{
347 // Find domain
348 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
349
350 if (!pConfigurableDomain) {
351
352 strError = "Configurable domain " + strDomain + " not found";
353
354 return false;
355 }
356 // Delegate
357 return pConfigurableDomain->saveConfiguration(strConfiguration, pMainBlackboard, strError);
358}
359
360// Last applied configurations
361void CConfigurableDomains::listLastAppliedConfigurations(string& strResult) const
362{
Patrick Benavoli68a91282011-08-31 11:23:23 +0200363 // Browse domains
364 uint32_t uiChild;
365 uint32_t uiNbConfigurableDomains = getNbChildren();
366
367 for (uiChild = 0; uiChild < uiNbConfigurableDomains; uiChild++) {
368
369 const CConfigurableDomain* pChildConfigurableDomain = static_cast<const CConfigurableDomain*>(getChild(uiChild));
370
371 strResult += pChildConfigurableDomain->getName() + ": " + pChildConfigurableDomain->getLastAppliedConfigurationName() + "\n";
372 }
373}
374
375// Configurable element - domain association
376bool CConfigurableDomains::addConfigurableElementToDomain(const string& strDomain, CConfigurableElement* pConfigurableElement, const CParameterBlackboard* pMainBlackboard, string& strError)
377{
378 // Find domain
379 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
380
381 if (!pConfigurableDomain) {
382
383 strError = "Configurable domain " + strDomain + " not found";
384
385 return false;
386 }
387 // Delegate
388 return pConfigurableDomain->addConfigurableElement(pConfigurableElement, pMainBlackboard, strError);
389}
390
391bool CConfigurableDomains::removeConfigurableElementFromDomain(const string& strDomain, CConfigurableElement* pConfigurableElement, string& strError)
392{
393 // Find domain
394 CConfigurableDomain* pConfigurableDomain = static_cast<CConfigurableDomain*>(findChild(strDomain));
395
396 if (!pConfigurableDomain) {
397
398 strError = "Configurable domain " + strDomain + " not found";
399
400 return false;
401 }
402 // Delegate
403 return pConfigurableDomain->removeConfigurableElement(pConfigurableElement, strError);
404}
405
406// Binary settings load/store
407bool CConfigurableDomains::serializeSettings(const string& strBinarySettingsFilePath, bool bOut, uint8_t uiStructureChecksum, string& strError)
408{
409 // Instantiate byte stream
410 CBinaryStream binarySettingsStream(strBinarySettingsFilePath, bOut, getDataSize(), uiStructureChecksum);
411
412 // Open file
413 if (!binarySettingsStream.open(strError)) {
414
415 strError = "Unable to open binary settings file " + strBinarySettingsFilePath + ": " + strError;
416
417 return false;
418 }
419
420 // Serialize
421 binarySerialize(binarySettingsStream);
422
423 // Close stream
424 binarySettingsStream.close();
425
426 return true;
427}