Open Kilda Java Documentation
IslUtils.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.testing.tools;
17 
18 import static org.hamcrest.CoreMatchers.equalTo;
19 import static org.hamcrest.MatcherAssert.assertThat;
20 import static org.hamcrest.Matchers.hasProperty;
21 import static org.hamcrest.core.Every.everyItem;
22 
32 
33 import net.jodah.failsafe.Failsafe;
34 import net.jodah.failsafe.RetryPolicy;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
37 
38 import java.util.Arrays;
39 import java.util.Collections;
40 import java.util.List;
41 import java.util.concurrent.TimeUnit;
42 import java.util.stream.Collectors;
43 
44 @Component
45 public class IslUtils {
46 
47  @Autowired
48  private NorthboundService northbound;
49 
50  @Autowired
51  private ASwitchService aswitchService;
52 
60  public void waitForIslStatus(List<Isl> isls, String expectedStatus, RetryPolicy retryPolicy) {
61  IslChangeType expectedIslState = IslChangeType.valueOf(expectedStatus);
62 
63  List<IslInfoData> actualIsl = Failsafe.with(retryPolicy
64  .retryIf(states -> ((List<IslInfoData>) states).stream()
65  .map(IslInfoData::getState)
66  .anyMatch(state -> !state.equals(expectedIslState))))
67  .get(() -> {
68  List<IslInfoData> allLinks = northbound.getAllLinks();
69  return isls.stream().map(isl -> getIslInfo(allLinks, isl)).collect(Collectors.toList());
70  });
71 
72  assertThat(actualIsl, everyItem(hasProperty("state", equalTo(expectedIslState))));
73  }
74 
75  public void waitForIslStatus(List<Isl> isls, String expectedStatus) {
76  waitForIslStatus(isls, expectedStatus, retryPolicy());
77  }
78 
85  public IslInfoData getIslInfo(Isl isl) {
86  return getIslInfo(northbound.getAllLinks(), isl);
87  }
88 
97  public IslInfoData getIslInfo(List<IslInfoData> islsInfo, Isl isl) {
98  return islsInfo.stream().filter(link -> {
99  PathNode src = link.getPath().get(0);
100  PathNode dst = link.getPath().get(1);
101  return src.getPortNo() == isl.getSrcPort() && dst.getPortNo() == isl.getDstPort()
102  && src.getSwitchId().equals(isl.getSrcSwitch().getDpId())
103  && dst.getSwitchId().equals(isl.getDstSwitch().getDpId());
104  }).findFirst().get();
105  }
106 
112  public Isl reverseIsl(Isl isl) {
113  ASwitch reversedAsw = null;
114  if (isl.getAswitch() != null) {
115  reversedAsw = ASwitch.factory(isl.getAswitch().getOutPort(), isl.getAswitch().getInPort());
116  }
117  return Isl.factory(isl.getDstSwitch(), isl.getDstPort(), isl.getSrcSwitch(),
118  isl.getSrcPort(), isl.getMaxBandwidth(), reversedAsw);
119  }
120 
130  public TopologyDefinition.Isl replug(TopologyDefinition.Isl srcIsl, boolean replugSource,
131  TopologyDefinition.Isl dstIsl, boolean plugIntoSource) {
132  ASwitch srcASwitch = srcIsl.getAswitch();
133  ASwitch dstASwitch = dstIsl.getAswitch();
134  //unplug
135  List<Integer> portsToUnplug = Collections.singletonList(
136  replugSource ? srcASwitch.getInPort() : srcASwitch.getOutPort());
137  aswitchService.portsDown(portsToUnplug);
138 
139  //change flow on aSwitch
140  //delete old flow
141  aswitchService.removeFlows(Arrays.asList(
142  new ASwitchFlow(srcASwitch.getInPort(), srcASwitch.getOutPort()),
143  new ASwitchFlow(srcASwitch.getOutPort(), srcASwitch.getInPort())));
144  //create new flow
145  ASwitchFlow aswFlowForward = new ASwitchFlow(srcASwitch.getInPort(),
146  plugIntoSource ? dstASwitch.getInPort() : dstASwitch.getOutPort());
147  ASwitchFlow aswFlowReverse = new ASwitchFlow(aswFlowForward.getOutPort(), aswFlowForward.getInPort());
148  aswitchService.addFlows(Arrays.asList(aswFlowForward, aswFlowReverse));
149 
150  //plug back
151  aswitchService.portsUp(portsToUnplug);
152 
153  return TopologyDefinition.Isl.factory(
154  replugSource ? (plugIntoSource ? dstIsl.getSrcSwitch() : dstIsl.getDstSwitch()) : srcIsl.getSrcSwitch(),
155  replugSource ? (plugIntoSource ? dstIsl.getSrcPort() : dstIsl.getDstPort()) : srcIsl.getSrcPort(),
156  replugSource ? srcIsl.getDstSwitch() : (plugIntoSource ? dstIsl.getSrcSwitch() : dstIsl.getDstSwitch()),
157  replugSource ? srcIsl.getDstPort() : (plugIntoSource ? dstIsl.getSrcPort() : dstIsl.getDstPort()),
158  0,
160  replugSource ? (plugIntoSource ? dstIsl.getAswitch().getInPort() :
161  dstIsl.getAswitch().getOutPort()) : srcIsl.getAswitch().getInPort(),
162  replugSource ? srcIsl.getAswitch().getOutPort() :
163  (plugIntoSource ? dstIsl.getAswitch().getInPort() : dstIsl.getAswitch().getOutPort())
164  ));
165  }
166 
167  private RetryPolicy retryPolicy() {
168  return new RetryPolicy()
169  .withDelay(3, TimeUnit.SECONDS)
170  .withMaxRetries(15);
171  }
172 }
void waitForIslStatus(List< Isl > isls, String expectedStatus)
Definition: IslUtils.java:75
TopologyDefinition.Isl replug(TopologyDefinition.Isl srcIsl, boolean replugSource, TopologyDefinition.Isl dstIsl, boolean plugIntoSource)
Definition: IslUtils.java:130
IslInfoData getIslInfo(Isl isl)
Definition: IslUtils.java:85
void waitForIslStatus(List< Isl > isls, String expectedStatus, RetryPolicy retryPolicy)
Definition: IslUtils.java:60
IslInfoData getIslInfo(List< IslInfoData > islsInfo, Isl isl)
Definition: IslUtils.java:97
net
Definition: plan-b.py:46