OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
netns_instance_adapter.cc
Go to the documentation of this file.
1 #include <fstream>
2 #include <boost/filesystem.hpp>
5 #include "oper/instance_task.h"
6 #include "agent.h"
7 #include "init/agent_param.h"
8 
10  std::stringstream cmd_str;
11 
12  if (netns_cmd_.length() == 0) {
13  return NULL;
14  }
15  cmd_str << netns_cmd_ << " create";
16 
17  cmd_str << " " << props.ServiceTypeString();
18  cmd_str << " " << UuidToString(props.instance_id);
19  cmd_str << " " << UuidToString(props.vmi_inside);
20  cmd_str << " " << UuidToString(props.vmi_outside);
21 
22  if (props.ip_prefix_len_inside != -1) {
23  cmd_str << " --vmi-left-ip " << props.ip_addr_inside << "/";
24  cmd_str << props.ip_prefix_len_inside;
25  } else {
26  cmd_str << " --vmi-left-ip 0.0.0.0/0";
27  }
28  cmd_str << " --vmi-right-ip " << props.ip_addr_outside << "/";
29  cmd_str << props.ip_prefix_len_outside;
30 
31  if (!props.mac_addr_inside.empty()) {
32  cmd_str << " --vmi-left-mac " << props.mac_addr_inside;
33  } else {
34  cmd_str << " --vmi-left-mac 00:00:00:00:00:00";
35  }
36  cmd_str << " --vmi-right-mac " << props.mac_addr_outside;
37  cmd_str << " --gw-ip " << props.gw_ip;
38 
40  if (props.loadbalancer_id.empty()) {
41  LOG(ERROR, "loadbalancer id is missing for service instance: "
42  << UuidToString(props.instance_id));
43  return NULL;
44  }
45  cmd_str << " --loadbalancer-id " << props.loadbalancer_id;
46  std::stringstream pathgen;
47  pathgen << loadbalancer_config_path_
48  << props.loadbalancer_id << ".conf";
49  const std::string &filename = pathgen.str();
50  std::ofstream fs(filename.c_str());
51  if (fs.fail()) {
52  LOG(ERROR, "File create " << filename << ": " << strerror(errno));
53  return NULL;
54  }
55  const std::vector<autogen::KeyValuePair> &kvps = props.instance_kvps;
56  bool kvp_found = false;
57  std::vector<autogen::KeyValuePair>::const_iterator iter;
58  autogen::KeyValuePair kvp;
59  for (iter = kvps.begin(); iter != kvps.end(); ++iter) {
60  kvp = *iter;
61  /* :::: - Key Value Seperator
62  ::::: - Key Value Pair Seperator */
63  if (kvp_found == true)
64  fs << ":::::";
65  fs << kvp.key << "::::" << kvp.value;
66  kvp_found = true;
67  }
68  if (!agent_->params()->si_lb_ssl_cert_path().empty()) {
69  fs << ":::::" << "lb_ssl_cert_path";
70  fs << "::::" << agent_->params()->si_lb_ssl_cert_path();
71  }
72  if (!agent_->params()->si_lbaas_auth_conf().empty()) {
73  fs << ":::::" << "lbaas_auth_conf";
74  fs << "::::" << agent_->params()->si_lbaas_auth_conf();
75  }
76  fs.close();
77  if (kvp_found == false) {
78  LOG(ERROR, "KeyValuePair is missing for loadbalancer: "
79  << props.loadbalancer_id);
80  return NULL;
81  }
82  cmd_str << " --cfg-file " << pathgen.str();
83  }
84 
85  if (update) {
86  cmd_str << " --update";
87  }
88  LOG(INFO, "Command to be executed: " + cmd_str.str());
89  return new InstanceTaskExecvp("NetNS", cmd_str.str(), START,
91 }
92 
94  std::stringstream cmd_str;
95 
96  if (netns_cmd_.length() == 0) {
97  return NULL;
98  }
99  cmd_str << netns_cmd_ << " destroy";
100 
101  if (props.instance_id.is_nil() ||
102  props.vmi_outside.is_nil()) {
103  return NULL;
104  }
105 
106  if (props.interface_count == 2 && props.vmi_inside.is_nil()) {
107  return NULL;
108  }
109 
110  cmd_str << " " << props.ServiceTypeString();
111  cmd_str << " " << UuidToString(props.instance_id);
112  cmd_str << " " << UuidToString(props.vmi_inside);
113  cmd_str << " " << UuidToString(props.vmi_outside);
115  if (props.loadbalancer_id.empty()) {
116  LOG(ERROR, "loadbalancer id is missing for service instance: "
117  << UuidToString(props.instance_id));
118  return NULL;
119  }
120  cmd_str << " --loadbalancer-id " << props.loadbalancer_id;
121  }
122  LOG(INFO, "Preparing to destroy with command: " + cmd_str.str());
123  return new InstanceTaskExecvp("NetNS", cmd_str.str(), STOP,
124  agent_->event_manager());
125 }
126 
129 }
const std::string & ServiceTypeString() const
std::string si_lb_ssl_cert_path() const
Definition: agent_param.h:277
static std::string UuidToString(const boost::uuids::uuid &id)
Definition: string_util.h:138
InstanceTask * CreateStopTask(const ServiceInstance::Properties &props)
boost::uuids::uuid instance_id
EventManager * event_manager() const
Definition: agent.h:1103
boost::uuids::uuid vmi_outside
std::vector< autogen::KeyValuePair > instance_kvps
AgentParam * params() const
Definition: agent.h:1218
bool isApplicable(const ServiceInstance::Properties &props)
std::string si_lbaas_auth_conf() const
Definition: agent_param.h:280
#define LOG(_Level, _Msg)
Definition: logging.h:33
boost::uuids::uuid vmi_inside
InstanceTask * CreateStartTask(const ServiceInstance::Properties &props, bool update)