OpenSDN source code
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 
8 #include <mutex>
9 #include <map>
10 #include <string>
11 
12 #include <libvirt/libvirt.h>
13 #include <boost/uuid/uuid_io.hpp>
14 #include <pugixml/pugixml.hpp>
16 #include "oper/service_instance.h"
17 #include "oper/instance_task.h"
18 
19 class InstanceTask;
20 
22  public:
23  LibvirtInstanceAdapter(Agent *agent, const std::string &libvirt_conn_addr)
24  : agent_(agent), libvirt_conn_addr_(libvirt_conn_addr), conn_(NULL) {}
26 
28  bool update);
30  bool isApplicable(const ServiceInstance::Properties &props);
31 
33  public:
35  const ServiceInstance::Properties &props,
36  bool update)
37  : parent_adapter_(parent_adapter), si_properties_(props),
38  update_(update) {}
39 
40  bool Run();
41  void Stop() {}
42  void Terminate() {}
43  bool IsSetup() { return true; }
44 
45  const std::string &cmd() const {
46  static const std::string cmdstr =
47  "libvirt domain start task";
48  return cmdstr;
49  }
50 
51  int cmd_type() const {
52  return START;
53  }
54 
55  private:
56  std::string XmlConf();
57  static void DomainXMLAssignUUID(
58  const std::string &libvirt_conf_str,
59  const pugi::xml_document &libvirt_xml_conf);
61  const pugi::xml_document &libvirt_xml_conf,
62  const std::string &dom_uuid);
63  static void DomainXMLAddInterface(
64  pugi::xml_node *devices_node,
65  const std::string &mac_addr,
66  const std::string &intf_name);
67  static bool CreateTAPInterfaces(const std::string &dom_uuid);
68 
71  const bool update_;
72  };
73 
75  public:
77  const ServiceInstance::Properties &props)
78  : parent_adapter_(parent_adapter), si_properties_(props) {}
79 
80  bool Run();
81  void Stop() {}
82  void Terminate() {}
83  bool IsSetup() { return true; }
84 
85  const std::string &cmd() const {
86  static const std::string cmdstr =
87  "libvirt domain stop task";
88  return cmdstr;
89  }
90 
91  int cmd_type() const {
92  return STOP;
93  }
94 
95  private:
98  };
99 
100  private:
101  bool EnsureConnected();
102  void CloseConnection();
103  void EnsureDestroyed(const std::string &dom_uuid_str,
104  const ServiceInstance::Properties &si_properties);
105  void UnregisterInterfaces(const ServiceInstance::Properties &si_properties);
106  bool RegisterInterfaces(const ServiceInstance::Properties &si_properties);
107  static std::string GenIntfName(const std::string &dom_uuid, char type);
108 
110  std::string libvirt_conn_addr_;
111  virConnectPtr conn_;
112  static std::mutex conn_mutex_;
113 };
114 #endif // SRC_VNSW_AGENT_OPER_LIBVIRT_INSTANCE_ADAPTER_H_
115 
Definition: agent.h:360
const ServiceInstance::Properties & si_properties_
void DomainXMLSetInterfaceData(const pugi::xml_document &libvirt_xml_conf, const std::string &dom_uuid)
static bool CreateTAPInterfaces(const std::string &dom_uuid)
static void DomainXMLAddInterface(pugi::xml_node *devices_node, const std::string &mac_addr, const std::string &intf_name)
static void DomainXMLAssignUUID(const std::string &libvirt_conf_str, const pugi::xml_document &libvirt_xml_conf)
DomainStartTask(LibvirtInstanceAdapter *parent_adapter, const ServiceInstance::Properties &props, bool update)
DomainStopTask(LibvirtInstanceAdapter *parent_adapter, const ServiceInstance::Properties &props)
const ServiceInstance::Properties & si_properties_
bool isApplicable(const ServiceInstance::Properties &props)
bool RegisterInterfaces(const ServiceInstance::Properties &si_properties)
InstanceTask * CreateStopTask(const ServiceInstance::Properties &props)
void EnsureDestroyed(const std::string &dom_uuid_str, const ServiceInstance::Properties &si_properties)
LibvirtInstanceAdapter(Agent *agent, const std::string &libvirt_conn_addr)
static std::string GenIntfName(const std::string &dom_uuid, char type)
InstanceTask * CreateStartTask(const ServiceInstance::Properties &props, bool update)
void UnregisterInterfaces(const ServiceInstance::Properties &si_properties)
uint8_t type
Definition: load_balance.h:2