Brian O'Connor | d087dd2 | 2015-05-21 12:15:29 -0700 | [diff] [blame^] | 1 | package org.onosproject.flowruletest.dispatch; |
| 2 | |
| 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
| 5 | import java.io.BufferedReader; |
| 6 | import java.io.File; |
| 7 | import java.io.FileReader; |
| 8 | import java.io.IOException; |
| 9 | |
| 10 | import org.onosproject.core.ApplicationId; |
| 11 | import org.onosproject.net.DeviceId; |
| 12 | import org.onosproject.net.device.DeviceService; |
| 13 | import org.onosproject.net.flow.DefaultFlowRule; |
| 14 | import org.onosproject.net.flow.FlowRule; |
| 15 | import org.onosproject.net.flow.FlowRuleExtPayLoad; |
| 16 | import org.onosproject.net.flow.FlowRuleService; |
| 17 | import org.slf4j.Logger; |
| 18 | |
| 19 | /** |
| 20 | * third party flow rule test code. |
| 21 | */ |
| 22 | public class FlowRuleTest { |
| 23 | |
| 24 | private final Logger log = getLogger(getClass()); |
| 25 | protected FlowRuleService flowRuleService; |
| 26 | protected DeviceService deviceService; |
| 27 | private ApplicationId appId; |
| 28 | private FlowRule[] flowSet = new DefaultFlowRule[10]; |
| 29 | private DeviceId deviceId; |
| 30 | private static final String FILE_NAME = "/src/main/resource/org/onosproject/flowruletest/resource/flowruletest.txt"; |
| 31 | |
| 32 | /** |
| 33 | * Creates a flow rule test object. |
| 34 | * @param flowRuleService service for injecting flow rules into the environment |
| 35 | * @param deviceService service for interacting with the inventory of infrastructure devices |
| 36 | * @param appId application identifier |
| 37 | */ |
| 38 | public FlowRuleTest(FlowRuleService flowRuleService, |
| 39 | DeviceService deviceService, ApplicationId appId) { |
| 40 | this.flowRuleService = flowRuleService; |
| 41 | this.deviceService = deviceService; |
| 42 | this.deviceId = deviceService.getAvailableDevices().iterator().next().id(); |
| 43 | this.appId = appId; |
| 44 | loadFile(); |
| 45 | } |
| 46 | |
| 47 | private void loadFile() { |
| 48 | String relativelyPath = System.getProperty("user.dir"); |
| 49 | File flowFile = new File(relativelyPath + FILE_NAME); |
| 50 | BufferedReader br = null; |
| 51 | try { |
| 52 | FileReader in = new FileReader(flowFile); |
| 53 | br = new BufferedReader(in); |
| 54 | FlowRule rule = null; |
| 55 | int i = 0; |
| 56 | String flow = ""; |
| 57 | while ((flow = br.readLine()) != null) { |
| 58 | rule = buildFlowRule(flow); |
| 59 | flowSet[i++] = rule; |
| 60 | } |
| 61 | } catch (IOException e) { |
| 62 | log.info("file does not exist."); |
| 63 | } finally { |
| 64 | if (br != null) { |
| 65 | try { |
| 66 | br.close(); |
| 67 | } catch (IOException e) { |
| 68 | log.info("nothing"); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private FlowRule buildFlowRule(String flow) { |
| 75 | FlowRuleExtPayLoad payLoad = FlowRuleExtPayLoad.flowRuleExtPayLoad(flow |
| 76 | .getBytes()); |
| 77 | FlowRule flowRule = new DefaultFlowRule(deviceId, null, null, 0, appId, |
| 78 | 0, false, payLoad); |
| 79 | return flowRule; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Apply flow rules to specific devices. |
| 84 | */ |
| 85 | public void applyFlowRules() { |
| 86 | flowRuleService.applyFlowRules(flowSet); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Remove flow rules from specific devices. |
| 91 | */ |
| 92 | public void removeFlowRules() { |
| 93 | flowRuleService.removeFlowRules(flowSet); |
| 94 | } |
| 95 | |
| 96 | } |