pub fn build_resource_traffic_shaping_controller(
    res: &String,
    rules_of_res: &HashSet<Arc<Rule>>,
    old_res_tcs: &mut Vec<Arc<Controller>>,
) -> Vec<Arc<Controller>> {
    let mut new_res_tcs = Vec::with_capacity(rules_of_res.len());
    for rule in rules_of_res {
        if res != &rule.resource {
            logging::error!("unmatched resource name expect: {}, actual: {}. Unmatched resource name in flow::build_resource_traffic_shaping_controller(), rule: {:?}", res, rule.resource, rule);
            continue;
        }
        let (eq_idx, reuse_stat_idx) = calculate_reuse_index_for(&rule, old_res_tcs);

        // First check equals scenario
        if eq_idx != usize::MAX {
            // reuse the old tc
            let eq_old_tc = Arc::clone(&old_res_tcs[eq_idx]);
            new_res_tcs.push(eq_old_tc);
            // remove old tc from old_res_tcs
            old_res_tcs.remove(eq_idx);
            continue;
        }

        let mut gen_fun_map = GEN_FUN_MAP.read().unwrap();
        let key = ControllerGenKey::new(
            rule.calculate_strategy.clone(),
            rule.control_strategy.clone(),
        );
        let generator = gen_fun_map.get(&key);

        if generator.is_none() {
            logging::error!("[FlowRuleManager build_resource_traffic_shaping_controller] Unsupported flow control strategy. Ignoring the rule due to unsupported control behavior in flow::build_resource_traffic_shaping_controller(), rule: {}",  rule);
            continue;
        }
        let generator = generator.unwrap();

        let tc = {
            if reuse_stat_idx != usize::MAX {
                generator(
                    Arc::clone(&rule),
                    Some(Arc::clone(old_res_tcs[reuse_stat_idx].stat())),
                )
            } else {
                generator(Arc::clone(&rule), None)
            }
        };

        if tc.is_err() {
            logging::error!("[FlowRuleManager build_resource_traffic_shaping_controller] Bad generated traffic controller. Ignoring the rule due to bad generated traffic controller in flow::build_resource_traffic_shaping_controller(), rule: {:?}, error: {:?}", rule, tc);
            continue;
        }
        let tc = tc.unwrap();
        if reuse_stat_idx != usize::MAX {
            // remove old tc from old_res_tcs
            old_res_tcs.remove(reuse_stat_idx);
        }
        new_res_tcs.push(tc);
    }
    new_res_tcs
}
/// build_resource_traffic_shaping_controller builds Controller slice from rules. the resource of rules must be equals to res
build_resource_traffic_shaping_controller builds Controller slice from rules. the resource of rules must be equals to res
build_resource_traffic_shaping_controller builds Controller slice from rules . the resource of rules must be equals to res
pub fn build_resource_traffic_shaping_controller(
    res: &String,
    rules_of_res: &HashSet<Arc<Rule>>,
    old_res_tcs: &mut Vec<Arc<Controller>>,
) -> Vec<Arc<Controller>> {
    let mut new_res_tcs = Vec::with_capacity(rules_of_res.len());
    for rule in rules_of_res {
        if res != &rule.resource {
            logging::error!("unmatched resource name expect: {}, actual: {}. Unmatched resource name in flow::build_resource_traffic_shaping_controller(), rule: {:?}", res, rule.resource, rule);
            continue;
        }
        let (eq_idx, reuse_stat_idx) = calculate_reuse_index_for(&rule, old_res_tcs);
        // First check equals scenario
        if eq_idx != usize::MAX {
            // reuse the old tc
            let eq_old_tc = Arc::clone(&old_res_tcs[eq_idx]);
            new_res_tcs.push(eq_old_tc);
            // remove old tc from old_res_tcs
            old_res_tcs.remove(eq_idx);
            continue;
        }
        let mut gen_fun_map = GEN_FUN_MAP.read().unwrap();
        let key = ControllerGenKey::new(
            rule.calculate_strategy.clone(),
            rule.control_strategy.clone(),
        );
        let generator = gen_fun_map.get(&key);
        if generator.is_none() {
            logging::error!("[FlowRuleManager build_resource_traffic_shaping_controller] Unsupported flow control strategy. Ignoring the rule due to unsupported control behavior in flow::build_resource_traffic_shaping_controller(), rule: {}",  rule);
            continue;
        }
        let generator = generator.unwrap();
        let tc = {
            if reuse_stat_idx != usize::MAX {
                generator(
                    Arc::clone(&rule),
                    Some(Arc::clone(old_res_tcs[reuse_stat_idx].stat())),
                )
            } else {
                generator(Arc::clone(&rule), None)
            }
        };
        if tc.is_err() {
            logging::error!("[FlowRuleManager build_resource_traffic_shaping_controller] Bad generated traffic controller. Ignoring the rule due to bad generated traffic controller in flow::build_resource_traffic_shaping_controller(), rule: {:?}, error: {:?}", rule, tc);
            continue;
        }
        let tc = tc.unwrap();
        if reuse_stat_idx != usize::MAX {
            // remove old tc from old_res_tcs
            old_res_tcs.remove(reuse_stat_idx);
        }
        new_res_tcs.push(tc);
    }
    new_res_tcs
}
pub fn build_resource_traffic_shaping_controller ( res : & String , rules_of_res : & HashSet < Arc < Rule > > , old_res_tcs : & mut Vec < Arc < Controller > > , ) -> Vec < Arc < Controller > > { let mut new_res_tcs = Vec :: with_capacity ( rules_of_res . len ( ) ) ; for rule in rules_of_res { if res != & rule . resource { logging :: error ! ( " " res rule resource rule ) ; continue ; } let ( eq_idx , reuse_stat_idx ) = calculate_reuse_index_for ( & rule , old_res_tcs ) ; // First check equals scenario if eq_idx != usize :: MAX { // reuse the old tc let eq_old_tc = Arc :: clone ( & old_res_tcs [ eq_idx ] ) ; new_res_tcs . push ( eq_old_tc ) ; // remove old tc from old_res_tcs old_res_tcs . remove ( eq_idx ) ; continue ; } let mut gen_fun_map = GEN_FUN_MAP . read ( ) . unwrap ( ) ; let key = ControllerGenKey :: new ( rule . calculate_strategy . clone ( ) , rule . control_strategy . clone ( ) , ) ; let generator = gen_fun_map . get ( & key ) ; if generator . is_none ( ) { logging :: error ! ( " " rule ) ; continue ; } let generator = generator . unwrap ( ) ; let tc = { if reuse_stat_idx != usize :: MAX { generator ( Arc :: clone ( & rule ) , Some ( Arc :: clone ( old_res_tcs [ reuse_stat_idx ] . stat ( ) ) ) , ) } else { generator ( Arc :: clone ( & rule ) , None ) } } ; if tc . is_err ( ) { logging :: error ! ( " " rule tc ) ; continue ; } let tc = tc . unwrap ( ) ; if reuse_stat_idx != usize :: MAX { // remove old tc from old_res_tcs old_res_tcs . remove ( reuse_stat_idx ) ; } new_res_tcs . push ( tc ) ; } new_res_tcs }
build_resource_traffic_shaping_controller builds Controller slice from rules.
build_resource_traffic_shaping_controller builds Controller slice from rules .
res
&String
rules_of_res
&HashSet<Arc<Rule>>
old_res_tcs
&mut Vec<Arc<Controller>>
res
&String
rules_of_res
&HashSet<Arc<Rule>>
old_res_tcs
&mut Vec<Arc<Controller>>