Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Sho SHIMIZU | 3a7c98e | 2015-02-25 17:27:04 -0800 | [diff] [blame^] | 16 | package org.onosproject.net.intent.impl.installer; |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 17 | |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 18 | import org.junit.Before; |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 19 | import org.junit.Test; |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 20 | import org.onosproject.net.ConnectPoint; |
| 21 | import org.onosproject.net.DefaultLink; |
| 22 | import org.onosproject.net.DefaultPath; |
| 23 | import org.onosproject.net.Link; |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 24 | import org.onosproject.net.flow.FlowRuleOperation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.intent.AbstractIntentTest; |
| 26 | import org.onosproject.net.intent.Constraint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.intent.IntentTestsMocks; |
| 28 | import org.onosproject.net.intent.PathIntent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
| 30 | import org.onosproject.net.intent.constraint.LambdaConstraint; |
| 31 | import org.onosproject.net.resource.Bandwidth; |
| 32 | import org.onosproject.net.resource.Lambda; |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 33 | |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 34 | import java.util.Arrays; |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 35 | import java.util.Collection; |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 36 | import java.util.List; |
| 37 | |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 38 | import static org.hamcrest.MatcherAssert.assertThat; |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 39 | import static org.hamcrest.Matchers.hasSize; |
| 40 | import static org.hamcrest.Matchers.instanceOf; |
| 41 | import static org.hamcrest.Matchers.notNullValue; |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 42 | import static org.junit.Assert.fail; |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 43 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
| 44 | import static org.onosproject.net.Link.Type.DIRECT; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import static org.onosproject.net.NetTestTools.APP_ID; |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 46 | import static org.onosproject.net.NetTestTools.PID; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 47 | import static org.onosproject.net.NetTestTools.connectPoint; |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 48 | import static org.onosproject.net.intent.IntentTestsMocks.MockResourceService.makeBandwidthResourceService; |
| 49 | import static org.onosproject.net.intent.IntentTestsMocks.MockResourceService.makeLambdaResourceService; |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Unit tests for calculating paths for intents with constraints. |
| 53 | */ |
| 54 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 55 | public class PathConstraintCalculationTest extends AbstractIntentTest { |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 56 | |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 57 | private final IntentTestsMocks.MockSelector selector = new IntentTestsMocks.MockSelector(); |
| 58 | private final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 59 | private final ConnectPoint d1p1 = connectPoint("s1", 0); |
| 60 | private final ConnectPoint d2p0 = connectPoint("s2", 0); |
| 61 | private final ConnectPoint d2p1 = connectPoint("s2", 1); |
| 62 | private final ConnectPoint d3p1 = connectPoint("s3", 1); |
| 63 | private final ConnectPoint d3p0 = connectPoint("s3", 10); |
| 64 | private final ConnectPoint d1p0 = connectPoint("s1", 10); |
| 65 | |
| 66 | private PathIntentInstaller sut; |
| 67 | |
| 68 | @Before |
| 69 | public void setUpIntentInstaller() { |
| 70 | sut = new PathIntentInstaller(); |
| 71 | sut.appId = APP_ID; |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Ray Milkey | 67c221f | 2015-02-25 08:34:55 -0800 | [diff] [blame] | 74 | private PathIntent createPathIntent(List<Link> links, List<Constraint> constraints) { |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 75 | int hops = links.size() - 1; |
Ray Milkey | 67c221f | 2015-02-25 08:34:55 -0800 | [diff] [blame] | 76 | return new PathIntent(APP_ID, selector, treatment, |
| 77 | new DefaultPath(PID, links, hops), constraints); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 81 | * Tests that installation of bandwidth constrained path intents are |
| 82 | * successful. |
| 83 | */ |
| 84 | @Test |
| 85 | public void testInstallBandwidthConstrainedIntentSuccess() { |
| 86 | |
Sho SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 87 | final Constraint constraint = new BandwidthConstraint(Bandwidth.bps(100.0)); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 88 | |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 89 | List<Link> links = Arrays.asList( |
| 90 | createEdgeLink(d1p0, true), |
| 91 | new DefaultLink(PID, d1p1, d2p0, DIRECT), |
| 92 | new DefaultLink(PID, d2p1, d3p1, DIRECT), |
| 93 | createEdgeLink(d3p0, false) |
| 94 | ); |
| 95 | PathIntent installable = createPathIntent(links, Arrays.asList(constraint)); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 96 | |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 97 | sut.resourceService = makeBandwidthResourceService(1000.0); |
| 98 | |
| 99 | final List<Collection<FlowRuleOperation>> flowOperations = sut.install(installable); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 100 | |
| 101 | assertThat(flowOperations, notNullValue()); |
| 102 | assertThat(flowOperations, hasSize(1)); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Tests that installation of bandwidth constrained path intents fail |
| 107 | * if there are no available resources. |
| 108 | */ |
| 109 | @Test |
| 110 | public void testInstallBandwidthConstrainedIntentFailure() { |
| 111 | |
Sho SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 112 | final Constraint constraint = new BandwidthConstraint(Bandwidth.bps(100.0)); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 113 | |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 114 | List<Link> links = Arrays.asList( |
| 115 | createEdgeLink(d1p0, true), |
| 116 | new DefaultLink(PID, d1p1, d2p0, DIRECT), |
| 117 | new DefaultLink(PID, d2p1, d3p1, DIRECT), |
| 118 | createEdgeLink(d3p0, false) |
| 119 | ); |
| 120 | PathIntent installable = createPathIntent(links, Arrays.asList(constraint)); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 121 | |
| 122 | // Make it look like the available bandwidth was consumed |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 123 | final IntentTestsMocks.MockResourceService resourceService = makeBandwidthResourceService(1000.0); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 124 | resourceService.setAvailableBandwidth(1.0); |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 125 | sut.resourceService = resourceService; |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 126 | |
| 127 | try { |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 128 | sut.install(installable); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 129 | fail("Bandwidth request with no available bandwidth did not fail."); |
| 130 | } catch (IntentTestsMocks.MockedAllocationFailure failure) { |
| 131 | assertThat(failure, |
| 132 | instanceOf(IntentTestsMocks.MockedAllocationFailure.class)); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Tests that installation of lambda constrained path intents are |
| 138 | * successful. |
| 139 | */ |
| 140 | @Test |
| 141 | public void testInstallLambdaConstrainedIntentSuccess() { |
| 142 | |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 143 | final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1)); |
| 144 | |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 145 | List<Link> links = Arrays.asList( |
| 146 | createEdgeLink(d1p0, true), |
| 147 | new DefaultLink(PID, d1p1, d2p0, DIRECT), |
| 148 | new DefaultLink(PID, d2p1, d3p1, DIRECT), |
| 149 | createEdgeLink(d3p0, false) |
| 150 | ); |
| 151 | PathIntent installable = createPathIntent(links, Arrays.asList(constraint)); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 152 | |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 153 | sut.resourceService = makeLambdaResourceService(1); |
| 154 | |
| 155 | final List<Collection<FlowRuleOperation>> flowOperations = sut.install(installable); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 156 | |
| 157 | assertThat(flowOperations, notNullValue()); |
| 158 | assertThat(flowOperations, hasSize(1)); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Tests that installation of lambda constrained path intents fail |
| 163 | * if there are no available resources. |
| 164 | */ |
| 165 | @Test |
| 166 | public void testInstallLambdaConstrainedIntentFailure() { |
| 167 | |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 168 | final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1)); |
| 169 | |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 170 | List<Link> links = Arrays.asList( |
| 171 | createEdgeLink(d1p0, true), |
| 172 | new DefaultLink(PID, d1p1, d2p0, DIRECT), |
| 173 | new DefaultLink(PID, d2p1, d3p1, DIRECT), |
| 174 | createEdgeLink(d3p0, false) |
| 175 | ); |
| 176 | PathIntent installable = createPathIntent(links, Arrays.asList(constraint)); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 177 | |
| 178 | // Make it look like the available lambda was consumed |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 179 | final IntentTestsMocks.MockResourceService resourceService = makeLambdaResourceService(1); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 180 | resourceService.setAvailableLambda(0); |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 181 | sut.resourceService = resourceService; |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 182 | |
| 183 | try { |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 184 | sut.install(installable); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 185 | fail("Lambda request with no available lambda did not fail."); |
| 186 | } catch (IntentTestsMocks.MockedAllocationFailure failure) { |
| 187 | assertThat(failure, |
| 188 | instanceOf(IntentTestsMocks.MockedAllocationFailure.class)); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | } |