/
netns_instance_adapter.cc
133 lines (118 loc) · 4.77 KB
/
netns_instance_adapter.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <fstream>
#include <boost/filesystem.hpp>
#include "oper/netns_instance_adapter.h"
#include "oper/service_instance.h"
#include "oper/instance_task.h"
#include "agent.h"
#include "init/agent_param.h"
InstanceTask* NetNSInstanceAdapter::CreateStartTask(const ServiceInstance::Properties &props, bool update) {
std::stringstream cmd_str;
if (netns_cmd_.length() == 0) {
return NULL;
}
cmd_str << netns_cmd_ << " create";
cmd_str << " " << props.ServiceTypeString();
cmd_str << " " << UuidToString(props.instance_id);
cmd_str << " " << UuidToString(props.vmi_inside);
cmd_str << " " << UuidToString(props.vmi_outside);
if (props.ip_prefix_len_inside != -1) {
cmd_str << " --vmi-left-ip " << props.ip_addr_inside << "/";
cmd_str << props.ip_prefix_len_inside;
} else {
cmd_str << " --vmi-left-ip 0.0.0.0/0";
}
cmd_str << " --vmi-right-ip " << props.ip_addr_outside << "/";
cmd_str << props.ip_prefix_len_outside;
if (!props.mac_addr_inside.empty()) {
cmd_str << " --vmi-left-mac " << props.mac_addr_inside;
} else {
cmd_str << " --vmi-left-mac 00:00:00:00:00:00";
}
cmd_str << " --vmi-right-mac " << props.mac_addr_outside;
cmd_str << " --gw-ip " << props.gw_ip;
if (props.service_type == ServiceInstance::LoadBalancer) {
if (props.loadbalancer_id.empty()) {
LOG(ERROR, "loadbalancer id is missing for service instance: "
<< UuidToString(props.instance_id));
return NULL;
}
cmd_str << " --loadbalancer-id " << props.loadbalancer_id;
if (!agent_->params()->si_lb_keystone_auth_conf_path().empty()) {
cmd_str << " --keystone-auth-cfg-file " <<
agent_->params()->si_lb_keystone_auth_conf_path();
}
std::stringstream pathgen;
pathgen << loadbalancer_config_path_
<< props.loadbalancer_id << ".conf";
const std::string &filename = pathgen.str();
std::ofstream fs(filename.c_str());
if (fs.fail()) {
LOG(ERROR, "File create " << filename << ": " << strerror(errno));
return NULL;
}
const std::vector<autogen::KeyValuePair> &kvps = props.instance_kvps;
bool kvp_found = false;
std::vector<autogen::KeyValuePair>::const_iterator iter;
autogen::KeyValuePair kvp;
for (iter = kvps.begin(); iter != kvps.end(); ++iter) {
kvp = *iter;
/* :::: - Key Value Seperator
::::: - Key Value Pair Seperator */
if (kvp_found == true)
fs << ":::::";
fs << kvp.key << "::::" << kvp.value;
kvp_found = true;
}
if (!agent_->params()->si_lb_ssl_cert_path().empty()) {
fs << ":::::" << "lb_ssl_cert_path";
fs << "::::" << agent_->params()->si_lb_ssl_cert_path();
}
if (!agent_->params()->si_lbaas_auth_conf().empty()) {
fs << ":::::" << "lbaas_auth_conf";
fs << "::::" << agent_->params()->si_lbaas_auth_conf();
}
fs.close();
if (kvp_found == false) {
LOG(ERROR, "KeyValuePair is missing for loadbalancer: "
<< props.loadbalancer_id);
return NULL;
}
cmd_str << " --cfg-file " << pathgen.str();
}
if (update) {
cmd_str << " --update";
}
return new InstanceTaskExecvp("NetNS", cmd_str.str(), START,
agent_->event_manager());
}
InstanceTask* NetNSInstanceAdapter::CreateStopTask(const ServiceInstance::Properties &props) {
std::stringstream cmd_str;
if (netns_cmd_.length() == 0) {
return NULL;
}
cmd_str << netns_cmd_ << " destroy";
if (props.instance_id.is_nil() ||
props.vmi_outside.is_nil()) {
return NULL;
}
if (props.interface_count == 2 && props.vmi_inside.is_nil()) {
return NULL;
}
cmd_str << " " << props.ServiceTypeString();
cmd_str << " " << UuidToString(props.instance_id);
cmd_str << " " << UuidToString(props.vmi_inside);
cmd_str << " " << UuidToString(props.vmi_outside);
if (props.service_type == ServiceInstance::LoadBalancer) {
if (props.loadbalancer_id.empty()) {
LOG(ERROR, "loadbalancer id is missing for service instance: "
<< UuidToString(props.instance_id));
return NULL;
}
cmd_str << " --loadbalancer-id " << props.loadbalancer_id;
}
return new InstanceTaskExecvp("NetNS", cmd_str.str(), STOP,
agent_->event_manager());
}
bool NetNSInstanceAdapter::isApplicable(const ServiceInstance::Properties &props) {
return (props.virtualization_type == ServiceInstance::NetworkNamespace);
}