Ray Milkey | bb32048 | 2015-02-25 15:16:46 -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 | */ |
Sho SHIMIZU | 3a7c98e | 2015-02-25 17:27:04 -0800 | [diff] [blame^] | 16 | package org.onosproject.net.intent.impl.installer; |
Ray Milkey | bb32048 | 2015-02-25 15:16:46 -0800 | [diff] [blame] | 17 | |
| 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.onosproject.core.ApplicationId; |
| 21 | import org.onosproject.core.CoreService; |
| 22 | import org.onosproject.core.CoreServiceAdapter; |
| 23 | import org.onosproject.core.IdGenerator; |
| 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.flow.FlowRuleOperation; |
| 27 | import org.onosproject.net.intent.FakeIntentManager; |
| 28 | import org.onosproject.net.intent.Intent; |
| 29 | import org.onosproject.net.intent.IntentInstaller; |
| 30 | import org.onosproject.net.intent.IntentTestsMocks; |
| 31 | import org.onosproject.net.intent.MockIdGenerator; |
| 32 | |
| 33 | import static org.hamcrest.MatcherAssert.assertThat; |
| 34 | import static org.hamcrest.Matchers.equalTo; |
| 35 | import static org.hamcrest.Matchers.is; |
| 36 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 37 | import static org.onosproject.net.NetTestTools.connectPoint; |
| 38 | |
| 39 | /** |
| 40 | * Base class for intent installer tests. |
| 41 | */ |
| 42 | public class IntentInstallerTest { |
| 43 | |
| 44 | /** |
| 45 | * Mock for core service. |
| 46 | */ |
| 47 | static class TestCoreService extends CoreServiceAdapter { |
| 48 | |
| 49 | String registeredId = ""; |
| 50 | |
| 51 | @Override |
| 52 | public ApplicationId registerApplication(String identifier) { |
| 53 | registeredId = identifier; |
| 54 | return APP_ID; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Mock for intent manager service. Checks that the PathIntent |
| 60 | * installer installs and uninstalls properly. |
| 61 | */ |
| 62 | static class MockIntentManager extends FakeIntentManager { |
| 63 | |
| 64 | boolean installerRegistered = false; |
| 65 | final Class expectedClass; |
| 66 | |
| 67 | private MockIntentManager() { |
| 68 | expectedClass = null; |
| 69 | } |
| 70 | |
| 71 | MockIntentManager(Class expectedInstaller) { |
| 72 | this.expectedClass = expectedInstaller; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public <T extends Intent> void registerInstaller( |
| 77 | Class<T> cls, |
| 78 | IntentInstaller<T> installer) { |
| 79 | assertThat(cls, equalTo(expectedClass)); |
| 80 | installerRegistered = true; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public <T extends Intent> void unregisterInstaller(Class<T> cls) { |
| 85 | assertThat(cls, equalTo(expectedClass)); |
| 86 | assertThat(installerRegistered, is(true)); |
| 87 | } |
| 88 | |
| 89 | } |
| 90 | |
| 91 | CoreService testCoreService; |
| 92 | IdGenerator idGenerator = new MockIdGenerator(); |
| 93 | IntentInstaller installer; |
| 94 | |
| 95 | final IntentTestsMocks.MockSelector selector = new IntentTestsMocks.MockSelector(); |
| 96 | final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 97 | final ConnectPoint d1p1 = connectPoint("s1", 0); |
| 98 | final ConnectPoint d2p0 = connectPoint("s2", 0); |
| 99 | final ConnectPoint d2p1 = connectPoint("s2", 1); |
| 100 | final ConnectPoint d3p1 = connectPoint("s3", 1); |
| 101 | final ConnectPoint d3p0 = connectPoint("s3", 10); |
| 102 | final ConnectPoint d1p0 = connectPoint("s1", 10); |
| 103 | |
| 104 | /** |
| 105 | * Configures objects used in all the test cases. |
| 106 | */ |
| 107 | @Before |
| 108 | public void setUp() { |
| 109 | testCoreService = new TestCoreService(); |
| 110 | Intent.bindIdGenerator(idGenerator); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Tears down objects used in all the test cases. |
| 115 | */ |
| 116 | @After |
| 117 | public void tearDown() { |
| 118 | Intent.unbindIdGenerator(idGenerator); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Checks that a flow operation contains the correct values. |
| 123 | * |
| 124 | * @param op flow rule operation to check |
| 125 | * @param type type the flow rule operation should have |
| 126 | * @param deviceId device id the flow rule operation should have |
| 127 | */ |
| 128 | void checkFlowOperation(FlowRuleOperation op, |
| 129 | FlowRuleOperation.Type type, |
| 130 | DeviceId deviceId) { |
| 131 | assertThat(op.type(), is(type)); |
| 132 | assertThat(op.rule().deviceId(), equalTo(deviceId)); |
| 133 | } |
| 134 | |
| 135 | } |