Пример #1
0
    def manage_flows(self, tgt_box_name):
        filepath = "./templates/" + tgt_box_name + ".json"
        tgt_tmpl_json = Parser.json_load_file(filepath)
        tgt_box_dict = Parser.json_parse_by_key(tgt_tmpl_json, "box")
        tgt_bridge_list = Parser.json_parse_by_key(tgt_tmpl_json, "bridge")
        tgt_flow_list = Parser.json_parse_by_key(tgt_tmpl_json, "flows")

        tgt_box_ip = tgt_box_dict["ipaddr"]
        tgt_ovsdb_pt = tgt_box_dict["ovsdb_port"]

        for tgt_flow_dict in tgt_flow_list:
            tgt_br_name = tgt_flow_dict["bridge"]

            tgt_bridge_dict = None
            for br in tgt_bridge_list:
                if br["name"] == tgt_br_name:
                    tgt_bridge_dict = br
                    break

            if tgt_bridge_dict is None:
                print "Failed: The bridge " + tgt_br_name + " is not exist."
                print "Failed: Therefore the flow " + tgt_flow_dict["name"] + " can't be created"
                continue

            tgt_ctrl = tgt_bridge_dict ["Controller"]

            if self.is_flow_exists(tgt_flow_dict, tgt_ctrl):
                pass
            self.manage_flow(tgt_flow_dict)


        return
Пример #2
0
    def manage_bridges(self, tgt_box_name):

        filepath = "./templates/" + tgt_box_name + ".json"
        tgt_tmpl_json = Parser.json_load_file(filepath)
        tgt_box_dicts = Parser.json_parse_by_key(tgt_tmpl_json, "box")
        tgt_br_list = Parser.json_parse_by_key(tgt_tmpl_json, "bridges")

        tgt_box_ip = tgt_box_dicts["ipaddr"]
        tgt_ovsdb_pt = tgt_box_dicts["ovsdb_port"]

        ex_all_br_ovsdb_list = Parser.divide_str_to_list_by_keyword(iface.ovs_vsctl_show(tgt_box_ip, tgt_ovsdb_pt),
                                                                    "Bridge")

        # Remove Unrequired field in the "ovs-vsctl show" result
        # (e.g ovs_version: "2.0.2")
        unreq_key = ["ovs_version"]

        for i in ex_all_br_ovsdb_list:
            for j in i:
                if j.split()[0] in unreq_key:
                    idx = i.index(j)
                    del i[idx]

        ex_all_br_dicts = Parser.convert_ovsshow_to_json_dicts(ex_all_br_ovsdb_list)


        # Manage each bridge in the bridge list defined in box's template
        for tgt_br_dict in tgt_br_list:
            print "Bridge !!"
            print tgt_br_dict
            self.manage_bridge(tgt_box_ip, tgt_ovsdb_pt, tgt_br_dict, ex_all_br_dicts)

        return
Пример #3
0
    def manage_tunnel_pair(self, tnl_pair_list):
        # self.prepare_tunnel_manager(tgtname)

        tnl_end1_dict = tnl_pair_list[0]
        tnl_end2_dict = tnl_pair_list[1]

        try:
            end1_filepath = "./templates/" + tnl_end1_dict["boxname"] + ".json"
            end2_filepath = "./templates/" + tnl_end2_dict["boxname"] + ".json"

            # end1_json = parser.json_load_file(end1_filepath)
            # end2_json = parser.json_load_file(end2_filepath)

            end1_box_dict = (Parser.json_load_file(end1_filepath))["box"]
            end2_box_dict = (Parser.json_load_file(end2_filepath))["box"]

            end1_box_ip = end1_box_dict["ipaddr"]
            end2_box_ip = end2_box_dict["ipaddr"]

            end1_box_pt = end1_box_dict["ovsdb_port"]
            end2_box_pt = end2_box_dict["ovsdb_port"]

            end1_all_br_ovsdb_list = Parser.divide_str_to_list_by_keyword(
                iface.ovs_vsctl_show(end1_box_ip, end1_box_pt), "bridge")
            end2_all_br_ovsdb_list = Parser.divide_str_to_list_by_keyword(
                iface.ovs_vsctl_show(end2_box_ip, end2_box_pt), "bridge")

            end1_all_br_dicts = Parser.convert_ovsshow_to_json_dicts(end1_all_br_ovsdb_list)
            end2_all_br_dicts = Parser.convert_ovsshow_to_json_dicts(end2_all_br_ovsdb_list)

            self.manage_tunnel(tnl_end1_dict, end1_all_br_dicts)
            self.manage_tunnel(tnl_end2_dict, end2_all_br_dicts)

            check1 = PingChecker.is_pingable_tunnel(end1_box_ip, tnl_end1_dict["bridge"],
                                                    tnl_end1_dict["options"]["remote_ip"], end1_box_dict["user"])
            check2 = PingChecker.is_pingable_tunnel(end2_box_ip, tnl_end2_dict["bridge"],
                                                    tnl_end2_dict["options"]["remote_ip"], end2_box_dict["user"])

        except IOError as e:
            print e
            return

        except ValueError as e:
            print e
            return

        return
Пример #4
0
    def scan_tunnels(self, tgt_list):
        for tgt_box in tgt_list:
            try:
                file_path = "./templates/" + tgt_box + ".json"
                tgt_tmpl_json = Parser.json_load_file(file_path)
                tgt_tnls_json = tgt_tmpl_json["tunnels"]

            except IOError as e:
                print e
                continue

            except ValueError as e:
                print e
                continue

            for tgt_tnl_json in tgt_tnls_json:
                peer_box_name = tgt_tnl_json["peer_box"]

                peer_box_json = {}
                for i in tgt_list:
                    if i == peer_box_name:
                        peer_file_path = "./templates/" + peer_box_name + ".json"
                        try:
                            peer_tmpl_json = Parser.json_load_file(peer_file_path)
                            peer_tnls_json = peer_tmpl_json["tunnels"]
                            break
                        except IOError as e:
                            print e
                            continue
                        except ValueError as e:
                            print e
                            continue

                if peer_tnls_json is None: continue

                peer_tnl_json = {}
                for tmp_tnl_json in peer_tnls_json:
                    try:

                        if tmp_tnl_json["peer_box"] == tgt_tnl_json["name"] and \
                                        tmp_tnl_json["options"]["local_ip"] == tgt_tnl_json["options"]["remote_ip"] and \
                                        tmp_tnl_json["options"]["remote_ip"] == tgt_tnl_json["options"]["local_ip"]:
                            peer_tnl_json = tmp_tnl_json
                            break
                        else:
                            continue

                    except ValueError as e:
                        print e
                        continue
                if peer_tnl_json is None: continue

                tnl_pair_list = []
                for tnl_pair in self.tnl_pair_lists:
                    if (tnl_pair[0]["name"] == tgt_tnl_json["name"] and tnl_pair[1]["name"] == peer_tnl_json["name"]) or \
                            (tnl_pair[1]["name"] == tgt_tnl_json["name"] and tnl_pair[0]["name"] == peer_tnl_json[
                                "name"]):
                        tnl_pair_list = tnl_pair
                        break

                if tnl_pair_list is not None:
                    continue

                tgt_tnl_json["boxname"] = tgt_tmpl_json["box"]["hostname"]
                peer_tnl_json["boxname"] = peer_tmpl_json["box"]["hostname"]
                tnl_pair_list.append(tgt_tnl_json)
                tnl_pair_list.append(peer_tnl_json)
                self.tnl_pair_lists.append(tnl_pair_list)