| Frédéric Boisnard | e42dacd | 2013-02-25 15:56:56 +0100 | [diff] [blame] | 1 | /* |
| David Wagner | b76c9d6 | 2014-02-05 18:30:24 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2011-2014, Intel Corporation |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without modification, |
| 6 | * are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | * list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | * this list of conditions and the following disclaimer in the documentation and/or |
| 13 | * other materials provided with the distribution. |
| 14 | * |
| 15 | * 3. Neither the name of the copyright holder nor the names of its contributors |
| 16 | * may be used to endorse or promote products derived from this software without |
| 17 | * specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 26 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 29 | */ |
| 30 | #include "ConfigurableDomain.h" |
| 31 | #include "DomainConfiguration.h" |
| 32 | #include "ConfigurableElement.h" |
| 33 | #include "ConfigurationAccessContext.h" |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 34 | #include "XmlDomainSerializingContext.h" |
| 35 | #include <assert.h> |
| 36 | |
| 37 | #define base CBinarySerializableElement |
| 38 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 39 | CConfigurableDomain::CConfigurableDomain(const string& strName) : base(strName), _bSequenceAware(false), _pLastAppliedConfiguration(NULL) |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 40 | { |
| 41 | } |
| 42 | |
| 43 | CConfigurableDomain::~CConfigurableDomain() |
| 44 | { |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 45 | // Remove all configurable elements |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 46 | ConfigurableElementListIterator it; |
| 47 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 48 | for (it = _configurableElementList.begin(); it != _configurableElementList.end(); ++it) { |
| 49 | |
| 50 | CConfigurableElement* pConfigurableElement = *it; |
| 51 | |
| 52 | // Remove from configurable element |
| 53 | pConfigurableElement->removeAttachedConfigurableDomain(this); |
| 54 | } |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 55 | |
| 56 | // Remove all associated syncer sets |
| 57 | ConfigurableElementToSyncerSetMapIterator mapIt; |
| 58 | |
| 59 | for (mapIt = _configurableElementToSyncerSetMap.begin(); mapIt != _configurableElementToSyncerSetMap.end(); ++mapIt) { |
| 60 | |
| 61 | delete mapIt->second; |
| 62 | } |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | string CConfigurableDomain::getKind() const |
| 66 | { |
| 67 | return "ConfigurableDomain"; |
| 68 | } |
| 69 | |
| 70 | bool CConfigurableDomain::childrenAreDynamic() const |
| 71 | { |
| 72 | return true; |
| 73 | } |
| 74 | |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 75 | // Content dumping |
| 76 | void CConfigurableDomain::logValue(string& strValue, CErrorContext& errorContext) const |
| 77 | { |
| 78 | (void)errorContext; |
| 79 | |
| 80 | strValue = "{"; |
| 81 | |
| 82 | // Sequence awareness |
| 83 | strValue += "Sequence aware: "; |
| 84 | strValue += _bSequenceAware ? "yes" : "no"; |
| 85 | |
| 86 | // Last applied configuration |
| 87 | strValue += ", Last applied configuration: "; |
| 88 | strValue += _pLastAppliedConfiguration ? _pLastAppliedConfiguration->getName() : "<none>"; |
| 89 | |
| 90 | strValue += "}"; |
| 91 | } |
| 92 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 93 | // Sequence awareness |
| 94 | void CConfigurableDomain::setSequenceAwareness(bool bSequenceAware) |
| 95 | { |
| 96 | if (_bSequenceAware != bSequenceAware) { |
| 97 | |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 98 | log_info("Making domain \"%s\" sequence %s", getName().c_str(), bSequenceAware ? "aware" : "unaware"); |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 99 | |
| 100 | _bSequenceAware = bSequenceAware; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | bool CConfigurableDomain::getSequenceAwareness() const |
| 105 | { |
| 106 | return _bSequenceAware; |
| 107 | } |
| 108 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 109 | // From IXmlSource |
| 110 | void CConfigurableDomain::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const |
| 111 | { |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 112 | // Sequence awareness |
| 113 | xmlElement.setAttributeBoolean("SequenceAware", _bSequenceAware); |
| 114 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 115 | // Configurations |
| 116 | composeDomainConfigurations(xmlElement, serializingContext); |
| 117 | |
| 118 | // Configurable Elements |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 119 | composeConfigurableElements(xmlElement); |
| 120 | |
| 121 | // Settings |
| 122 | composeSettings(xmlElement, serializingContext); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // XML composing |
| 126 | void CConfigurableDomain::composeDomainConfigurations(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const |
| 127 | { |
| 128 | // Create Configurations element |
| 129 | CXmlElement xmlConfigurationsElement; |
| 130 | |
| 131 | xmlElement.createChild(xmlConfigurationsElement, "Configurations"); |
| 132 | |
| 133 | // Delegate to base |
| 134 | base::toXml(xmlConfigurationsElement, serializingContext); |
| 135 | } |
| 136 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 137 | void CConfigurableDomain::composeConfigurableElements(CXmlElement& xmlElement) const |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 138 | { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 139 | // Create ConfigurableElements element |
| 140 | CXmlElement xmlConfigurableElementsElement; |
| 141 | |
| 142 | xmlElement.createChild(xmlConfigurableElementsElement, "ConfigurableElements"); |
| 143 | |
| 144 | // Serialize out all configurable elements settings |
| 145 | ConfigurableElementListIterator it; |
| 146 | |
| 147 | for (it = _configurableElementList.begin(); it != _configurableElementList.end(); ++it) { |
| 148 | |
| 149 | const CConfigurableElement* pConfigurableElement = *it; |
| 150 | |
| 151 | // Create corresponding XML child element |
| 152 | CXmlElement xmlChildConfigurableElement; |
| 153 | |
| 154 | xmlConfigurableElementsElement.createChild(xmlChildConfigurableElement, "ConfigurableElement"); |
| 155 | |
| 156 | // Set Path attribute |
| 157 | xmlChildConfigurableElement.setAttributeString("Path", pConfigurableElement->getPath()); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 161 | void CConfigurableDomain::composeSettings(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 162 | { |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 163 | // Context |
| 164 | const CXmlDomainSerializingContext& xmlDomainSerializingContext = static_cast<const CXmlDomainSerializingContext&>(serializingContext); |
| 165 | |
| 166 | if (!xmlDomainSerializingContext.withSettings()) { |
| 167 | |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | // Create Settings element |
| 172 | CXmlElement xmlSettingsElement; |
| 173 | |
| 174 | xmlElement.createChild(xmlSettingsElement, "Settings"); |
| 175 | |
| 176 | // Serialize out all configurations settings |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 177 | uint32_t uiNbConfigurations = getNbChildren(); |
| 178 | uint32_t uiChildConfiguration; |
| 179 | |
| 180 | for (uiChildConfiguration = 0; uiChildConfiguration < uiNbConfigurations; uiChildConfiguration++) { |
| 181 | |
| 182 | const CDomainConfiguration* pDomainConfiguration = static_cast<const CDomainConfiguration*>(getChild(uiChildConfiguration)); |
| 183 | |
| 184 | // Create child xml element for that configuration |
| 185 | CXmlElement xmlConfigurationSettingsElement; |
| 186 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 187 | xmlSettingsElement.createChild(xmlConfigurationSettingsElement, pDomainConfiguration->getKind()); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 188 | |
| 189 | // Set its name attribute |
| 190 | xmlConfigurationSettingsElement.setNameAttribute(pDomainConfiguration->getName()); |
| 191 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 192 | // Serialize out configuration settings |
| 193 | pDomainConfiguration->composeSettings(xmlConfigurationSettingsElement, serializingContext); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | // From IXmlSink |
| 198 | bool CConfigurableDomain::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) |
| 199 | { |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 200 | // Context |
| 201 | CXmlDomainSerializingContext& xmlDomainSerializingContext = static_cast<CXmlDomainSerializingContext&>(serializingContext); |
| 202 | |
| 203 | // Sequence awareness (optional) |
| 204 | _bSequenceAware = xmlElement.hasAttribute("SequenceAware") && xmlElement.getAttributeBoolean("SequenceAware"); |
| 205 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 206 | // Local parsing. Do not dig |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 207 | if (!parseDomainConfigurations(xmlElement, serializingContext) || !parseConfigurableElements(xmlElement, serializingContext) || !parseSettings(xmlElement, serializingContext)) { |
| 208 | |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | // All provided configurations are parsed |
| 213 | // Attempt validation on areas of non provided configurations for all configurable elements if required |
| 214 | if (xmlDomainSerializingContext.autoValidationRequired()) { |
| 215 | |
| 216 | autoValidateAll(); |
| 217 | } |
| 218 | |
| 219 | return true; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // XML parsing |
| 223 | bool CConfigurableDomain::parseDomainConfigurations(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) |
| 224 | { |
| 225 | // We're supposedly clean |
| 226 | assert(_configurableElementList.empty()); |
| 227 | |
| 228 | // Get Configurations element |
| 229 | CXmlElement xmlConfigurationsElement; |
| 230 | |
| 231 | xmlElement.getChildElement("Configurations", xmlConfigurationsElement); |
| 232 | |
| 233 | // Parse it and create domain configuration objects |
| 234 | return base::fromXml(xmlConfigurationsElement, serializingContext); |
| 235 | } |
| 236 | |
| 237 | // Parse configurable elements |
| 238 | bool CConfigurableDomain::parseConfigurableElements(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) |
| 239 | { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 240 | // Get System Class Element |
| 241 | CElement* pRootElement = getRoot(); |
| 242 | |
| 243 | CElement* pSystemClassElement = pRootElement->findChildOfKind("SystemClass"); |
| 244 | |
| 245 | assert(pSystemClassElement); |
| 246 | |
| 247 | // Get ConfigurableElements element |
| 248 | CXmlElement xmlConfigurableElementsElement; |
| 249 | xmlElement.getChildElement("ConfigurableElements", xmlConfigurableElementsElement); |
| 250 | |
| 251 | // Parse it and associate found configurable elements to it |
| 252 | CXmlElement::CChildIterator it(xmlConfigurableElementsElement); |
| 253 | |
| 254 | CXmlElement xmlConfigurableElementElement; |
| 255 | |
| 256 | while (it.next(xmlConfigurableElementElement)) { |
| 257 | |
| 258 | // Locate configurable element |
| 259 | string strConfigurableElementPath = xmlConfigurableElementElement.getAttributeString("Path"); |
| 260 | |
| 261 | CPathNavigator pathNavigator(strConfigurableElementPath); |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 262 | string strError; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 263 | |
| 264 | // Is there an element and does it match system class name? |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 265 | if (!pathNavigator.navigateThrough(pSystemClassElement->getName(), strError)) { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 266 | |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 267 | serializingContext.setError("Could not find configurable element of path " + strConfigurableElementPath + " from ConfigurableDomain description " + getName() + " (" + strError + ")"); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 268 | |
| 269 | return false; |
| 270 | } |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 271 | // Browse system class for configurable element |
| 272 | CConfigurableElement* pConfigurableElement = static_cast<CConfigurableElement*>(pSystemClassElement->findDescendant(pathNavigator)); |
| 273 | |
| 274 | if (!pConfigurableElement) { |
| 275 | |
| 276 | serializingContext.setError("Could not find configurable element of path " + strConfigurableElementPath + " from ConfigurableDomain description " + getName()); |
| 277 | |
| 278 | return false; |
| 279 | } |
| 280 | // Add found element to domain |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 281 | if (!addConfigurableElement(pConfigurableElement, NULL, strError)) { |
| 282 | |
| 283 | serializingContext.setError(strError); |
| 284 | |
| 285 | return false; |
| 286 | } |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 287 | } |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 288 | |
| 289 | return true; |
| 290 | } |
| 291 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 292 | // Parse settings |
| 293 | bool CConfigurableDomain::parseSettings(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 294 | { |
| 295 | // Context |
| 296 | CXmlDomainSerializingContext& xmlDomainSerializingContext = static_cast<CXmlDomainSerializingContext&>(serializingContext); |
| 297 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 298 | // Check we actually need to parse configuration settings |
| 299 | if (!xmlDomainSerializingContext.withSettings()) { |
| 300 | |
| 301 | // No parsing required |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | // Get Settings element |
| 306 | CXmlElement xmlSettingsElement; |
| 307 | if (!xmlElement.getChildElement("Settings", xmlSettingsElement)) { |
| 308 | |
| 309 | // No settings, bail out successfully |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | // Parse configuration settings |
| 314 | CXmlElement::CChildIterator it(xmlSettingsElement); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 315 | |
| 316 | CXmlElement xmlConfigurationSettingsElement; |
| 317 | |
| 318 | while (it.next(xmlConfigurationSettingsElement)) { |
| 319 | // Get domain configuration |
| 320 | CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(findChild(xmlConfigurationSettingsElement.getNameAttribute())); |
| 321 | |
| 322 | if (!pDomainConfiguration) { |
| 323 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 324 | xmlDomainSerializingContext.setError("Could not find domain configuration referred to by configurable domain " + getName()); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 325 | |
| 326 | return false; |
| 327 | } |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 328 | // Have domain configuration parse settings for all configurable elements |
| 329 | if (!pDomainConfiguration->parseSettings(xmlConfigurationSettingsElement, xmlDomainSerializingContext)) { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 330 | |
| 331 | return false; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | return true; |
| 336 | } |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 337 | // Configurable elements association |
| 338 | bool CConfigurableDomain::addConfigurableElement(CConfigurableElement* pConfigurableElement, const CParameterBlackboard* pMainBlackboard, string& strError) |
| 339 | { |
| 340 | // Already associated? |
| 341 | if (containsConfigurableElement(pConfigurableElement)) { |
| 342 | |
| 343 | strError = "Configurable element " + pConfigurableElement->getPath() + " already associated to configuration domain " + getName(); |
| 344 | |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | // Already owned? |
| 349 | if (pConfigurableElement->belongsTo(this)) { |
| 350 | |
| 351 | strError = "Configurable element " + pConfigurableElement->getPath() + " already owned by configuration domain " + getName(); |
| 352 | |
| 353 | return false; |
| 354 | } |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 355 | |
| 356 | // Do add |
| Frédéric Boisnard | 9620e44 | 2012-05-30 16:15:02 +0200 | [diff] [blame] | 357 | doAddConfigurableElement(pConfigurableElement, pMainBlackboard); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 358 | |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | bool CConfigurableDomain::removeConfigurableElement(CConfigurableElement* pConfigurableElement, string& strError) |
| 363 | { |
| 364 | // Not associated? |
| 365 | if (!containsConfigurableElement(pConfigurableElement)) { |
| 366 | |
| 367 | strError = "Configurable element " + pConfigurableElement->getPath() + " not associated to configuration domain " + getName(); |
| 368 | |
| 369 | return false; |
| 370 | } |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 371 | log_info("Removing configurable element \"%s\" from domain \"%s\"", pConfigurableElement->getPath().c_str(), getName().c_str()); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 372 | |
| 373 | // Do remove |
| 374 | doRemoveConfigurableElement(pConfigurableElement, true); |
| 375 | |
| 376 | return true; |
| 377 | } |
| 378 | |
| Frédéric Boisnard | e42dacd | 2013-02-25 15:56:56 +0100 | [diff] [blame] | 379 | /** |
| 380 | * Blackboard Configuration and Base Offset retrieval. |
| 381 | * |
| 382 | * This method fetches the Blackboard associated to the ConfigurableElement |
| 383 | * given in parameter, for a specific Configuration. The ConfigurableElement |
| 384 | * must belong to the Domain. If a Blackboard is found, the base offset of |
| 385 | * the ConfigurableElement is returned as well. This base offset corresponds to |
| 386 | * the offset of the ancestor of the ConfigurableElement associated to the Configuration. |
| 387 | * |
| 388 | * @param[in] strConfiguration Name of the Configuration. |
| 389 | * @param[in] pCandidateDescendantConfigurableElement Pointer to a CConfigurableElement that |
| 390 | * belongs to the Domain. |
| 391 | * @param[out] uiBaseOffset The base offset of the CConfigurableElement. |
| 392 | * @param[out] bIsLastApplied Boolean indicating that the Configuration is |
| 393 | * the last one applied of the Domain. |
| 394 | * @param[out] strError Error message |
| 395 | * |
| 396 | * return Pointer to the Blackboard of the Configuration. |
| 397 | */ |
| 398 | CParameterBlackboard* CConfigurableDomain::findConfigurationBlackboard(const string& strConfiguration, |
| 399 | const CConfigurableElement* pCandidateDescendantConfigurableElement, |
| 400 | uint32_t& uiBaseOffset, |
| 401 | bool& bIsLastApplied, |
| 402 | string& strError) const |
| 403 | { |
| 404 | // Find Configuration |
| 405 | const CDomainConfiguration* pDomainConfiguration = static_cast<const CDomainConfiguration*>(findChild(strConfiguration)); |
| 406 | |
| 407 | if (!pDomainConfiguration) { |
| 408 | |
| 409 | strError = "Domain configuration " + strConfiguration + " not found"; |
| 410 | |
| 411 | return NULL; |
| 412 | } |
| 413 | |
| 414 | // Parse all configurable elements |
| 415 | ConfigurableElementListIterator it; |
| 416 | |
| 417 | for (it = _configurableElementList.begin(); it != _configurableElementList.end(); ++it) { |
| 418 | |
| 419 | const CConfigurableElement* pAssociatedConfigurableElement = *it; |
| 420 | |
| 421 | // Check if the the associated element is the configurable element or one of its ancestors |
| 422 | if ((pCandidateDescendantConfigurableElement == pAssociatedConfigurableElement) || |
| 423 | (pCandidateDescendantConfigurableElement->isDescendantOf(pAssociatedConfigurableElement))) { |
| 424 | |
| 425 | uiBaseOffset = pAssociatedConfigurableElement->getOffset(); |
| 426 | bIsLastApplied = (pDomainConfiguration == _pLastAppliedConfiguration); |
| 427 | |
| 428 | return pDomainConfiguration->getBlackboard(pAssociatedConfigurableElement); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | strError = "Element not associated to the Domain"; |
| 433 | |
| 434 | return NULL; |
| 435 | } |
| 436 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 437 | // Domain splitting |
| 438 | bool CConfigurableDomain::split(CConfigurableElement* pConfigurableElement, string& strError) |
| 439 | { |
| 440 | // Not associated? |
| 441 | if (!containsConfigurableElement(pConfigurableElement)) { |
| 442 | |
| 443 | strError = "Configurable element " + pConfigurableElement->getPath() + " not associated to configuration domain " + getName(); |
| 444 | |
| 445 | return false; |
| 446 | } |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 447 | log_info("Splitting configurable element \"%s\" domain \"%s\"", pConfigurableElement->getPath().c_str(), getName().c_str()); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 448 | |
| 449 | // Create sub domain areas for all configurable element's children |
| 450 | uint32_t uiNbConfigurableElementChildren = pConfigurableElement->getNbChildren(); |
| 451 | |
| 452 | if (!uiNbConfigurableElementChildren) { |
| 453 | |
| 454 | strError = "Configurable element " + pConfigurableElement->getPath() + " has no children to split configurable domain to"; |
| 455 | |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | uint32_t uiChild; |
| 460 | |
| 461 | for (uiChild = 0; uiChild < uiNbConfigurableElementChildren; uiChild++) { |
| 462 | |
| 463 | CConfigurableElement* pChildConfigurableElement = static_cast<CConfigurableElement*>(pConfigurableElement->getChild(uiChild)); |
| 464 | |
| 465 | doAddConfigurableElement(pChildConfigurableElement); |
| 466 | } |
| 467 | |
| 468 | // Delegate to configurations |
| 469 | uint32_t uiNbConfigurations = getNbChildren(); |
| 470 | |
| 471 | for (uiChild = 0; uiChild < uiNbConfigurations; uiChild++) { |
| 472 | |
| 473 | CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(getChild(uiChild)); |
| 474 | |
| 475 | pDomainConfiguration->split(pConfigurableElement); |
| 476 | } |
| 477 | |
| 478 | // Remove given configurable element from this domain |
| 479 | // Note: we shouldn't need to recompute the sync set in that case, as the splitted element should include the syncers of its children elements |
| 480 | doRemoveConfigurableElement(pConfigurableElement, false); |
| 481 | |
| 482 | return true; |
| 483 | } |
| 484 | |
| Frédéric Boisnard | 8b243f5 | 2012-09-06 18:03:20 +0200 | [diff] [blame] | 485 | // Check if there is a pending configuration for this domain: i.e. an applicable configuration different from the last applied configuration |
| 486 | const CDomainConfiguration* CConfigurableDomain::getPendingConfiguration() const |
| 487 | { |
| 488 | const CDomainConfiguration* pApplicableDomainConfiguration = findApplicableDomainConfiguration(); |
| 489 | |
| 490 | if (pApplicableDomainConfiguration) { |
| 491 | |
| 492 | // Check not the last one before applying |
| 493 | if (!_pLastAppliedConfiguration || (_pLastAppliedConfiguration != pApplicableDomainConfiguration)) { |
| 494 | |
| 495 | return pApplicableDomainConfiguration; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | return NULL; |
| 500 | } |
| 501 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 502 | // Configuration application if required |
| Guillaume Denneulin | f2fd15a | 2012-12-20 17:53:29 +0100 | [diff] [blame] | 503 | void CConfigurableDomain::apply(CParameterBlackboard* pParameterBlackboard, CSyncerSet* pSyncerSet, bool bForce) const |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 504 | { |
| Guillaume Denneulin | f2fd15a | 2012-12-20 17:53:29 +0100 | [diff] [blame] | 505 | // Apply configuration only if the blackboard will |
| 506 | // be synchronized either now or by syncerSet. |
| 507 | if(!pSyncerSet ^ _bSequenceAware) { |
| 508 | // The configuration can not be syncronised |
| 509 | return; |
| 510 | } |
| 511 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 512 | if (bForce) { |
| 513 | // Force a configuration restore by forgetting about last applied configuration |
| 514 | _pLastAppliedConfiguration = NULL; |
| 515 | } |
| 516 | const CDomainConfiguration* pApplicableDomainConfiguration = findApplicableDomainConfiguration(); |
| 517 | |
| 518 | if (pApplicableDomainConfiguration) { |
| 519 | |
| 520 | // Check not the last one before applying |
| 521 | if (!_pLastAppliedConfiguration || _pLastAppliedConfiguration != pApplicableDomainConfiguration) { |
| 522 | |
| Guillaume Denneulin | f2fd15a | 2012-12-20 17:53:29 +0100 | [diff] [blame] | 523 | log_info("Applying configuration \"%s\" from domain \"%s\"", |
| 524 | pApplicableDomainConfiguration->getName().c_str(), |
| 525 | getName().c_str()); |
| 526 | |
| 527 | // Check if we need to synchronize during restore |
| 528 | bool bSync = !pSyncerSet && _bSequenceAware; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 529 | |
| 530 | // Do the restore |
| Guillaume Denneulin | f2fd15a | 2012-12-20 17:53:29 +0100 | [diff] [blame] | 531 | pApplicableDomainConfiguration->restore(pParameterBlackboard, bSync, NULL); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 532 | |
| 533 | // Record last applied configuration |
| 534 | _pLastAppliedConfiguration = pApplicableDomainConfiguration; |
| 535 | |
| Guillaume Denneulin | f2fd15a | 2012-12-20 17:53:29 +0100 | [diff] [blame] | 536 | // Check we need to provide syncer set to caller |
| 537 | if (pSyncerSet && !_bSequenceAware) { |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 538 | |
| 539 | // Since we applied changes, add our own sync set to the given one |
| Guillaume Denneulin | f2fd15a | 2012-12-20 17:53:29 +0100 | [diff] [blame] | 540 | *pSyncerSet += _syncerSet; |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 541 | } |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | // Return applicable configuration validity for given configurable element |
| 547 | bool CConfigurableDomain::isApplicableConfigurationValid(const CConfigurableElement* pConfigurableElement) const |
| 548 | { |
| 549 | const CDomainConfiguration* pApplicableDomainConfiguration = findApplicableDomainConfiguration(); |
| 550 | |
| 551 | return pApplicableDomainConfiguration && pApplicableDomainConfiguration->isValid(pConfigurableElement); |
| 552 | } |
| 553 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 554 | // In case configurable element was removed |
| 555 | void CConfigurableDomain::computeSyncSet() |
| 556 | { |
| 557 | // Clean sync set first |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 558 | _syncerSet.clear(); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 559 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 560 | // Add syncer sets for all associated configurable elements |
| 561 | ConfigurableElementToSyncerSetMapIterator mapIt; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 562 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 563 | for (mapIt = _configurableElementToSyncerSetMap.begin(); mapIt != _configurableElementToSyncerSetMap.end(); ++mapIt) { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 564 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 565 | const CSyncerSet* pSyncerSet = mapIt->second; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 566 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 567 | _syncerSet += *pSyncerSet; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
| 571 | // Configuration Management |
| 572 | bool CConfigurableDomain::createConfiguration(const string& strName, const CParameterBlackboard* pMainBlackboard, string& strError) |
| 573 | { |
| 574 | // Already exists? |
| 575 | if (findChild(strName)) { |
| 576 | |
| 577 | strError = "Already existing configuration"; |
| 578 | |
| 579 | return false; |
| 580 | } |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 581 | log_info("Creating domain configuration \"%s\" into domain \"%s\"", strName.c_str(), getName().c_str()); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 582 | |
| 583 | // Creation |
| 584 | CDomainConfiguration* pDomainConfiguration = new CDomainConfiguration(strName); |
| 585 | |
| 586 | // Configurable elements association |
| 587 | ConfigurableElementListIterator it; |
| 588 | |
| 589 | for (it = _configurableElementList.begin(); it != _configurableElementList.end(); ++it) { |
| 590 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 591 | const CConfigurableElement* pConfigurableElement = *it;; |
| 592 | |
| 593 | // Retrieve associated syncer set |
| 594 | CSyncerSet* pSyncerSet = getSyncerSet(pConfigurableElement); |
| 595 | |
| 596 | // Associate to configuration |
| 597 | pDomainConfiguration->addConfigurableElement(pConfigurableElement, pSyncerSet); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | // Hierarchy |
| 601 | addChild(pDomainConfiguration); |
| 602 | |
| 603 | // Ensure validity of fresh new domain configuration |
| 604 | // Attempt auto validation, so that the user gets his/her own settings by defaults |
| 605 | if (!autoValidateConfiguration(pDomainConfiguration)) { |
| 606 | |
| 607 | // No valid configuration found to copy in from, validate againt main blackboard (will concerned remaining invalid parts) |
| 608 | pDomainConfiguration->validate(pMainBlackboard); |
| 609 | } |
| 610 | |
| 611 | return true; |
| 612 | } |
| 613 | |
| 614 | bool CConfigurableDomain::deleteConfiguration(const string& strName, string& strError) |
| 615 | { |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 616 | CDomainConfiguration* pDomainConfiguration = findConfiguration(strName, strError); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 617 | |
| 618 | if (!pDomainConfiguration) { |
| 619 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 620 | return false; |
| 621 | } |
| 622 | |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 623 | log_info("Deleting configuration \"%s\" from domain \"%s\"", strName.c_str(), getName().c_str()); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 624 | |
| 625 | // Was the last applied? |
| 626 | if (pDomainConfiguration == _pLastAppliedConfiguration) { |
| 627 | |
| 628 | // Forget about it |
| 629 | _pLastAppliedConfiguration = NULL; |
| 630 | } |
| 631 | |
| 632 | // Hierarchy |
| 633 | removeChild(pDomainConfiguration); |
| 634 | |
| 635 | // Destroy |
| 636 | delete pDomainConfiguration; |
| 637 | |
| 638 | return true; |
| 639 | } |
| 640 | |
| 641 | void CConfigurableDomain::listAssociatedToElements(string& strResult) const |
| 642 | { |
| 643 | strResult = "\n"; |
| 644 | |
| 645 | ConfigurableElementListIterator it; |
| 646 | |
| 647 | // Browse all configurable elements |
| 648 | for (it = _configurableElementList.begin(); it != _configurableElementList.end(); ++it) { |
| 649 | |
| 650 | const CConfigurableElement* pConfigurableElement = *it; |
| 651 | |
| 652 | strResult += pConfigurableElement->getPath() + "\n"; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | bool CConfigurableDomain::renameConfiguration(const string& strName, const string& strNewName, string& strError) |
| 657 | { |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 658 | CDomainConfiguration* pDomainConfiguration = findConfiguration(strName, strError); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 659 | |
| 660 | if (!pDomainConfiguration) { |
| 661 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 662 | return false; |
| 663 | } |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 664 | log_info("Renaming domain \"%s\"'s configuration \"%s\" to \"%s\"", getName().c_str(), strName.c_str(), strNewName.c_str()); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 665 | |
| 666 | // Rename |
| 667 | return pDomainConfiguration->rename(strNewName, strError); |
| 668 | } |
| 669 | |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 670 | bool CConfigurableDomain::restoreConfiguration(const string& strName, CParameterBlackboard* pMainBlackboard, bool bAutoSync, list<string>& lstrError) const |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 671 | { |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 672 | string strError; |
| 673 | |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 674 | const CDomainConfiguration* pDomainConfiguration = findConfiguration(strName, strError); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 675 | |
| 676 | if (!pDomainConfiguration) { |
| 677 | |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 678 | lstrError.push_back(strError); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 679 | return false; |
| 680 | } |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 681 | log_info("Restoring domain \"%s\"'s configuration \"%s\" to parameter blackboard", getName().c_str(), pDomainConfiguration->getName().c_str()); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 682 | |
| 683 | // Delegate |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 684 | bool bSuccess = pDomainConfiguration->restore(pMainBlackboard, bAutoSync && _bSequenceAware, &lstrError); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 685 | |
| 686 | // Record last applied configuration |
| 687 | _pLastAppliedConfiguration = pDomainConfiguration; |
| 688 | |
| 689 | // Synchronize |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 690 | if (bAutoSync && !_bSequenceAware) { |
| 691 | |
| 692 | bSuccess &= _syncerSet.sync(*pMainBlackboard, false, &lstrError); |
| 693 | } |
| 694 | return bSuccess; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | bool CConfigurableDomain::saveConfiguration(const string& strName, const CParameterBlackboard* pMainBlackboard, string& strError) |
| 698 | { |
| 699 | // Find Domain configuration |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 700 | CDomainConfiguration* pDomainConfiguration = findConfiguration(strName, strError); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 701 | |
| 702 | if (!pDomainConfiguration) { |
| 703 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 704 | return false; |
| 705 | } |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 706 | log_info("Saving domain \"%s\"'s configuration \"%s\" from parameter blackboard", getName().c_str(), pDomainConfiguration->getName().c_str()); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 707 | |
| 708 | // Delegate |
| 709 | pDomainConfiguration->save(pMainBlackboard); |
| 710 | |
| 711 | return true; |
| 712 | } |
| 713 | |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 714 | bool CConfigurableDomain::setElementSequence(const string& strConfiguration, const vector<string>& astrNewElementSequence, string& strError) |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 715 | { |
| 716 | // Find Domain configuration |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 717 | CDomainConfiguration* pDomainConfiguration = findConfiguration(strConfiguration, strError); |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 718 | |
| 719 | if (!pDomainConfiguration) { |
| 720 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 721 | return false; |
| 722 | } |
| 723 | |
| 724 | // Delegate to configuration |
| 725 | return pDomainConfiguration->setElementSequence(astrNewElementSequence, strError); |
| 726 | } |
| 727 | |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 728 | bool CConfigurableDomain::getElementSequence(const string& strConfiguration, string& strResult) const |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 729 | { |
| 730 | // Find Domain configuration |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 731 | const CDomainConfiguration* pDomainConfiguration = findConfiguration(strConfiguration, strResult); |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 732 | |
| 733 | if (!pDomainConfiguration) { |
| 734 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 735 | return false; |
| 736 | } |
| 737 | |
| 738 | // Delegate to configuration |
| 739 | pDomainConfiguration->getElementSequence(strResult); |
| 740 | |
| 741 | return true; |
| 742 | } |
| 743 | |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 744 | bool CConfigurableDomain::setApplicationRule(const string& strConfiguration, const string& strApplicationRule, const CSelectionCriteriaDefinition* pSelectionCriteriaDefinition, string& strError) |
| 745 | { |
| 746 | // Find Domain configuration |
| 747 | CDomainConfiguration* pDomainConfiguration = findConfiguration(strConfiguration, strError); |
| 748 | |
| 749 | if (!pDomainConfiguration) { |
| 750 | |
| 751 | return false; |
| 752 | } |
| 753 | |
| 754 | // Delegate to configuration |
| 755 | return pDomainConfiguration->setApplicationRule(strApplicationRule, pSelectionCriteriaDefinition, strError); |
| 756 | } |
| 757 | |
| 758 | bool CConfigurableDomain::clearApplicationRule(const string& strConfiguration, string& strError) |
| 759 | { |
| 760 | // Find Domain configuration |
| 761 | CDomainConfiguration* pDomainConfiguration = findConfiguration(strConfiguration, strError); |
| 762 | |
| 763 | if (!pDomainConfiguration) { |
| 764 | |
| 765 | return false; |
| 766 | } |
| 767 | |
| 768 | // Delegate to configuration |
| 769 | pDomainConfiguration->clearApplicationRule(); |
| 770 | |
| 771 | return true; |
| 772 | } |
| 773 | |
| 774 | bool CConfigurableDomain::getApplicationRule(const string& strConfiguration, string& strResult) const |
| 775 | { |
| 776 | // Find Domain configuration |
| 777 | const CDomainConfiguration* pDomainConfiguration = findConfiguration(strConfiguration, strResult); |
| 778 | |
| 779 | if (!pDomainConfiguration) { |
| 780 | |
| 781 | return false; |
| 782 | } |
| 783 | |
| 784 | // Delegate to configuration |
| 785 | pDomainConfiguration->getApplicationRule(strResult); |
| 786 | |
| 787 | return true; |
| 788 | } |
| 789 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 790 | // Last applied configuration |
| 791 | string CConfigurableDomain::getLastAppliedConfigurationName() const |
| 792 | { |
| 793 | if (_pLastAppliedConfiguration) { |
| 794 | |
| 795 | return _pLastAppliedConfiguration->getName(); |
| 796 | } |
| 797 | return "<none>"; |
| 798 | } |
| 799 | |
| Frédéric Boisnard | 8b243f5 | 2012-09-06 18:03:20 +0200 | [diff] [blame] | 800 | // Pending configuration |
| 801 | string CConfigurableDomain::getPendingConfigurationName() const |
| 802 | { |
| 803 | const CDomainConfiguration* pPendingConfiguration = getPendingConfiguration(); |
| 804 | |
| 805 | if (pPendingConfiguration) { |
| 806 | |
| 807 | return pPendingConfiguration->getName(); |
| 808 | } |
| 809 | return "<none>"; |
| 810 | } |
| 811 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 812 | // Ensure validity on whole domain from main blackboard |
| 813 | void CConfigurableDomain::validate(const CParameterBlackboard* pMainBlackboard) |
| 814 | { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 815 | |
| 816 | // Propagate |
| 817 | uint32_t uiNbConfigurations = getNbChildren(); |
| 818 | uint32_t uiChild; |
| 819 | |
| 820 | for (uiChild = 0; uiChild < uiNbConfigurations; uiChild++) { |
| 821 | |
| 822 | CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(getChild(uiChild)); |
| 823 | |
| 824 | pDomainConfiguration->validate(pMainBlackboard); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | // Ensure validity on areas related to configurable element |
| 829 | void CConfigurableDomain::validateAreas(const CConfigurableElement* pConfigurableElement, const CParameterBlackboard* pMainBlackboard) |
| 830 | { |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 831 | log_info("Validating domain \"" + getName() + "\" against main blackboard for configurable element \"" + pConfigurableElement->getPath() + "\""); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 832 | |
| 833 | // Propagate |
| 834 | uint32_t uiNbConfigurations = getNbChildren(); |
| 835 | uint32_t uiChild; |
| 836 | |
| 837 | for (uiChild = 0; uiChild < uiNbConfigurations; uiChild++) { |
| 838 | |
| 839 | CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(getChild(uiChild)); |
| 840 | |
| 841 | pDomainConfiguration->validate(pConfigurableElement, pMainBlackboard); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | // Attempt validation for all configurable element's areas, relying on already existing valid configuration inside domain |
| 846 | void CConfigurableDomain::autoValidateAll() |
| 847 | { |
| 848 | // Validate |
| 849 | ConfigurableElementListIterator it; |
| 850 | |
| 851 | // Browse all configurable elements for configuration validation |
| 852 | for (it = _configurableElementList.begin(); it != _configurableElementList.end(); ++it) { |
| 853 | |
| 854 | const CConfigurableElement* pConfigurableElement = *it; |
| 855 | |
| 856 | // Auto validate element |
| 857 | autoValidateAreas(pConfigurableElement); |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | // Attempt validation for configurable element's areas, relying on already existing valid configuration inside domain |
| 862 | void CConfigurableDomain::autoValidateAreas(const CConfigurableElement* pConfigurableElement) |
| 863 | { |
| 864 | // Find first valid configuration for given configurable element |
| 865 | const CDomainConfiguration* pValidDomainConfiguration = findValidDomainConfiguration(pConfigurableElement); |
| 866 | |
| 867 | // No valid configuration found, give up |
| 868 | if (!pValidDomainConfiguration) { |
| 869 | |
| 870 | return; |
| 871 | } |
| 872 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 873 | // Validate all other configurations against found one, if any |
| 874 | uint32_t uiNbConfigurations = getNbChildren(); |
| 875 | uint32_t uiChild; |
| 876 | |
| 877 | for (uiChild = 0; uiChild < uiNbConfigurations; uiChild++) { |
| 878 | |
| 879 | CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(getChild(uiChild)); |
| 880 | |
| 881 | if (pDomainConfiguration != pValidDomainConfiguration && !pDomainConfiguration->isValid(pConfigurableElement)) { |
| 882 | // Validate |
| 883 | pDomainConfiguration->validateAgainst(pValidDomainConfiguration, pConfigurableElement); |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | // Attempt configuration validation for all configurable elements' areas, relying on already existing valid configuration inside domain |
| 889 | bool CConfigurableDomain::autoValidateConfiguration(CDomainConfiguration* pDomainConfiguration) |
| 890 | { |
| 891 | // Find another configuration than this one, that ought to be valid! |
| 892 | uint32_t uiNbConfigurations = getNbChildren(); |
| 893 | uint32_t uiChild; |
| 894 | |
| 895 | for (uiChild = 0; uiChild < uiNbConfigurations; uiChild++) { |
| 896 | |
| 897 | const CDomainConfiguration* pPotententialValidDomainConfiguration = static_cast<const CDomainConfiguration*>(getChild(uiChild)); |
| 898 | |
| 899 | if (pPotententialValidDomainConfiguration != pDomainConfiguration) { |
| 900 | |
| 901 | // Validate against it |
| 902 | pDomainConfiguration->validateAgainst(pPotententialValidDomainConfiguration); |
| 903 | |
| 904 | return true; |
| 905 | } |
| 906 | } |
| 907 | return false; |
| 908 | } |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 909 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 910 | // Search for a valid configuration for given configurable element |
| 911 | const CDomainConfiguration* CConfigurableDomain::findValidDomainConfiguration(const CConfigurableElement* pConfigurableElement) const |
| 912 | { |
| 913 | uint32_t uiNbConfigurations = getNbChildren(); |
| 914 | uint32_t uiChild; |
| 915 | |
| 916 | for (uiChild = 0; uiChild < uiNbConfigurations; uiChild++) { |
| 917 | |
| 918 | const CDomainConfiguration* pDomainConfiguration = static_cast<const CDomainConfiguration*>(getChild(uiChild)); |
| 919 | |
| 920 | if (pDomainConfiguration->isValid(pConfigurableElement)) { |
| 921 | |
| 922 | return pDomainConfiguration; |
| 923 | } |
| 924 | } |
| 925 | return NULL; |
| 926 | } |
| 927 | |
| 928 | // Search for an applicable configuration |
| 929 | const CDomainConfiguration* CConfigurableDomain::findApplicableDomainConfiguration() const |
| 930 | { |
| 931 | uint32_t uiNbConfigurations = getNbChildren(); |
| 932 | uint32_t uiChild; |
| 933 | |
| 934 | for (uiChild = 0; uiChild < uiNbConfigurations; uiChild++) { |
| 935 | |
| 936 | const CDomainConfiguration* pDomainConfiguration = static_cast<const CDomainConfiguration*>(getChild(uiChild)); |
| 937 | |
| 938 | if (pDomainConfiguration->isApplicable()) { |
| 939 | |
| 940 | return pDomainConfiguration; |
| 941 | } |
| 942 | } |
| 943 | return NULL; |
| 944 | } |
| 945 | |
| 946 | // Gather set of configurable elements |
| 947 | void CConfigurableDomain::gatherConfigurableElements(set<const CConfigurableElement*>& configurableElementSet) const |
| 948 | { |
| 949 | // Insert all configurable elements |
| 950 | configurableElementSet.insert(_configurableElementList.begin(), _configurableElementList.end()); |
| 951 | } |
| 952 | |
| 953 | // Check configurable element already attached |
| 954 | bool CConfigurableDomain::containsConfigurableElement(const CConfigurableElement* pConfigurableCandidateElement) const |
| 955 | { |
| 956 | ConfigurableElementListIterator it; |
| 957 | |
| 958 | // Browse all configurable elements for comparison |
| 959 | for (it = _configurableElementList.begin(); it != _configurableElementList.end(); ++it) { |
| 960 | |
| 961 | if (pConfigurableCandidateElement == *it) { |
| 962 | |
| 963 | return true; |
| 964 | } |
| 965 | } |
| 966 | return false; |
| 967 | } |
| 968 | |
| 969 | // Merge any descended configurable element to this one with this one |
| 970 | void CConfigurableDomain::mergeAlreadyAssociatedDescendantConfigurableElements(CConfigurableElement* pNewConfigurableElement) |
| 971 | { |
| 972 | list<CConfigurableElement*> mergedConfigurableElementList; |
| 973 | |
| 974 | ConfigurableElementListIterator it; |
| 975 | |
| 976 | // Browse all configurable elements (new one not yet in the list!) |
| 977 | for (it = _configurableElementList.begin(); it != _configurableElementList.end(); ++it) { |
| 978 | |
| 979 | CConfigurableElement* pConfigurablePotentialDescendantElement = *it; |
| 980 | |
| 981 | if (pConfigurablePotentialDescendantElement->isDescendantOf(pNewConfigurableElement)) { |
| 982 | |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 983 | log_info("In domain \"%s\", merging descendant configurable element's configurations \"%s\" into its ascendant \"%s\" ones", getName().c_str(), pConfigurablePotentialDescendantElement->getName().c_str(), pNewConfigurableElement->getName().c_str()); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 984 | |
| 985 | // Merge configuration data |
| 986 | mergeConfigurations(pNewConfigurableElement, pConfigurablePotentialDescendantElement); |
| 987 | |
| 988 | // Keep track for removal |
| 989 | mergedConfigurableElementList.push_back(pConfigurablePotentialDescendantElement); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | // Remove all merged elements (new one not yet in the list!) |
| 994 | for (it = mergedConfigurableElementList.begin(); it != mergedConfigurableElementList.end(); ++it) { |
| 995 | |
| 996 | CConfigurableElement* pMergedConfigurableElement = *it; |
| 997 | |
| 998 | // Remove merged from configurable element from internal tracking list |
| 999 | // Note: we shouldn't need to recompute the sync set in that case, as the merged to element should include the syncers of merged from elements |
| 1000 | doRemoveConfigurableElement(pMergedConfigurableElement, false); |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | void CConfigurableDomain::mergeConfigurations(CConfigurableElement* pToConfigurableElement, CConfigurableElement* pFromConfigurableElement) |
| 1005 | { |
| 1006 | // Propagate to domain configurations |
| 1007 | uint32_t uiNbConfigurations = getNbChildren(); |
| 1008 | uint32_t uiChild; |
| 1009 | |
| 1010 | for (uiChild = 0; uiChild < uiNbConfigurations; uiChild++) { |
| 1011 | |
| 1012 | CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(getChild(uiChild)); |
| 1013 | |
| 1014 | // Do the merge. |
| 1015 | pDomainConfiguration->merge(pToConfigurableElement, pFromConfigurableElement); |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | // Configurable elements association |
| Frédéric Boisnard | 9620e44 | 2012-05-30 16:15:02 +0200 | [diff] [blame] | 1020 | void CConfigurableDomain::doAddConfigurableElement(CConfigurableElement* pConfigurableElement, const CParameterBlackboard *pMainBlackboard) |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 1021 | { |
| 1022 | // Inform configurable element |
| 1023 | pConfigurableElement->addAttachedConfigurableDomain(this); |
| 1024 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 1025 | // Create associated syncer set |
| 1026 | CSyncerSet* pSyncerSet = new CSyncerSet; |
| 1027 | |
| 1028 | // Add to sync set the configurable element one |
| 1029 | pConfigurableElement->fillSyncerSet(*pSyncerSet); |
| 1030 | |
| 1031 | // Store it |
| 1032 | _configurableElementToSyncerSetMap[pConfigurableElement] = pSyncerSet; |
| 1033 | |
| 1034 | // Add it to global one |
| 1035 | _syncerSet += *pSyncerSet; |
| 1036 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 1037 | // Inform configurations |
| 1038 | uint32_t uiNbConfigurations = getNbChildren(); |
| 1039 | uint32_t uiChild; |
| 1040 | |
| 1041 | for (uiChild = 0; uiChild < uiNbConfigurations; uiChild++) { |
| 1042 | |
| 1043 | CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(getChild(uiChild)); |
| 1044 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 1045 | pDomainConfiguration->addConfigurableElement(pConfigurableElement, pSyncerSet); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 1046 | } |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 1047 | |
| Frédéric Boisnard | 9620e44 | 2012-05-30 16:15:02 +0200 | [diff] [blame] | 1048 | // Ensure area validity for that configurable element (if main blackboard provided) |
| 1049 | if (pMainBlackboard) { |
| 1050 | |
| 1051 | // Need to validate against main blackboard |
| 1052 | validateAreas(pConfigurableElement, pMainBlackboard); |
| 1053 | } |
| 1054 | |
| 1055 | // Already associated descendend configurable elements need a merge of their configuration data |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 1056 | mergeAlreadyAssociatedDescendantConfigurableElements(pConfigurableElement); |
| 1057 | |
| 1058 | // Add to list |
| 1059 | _configurableElementList.push_back(pConfigurableElement); |
| 1060 | } |
| 1061 | |
| 1062 | void CConfigurableDomain::doRemoveConfigurableElement(CConfigurableElement* pConfigurableElement, bool bRecomputeSyncSet) |
| 1063 | { |
| 1064 | // Remove from list |
| 1065 | _configurableElementList.remove(pConfigurableElement); |
| 1066 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 1067 | // Remove associated syncer set |
| 1068 | CSyncerSet* pSyncerSet = getSyncerSet(pConfigurableElement); |
| 1069 | |
| 1070 | _configurableElementToSyncerSetMap.erase(pConfigurableElement); |
| 1071 | |
| 1072 | delete pSyncerSet; |
| 1073 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 1074 | // Inform configurable element |
| 1075 | pConfigurableElement->removeAttachedConfigurableDomain(this); |
| 1076 | |
| 1077 | // Inform configurations |
| 1078 | uint32_t uiNbConfigurations = getNbChildren(); |
| 1079 | uint32_t uiChild; |
| 1080 | |
| 1081 | for (uiChild = 0; uiChild < uiNbConfigurations; uiChild++) { |
| 1082 | |
| 1083 | CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(getChild(uiChild)); |
| 1084 | |
| 1085 | pDomainConfiguration->removeConfigurableElement(pConfigurableElement); |
| 1086 | } |
| 1087 | // Recompute our sync set if needed |
| 1088 | if (bRecomputeSyncSet) { |
| 1089 | |
| 1090 | computeSyncSet(); |
| 1091 | } |
| 1092 | } |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 1093 | |
| 1094 | // Syncer set retrieval from configurable element |
| 1095 | CSyncerSet* CConfigurableDomain::getSyncerSet(const CConfigurableElement* pConfigurableElement) const |
| 1096 | { |
| 1097 | ConfigurableElementToSyncerSetMapIterator mapIt = _configurableElementToSyncerSetMap.find(pConfigurableElement); |
| 1098 | |
| 1099 | assert(mapIt != _configurableElementToSyncerSetMap.end()); |
| 1100 | |
| 1101 | return mapIt->second; |
| 1102 | } |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 1103 | |
| 1104 | // Configuration retrieval |
| 1105 | CDomainConfiguration* CConfigurableDomain::findConfiguration(const string& strConfiguration, string& strError) |
| 1106 | { |
| 1107 | CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(findChild(strConfiguration)); |
| 1108 | |
| 1109 | if (!pDomainConfiguration) { |
| 1110 | |
| 1111 | strError = "Domain configuration " + strConfiguration + " not found"; |
| 1112 | |
| 1113 | return NULL; |
| 1114 | } |
| 1115 | return pDomainConfiguration; |
| 1116 | } |
| 1117 | |
| 1118 | const CDomainConfiguration* CConfigurableDomain::findConfiguration(const string& strConfiguration, string& strError) const |
| 1119 | { |
| 1120 | const CDomainConfiguration* pDomainConfiguration = static_cast<const CDomainConfiguration*>(findChild(strConfiguration)); |
| 1121 | |
| 1122 | if (!pDomainConfiguration) { |
| 1123 | |
| 1124 | strError = "Domain configuration " + strConfiguration + " not found"; |
| 1125 | |
| 1126 | return NULL; |
| 1127 | } |
| 1128 | return pDomainConfiguration; |
| 1129 | } |