Open Kilda Java Documentation
FlowVerificationTest.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.atdd;
17 
18 import org.openkilda.LinksUtils;
28 
29 import cucumber.api.java.en.Given;
30 import cucumber.api.java.en.Then;
31 import org.junit.Assert;
32 
33 import java.util.HashMap;
34 
35 public class FlowVerificationTest {
36  private final HashMap<String, FlowPayload> ongoingFlows = new HashMap<>();
37  private final HashMap<String, VerificationOutput> flowVerificationResults = new HashMap<>();
38 
39  @Given("^flow ((?:[0-9a-f]{2})(?::[0-9a-f]{2}){7})\\((\\d+)\\) "
40  + "and ((?:[0-9a-f]{2})(?::[0-9a-f]{2}){7})\\((\\d+)\\) with id=\"([^\"]+)\" is created$")
41  public void flowIsCreated(String sourceId, int sourcePort, String destId, int destPort, String flowId)
42  throws Throwable {
43  String randomFlowId = FlowUtils.getFlowName(flowId);
44  FlowEndpointPayload sourcePoint = new FlowEndpointPayload(new SwitchId(sourceId), sourcePort, 96);
45  FlowEndpointPayload destPoint = new FlowEndpointPayload(new SwitchId(destId), destPort, 112);
46  FlowPayload requestPayload = new FlowPayload(
47  randomFlowId, sourcePoint, destPoint, 1000, false, "ATDD flow", null,
48  FlowState.UP.getState());
49 
50  System.out.println(String.format("==> Send flow CREATE request (%s <--> %s)", sourcePoint, destPoint));
51  FlowPayload response = FlowUtils.putFlow(requestPayload);
52  Assert.assertNotNull(response);
53  response.setLastUpdated(null);
54 
55  System.out.println(String.format("==> Wait till flow become \"UP\" (%s <--> %s)", sourcePoint, destPoint));
57  Assert.assertNotNull(status);
58  Assert.assertEquals(FlowState.UP, status.getStatus());
59 
60  ongoingFlows.put(flowId, response);
61  }
62 
63  @Then("^use flow verification for flow id=\"([^\"]*)\"$")
64  public void useFlowVerificationFor(String flowId) throws Throwable {
65  FlowPayload flow = ongoingFlows.get(flowId);
66 
67  System.out.println(String.format(
68  "==> Send flow VERIFY request (%s <--> %s)", flow.getSource(), flow.getDestination()));
69 
70  VerificationInput payload = new VerificationInput(4 * 1000);
71  VerificationOutput response = FlowUtils.verifyFlow(flow.getId(), payload);
72  Assert.assertNotNull("Verification request failed", response);
73 
74  flowVerificationResults.put(flowId, response);
75  }
76 
77  @Then("^(forward|reverse) flow path is broken$")
78  public void flowChainIsBroken(String direction) {
79  String targetPort = "forward".equals(direction) ? "1" : "2";
80  LinksUtils.islFail("00000002", targetPort);
81  }
82 
83  @Then("^flow verification for flow id=\"([^\"]*)\" is (ok|fail) (ok|fail)$")
85  String flowId, String expectForward, String expectReverse) throws Throwable {
86  VerificationOutput output = flowVerificationResults.get(flowId);
87 
88  dumpVerificationOutput(output);
89 
90  UniFlowVerificationOutput forward = output.getForward();
91  UniFlowVerificationOutput reverse = output.getReverse();
92 
93  Assert.assertEquals(String.format(
94  "Flow verification(forward) status don't match expected status (expect: %s, actual: %s, error: %s)",
95  expectForward, forward.isPingSuccess() ? "ok" : "fail", forward.getError()),
96  "ok".equals(expectForward), forward.isPingSuccess());
97  Assert.assertEquals(String.format(
98  "Flow verification(reverse) status don't match expected status (expect: %s, actual: %s, error: %s)",
99  expectReverse, reverse.isPingSuccess() ? "ok" : "fail", reverse.getError()),
100  "ok".equals(expectReverse), reverse.isPingSuccess());
101  }
102 
103  private void dumpVerificationOutput(VerificationOutput output) {
104  String flowId = output.getFlowId();
105  UniFlowVerificationOutput forward = output.getForward();
106  UniFlowVerificationOutput reverse = output.getReverse();
107 
108  System.out.println(String.format("Flow's %s VERIFICATION forward part response - %s", flowId, forward));
109  System.out.println(String.format("Flow's %s VERIFICATION reverse part response - %s", flowId, reverse));
110  }
111 }
static VerificationOutput verifyFlow(String flowId, VerificationInput payload)
Definition: FlowUtils.java:827
def status()
Definition: rest.py:593
static FlowPayload putFlow(final FlowPayload payload)
Definition: FlowUtils.java:163
void flowVerificationIsSuccessful(String flowId, String expectForward, String expectReverse)
void flowIsCreated(String sourceId, int sourcePort, String destId, int destPort, String flowId)
static FlowIdStatusPayload waitFlowStatus(String flowName, FlowState expected)
Definition: FlowUtils.java:325
static String getFlowName(final String flowId)
Definition: FlowUtils.java:595