/src/topology/physicalDataCenterFactory.cpp

https://github.com/deltaforge/nebu-common-cpp · C++ · 55 lines · 42 code · 11 blank · 2 comment · 2 complexity · 204c48c88ac933c8d1fe2492dd394069 MD5 · raw file

  1. #include "nebu/topology/physicalDataCenterFactory.h"
  2. #include "nebu/topology/physicalRackFactory.h"
  3. #include "nebu/util/xmlUtil.h"
  4. // Using declarations - tinyxml2
  5. using tinyxml2::XMLAttribute;
  6. using tinyxml2::XMLElement;
  7. // Using declarations - standard library
  8. using std::make_shared;
  9. using std::shared_ptr;
  10. using std::string;
  11. namespace nebu
  12. {
  13. namespace common
  14. {
  15. shared_ptr<PhysicalDataCenterFactory> PhysicalDataCenterFactory::instance;
  16. const string PhysicalDataCenterFactory::ROOT_NAME = "dataCenter";
  17. const string PhysicalDataCenterFactory::ATTR_ID = "id";
  18. shared_ptr<PhysicalDataCenterFactory> PhysicalDataCenterFactory::getInstance()
  19. {
  20. if (!PhysicalDataCenterFactory::instance) {
  21. PhysicalDataCenterFactory::setInstance(make_shared<PhysicalDataCenterFactory>());
  22. }
  23. return PhysicalDataCenterFactory::instance;
  24. }
  25. void PhysicalDataCenterFactory::setInstance(std::shared_ptr<PhysicalDataCenterFactory> instance)
  26. {
  27. PhysicalDataCenterFactory::instance = instance;
  28. }
  29. Traits<PhysicalDataCenter>::Ptr PhysicalDataCenterFactory::parseXML(const tinyxml2::XMLElement *elem) const
  30. {
  31. XMLUtil::verifyElementName(elem, PhysicalDataCenterFactory::ROOT_NAME);
  32. const XMLAttribute *idAttr = XMLUtil::findAttribute(elem, PhysicalDataCenterFactory::ATTR_ID, XMLUtil::MANDATORY);
  33. Traits<PhysicalDataCenter>::Ptr dataCenter = Traits<PhysicalDataCenter>::MakePtr(idAttr->Value());
  34. const XMLElement *rackElem = XMLUtil::findChildElement(elem, PhysicalRackFactory::ROOT_NAME);
  35. while (rackElem) {
  36. Traits<PhysicalRack>::Ptr rack = PhysicalRackFactory::getInstance()->parseXML(rackElem);
  37. dataCenter->addRack(rack);
  38. rack->setParent(dataCenter.get());
  39. rackElem = XMLUtil::findNextSibling(rackElem, PhysicalRackFactory::ROOT_NAME);
  40. }
  41. return dataCenter;
  42. }
  43. }
  44. }