Sho SHIMIZU | 0a3bb1d9 | 2015-02-24 15:24:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 16 | package org.onosproject.net.intent.impl.phase; |
| 17 | |
| 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 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; |
| 27 | import org.onosproject.net.Link; |
| 28 | import org.onosproject.net.Path; |
| 29 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 30 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Sho SHIMIZU | 0a3bb1d9 | 2015-02-24 15:24:58 -0800 | [diff] [blame] | 31 | import org.onosproject.net.flow.TrafficSelector; |
| 32 | import org.onosproject.net.flow.TrafficTreatment; |
| 33 | import org.onosproject.net.intent.Intent; |
| 34 | import org.onosproject.net.intent.IntentData; |
| 35 | import org.onosproject.net.intent.MockIdGenerator; |
| 36 | import org.onosproject.net.intent.PathIntent; |
| 37 | import org.onosproject.net.intent.PointToPointIntent; |
| 38 | import org.onosproject.net.intent.impl.IntentInstallationException; |
| 39 | import org.onosproject.net.intent.impl.IntentProcessor; |
| 40 | import org.onosproject.net.provider.ProviderId; |
| 41 | import org.onosproject.store.Timestamp; |
| 42 | |
| 43 | import java.util.Arrays; |
| 44 | import java.util.List; |
| 45 | import java.util.Optional; |
| 46 | |
| 47 | import static org.easymock.EasyMock.createMock; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 48 | import static org.easymock.EasyMock.expectLastCall; |
Sho SHIMIZU | 0a3bb1d9 | 2015-02-24 15:24:58 -0800 | [diff] [blame] | 49 | import static org.easymock.EasyMock.replay; |
| 50 | import static org.easymock.EasyMock.verify; |
| 51 | import static org.hamcrest.Matchers.instanceOf; |
| 52 | import static org.hamcrest.Matchers.is; |
| 53 | import static org.junit.Assert.assertThat; |
| 54 | import static org.onosproject.net.DeviceId.deviceId; |
| 55 | import static org.onosproject.net.Link.Type.DIRECT; |
| 56 | import static org.onosproject.net.PortNumber.portNumber; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 57 | import static org.onosproject.net.intent.IntentState.INSTALLED; |
Sho SHIMIZU | 0a3bb1d9 | 2015-02-24 15:24:58 -0800 | [diff] [blame] | 58 | import static org.onosproject.net.intent.IntentState.INSTALL_REQ; |
| 59 | |
| 60 | /** |
| 61 | * Unit tests for InstallingCoordinating phase. |
| 62 | */ |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 63 | public class ReplacingTest { |
Sho SHIMIZU | 0a3bb1d9 | 2015-02-24 15:24:58 -0800 | [diff] [blame] | 64 | |
| 65 | private final ApplicationId appId = new TestApplicationId("test"); |
| 66 | private final ProviderId pid = new ProviderId("of", "test"); |
Brian O'Connor | 6b52813 | 2015-03-10 16:39:52 -0700 | [diff] [blame] | 67 | private final TrafficSelector selector = DefaultTrafficSelector.emptySelector(); |
| 68 | private final TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
Sho SHIMIZU | 0a3bb1d9 | 2015-02-24 15:24:58 -0800 | [diff] [blame] | 69 | private final ConnectPoint cp1 = new ConnectPoint(deviceId("1"), portNumber(1)); |
| 70 | private final ConnectPoint cp2 = new ConnectPoint(deviceId("1"), portNumber(2)); |
| 71 | private final ConnectPoint cp3 = new ConnectPoint(deviceId("2"), portNumber(1)); |
| 72 | private final ConnectPoint cp4 = new ConnectPoint(deviceId("2"), portNumber(2)); |
| 73 | |
| 74 | private final List<Link> links = Arrays.asList(new DefaultLink(pid, cp2, cp4, DIRECT)); |
| 75 | private final Path path = new DefaultPath(pid, links, 10); |
| 76 | |
| 77 | private PointToPointIntent input; |
| 78 | private PathIntent compiled; |
| 79 | |
| 80 | private IdGenerator idGenerator; |
| 81 | private IntentProcessor processor; |
| 82 | private Timestamp version; |
| 83 | |
| 84 | @Before |
| 85 | public void setUp() { |
| 86 | processor = createMock(IntentProcessor.class); |
| 87 | version = createMock(Timestamp.class); |
| 88 | |
| 89 | idGenerator = new MockIdGenerator(); |
| 90 | |
| 91 | Intent.bindIdGenerator(idGenerator); |
| 92 | |
| 93 | // Intent creation should be placed after binding an ID generator |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 94 | input = PointToPointIntent.builder() |
| 95 | .appId(appId) |
| 96 | .selector(selector) |
| 97 | .treatment(treatment) |
| 98 | .ingressPoint(cp1) |
| 99 | .egressPoint(cp3) |
| 100 | .build(); |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 101 | compiled = PathIntent.builder() |
| 102 | .appId(appId) |
| 103 | .selector(selector) |
| 104 | .treatment(treatment) |
| 105 | .path(path) |
| 106 | .build(); |
Sho SHIMIZU | 0a3bb1d9 | 2015-02-24 15:24:58 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
| 110 | @After |
| 111 | public void tearDown() { |
| 112 | Intent.unbindIdGenerator(idGenerator); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Tests a next phase when no exception occurs. |
| 117 | */ |
| 118 | @Test |
| 119 | public void testMoveToNextPhaseWithoutError() { |
| 120 | IntentData pending = new IntentData(input, INSTALL_REQ, version); |
| 121 | pending.setInstallables(Arrays.asList(compiled)); |
| 122 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 123 | IntentData current = new IntentData(input, INSTALLED, version); |
| 124 | current.setInstallables(Arrays.asList(compiled)); |
Sho SHIMIZU | 0a3bb1d9 | 2015-02-24 15:24:58 -0800 | [diff] [blame] | 125 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 126 | processor.uninstall(current); |
Sho SHIMIZU | 0a3bb1d9 | 2015-02-24 15:24:58 -0800 | [diff] [blame] | 127 | replay(processor); |
| 128 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 129 | Replacing sut = new Replacing(processor, pending, current); |
Sho SHIMIZU | 0a3bb1d9 | 2015-02-24 15:24:58 -0800 | [diff] [blame] | 130 | |
| 131 | Optional<IntentProcessPhase> executed = sut.execute(); |
| 132 | |
| 133 | verify(processor); |
| 134 | assertThat(executed.get(), is(instanceOf(Installing.class))); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Tests a next phase when IntentInstallationException occurs. |
| 139 | */ |
| 140 | @Test |
| 141 | public void testWhenIntentInstallExceptionOccurs() { |
| 142 | IntentData pending = new IntentData(input, INSTALL_REQ, version); |
| 143 | pending.setInstallables(Arrays.asList(compiled)); |
| 144 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 145 | IntentData current = new IntentData(input, INSTALLED, version); |
| 146 | |
| 147 | processor.uninstall(current); |
| 148 | expectLastCall().andThrow(new IntentInstallationException()); |
Sho SHIMIZU | 0a3bb1d9 | 2015-02-24 15:24:58 -0800 | [diff] [blame] | 149 | replay(processor); |
| 150 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 151 | Replacing sut = new Replacing(processor, pending, current); |
Sho SHIMIZU | 0a3bb1d9 | 2015-02-24 15:24:58 -0800 | [diff] [blame] | 152 | |
| 153 | Optional<IntentProcessPhase> executed = sut.execute(); |
| 154 | |
| 155 | verify(processor); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 156 | assertThat(executed.get(), is(instanceOf(ReplaceFailed.class))); |
Sho SHIMIZU | 0a3bb1d9 | 2015-02-24 15:24:58 -0800 | [diff] [blame] | 157 | } |
| 158 | } |