Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.net.intent.impl.phase; |
| 17 | |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 20 | import org.onlab.graph.ScalarWeight; |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 21 | import org.onosproject.TestApplicationId; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.core.IdGenerator; |
| 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.DefaultLink; |
| 26 | import org.onosproject.net.DefaultPath; |
Ray Milkey | a2cf3a1 | 2018-02-15 16:13:56 -0800 | [diff] [blame^] | 27 | import org.onosproject.net.FilteredConnectPoint; |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 28 | import org.onosproject.net.Link; |
| 29 | import org.onosproject.net.Path; |
| 30 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 31 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 32 | import org.onosproject.net.flow.TrafficSelector; |
| 33 | import org.onosproject.net.flow.TrafficTreatment; |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 34 | import org.onosproject.net.intent.AbstractIntentTest; |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 35 | import org.onosproject.net.intent.IntentCompilationException; |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 36 | import org.onosproject.net.intent.IntentData; |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 37 | import org.onosproject.net.intent.PathIntent; |
| 38 | import org.onosproject.net.intent.PointToPointIntent; |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 39 | import org.onosproject.net.intent.impl.IntentProcessor; |
| 40 | import org.onosproject.net.provider.ProviderId; |
| 41 | import org.onosproject.store.Timestamp; |
| 42 | |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 43 | import java.util.Collections; |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 44 | import java.util.List; |
| 45 | import java.util.Optional; |
| 46 | |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 47 | import static org.easymock.EasyMock.*; |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 48 | import static org.hamcrest.Matchers.instanceOf; |
| 49 | import static org.hamcrest.Matchers.is; |
| 50 | import static org.junit.Assert.assertThat; |
| 51 | import static org.onosproject.net.DeviceId.deviceId; |
| 52 | import static org.onosproject.net.Link.Type.DIRECT; |
| 53 | import static org.onosproject.net.PortNumber.portNumber; |
| 54 | import static org.onosproject.net.intent.IntentState.INSTALL_REQ; |
| 55 | |
| 56 | /** |
| 57 | * Unit tests for Compiling phase. |
| 58 | */ |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 59 | public class CompilingTest extends AbstractIntentTest { |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 60 | |
| 61 | private final ApplicationId appId = new TestApplicationId("test"); |
| 62 | private final ProviderId pid = new ProviderId("of", "test"); |
Brian O'Connor | 6b52813 | 2015-03-10 16:39:52 -0700 | [diff] [blame] | 63 | private final TrafficSelector selector = DefaultTrafficSelector.emptySelector(); |
| 64 | private final TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 65 | private final ConnectPoint cp1 = new ConnectPoint(deviceId("1"), portNumber(1)); |
| 66 | private final ConnectPoint cp2 = new ConnectPoint(deviceId("1"), portNumber(2)); |
| 67 | private final ConnectPoint cp3 = new ConnectPoint(deviceId("2"), portNumber(1)); |
| 68 | private final ConnectPoint cp4 = new ConnectPoint(deviceId("2"), portNumber(2)); |
| 69 | |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 70 | private final List<Link> links = Collections.singletonList( |
| 71 | DefaultLink.builder().providerId(pid).src(cp2).dst(cp4).type(DIRECT).build()); |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 72 | private final Path path = new DefaultPath(pid, links, ScalarWeight.toWeight(10)); |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 73 | |
| 74 | private PointToPointIntent input; |
| 75 | private PathIntent compiled; |
| 76 | |
| 77 | private IdGenerator idGenerator; |
| 78 | private IntentProcessor processor; |
| 79 | private Timestamp version; |
| 80 | |
| 81 | @Before |
| 82 | public void setUp() { |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 83 | super.setUp(); |
| 84 | |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 85 | processor = createMock(IntentProcessor.class); |
| 86 | version = createMock(Timestamp.class); |
| 87 | |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 88 | // Intent creation should be placed after binding an ID generator |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 89 | input = PointToPointIntent.builder() |
| 90 | .appId(appId) |
| 91 | .selector(selector) |
| 92 | .treatment(treatment) |
Ray Milkey | a2cf3a1 | 2018-02-15 16:13:56 -0800 | [diff] [blame^] | 93 | .filteredIngressPoint(new FilteredConnectPoint(cp1)) |
| 94 | .filteredEgressPoint(new FilteredConnectPoint(cp3)) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 95 | .build(); |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 96 | compiled = PathIntent.builder() |
| 97 | .appId(appId) |
| 98 | .selector(selector) |
| 99 | .treatment(treatment) |
| 100 | .path(path) |
| 101 | .build(); |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 104 | /** |
| 105 | * Tests a next phase when no exception occurs. |
| 106 | */ |
| 107 | @Test |
| 108 | public void testMoveToNextPhaseWithoutError() { |
| 109 | IntentData pending = new IntentData(input, INSTALL_REQ, version); |
| 110 | |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 111 | expect(processor.compile(input, null)).andReturn(Collections.singletonList(compiled)); |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 112 | replay(processor); |
| 113 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 114 | Compiling sut = new Compiling(processor, pending, Optional.empty()); |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 115 | |
| 116 | Optional<IntentProcessPhase> output = sut.execute(); |
| 117 | |
| 118 | verify(processor); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 119 | assertThat(output.get(), is(instanceOf(Installing.class))); |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Tests a next phase when IntentCompilationException occurs. |
| 124 | */ |
| 125 | @Test |
| 126 | public void testWhenIntentCompilationExceptionOccurs() { |
| 127 | IntentData pending = new IntentData(input, INSTALL_REQ, version); |
| 128 | |
| 129 | expect(processor.compile(input, null)).andThrow(new IntentCompilationException()); |
| 130 | replay(processor); |
| 131 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 132 | Compiling sut = new Compiling(processor, pending, Optional.empty()); |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 133 | |
| 134 | Optional<IntentProcessPhase> output = sut.execute(); |
| 135 | |
| 136 | verify(processor); |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 137 | assertThat(output.get(), is(instanceOf(Failed.class))); |
Sho SHIMIZU | 892509a | 2015-02-24 14:44:31 -0800 | [diff] [blame] | 138 | } |
| 139 | } |