2012-06-06 14:38:28 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-06-01 16:49:33 +02:00
|
|
|
#include "i2-dyn.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
ConfigItem::ItemMap ConfigItem::m_Items;
|
|
|
|
boost::signal<void (const ConfigItem::Ptr&)> ConfigItem::OnCommitted;
|
|
|
|
boost::signal<void (const ConfigItem::Ptr&)> ConfigItem::OnRemoved;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
ConfigItem::ConfigItem(const String& type, const String& name,
|
|
|
|
const ExpressionList::Ptr& exprl, const vector<String>& parents,
|
2012-07-06 14:33:10 +02:00
|
|
|
const DebugInfo& debuginfo)
|
|
|
|
: m_Type(type), m_Name(name), m_ExpressionList(exprl),
|
|
|
|
m_Parents(parents), m_DebugInfo(debuginfo)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String ConfigItem::GetType(void) const
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
|
|
|
return m_Type;
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String ConfigItem::GetName(void) const
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
|
|
|
return m_Name;
|
|
|
|
}
|
|
|
|
|
2012-07-06 11:22:38 +02:00
|
|
|
DebugInfo ConfigItem::GetDebugInfo(void) const
|
|
|
|
{
|
|
|
|
return m_DebugInfo;
|
|
|
|
}
|
|
|
|
|
2012-06-06 14:38:28 +02:00
|
|
|
ExpressionList::Ptr ConfigItem::GetExpressionList(void) const
|
2012-06-01 16:49:33 +02:00
|
|
|
{
|
|
|
|
return m_ExpressionList;
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
vector<String> ConfigItem::GetParents(void) const
|
2012-06-01 16:49:33 +02:00
|
|
|
{
|
|
|
|
return m_Parents;
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
void ConfigItem::CalculateProperties(const Dictionary::Ptr& dictionary) const
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
BOOST_FOREACH(const String& name, m_Parents) {
|
2012-07-16 22:00:50 +02:00
|
|
|
ConfigItem::Ptr parent = ConfigItem::GetObject(GetType(), name);
|
2012-06-05 15:05:15 +02:00
|
|
|
|
|
|
|
if (!parent) {
|
|
|
|
stringstream message;
|
2012-07-16 22:00:50 +02:00
|
|
|
message << "Parent object '" << name << "' does not exist (" << m_DebugInfo << ")";
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(domain_error(message.str()));
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
2012-06-06 20:33:25 +02:00
|
|
|
parent->CalculateProperties(dictionary);
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
2012-06-06 20:33:25 +02:00
|
|
|
m_ExpressionList->Execute(dictionary);
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::Ptr ConfigItem::Commit(void)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::Ptr dobj = m_DynamicObject.lock();
|
2012-06-06 14:38:28 +02:00
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
Dictionary::Ptr properties = boost::make_shared<Dictionary>();
|
2012-06-12 09:36:18 +02:00
|
|
|
CalculateProperties(properties);
|
2012-06-06 14:38:28 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
/* Create a fake update in the format that
|
|
|
|
* DynamicObject::ApplyUpdate expects. */
|
|
|
|
Dictionary::Ptr attrs = boost::make_shared<Dictionary>();
|
|
|
|
|
|
|
|
String key;
|
|
|
|
Value data;
|
|
|
|
BOOST_FOREACH(tie(key, data), properties) {
|
|
|
|
Dictionary::Ptr attr = boost::make_shared<Dictionary>();
|
|
|
|
attr->Set("data", data);
|
|
|
|
attr->Set("type", Attribute_Config);
|
|
|
|
attr->Set("tx", DynamicObject::GetCurrentTx());
|
|
|
|
attrs->Set(key, attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary::Ptr update = boost::make_shared<Dictionary>();
|
|
|
|
update->Set("attrs", attrs);
|
|
|
|
update->Set("configTx", DynamicObject::GetCurrentTx());
|
|
|
|
|
2012-06-12 09:36:18 +02:00
|
|
|
if (!dobj)
|
2012-07-30 10:17:29 +02:00
|
|
|
dobj = DynamicObject::GetObject(GetType(), GetName());
|
2012-06-06 14:38:28 +02:00
|
|
|
|
2012-06-12 09:36:18 +02:00
|
|
|
if (!dobj)
|
2012-08-02 09:38:08 +02:00
|
|
|
dobj = DynamicObject::Create(GetType(), update);
|
2012-06-12 09:36:18 +02:00
|
|
|
else
|
2012-08-02 09:38:08 +02:00
|
|
|
dobj->ApplyUpdate(update);
|
2012-06-12 09:36:18 +02:00
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
m_DynamicObject = dobj;
|
2012-06-06 14:38:28 +02:00
|
|
|
|
2012-06-13 13:42:55 +02:00
|
|
|
if (dobj->IsAbstract())
|
|
|
|
dobj->Unregister();
|
|
|
|
else
|
2012-08-02 09:38:08 +02:00
|
|
|
dobj->Register();
|
2012-06-06 14:38:28 +02:00
|
|
|
|
2012-07-06 11:22:38 +02:00
|
|
|
/* TODO: Figure out whether there are any child objects which inherit
|
|
|
|
* from this config item and Commit() them as well */
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
m_Items[make_pair(GetType(), GetName())] = GetSelf();
|
|
|
|
|
|
|
|
OnCommitted(GetSelf());
|
2012-07-06 11:22:38 +02:00
|
|
|
|
|
|
|
return dobj;
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
2012-06-06 14:38:28 +02:00
|
|
|
void ConfigItem::Unregister(void)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::Ptr dobj = m_DynamicObject.lock();
|
2012-06-27 18:43:34 +02:00
|
|
|
|
|
|
|
if (dobj)
|
|
|
|
dobj->Unregister();
|
2012-06-06 14:38:28 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
ConfigItem::ItemMap::iterator it;
|
|
|
|
it = m_Items.find(make_pair(GetType(), GetName()));
|
|
|
|
|
|
|
|
if (it != m_Items.end())
|
|
|
|
m_Items.erase(it);
|
|
|
|
|
|
|
|
OnRemoved(GetSelf());
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::Ptr ConfigItem::GetDynamicObject(void) const
|
2012-07-06 11:22:38 +02:00
|
|
|
{
|
2012-07-30 10:17:29 +02:00
|
|
|
return m_DynamicObject.lock();
|
2012-07-06 11:22:38 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
ConfigItem::Ptr ConfigItem::GetObject(const String& type, const String& name)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2012-07-27 16:05:02 +02:00
|
|
|
ConfigItem::ItemMap::iterator it;
|
|
|
|
it = m_Items.find(make_pair(type, name));
|
2012-06-05 15:05:15 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
if (it == m_Items.end())
|
2012-06-06 14:38:28 +02:00
|
|
|
return ConfigItem::Ptr();
|
2012-07-27 16:05:02 +02:00
|
|
|
|
|
|
|
return it->second;
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|