OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
libvirt_instance_adapter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Codilime
3  */
4 
5 #ifndef SRC_VNSW_AGENT_OPER_LIBVIRT_INSTANCE_ADAPTER_H_
6 #define SRC_VNSW_AGENT_OPER_LIBVIRT_INSTANCE_ADAPTER_H_
7 #include <tbb/tbb.h>
8 #include <libvirt/libvirt.h>
9 #include <boost/uuid/uuid_io.hpp>
10 #include <pugixml/pugixml.hpp>
11 #include <map>
12 #include <string>
14 #include "oper/service_instance.h"
15 #include "oper/instance_task.h"
16 
17 class InstanceTask;
18 
20  public:
21  LibvirtInstanceAdapter(Agent *agent, const std::string &libvirt_conn_addr)
22  : agent_(agent), libvirt_conn_addr_(libvirt_conn_addr), conn_(NULL) {}
24 
26  bool update);
28  bool isApplicable(const ServiceInstance::Properties &props);
29 
31  public:
33  const ServiceInstance::Properties &props,
34  bool update)
35  : parent_adapter_(parent_adapter), si_properties_(props),
36  update_(update) {}
37 
38  bool Run();
39  void Stop() {}
40  void Terminate() {}
41  bool IsSetup() { return true; }
42 
43  const std::string &cmd() const {
44  static const std::string cmdstr =
45  "libvirt domain start task";
46  return cmdstr;
47  }
48 
49  int cmd_type() const {
50  return START;
51  }
52 
53  private:
54  std::string XmlConf();
55  static void DomainXMLAssignUUID(
56  const std::string &libvirt_conf_str,
57  const pugi::xml_document &libvirt_xml_conf);
59  const pugi::xml_document &libvirt_xml_conf,
60  const std::string &dom_uuid);
61  static void DomainXMLAddInterface(
62  pugi::xml_node *devices_node,
63  const std::string &mac_addr,
64  const std::string &intf_name);
65  static bool CreateTAPInterfaces(const std::string &dom_uuid);
66 
69  const bool update_;
70  };
71 
73  public:
75  const ServiceInstance::Properties &props)
76  : parent_adapter_(parent_adapter), si_properties_(props) {}
77 
78  bool Run();
79  void Stop() {}
80  void Terminate() {}
81  bool IsSetup() { return true; }
82 
83  const std::string &cmd() const {
84  static const std::string cmdstr =
85  "libvirt domain stop task";
86  return cmdstr;
87  }
88 
89  int cmd_type() const {
90  return STOP;
91  }
92 
93  private:
96  };
97 
98  private:
99  bool EnsureConnected();
100  void CloseConnection();
101  void EnsureDestroyed(const std::string &dom_uuid_str,
102  const ServiceInstance::Properties &si_properties);
103  void UnregisterInterfaces(const ServiceInstance::Properties &si_properties);
104  bool RegisterInterfaces(const ServiceInstance::Properties &si_properties);
105  static std::string GenIntfName(const std::string &dom_uuid, char type);
106 
108  std::string libvirt_conn_addr_;
109  virConnectPtr conn_;
110  static tbb::mutex conn_mutex_;
111 };
112 #endif // SRC_VNSW_AGENT_OPER_LIBVIRT_INSTANCE_ADAPTER_H_
113 
LibvirtInstanceAdapter(Agent *agent, const std::string &libvirt_conn_addr)
DomainStopTask(LibvirtInstanceAdapter *parent_adapter, const ServiceInstance::Properties &props)
static bool CreateTAPInterfaces(const std::string &dom_uuid)
static std::string GenIntfName(const std::string &dom_uuid, char type)
InstanceTask * CreateStartTask(const ServiceInstance::Properties &props, bool update)
static void DomainXMLAddInterface(pugi::xml_node *devices_node, const std::string &mac_addr, const std::string &intf_name)
void DomainXMLSetInterfaceData(const pugi::xml_document &libvirt_xml_conf, const std::string &dom_uuid)
const ServiceInstance::Properties & si_properties_
void EnsureDestroyed(const std::string &dom_uuid_str, const ServiceInstance::Properties &si_properties)
DomainStartTask(LibvirtInstanceAdapter *parent_adapter, const ServiceInstance::Properties &props, bool update)
uint8_t type
Definition: load_balance.h:109
Definition: agent.h:358
bool RegisterInterfaces(const ServiceInstance::Properties &si_properties)
bool isApplicable(const ServiceInstance::Properties &props)
static void DomainXMLAssignUUID(const std::string &libvirt_conf_str, const pugi::xml_document &libvirt_xml_conf)
void UnregisterInterfaces(const ServiceInstance::Properties &si_properties)
InstanceTask * CreateStopTask(const ServiceInstance::Properties &props)
const ServiceInstance::Properties & si_properties_