Firewall implements IFirewallService, IOFMessageListener,
IFloodlightModule { protected ArrayList<FirewallRule> readRulesFromStorage() { ArrayList<FirewallRule> l = new ArrayList<FirewallRule>(); try { Map<String, Object> row; IResultSet resultSet = storageSource.executeQuery(TABLE_NAME, ColumnNames, null, null); for (Iterator<IResultSet> it = resultSet.iterator(); it.hasNext();) { row = it.next().getRow(); FirewallRule r = new FirewallRule(); if (!row.containsKey(COLUMN_RULEID) || !row.containsKey(COLUMN_DPID)) { logger.error( "skipping entry with missing required 'ruleid' or 'switchid' entry: {}", row); return l; } try { r.ruleid = Integer .parseInt((String) row.get(COLUMN_RULEID)); r.dpid = DatapathId.of((String) row.get(COLUMN_DPID)); for (String key : row.keySet()) { if (row.get(key) == null) { continue; } if (key.equals(COLUMN_RULEID) || key.equals(COLUMN_DPID) || key.equals("id")) { continue; } else if (key.equals(COLUMN_IN_PORT)) { r.in_port = OFPort.of(Integer.parseInt((String) row.get(COLUMN_IN_PORT))); } else if (key.equals(COLUMN_DL_SRC)) { r.dl_src = MacAddress.of(Long.parseLong((String) row.get(COLUMN_DL_SRC))); } else if (key.equals(COLUMN_DL_DST)) { r.dl_dst = MacAddress.of(Long.parseLong((String) row.get(COLUMN_DL_DST))); } else if (key.equals(COLUMN_DL_TYPE)) { r.dl_type = EthType.of(Integer.parseInt((String) row.get(COLUMN_DL_TYPE))); } else if (key.equals(COLUMN_NW_SRC_PREFIX)) { r.nw_src_prefix_and_mask = IPv4AddressWithMask.of(IPv4Address.of(Integer.parseInt((String) row.get(COLUMN_NW_SRC_PREFIX))), r.nw_src_prefix_and_mask.getMask()); } else if (key.equals(COLUMN_NW_SRC_MASKBITS)) { r.nw_src_prefix_and_mask = IPv4AddressWithMask.of(r.nw_src_prefix_and_mask.getValue(), IPv4Address.of(Integer.parseInt((String) row.get(COLUMN_NW_SRC_MASKBITS)))); } else if (key.equals(COLUMN_NW_DST_PREFIX)) { r.nw_dst_prefix_and_mask = IPv4AddressWithMask.of(IPv4Address.of(Integer.parseInt((String) row.get(COLUMN_NW_DST_PREFIX))), r.nw_dst_prefix_and_mask.getMask()); } else if (key.equals(COLUMN_NW_DST_MASKBITS)) { r.nw_dst_prefix_and_mask = IPv4AddressWithMask.of(r.nw_dst_prefix_and_mask.getValue(), IPv4Address.of(Integer.parseInt((String) row.get(COLUMN_NW_DST_MASKBITS)))); } else if (key.equals(COLUMN_NW_PROTO)) { r.nw_proto = IpProtocol.of(Short.parseShort((String) row.get(COLUMN_NW_PROTO))); } else if (key.equals(COLUMN_TP_SRC)) { r.tp_src = TransportPort.of(Integer.parseInt((String) row.get(COLUMN_TP_SRC))); } else if (key.equals(COLUMN_TP_DST)) { r.tp_dst = TransportPort.of(Integer.parseInt((String) row.get(COLUMN_TP_DST))); } else if (key.equals(COLUMN_WILDCARD_DPID)) { r.any_dpid = Boolean.parseBoolean((String) row.get(COLUMN_WILDCARD_DPID)); } else if (key.equals(COLUMN_WILDCARD_IN_PORT)) { r.any_in_port = Boolean.parseBoolean((String) row.get(COLUMN_WILDCARD_IN_PORT)); } else if (key.equals(COLUMN_WILDCARD_DL_SRC)) { r.any_dl_src = Boolean.parseBoolean((String) row.get(COLUMN_WILDCARD_DL_SRC)); } else if (key.equals(COLUMN_WILDCARD_DL_DST)) { r.any_dl_dst = Boolean.parseBoolean((String) row.get(COLUMN_WILDCARD_DL_DST)); } else if (key.equals(COLUMN_WILDCARD_DL_TYPE)) { r.any_dl_type = Boolean.parseBoolean((String) row.get(COLUMN_WILDCARD_DL_TYPE)); } else if (key.equals(COLUMN_WILDCARD_NW_SRC)) { r.any_nw_src = Boolean.parseBoolean((String) row.get(COLUMN_WILDCARD_NW_SRC)); } else if (key.equals(COLUMN_WILDCARD_NW_DST)) { r.any_nw_dst = Boolean.parseBoolean((String) row.get(COLUMN_WILDCARD_NW_DST)); } else if (key.equals(COLUMN_WILDCARD_NW_PROTO)) { r.any_nw_proto = Boolean.parseBoolean((String) row.get(COLUMN_WILDCARD_NW_PROTO)); } else if (key.equals(COLUMN_PRIORITY)) { r.priority = Integer.parseInt((String) row.get(COLUMN_PRIORITY)); } else if (key.equals(COLUMN_ACTION)) { int tmp = Integer.parseInt((String) row.get(COLUMN_ACTION)); if (tmp == FirewallRule.FirewallAction.DROP.ordinal()) { r.action = FirewallRule.FirewallAction.DROP; } else if (tmp == FirewallRule.FirewallAction.ALLOW.ordinal()) { r.action = FirewallRule.FirewallAction.ALLOW; } else { r.action = null; logger.error("action not recognized"); } } } } catch (ClassCastException e) { logger.error("skipping rule {} with bad data : " + e.getMessage(), r.ruleid); } if (r.action != null) { l.add(r); } } } catch (StorageException e) { logger.error("failed to access storage: {}", e.getMessage()); } Collections.sort(l); return l; }  @Override String getName(); @Override boolean isCallbackOrderingPrereq(OFType type, String name); @Override boolean isCallbackOrderingPostreq(OFType type, String name); @Override Collection<Class<? extends IFloodlightService>> getModuleServices(); @Override Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls(); @Override Collection<Class<? extends IFloodlightService>> getModuleDependencies(); @Override void init(FloodlightModuleContext context); @Override void startUp(FloodlightModuleContext context); @Override Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx); @Override void enableFirewall(boolean enabled); @Override List<FirewallRule> getRules(); @Override List<Map<String, Object>> getStorageRules(); @Override String getSubnetMask(); @Override void setSubnetMask(String newMask); @Override synchronized void addRule(FirewallRule rule); @Override synchronized void deleteRule(int ruleid); Command processPacketInMessage(IOFSwitch sw, OFPacketIn pi, IRoutingDecision decision, FloodlightContext cntx); @Override boolean isEnabled(); static final String TABLE_NAME; static final String COLUMN_RULEID; static final String COLUMN_DPID; static final String COLUMN_IN_PORT; static final String COLUMN_DL_SRC; static final String COLUMN_DL_DST; static final String COLUMN_DL_TYPE; static final String COLUMN_NW_SRC_PREFIX; static final String COLUMN_NW_SRC_MASKBITS; static final String COLUMN_NW_DST_PREFIX; static final String COLUMN_NW_DST_MASKBITS; static final String COLUMN_NW_PROTO; static final String COLUMN_TP_SRC; static final String COLUMN_TP_DST; static final String COLUMN_WILDCARD_DPID; static final String COLUMN_WILDCARD_IN_PORT; static final String COLUMN_WILDCARD_DL_SRC; static final String COLUMN_WILDCARD_DL_DST; static final String COLUMN_WILDCARD_DL_TYPE; static final String COLUMN_WILDCARD_NW_SRC; static final String COLUMN_WILDCARD_NW_DST; static final String COLUMN_WILDCARD_NW_PROTO; static final String COLUMN_WILDCARD_TP_SRC; static final String COLUMN_WILDCARD_TP_DST; static final String COLUMN_PRIORITY; static final String COLUMN_ACTION; static String ColumnNames; }
@Test public void testReadRulesFromStorage() throws Exception { FirewallRule rule = new FirewallRule(); rule.in_port = OFPort.of(2); rule.dl_src = MacAddress.of("00:00:00:00:00:01"); rule.dl_dst = MacAddress.of("00:00:00:00:00:02"); rule.priority = 1; rule.action = FirewallRule.FirewallAction.DROP; firewall.addRule(rule); rule = new FirewallRule(); rule.in_port = OFPort.of(3); rule.dl_src = MacAddress.of("00:00:00:00:00:02"); rule.dl_dst = MacAddress.of("00:00:00:00:00:01"); rule.nw_proto = IpProtocol.TCP; rule.any_nw_proto = false; rule.tp_dst = TransportPort.of(80); rule.priority = 2; rule.action = FirewallRule.FirewallAction.ALLOW; firewall.addRule(rule); List<FirewallRule> rules = firewall.readRulesFromStorage(); FirewallRule r = rules.get(0); assertEquals(r.in_port, OFPort.of(2)); assertEquals(r.priority, 1); assertEquals(r.dl_src, MacAddress.of("00:00:00:00:00:01")); assertEquals(r.dl_dst, MacAddress.of("00:00:00:00:00:02")); assertEquals(r.action, FirewallRule.FirewallAction.DROP); r = rules.get(1); assertEquals(r.in_port, OFPort.of(3)); assertEquals(r.priority, 2); assertEquals(r.dl_src, MacAddress.of("00:00:00:00:00:02")); assertEquals(r.dl_dst, MacAddress.of("00:00:00:00:00:01")); assertEquals(r.nw_proto, IpProtocol.TCP); assertEquals(r.tp_dst, TransportPort.of(80)); assertEquals(r.any_nw_proto, false); assertEquals(r.action, FirewallRule.FirewallAction.ALLOW); }