Floodlight源码阅读之TreeMap存储ACL(一)

来源:互联网 发布:网络拓扑发现算法 编辑:程序博客网 时间:2024/04/27 21:53

<span style="font-family: 'Microsoft YaHei', Verdana, sans-serif, SimSun; font-size: 13.3333px; line-height: 21.3333px; background-color: rgb(255, 255, 255);">Floodlight 控制器是一个企业级的,使用apache协议的,使用Java开发的OpenFlow控制器</span>

Floodlight的所有ACL的规则都存储在TreeMap中,提供存储和删除接口。ok,下面直接看代码

提供一个保存的方法

@Postpublic String store(String json) {IACLService aclService = (IACLService) getContext().getAttributes().get(IACLService.class.getCanonicalName());ACLRule newRule;try {newRule = jsonToRule(json);} catch (Exception e) {log.error("Error parsing ACL rule: " + json, e);return "{\"status\" : \"Failed! " + e.getMessage() + "\"}";}String status = null;String nw_src = newRule.getNw_src();String nw_dst = newRule.getNw_dst();if (nw_src == null && nw_dst == null){status = "Failed! Either nw_src or nw_dst must be specified.";return ("{\"status\" : \"" + status + "\"}");}if(aclService.addRule(newRule)){status = "Success! New rule added.";}else{status = "Failed! The new ACL rule matches an existing rule.";}return ("{\"status\" : \"" + status + "\"}");}

使用restlet框架,接收Post请求。jsonToRule将json字符串转化为对象,直接看保存方法addRule,在这个方法里面首先对下标加+1,如果判断对象存在就减-1返回false。

通过put方法加入加入到aclRules中。这是aclRules的定义:Map<Integer, ACLRule> aclRules;

@Overridepublic boolean addRule(ACLRule rule) {rule.setId(lastRuleId++);if (checkRuleMatch(rule)) {lastRuleId--;return false;}aclRules.put(rule.getId(), rule);logger.info("ACL rule(id:{}) is added.", rule.getId());if (rule.getAction() != Action.ALLOW) {enforceAddedRule(rule);}return true;}

它同样提供一个删除的方法,首先是while循环遍历元素是否存在,如果存在则删除。

@Deletepublic String remove(String json) {IACLService ACL = (IACLService) getContext().getAttributes().get(IACLService.class.getCanonicalName());ACLRule rule;try {rule = jsonToRule(json);} catch (Exception e) {log.error("Error parsing ACL rule: " + json, e);return "{\"status\" : \"Failed! " + e.getMessage() + "\"}";}// check whether the rule existsboolean exists = false;Iterator<ACLRule> iter = ACL.getRules().iterator();while (iter.hasNext()) {ACLRule r = iter.next();if (r.getId() == rule.getId()) {exists = true;break;}}String status = null;if (!exists) {status = "Failed! a rule with this ID doesn't exist.";log.error(status);} else {ACL.removeRule(rule.getId());status = "Success! Rule deleted";}return ("{\"status\" : \"" + status + "\"}");}

删除的方法如下
@Overridepublic void removeRule(int ruleId) {aclRules.remove(ruleId);logger.info("ACL rule(id:{}) is removed.", ruleId);enforceRemovedRule(ruleId);}
至于enforceRemovedRule这个函数的作用,就是在流表中删除该ACL。


1 0
原创粉丝点击