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