diff options
author | CNlucius <lukai1@huawei.com> | 2016-09-13 11:40:12 +0800 |
---|---|---|
committer | CNlucius <lukai1@huawei.com> | 2016-09-13 11:41:53 +0800 |
commit | b731e2f1dd0972409b136aebc7b463dd72c9cfad (patch) | |
tree | 5107d7d80c19ad8076c2c97c2b5ef8d1cf3ab903 /framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl | |
parent | ee93993458266114c29271a481ef9ce7ce621b2a (diff) |
ONOSFW-171
O/S-SFC-ONOS scenario documentation
Change-Id: I51ae1cf736ea24ab6680f8edca1b2bf5dd598365
Signed-off-by: CNlucius <lukai1@huawei.com>
Diffstat (limited to 'framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl')
16 files changed, 0 insertions, 3648 deletions
diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/IntentAccumulatorTest.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/IntentAccumulatorTest.java deleted file mode 100644 index 219d2fd5..00000000 --- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/IntentAccumulatorTest.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.net.intent.impl; - -import java.util.Collection; -import java.util.List; - -import org.hamcrest.Description; -import org.hamcrest.TypeSafeDiagnosingMatcher; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onosproject.core.IdGenerator; -import org.onosproject.net.intent.Intent; -import org.onosproject.net.intent.IntentBatchDelegate; -import org.onosproject.net.intent.IntentData; -import org.onosproject.net.intent.IntentState; -import org.onosproject.net.intent.IntentTestsMocks.MockIntent; -import org.onosproject.net.intent.IntentTestsMocks.MockTimestamp; -import org.onosproject.net.intent.MockIdGenerator; - -import com.google.common.collect.ImmutableList; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasSize; - -/** - * Unit tests for the intent accumulator. - */ -public class IntentAccumulatorTest { - - Intent intent1; - Intent intent2; - Intent intent3; - IdGenerator mockGenerator; - - private static IntentDataMatcher containsIntent(Intent intent) { - return new IntentDataMatcher(intent); - } - - /** - * Creates mock intents used by the test. - */ - @Before - public void localSetup() { - mockGenerator = new MockIdGenerator(); - Intent.bindIdGenerator(mockGenerator); - - intent1 = new MockIntent(1L); - intent2 = new MockIntent(2L); - intent3 = new MockIntent(3L); - } - - /** - * Removes id generator from the Intent class. - */ - @After - public void localTearDown() { - Intent.unbindIdGenerator(mockGenerator); - } - - /** - * Hamcrest matcher to check that a collection of intent data objects - * contains an entry for a given intent. - */ - private static final class IntentDataMatcher - extends TypeSafeDiagnosingMatcher<Collection<IntentData>> { - - final Intent intent; - - public IntentDataMatcher(Intent intent) { - this.intent = intent; - } - - /** - * Check that the given collection of intent data contains a specific - * intent. - * - * @param operations collection of intent data - * @param description description - * @return true if the collection contains the intent, false otherwise. - */ - public boolean matchesSafely(Collection<IntentData> operations, - Description description) { - for (IntentData operation : operations) { - if (operation.key().equals(intent.key())) { - if (operation.state() != IntentState.INSTALLED) { - description.appendText("state was " + operation.state()); - return false; - } - return true; - } - } - description.appendText("key was not found " + intent.key()); - return false; - } - - @Override - public void describeTo(Description description) { - description.appendText("INSTALLED state intent with key " + intent.key()); - } - } - - /** - * Mock batch delegate class. Gets calls from the accumulator and checks - * that the operations have been properly compressed. - */ - private class MockIntentBatchDelegate - implements IntentBatchDelegate { - public void execute(Collection<IntentData> operations) { - assertThat(operations, hasSize(3)); - assertThat(operations, containsIntent(intent1)); - assertThat(operations, containsIntent(intent2)); - assertThat(operations, containsIntent(intent3)); - } - } - - /** - * Tests that the accumulator properly compresses operations on the same - * intents. - */ - @Test - public void checkAccumulator() { - - MockIntentBatchDelegate delegate = new MockIntentBatchDelegate(); - IntentAccumulator accumulator = new IntentAccumulator(delegate); - - List<IntentData> intentDataItems = ImmutableList.of( - new IntentData(intent1, IntentState.INSTALLING, - new MockTimestamp(1)), - new IntentData(intent2, IntentState.INSTALLING, - new MockTimestamp(1)), - new IntentData(intent3, IntentState.INSTALLED, - new MockTimestamp(1)), - new IntentData(intent2, IntentState.INSTALLED, - new MockTimestamp(1)), - new IntentData(intent2, IntentState.INSTALLED, - new MockTimestamp(1)), - new IntentData(intent1, IntentState.INSTALLED, - new MockTimestamp(1))); - - - accumulator.processItems(intentDataItems); - } - - -} diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/IntentCleanupTest.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/IntentCleanupTest.java deleted file mode 100644 index 0f6ce67c..00000000 --- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/IntentCleanupTest.java +++ /dev/null @@ -1,261 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.net.intent.impl; - -import com.google.common.collect.Sets; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onosproject.cfg.ComponentConfigAdapter; -import org.onosproject.core.IdGenerator; -import org.onosproject.net.intent.Intent; -import org.onosproject.net.intent.IntentData; -import org.onosproject.net.intent.IntentEvent; -import org.onosproject.net.intent.IntentServiceAdapter; -import org.onosproject.net.intent.IntentStore; -import org.onosproject.net.intent.IntentStoreDelegate; -import org.onosproject.net.intent.MockIdGenerator; -import org.onosproject.store.Timestamp; -import org.onosproject.store.trivial.SimpleIntentStore; -import org.onosproject.store.trivial.SystemClockTimestamp; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.onosproject.net.intent.IntentState.*; -import static org.onosproject.net.intent.IntentTestsMocks.MockIntent; - -/** - * Test intent cleanup. - */ -public class IntentCleanupTest { - - private IntentCleanup cleanup; - private MockIntentService service; - private IntentStore store; - protected IdGenerator idGenerator; // global or one per test? per test for now. - - private static class MockIntentService extends IntentServiceAdapter { - - private int submitCounter = 0; - - @Override - public void submit(Intent intent) { - submitCounter++; - } - - public int submitCounter() { - return submitCounter; - } - } - - @Before - public void setUp() { - service = new MockIntentService(); - store = new SimpleIntentStore(); - cleanup = new IntentCleanup(); - idGenerator = new MockIdGenerator(); - - cleanup.cfgService = new ComponentConfigAdapter(); - cleanup.service = service; - cleanup.store = store; - cleanup.period = 10; - cleanup.retryThreshold = 3; - cleanup.activate(); - - assertTrue("store should be empty", - Sets.newHashSet(cleanup.store.getIntents()).isEmpty()); - - Intent.bindIdGenerator(idGenerator); - } - - @After - public void tearDown() { - cleanup.deactivate(); - - Intent.unbindIdGenerator(idGenerator); - } - - /** - * Trigger resubmit of intent in CORRUPT during periodic poll. - */ - @Test - public void corruptPoll() { - IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { - @Override - public void process(IntentData intentData) { - intentData.setState(CORRUPT); - store.write(intentData); - } - - @Override - public void notify(IntentEvent event) {} - }; - store.setDelegate(mockDelegate); - - Intent intent = new MockIntent(1L); - Timestamp version = new SystemClockTimestamp(1L); - IntentData data = new IntentData(intent, INSTALL_REQ, version); - store.addPending(data); - - cleanup.run(); //FIXME broken? - assertEquals("Expect number of submits incorrect", - 1, service.submitCounter()); - } - - /** - * Trigger resubmit of intent in INSTALL_REQ for too long. - */ - @Test - public void pendingPoll() { - IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { - @Override - public void process(IntentData intentData) {} - - @Override - public void notify(IntentEvent event) { - cleanup.event(event); - } - }; - store.setDelegate(mockDelegate); - - Intent intent = new MockIntent(1L); - Timestamp version = new SystemClockTimestamp(1L); - IntentData data = new IntentData(intent, INSTALL_REQ, version); - store.addPending(data); - - cleanup.run(); - assertEquals("Expect number of submits incorrect", - 1, service.submitCounter()); - - } - - /** - * Trigger resubmit of intent in INSTALLING for too long. - */ - @Test - public void installingPoll() { - IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { - @Override - public void process(IntentData intentData) { - intentData.setState(INSTALLING); - store.write(intentData); - } - - @Override - public void notify(IntentEvent event) { - cleanup.event(event); - } - }; - store.setDelegate(mockDelegate); - - Intent intent = new MockIntent(1L); - Timestamp version = new SystemClockTimestamp(1L); - IntentData data = new IntentData(intent, INSTALL_REQ, version); - store.addPending(data); - - cleanup.run(); - assertEquals("Expect number of submits incorrect", - 1, service.submitCounter()); - - } - - /** - * Only submit one of two intents because one is too new. - */ - @Test - public void skipPoll() { - IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { - @Override - public void process(IntentData intentData) { - intentData.setState(CORRUPT); - store.write(intentData); - } - - @Override - public void notify(IntentEvent event) {} - }; - store.setDelegate(mockDelegate); - - Intent intent = new MockIntent(1L); - IntentData data = new IntentData(intent, INSTALL_REQ, null); - store.addPending(data); - - Intent intent2 = new MockIntent(2L); - Timestamp version = new SystemClockTimestamp(1L); - data = new IntentData(intent2, INSTALL_REQ, version); - store.addPending(data); - - cleanup.run(); - assertEquals("Expect number of submits incorrect", - 1, service.submitCounter()); - } - - /** - * Verify resubmit in response to CORRUPT event. - */ - @Test - public void corruptEvent() { - IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { - @Override - public void process(IntentData intentData) { - intentData.setState(CORRUPT); - store.write(intentData); - } - - @Override - public void notify(IntentEvent event) { - cleanup.event(event); - } - }; - store.setDelegate(mockDelegate); - - - Intent intent = new MockIntent(1L); - IntentData data = new IntentData(intent, INSTALL_REQ, null); - - store.addPending(data); - assertEquals("Expect number of submits incorrect", - 1, service.submitCounter()); - } - - /** - * Intent should not be retried because threshold is reached. - */ - @Test - public void corruptEventThreshold() { - IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { - @Override - public void process(IntentData intentData) { - intentData.setState(CORRUPT); - intentData.setErrorCount(cleanup.retryThreshold); - store.write(intentData); - } - - @Override - public void notify(IntentEvent event) { - cleanup.event(event); - } - }; - store.setDelegate(mockDelegate); - - Intent intent = new MockIntent(1L); - IntentData data = new IntentData(intent, INSTALL_REQ, null); - - store.addPending(data); - assertEquals("Expect number of submits incorrect", - 0, service.submitCounter()); - } -}
\ No newline at end of file diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/IntentCleanupTestMock.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/IntentCleanupTestMock.java deleted file mode 100644 index 15ee24e6..00000000 --- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/IntentCleanupTestMock.java +++ /dev/null @@ -1,285 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.net.intent.impl; - -import com.google.common.collect.Sets; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onosproject.cfg.ComponentConfigAdapter; -import org.onosproject.core.IdGenerator; -import org.onosproject.net.intent.Intent; -import org.onosproject.net.intent.IntentData; -import org.onosproject.net.intent.IntentEvent; -import org.onosproject.net.intent.IntentService; -import org.onosproject.net.intent.IntentStore; -import org.onosproject.net.intent.IntentStoreDelegate; -import org.onosproject.net.intent.MockIdGenerator; -import org.onosproject.store.Timestamp; -import org.onosproject.store.trivial.SimpleIntentStore; -import org.onosproject.store.trivial.SystemClockTimestamp; - -import static org.easymock.EasyMock.*; -import static org.junit.Assert.assertTrue; -import static org.onosproject.net.intent.IntentState.*; -import static org.onosproject.net.intent.IntentTestsMocks.MockIntent; - -/** - * Test intent cleanup using Mocks. - * FIXME remove this or IntentCleanupTest - */ -public class IntentCleanupTestMock { - - private IntentCleanup cleanup; - private IntentService service; - private IntentStore store; - protected IdGenerator idGenerator; // global or one per test? per test for now. - - @Before - public void setUp() { - service = createMock(IntentService.class); - store = new SimpleIntentStore(); - cleanup = new IntentCleanup(); - idGenerator = new MockIdGenerator(); - - service.addListener(cleanup); - expectLastCall().once(); - replay(service); - - cleanup.cfgService = new ComponentConfigAdapter(); - cleanup.service = service; - cleanup.store = store; - cleanup.period = 1000; - cleanup.retryThreshold = 3; - cleanup.activate(); - - verify(service); - reset(service); - - assertTrue("store should be empty", - Sets.newHashSet(cleanup.store.getIntents()).isEmpty()); - - Intent.bindIdGenerator(idGenerator); - } - - @After - public void tearDown() { - service.removeListener(cleanup); - expectLastCall().once(); - replay(service); - - cleanup.deactivate(); - - verify(service); - reset(service); - - Intent.unbindIdGenerator(idGenerator); - } - - /** - * Trigger resubmit of intent in CORRUPT during periodic poll. - */ - @Test - public void corruptPoll() { - IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { - @Override - public void process(IntentData intentData) { - intentData.setState(CORRUPT); - store.write(intentData); - } - - @Override - public void notify(IntentEvent event) {} - }; - store.setDelegate(mockDelegate); - - Intent intent = new MockIntent(1L); - Timestamp version = new SystemClockTimestamp(1L); - IntentData data = new IntentData(intent, INSTALL_REQ, version); - store.addPending(data); - - service.submit(intent); - expectLastCall().once(); - replay(service); - - cleanup.run(); //FIXME broken? - verify(service); - reset(service); - } - - /** - * Trigger resubmit of intent in INSTALL_REQ for too long. - */ - @Test - public void pendingPoll() { - IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { - @Override - public void process(IntentData intentData) {} - - @Override - public void notify(IntentEvent event) { - cleanup.event(event); - } - }; - store.setDelegate(mockDelegate); - - Intent intent = new MockIntent(1L); - Timestamp version = new SystemClockTimestamp(1L); - IntentData data = new IntentData(intent, INSTALL_REQ, version); - store.addPending(data); - - service.submit(intent); - expectLastCall().once(); - replay(service); - - cleanup.run(); - verify(service); - reset(service); - } - - /** - * Trigger resubmit of intent in INSTALLING for too long. - */ - @Test - public void installingPoll() { - IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { - @Override - public void process(IntentData intentData) { - intentData.setState(INSTALLING); - store.write(intentData); - } - - @Override - public void notify(IntentEvent event) { - cleanup.event(event); - } - }; - store.setDelegate(mockDelegate); - - Intent intent = new MockIntent(1L); - Timestamp version = new SystemClockTimestamp(1L); - IntentData data = new IntentData(intent, INSTALL_REQ, version); - store.addPending(data); - - service.submit(intent); - expectLastCall().once(); - replay(service); - - cleanup.run(); - verify(service); - reset(service); - } - - /** - * Only submit one of two intents because one is too new. - */ - @Test - public void skipPoll() { - IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { - @Override - public void process(IntentData intentData) { - intentData.setState(CORRUPT); - store.write(intentData); - } - - @Override - public void notify(IntentEvent event) {} - }; - store.setDelegate(mockDelegate); - - Intent intent = new MockIntent(1L); - IntentData data = new IntentData(intent, INSTALL_REQ, null); - store.addPending(data); - - Intent intent2 = new MockIntent(2L); - Timestamp version = new SystemClockTimestamp(1L); - data = new IntentData(intent2, INSTALL_REQ, version); - store.addPending(data); - - service.submit(intent2); - expectLastCall().once(); - replay(service); - - cleanup.run(); - verify(service); - reset(service); - } - - /** - * Verify resubmit in response to CORRUPT event. - */ - @Test - public void corruptEvent() { - IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { - @Override - public void process(IntentData intentData) { - intentData.setState(CORRUPT); - store.write(intentData); - } - - @Override - public void notify(IntentEvent event) { - cleanup.event(event); - } - }; - store.setDelegate(mockDelegate); - - - Intent intent = new MockIntent(1L); - IntentData data = new IntentData(intent, INSTALL_REQ, null); - - service.submit(intent); - expectLastCall().once(); - replay(service); - - store.addPending(data); - - verify(service); - reset(service); - } - - /** - * Intent should not be retried because threshold is reached. - */ - @Test - public void corruptEventThreshold() { - IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { - @Override - public void process(IntentData intentData) { - intentData.setState(CORRUPT); - intentData.setErrorCount(cleanup.retryThreshold); - store.write(intentData); - } - - @Override - public void notify(IntentEvent event) { - cleanup.event(event); - } - }; - store.setDelegate(mockDelegate); - - - Intent intent = new MockIntent(1L); - IntentData data = new IntentData(intent, INSTALL_REQ, null); - - replay(service); - - store.addPending(data); - - verify(service); - reset(service); - } -}
\ No newline at end of file diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/IntentManagerTest.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/IntentManagerTest.java deleted file mode 100644 index 3f40de09..00000000 --- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/IntentManagerTest.java +++ /dev/null @@ -1,672 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.net.intent.impl; - -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Multimap; -import com.google.common.collect.Sets; -import org.hamcrest.Description; -import org.hamcrest.TypeSafeMatcher; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.onosproject.TestApplicationId; -import org.onosproject.cfg.ComponentConfigAdapter; -import org.onosproject.common.event.impl.TestEventDispatcher; -import org.onosproject.core.ApplicationId; -import org.onosproject.core.impl.TestCoreManager; -import org.onosproject.net.NetworkResource; -import org.onosproject.net.intent.FlowRuleIntent; -import org.onosproject.net.intent.Intent; -import org.onosproject.net.intent.IntentCompiler; -import org.onosproject.net.intent.IntentData; -import org.onosproject.net.intent.IntentEvent; -import org.onosproject.net.intent.IntentEvent.Type; -import org.onosproject.net.intent.IntentExtensionService; -import org.onosproject.net.intent.IntentId; -import org.onosproject.net.intent.IntentListener; -import org.onosproject.net.intent.IntentService; -import org.onosproject.net.intent.IntentState; -import org.onosproject.net.intent.Key; -import org.onosproject.net.resource.link.LinkResourceAllocations; -import org.onosproject.store.trivial.SimpleIntentStore; - -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.*; -import static org.onlab.junit.TestTools.assertAfter; -import static org.onlab.util.Tools.delay; -import static org.onosproject.net.NetTestTools.injectEventDispatcher; -import static org.onosproject.net.intent.IntentState.*; -import static org.onosproject.net.intent.IntentTestsMocks.MockFlowRule; -import static org.onosproject.net.intent.IntentTestsMocks.MockIntent; - -/** - * Test intent manager and transitions. - * - * TODO implement the following tests: - * - {submit, withdraw, update, replace} intent - * - {submit, update, recompiling} intent with failed compilation - * - failed reservation - * - push timeout recovery - * - failed items recovery - * - * in general, verify intents store, flow store, and work queue - */ - -public class IntentManagerTest { - - private static final int SUBMIT_TIMEOUT_MS = 1000; - private static final ApplicationId APPID = new TestApplicationId("manager-test"); - - private IntentManager manager; - private MockFlowRuleService flowRuleService; - - protected IntentService service; - protected IntentExtensionService extensionService; - protected TestListener listener = new TestListener(); - protected TestIntentCompiler compiler = new TestIntentCompiler(); - - private static class TestListener implements IntentListener { - final Multimap<IntentEvent.Type, IntentEvent> events = HashMultimap.create(); - Map<IntentEvent.Type, CountDownLatch> latchMap = Maps.newHashMap(); - - @Override - public void event(IntentEvent event) { - events.put(event.type(), event); - if (latchMap.containsKey(event.type())) { - latchMap.get(event.type()).countDown(); - } - } - - public int getCounts(IntentEvent.Type type) { - return events.get(type).size(); - } - - public void setLatch(int count, IntentEvent.Type type) { - latchMap.put(type, new CountDownLatch(count)); - } - - public void await(IntentEvent.Type type) { - try { - assertTrue("Timed out waiting for: " + type, - latchMap.get(type).await(5, TimeUnit.SECONDS)); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - - private static class TestIntentTracker implements ObjectiveTrackerService { - private TopologyChangeDelegate delegate; - @Override - public void setDelegate(TopologyChangeDelegate delegate) { - this.delegate = delegate; - } - - @Override - public void unsetDelegate(TopologyChangeDelegate delegate) { - if (delegate.equals(this.delegate)) { - this.delegate = null; - } - } - - @Override - public void addTrackedResources(Key key, Collection<NetworkResource> resources) { - //TODO - } - - @Override - public void removeTrackedResources(Key key, Collection<NetworkResource> resources) { - //TODO - } - - @Override - public void trackIntent(IntentData intentData) { - //TODO - } - } - - private static class MockInstallableIntent extends FlowRuleIntent { - - public MockInstallableIntent() { - super(APPID, Collections.singletonList(new MockFlowRule(100)), Collections.emptyList()); - } - } - - private static class TestIntentCompiler implements IntentCompiler<MockIntent> { - @Override - public List<Intent> compile(MockIntent intent, List<Intent> installable, - Set<LinkResourceAllocations> resources) { - return Lists.newArrayList(new MockInstallableIntent()); - } - } - - private static class TestIntentCompilerMultipleFlows implements IntentCompiler<MockIntent> { - @Override - public List<Intent> compile(MockIntent intent, List<Intent> installable, - Set<LinkResourceAllocations> resources) { - - return IntStream.rangeClosed(1, 5) - .mapToObj(mock -> (new MockInstallableIntent())) - .collect(Collectors.toList()); - } - } - - - private static class TestIntentCompilerError implements IntentCompiler<MockIntent> { - @Override - public List<Intent> compile(MockIntent intent, List<Intent> installable, - Set<LinkResourceAllocations> resources) { - throw new IntentCompilationException("Compilation always fails"); - } - } - - /** - * Hamcrest matcher to check that a collection of Intents contains an - * Intent with the specified Intent Id. - */ - public static class EntryForIntentMatcher extends TypeSafeMatcher<Collection<Intent>> { - private final IntentId id; - - public EntryForIntentMatcher(IntentId idValue) { - id = idValue; - } - - @Override - public boolean matchesSafely(Collection<Intent> intents) { - for (Intent intent : intents) { - if (intent.id().equals(id)) { - return true; - } - } - return false; - } - - @Override - public void describeTo(Description description) { - description.appendText("an intent with id \" "). - appendText(id.toString()). - appendText("\""); - } - } - - private static EntryForIntentMatcher hasIntentWithId(IntentId id) { - return new EntryForIntentMatcher(id); - } - - @Before - public void setUp() { - manager = new IntentManager(); - flowRuleService = new MockFlowRuleService(); - manager.store = new SimpleIntentStore(); - injectEventDispatcher(manager, new TestEventDispatcher()); - manager.trackerService = new TestIntentTracker(); - manager.flowRuleService = flowRuleService; - manager.coreService = new TestCoreManager(); - service = manager; - extensionService = manager; - - manager.activate(); - service.addListener(listener); - extensionService.registerCompiler(MockIntent.class, compiler); - - assertTrue("store should be empty", - Sets.newHashSet(service.getIntents()).isEmpty()); - assertEquals(0L, flowRuleService.getFlowRuleCount()); - } - - public void verifyState() { - // verify that all intents are parked and the batch operation is unblocked - Set<IntentState> parked = Sets.newHashSet(INSTALLED, WITHDRAWN, FAILED, CORRUPT); - for (Intent i : service.getIntents()) { - IntentState state = service.getIntentState(i.key()); - assertTrue("Intent " + i.id() + " is in invalid state " + state, - parked.contains(state)); - } - //the batch has not yet been removed when we receive the last event - // FIXME: this doesn't guarantee to avoid the race - - //FIXME -// for (int tries = 0; tries < 10; tries++) { -// if (manager.batchService.getPendingOperations().isEmpty()) { -// break; -// } -// delay(10); -// } -// assertTrue("There are still pending batch operations.", -// manager.batchService.getPendingOperations().isEmpty()); - - } - - @After - public void tearDown() { - extensionService.unregisterCompiler(MockIntent.class); - service.removeListener(listener); - manager.deactivate(); - // TODO null the other refs? - } - - @Test - public void submitIntent() { - flowRuleService.setFuture(true); - - listener.setLatch(1, Type.INSTALL_REQ); - listener.setLatch(1, Type.INSTALLED); - Intent intent = new MockIntent(MockIntent.nextId()); - service.submit(intent); - listener.await(Type.INSTALL_REQ); - listener.await(Type.INSTALLED); - assertEquals(1L, service.getIntentCount()); - assertEquals(1L, flowRuleService.getFlowRuleCount()); - verifyState(); - } - - @Test - public void withdrawIntent() { - flowRuleService.setFuture(true); - - listener.setLatch(1, Type.INSTALLED); - Intent intent = new MockIntent(MockIntent.nextId()); - service.submit(intent); - listener.await(Type.INSTALLED); - assertEquals(1L, service.getIntentCount()); - assertEquals(1L, flowRuleService.getFlowRuleCount()); - - listener.setLatch(1, Type.WITHDRAWN); - service.withdraw(intent); - listener.await(Type.WITHDRAWN); - assertEquals(0L, flowRuleService.getFlowRuleCount()); - verifyState(); - } - - @Test - @Ignore("This is disabled because we are seeing intermittent failures on Jenkins") - public void stressSubmitWithdrawUnique() { - flowRuleService.setFuture(true); - - int count = 500; - Intent[] intents = new Intent[count]; - - listener.setLatch(count, Type.WITHDRAWN); - - for (int i = 0; i < count; i++) { - intents[i] = new MockIntent(MockIntent.nextId()); - service.submit(intents[i]); - } - - for (int i = 0; i < count; i++) { - service.withdraw(intents[i]); - } - - listener.await(Type.WITHDRAWN); - assertEquals(0L, flowRuleService.getFlowRuleCount()); - verifyState(); - } - - @Test - public void stressSubmitWithdrawSame() { - flowRuleService.setFuture(true); - - int count = 50; - - Intent intent = new MockIntent(MockIntent.nextId()); - for (int i = 0; i < count; i++) { - service.submit(intent); - service.withdraw(intent); - } - - assertAfter(SUBMIT_TIMEOUT_MS, () -> { - assertEquals(1L, service.getIntentCount()); - assertEquals(0L, flowRuleService.getFlowRuleCount()); - }); - verifyState(); - } - - - /** - * Tests for proper behavior of installation of an intent that triggers - * a compilation error. - */ - @Test - public void errorIntentCompile() { - final TestIntentCompilerError errorCompiler = new TestIntentCompilerError(); - extensionService.registerCompiler(MockIntent.class, errorCompiler); - MockIntent intent = new MockIntent(MockIntent.nextId()); - listener.setLatch(1, Type.INSTALL_REQ); - listener.setLatch(1, Type.FAILED); - service.submit(intent); - listener.await(Type.INSTALL_REQ); - listener.await(Type.FAILED); - verifyState(); - } - - /** - * Tests handling a future that contains an error as a result of - * installing an intent. - */ - @Ignore("skipping until we fix update ordering problem") - @Test - public void errorIntentInstallFromFlows() { - final Long id = MockIntent.nextId(); - flowRuleService.setFuture(false); - MockIntent intent = new MockIntent(id); - listener.setLatch(1, Type.FAILED); - listener.setLatch(1, Type.INSTALL_REQ); - service.submit(intent); - listener.await(Type.INSTALL_REQ); - listener.await(Type.FAILED); - // FIXME the intent will be moved into INSTALLED immediately which overrides FAILED - // ... the updates come out of order - verifyState(); - } - - /** - * Tests handling a future that contains an unresolvable error as a result of - * installing an intent. - */ - @Test - public void errorIntentInstallNeverTrue() { - final Long id = MockIntent.nextId(); - flowRuleService.setFuture(false); - MockIntent intent = new MockIntent(id); - listener.setLatch(1, Type.CORRUPT); - listener.setLatch(1, Type.INSTALL_REQ); - service.submit(intent); - listener.await(Type.INSTALL_REQ); - // The delay here forces the retry loop in the intent manager to time out - delay(100); - flowRuleService.setFuture(false); - service.withdraw(intent); - listener.await(Type.CORRUPT); - verifyState(); - } - - /** - * Tests that a compiler for a subclass of an intent that already has a - * compiler is automatically added. - */ - @Test - public void intentSubclassCompile() { - class MockIntentSubclass extends MockIntent { - public MockIntentSubclass(Long number) { - super(number); - } - } - flowRuleService.setFuture(true); - - listener.setLatch(1, Type.INSTALL_REQ); - listener.setLatch(1, Type.INSTALLED); - Intent intent = new MockIntentSubclass(MockIntent.nextId()); - service.submit(intent); - listener.await(Type.INSTALL_REQ); - listener.await(Type.INSTALLED); - assertEquals(1L, service.getIntentCount()); - assertEquals(1L, flowRuleService.getFlowRuleCount()); - - final Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> compilers = - extensionService.getCompilers(); - assertEquals(2, compilers.size()); - assertNotNull(compilers.get(MockIntentSubclass.class)); - assertNotNull(compilers.get(MockIntent.class)); - verifyState(); - } - - /** - * Tests an intent with no compiler. - */ - @Test - public void intentWithoutCompiler() { - class IntentNoCompiler extends Intent { - IntentNoCompiler() { - super(APPID, null, Collections.emptyList(), - Intent.DEFAULT_INTENT_PRIORITY); - } - } - - Intent intent = new IntentNoCompiler(); - listener.setLatch(1, Type.INSTALL_REQ); - listener.setLatch(1, Type.FAILED); - service.submit(intent); - listener.await(Type.INSTALL_REQ); - listener.await(Type.FAILED); - verifyState(); - } - - /** - * Tests an intent with no installer. - */ - @Test - public void intentWithoutInstaller() { - MockIntent intent = new MockIntent(MockIntent.nextId()); - listener.setLatch(1, Type.INSTALL_REQ); - listener.setLatch(1, Type.CORRUPT); - service.submit(intent); - listener.await(Type.INSTALL_REQ); - listener.await(Type.CORRUPT); - verifyState(); - } - - /** - * Tests that the intent fetching methods are correct. - */ - @Test - public void testIntentFetching() { - List<Intent> intents; - - flowRuleService.setFuture(true); - - intents = Lists.newArrayList(service.getIntents()); - assertThat(intents, hasSize(0)); - - final MockIntent intent1 = new MockIntent(MockIntent.nextId()); - final MockIntent intent2 = new MockIntent(MockIntent.nextId()); - - listener.setLatch(2, Type.INSTALL_REQ); - listener.setLatch(2, Type.INSTALLED); - service.submit(intent1); - service.submit(intent2); - listener.await(Type.INSTALL_REQ); - listener.await(Type.INSTALL_REQ); - listener.await(Type.INSTALLED); - listener.await(Type.INSTALLED); - - intents = Lists.newArrayList(service.getIntents()); - assertThat(intents, hasSize(2)); - - assertThat(intents, hasIntentWithId(intent1.id())); - assertThat(intents, hasIntentWithId(intent2.id())); - verifyState(); - } - - /** - * Tests that removing all intents results in no flows remaining. - */ - @Test - public void testFlowRemoval() { - List<Intent> intents; - - flowRuleService.setFuture(true); - - intents = Lists.newArrayList(service.getIntents()); - assertThat(intents, hasSize(0)); - - final MockIntent intent1 = new MockIntent(MockIntent.nextId()); - final MockIntent intent2 = new MockIntent(MockIntent.nextId()); - - listener.setLatch(1, Type.INSTALL_REQ); - listener.setLatch(1, Type.INSTALLED); - - service.submit(intent1); - listener.await(Type.INSTALL_REQ); - listener.await(Type.INSTALLED); - - - listener.setLatch(1, Type.INSTALL_REQ); - listener.setLatch(1, Type.INSTALLED); - - service.submit(intent2); - listener.await(Type.INSTALL_REQ); - listener.await(Type.INSTALLED); - - assertThat(listener.getCounts(Type.INSTALLED), is(2)); - assertThat(flowRuleService.getFlowRuleCount(), is(2)); - - listener.setLatch(1, Type.WITHDRAWN); - service.withdraw(intent1); - listener.await(Type.WITHDRAWN); - - listener.setLatch(1, Type.WITHDRAWN); - service.withdraw(intent2); - listener.await(Type.WITHDRAWN); - - assertThat(listener.getCounts(Type.WITHDRAWN), is(2)); - assertThat(flowRuleService.getFlowRuleCount(), is(0)); - } - - /** - * Test failure to install an intent, then succeed on retry via IntentCleanup. - */ - @Test - public void testCorruptCleanup() { - IntentCleanup cleanup = new IntentCleanup(); - cleanup.service = manager; - cleanup.store = manager.store; - cleanup.cfgService = new ComponentConfigAdapter(); - - try { - cleanup.activate(); - - final TestIntentCompilerMultipleFlows errorCompiler = new TestIntentCompilerMultipleFlows(); - extensionService.registerCompiler(MockIntent.class, errorCompiler); - List<Intent> intents; - - flowRuleService.setFuture(false); - - intents = Lists.newArrayList(service.getIntents()); - assertThat(intents, hasSize(0)); - - final MockIntent intent1 = new MockIntent(MockIntent.nextId()); - - listener.setLatch(1, Type.INSTALL_REQ); - listener.setLatch(1, Type.CORRUPT); - listener.setLatch(1, Type.INSTALLED); - - service.submit(intent1); - - listener.await(Type.INSTALL_REQ); - listener.await(Type.CORRUPT); - - flowRuleService.setFuture(true); - - listener.await(Type.INSTALLED); - - assertThat(listener.getCounts(Type.CORRUPT), is(1)); - assertThat(listener.getCounts(Type.INSTALLED), is(1)); - assertEquals(INSTALLED, manager.getIntentState(intent1.key())); - assertThat(flowRuleService.getFlowRuleCount(), is(5)); - } finally { - cleanup.deactivate(); - } - } - - /** - * Test failure to install an intent, and verify retries. - */ - @Test - public void testCorruptRetry() { - IntentCleanup cleanup = new IntentCleanup(); - cleanup.service = manager; - cleanup.store = manager.store; - cleanup.cfgService = new ComponentConfigAdapter(); - cleanup.period = 1_000_000; - cleanup.retryThreshold = 3; - - try { - cleanup.activate(); - - final TestIntentCompilerMultipleFlows errorCompiler = new TestIntentCompilerMultipleFlows(); - extensionService.registerCompiler(MockIntent.class, errorCompiler); - List<Intent> intents; - - flowRuleService.setFuture(false); - - intents = Lists.newArrayList(service.getIntents()); - assertThat(intents, hasSize(0)); - - final MockIntent intent1 = new MockIntent(MockIntent.nextId()); - - listener.setLatch(1, Type.INSTALL_REQ); - listener.setLatch(cleanup.retryThreshold, Type.CORRUPT); - listener.setLatch(1, Type.INSTALLED); - - service.submit(intent1); - - listener.await(Type.INSTALL_REQ); - listener.await(Type.CORRUPT); - assertEquals(CORRUPT, manager.getIntentState(intent1.key())); - assertThat(listener.getCounts(Type.CORRUPT), is(cleanup.retryThreshold)); - - } finally { - cleanup.deactivate(); - } - } - - /** - * Tests that an intent that fails installation results in no flows remaining. - */ - @Test - @Ignore("MockFlowRule numbering issue") //test works if run independently - public void testFlowRemovalInstallError() { - final TestIntentCompilerMultipleFlows errorCompiler = new TestIntentCompilerMultipleFlows(); - extensionService.registerCompiler(MockIntent.class, errorCompiler); - List<Intent> intents; - - flowRuleService.setFuture(true); - //FIXME relying on "3" is brittle - flowRuleService.setErrorFlow(3); - - intents = Lists.newArrayList(service.getIntents()); - assertThat(intents, hasSize(0)); - - final MockIntent intent1 = new MockIntent(MockIntent.nextId()); - - listener.setLatch(1, Type.INSTALL_REQ); - listener.setLatch(1, Type.CORRUPT); - - service.submit(intent1); - listener.await(Type.INSTALL_REQ); - listener.await(Type.CORRUPT); - - assertThat(listener.getCounts(Type.CORRUPT), is(1)); - // in this test, there will still be flows abandoned on the data plane - //assertThat(flowRuleService.getFlowRuleCount(), is(0)); - } -} diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/MockFlowRuleService.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/MockFlowRuleService.java deleted file mode 100644 index f23a049d..00000000 --- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/MockFlowRuleService.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.net.intent.impl; - -import com.google.common.collect.Sets; -import org.onosproject.core.ApplicationId; -import org.onosproject.net.DeviceId; -import org.onosproject.net.flow.DefaultFlowEntry; -import org.onosproject.net.flow.FlowEntry; -import org.onosproject.net.flow.FlowRule; -import org.onosproject.net.flow.FlowRuleOperations; -import org.onosproject.net.flow.FlowRuleServiceAdapter; - -import java.util.Collections; -import java.util.Set; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.stream.Collectors; - -public class MockFlowRuleService extends FlowRuleServiceAdapter { - - final Set<FlowRule> flows = Sets.newHashSet(); - boolean success; - - int errorFlow = -1; - public void setErrorFlow(int errorFlow) { - this.errorFlow = errorFlow; - } - - public void setFuture(boolean success) { - this.success = success; - } - - @Override - public void apply(FlowRuleOperations ops) { - AtomicBoolean thisSuccess = new AtomicBoolean(success); - ops.stages().forEach(stage -> stage.forEach(flow -> { - if (errorFlow == flow.rule().id().value()) { - thisSuccess.set(false); - } else { - switch (flow.type()) { - case ADD: - case MODIFY: //TODO is this the right behavior for modify? - flows.add(flow.rule()); - break; - case REMOVE: - flows.remove(flow.rule()); - break; - default: - break; - } - } - })); - if (thisSuccess.get()) { - ops.callback().onSuccess(ops); - } else { - ops.callback().onError(ops); - } - } - - @Override - public int getFlowRuleCount() { - return flows.size(); - } - - @Override - public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { - return flows.stream() - .filter(flow -> flow.deviceId().equals(deviceId)) - .map(DefaultFlowEntry::new) - .collect(Collectors.toList()); - } - - @Override - public void applyFlowRules(FlowRule... flowRules) { - Collections.addAll(flows, flowRules); - } - - @Override - public void removeFlowRules(FlowRule... flowRules) { - for (FlowRule flow : flowRules) { - flows.remove(flow); - } - } - - @Override - public Iterable<FlowRule> getFlowRulesById(ApplicationId id) { - return flows.stream() - .filter(flow -> flow.appId() == id.id()) - .collect(Collectors.toList()); - } - - @Override - public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) { - return flows.stream() - .filter(flow -> flow.appId() == appId.id() && flow.groupId().id() == groupId) - .collect(Collectors.toList()); - } -} - diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/ObjectiveTrackerTest.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/ObjectiveTrackerTest.java deleted file mode 100644 index 7cee0d0e..00000000 --- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/ObjectiveTrackerTest.java +++ /dev/null @@ -1,328 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.net.intent.impl; - -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onlab.junit.TestUtils; -import org.onlab.junit.TestUtils.TestUtilsException; -import org.onosproject.core.IdGenerator; -import org.onosproject.event.Event; -import org.onosproject.net.Device; -import org.onosproject.net.DeviceId; -import org.onosproject.net.Link; -import org.onosproject.net.NetworkResource; -import org.onosproject.net.PortNumber; -import org.onosproject.net.device.DeviceEvent; -import org.onosproject.net.device.DeviceListener; -import org.onosproject.net.intent.Intent; -import org.onosproject.net.intent.Key; -import org.onosproject.net.intent.MockIdGenerator; -import org.onosproject.net.link.LinkEvent; -import org.onosproject.net.newresource.ResourceEvent; -import org.onosproject.net.newresource.ResourceListener; -import org.onosproject.net.newresource.ResourcePath; -import org.onosproject.net.topology.Topology; -import org.onosproject.net.topology.TopologyEvent; -import org.onosproject.net.topology.TopologyListener; - -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Lists; - -import static org.easymock.EasyMock.createMock; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.onosproject.net.newresource.ResourceEvent.Type.*; -import static org.onosproject.net.NetTestTools.APP_ID; -import static org.onosproject.net.NetTestTools.device; -import static org.onosproject.net.NetTestTools.link; - -/** - * Tests for the objective tracker. - */ -public class ObjectiveTrackerTest { - private static final int WAIT_TIMEOUT_SECONDS = 2; - private Topology topology; - private ObjectiveTracker tracker; - private TestTopologyChangeDelegate delegate; - private List<Event> reasons; - private TopologyListener listener; - private DeviceListener deviceListener; - private ResourceListener resourceListener; - private IdGenerator mockGenerator; - - /** - * Initialization shared by all test cases. - * - * @throws TestUtilsException if any filed look ups fail - */ - @Before - public void setUp() throws TestUtilsException { - topology = createMock(Topology.class); - tracker = new ObjectiveTracker(); - delegate = new TestTopologyChangeDelegate(); - tracker.setDelegate(delegate); - reasons = new LinkedList<>(); - listener = TestUtils.getField(tracker, "listener"); - deviceListener = TestUtils.getField(tracker, "deviceListener"); - resourceListener = TestUtils.getField(tracker, "resourceListener"); - mockGenerator = new MockIdGenerator(); - Intent.bindIdGenerator(mockGenerator); - } - - /** - * Code to clean up shared by all test case. - */ - @After - public void tearDown() { - tracker.unsetDelegate(delegate); - Intent.unbindIdGenerator(mockGenerator); - } - - /** - * Topology change delegate mock that tracks the events coming into it - * and saves them. It provides a latch so that tests can wait for events - * to be generated. - */ - static class TestTopologyChangeDelegate implements TopologyChangeDelegate { - - CountDownLatch latch = new CountDownLatch(1); - List<Key> intentIdsFromEvent; - boolean compileAllFailedFromEvent; - - @Override - public void triggerCompile(Iterable<Key> intentKeys, - boolean compileAllFailed) { - intentIdsFromEvent = Lists.newArrayList(intentKeys); - compileAllFailedFromEvent = compileAllFailed; - latch.countDown(); - } - } - - /** - * Tests an event with no associated reasons. - * - * @throws InterruptedException if the latch wait fails. - */ - @Test - public void testEventNoReasons() throws InterruptedException { - final TopologyEvent event = new TopologyEvent( - TopologyEvent.Type.TOPOLOGY_CHANGED, - topology, - null); - - listener.event(event); - assertThat( - delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS), - is(true)); - - assertThat(delegate.intentIdsFromEvent, hasSize(0)); - assertThat(delegate.compileAllFailedFromEvent, is(true)); - } - - /** - * Tests an event for a link down where none of the reasons match - * currently installed intents. - * - * @throws InterruptedException if the latch wait fails. - */ - @Test - public void testEventLinkDownNoMatches() throws InterruptedException { - final Link link = link("src", 1, "dst", 2); - final LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link); - reasons.add(linkEvent); - - final TopologyEvent event = new TopologyEvent( - TopologyEvent.Type.TOPOLOGY_CHANGED, - topology, - reasons); - - listener.event(event); - assertThat( - delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS), - is(true)); - - assertThat(delegate.intentIdsFromEvent, hasSize(0)); - assertThat(delegate.compileAllFailedFromEvent, is(false)); - } - - /** - * Tests an event for a link being added. - * - * @throws InterruptedException if the latch wait fails. - */ - @Test - public void testEventLinkAdded() throws InterruptedException { - final Link link = link("src", 1, "dst", 2); - final LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_ADDED, link); - reasons.add(linkEvent); - - final TopologyEvent event = new TopologyEvent( - TopologyEvent.Type.TOPOLOGY_CHANGED, - topology, - reasons); - - listener.event(event); - assertThat( - delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS), - is(true)); - - assertThat(delegate.intentIdsFromEvent, hasSize(0)); - assertThat(delegate.compileAllFailedFromEvent, is(true)); - } - - /** - * Tests an event for a link down where the link matches existing intents. - * - * @throws InterruptedException if the latch wait fails. - */ - @Test - public void testEventLinkDownMatch() throws Exception { - final Link link = link("src", 1, "dst", 2); - final LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link); - reasons.add(linkEvent); - - final TopologyEvent event = new TopologyEvent( - TopologyEvent.Type.TOPOLOGY_CHANGED, - topology, - reasons); - - final Key key = Key.of(0x333L, APP_ID); - Collection<NetworkResource> resources = ImmutableSet.of(link); - tracker.addTrackedResources(key, resources); - - listener.event(event); - assertThat( - delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS), - is(true)); - - assertThat(delegate.intentIdsFromEvent, hasSize(1)); - assertThat(delegate.compileAllFailedFromEvent, is(false)); - assertThat(delegate.intentIdsFromEvent.get(0).toString(), - equalTo("0x333")); - } - - /** - * Tests a resource available event. - * - * @throws InterruptedException if the latch wait fails. - */ - @Test - public void testResourceEvent() throws Exception { - ResourceEvent event = new ResourceEvent(RESOURCE_ADDED, - ResourcePath.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1))); - resourceListener.event(event); - - assertThat( - delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS), - is(true)); - - assertThat(delegate.intentIdsFromEvent, hasSize(0)); - assertThat(delegate.compileAllFailedFromEvent, is(true)); - } - - /** - * Tests an event for a host becoming available that matches an intent. - * - * @throws InterruptedException if the latch wait fails. - */ - - @Test - public void testEventHostAvailableMatch() throws Exception { - final Device host = device("host1"); - - final DeviceEvent deviceEvent = - new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, host); - reasons.add(deviceEvent); - - final Key key = Key.of(0x333L, APP_ID); - Collection<NetworkResource> resources = ImmutableSet.of(host.id()); - tracker.addTrackedResources(key, resources); - - deviceListener.event(deviceEvent); - assertThat( - delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS), - is(true)); - - assertThat(delegate.intentIdsFromEvent, hasSize(1)); - assertThat(delegate.compileAllFailedFromEvent, is(true)); - assertThat(delegate.intentIdsFromEvent.get(0).toString(), - equalTo("0x333")); - } - - /** - * Tests an event for a host becoming unavailable that matches an intent. - * - * @throws InterruptedException if the latch wait fails. - */ - - @Test - public void testEventHostUnavailableMatch() throws Exception { - final Device host = device("host1"); - - final DeviceEvent deviceEvent = - new DeviceEvent(DeviceEvent.Type.DEVICE_REMOVED, host); - reasons.add(deviceEvent); - - final Key key = Key.of(0x333L, APP_ID); - Collection<NetworkResource> resources = ImmutableSet.of(host.id()); - tracker.addTrackedResources(key, resources); - - deviceListener.event(deviceEvent); - assertThat( - delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS), - is(true)); - - assertThat(delegate.intentIdsFromEvent, hasSize(1)); - assertThat(delegate.compileAllFailedFromEvent, is(false)); - assertThat(delegate.intentIdsFromEvent.get(0).toString(), - equalTo("0x333")); - } - - /** - * Tests an event for a host becoming available that matches an intent. - * - * @throws InterruptedException if the latch wait fails. - */ - - @Test - public void testEventHostAvailableNoMatch() throws Exception { - final Device host = device("host1"); - - final DeviceEvent deviceEvent = - new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, host); - reasons.add(deviceEvent); - - deviceListener.event(deviceEvent); - assertThat( - delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS), - is(true)); - - assertThat(delegate.intentIdsFromEvent, hasSize(0)); - assertThat(delegate.compileAllFailedFromEvent, is(true)); - } - - -} diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/HostToHostIntentCompilerTest.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/HostToHostIntentCompilerTest.java deleted file mode 100644 index 5588904d..00000000 --- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/HostToHostIntentCompilerTest.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.net.intent.impl.compiler; - -import org.hamcrest.Matchers; -import org.junit.Before; -import org.junit.Test; -import org.onosproject.core.ApplicationId; -import org.onosproject.TestApplicationId; -import org.onosproject.net.Host; -import org.onosproject.net.HostId; -import org.onosproject.net.flow.TrafficSelector; -import org.onosproject.net.flow.TrafficTreatment; -import org.onosproject.net.host.HostService; -import org.onosproject.net.intent.AbstractIntentTest; -import org.onosproject.net.intent.HostToHostIntent; -import org.onosproject.net.intent.Intent; -import org.onosproject.net.intent.IntentTestsMocks; -import org.onosproject.net.intent.PathIntent; -import org.onlab.packet.MacAddress; -import org.onlab.packet.VlanId; - -import java.util.List; - -import static org.easymock.EasyMock.*; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.onosproject.net.NetTestTools.hid; -import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath; - -/** - * Unit tests for the HostToHost intent compiler. - */ -public class HostToHostIntentCompilerTest extends AbstractIntentTest { - private static final String HOST_ONE_MAC = "00:00:00:00:00:01"; - private static final String HOST_TWO_MAC = "00:00:00:00:00:02"; - private static final String HOST_ONE_VLAN = "-1"; - private static final String HOST_TWO_VLAN = "-1"; - private static final String HOST_ONE = HOST_ONE_MAC + "/" + HOST_ONE_VLAN; - private static final String HOST_TWO = HOST_TWO_MAC + "/" + HOST_TWO_VLAN; - - private static final ApplicationId APPID = new TestApplicationId("foo"); - - private TrafficSelector selector = new IntentTestsMocks.MockSelector(); - private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); - - private HostId hostOneId = HostId.hostId(HOST_ONE); - private HostId hostTwoId = HostId.hostId(HOST_TWO); - private HostService mockHostService; - - @Before - public void setUp() throws Exception { - super.setUp(); - Host hostOne = createMock(Host.class); - expect(hostOne.mac()).andReturn(new MacAddress(HOST_ONE_MAC.getBytes())).anyTimes(); - expect(hostOne.vlan()).andReturn(VlanId.vlanId()).anyTimes(); - replay(hostOne); - - Host hostTwo = createMock(Host.class); - expect(hostTwo.mac()).andReturn(new MacAddress(HOST_TWO_MAC.getBytes())).anyTimes(); - expect(hostTwo.vlan()).andReturn(VlanId.vlanId()).anyTimes(); - replay(hostTwo); - - mockHostService = createMock(HostService.class); - expect(mockHostService.getHost(eq(hostOneId))).andReturn(hostOne).anyTimes(); - expect(mockHostService.getHost(eq(hostTwoId))).andReturn(hostTwo).anyTimes(); - replay(mockHostService); - } - - /** - * Creates a HostToHost intent based on two host Ids. - * - * @param oneIdString string for host one id - * @param twoIdString string for host two id - * @return HostToHostIntent for the two hosts - */ - private HostToHostIntent makeIntent(String oneIdString, String twoIdString) { - return HostToHostIntent.builder() - .appId(APPID) - .one(hid(oneIdString)) - .two(hid(twoIdString)) - .selector(selector) - .treatment(treatment) - .build(); - } - - /** - * Creates a compiler for HostToHost intents. - * - * @param hops string array describing the path hops to use when compiling - * @return HostToHost intent compiler - */ - private HostToHostIntentCompiler makeCompiler(String[] hops) { - HostToHostIntentCompiler compiler = - new HostToHostIntentCompiler(); - compiler.pathService = new IntentTestsMocks.MockPathService(hops); - compiler.hostService = mockHostService; - return compiler; - } - - - /** - * Tests a pair of hosts with 8 hops between them. - */ - @Test - public void testSingleLongPathCompilation() { - - HostToHostIntent intent = makeIntent(HOST_ONE, - HOST_TWO); - assertThat(intent, is(notNullValue())); - - String[] hops = {HOST_ONE, "h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", HOST_TWO}; - HostToHostIntentCompiler compiler = makeCompiler(hops); - assertThat(compiler, is(notNullValue())); - - List<Intent> result = compiler.compile(intent, null, null); - assertThat(result, is(Matchers.notNullValue())); - assertThat(result, hasSize(2)); - Intent forwardResultIntent = result.get(0); - assertThat(forwardResultIntent instanceof PathIntent, is(true)); - Intent reverseResultIntent = result.get(1); - assertThat(reverseResultIntent instanceof PathIntent, is(true)); - - if (forwardResultIntent instanceof PathIntent) { - PathIntent forwardPathIntent = (PathIntent) forwardResultIntent; - assertThat(forwardPathIntent.path().links(), hasSize(9)); - assertThat(forwardPathIntent.path().links(), linksHasPath(HOST_ONE, "h1")); - assertThat(forwardPathIntent.path().links(), linksHasPath("h1", "h2")); - assertThat(forwardPathIntent.path().links(), linksHasPath("h2", "h3")); - assertThat(forwardPathIntent.path().links(), linksHasPath("h3", "h4")); - assertThat(forwardPathIntent.path().links(), linksHasPath("h4", "h5")); - assertThat(forwardPathIntent.path().links(), linksHasPath("h5", "h6")); - assertThat(forwardPathIntent.path().links(), linksHasPath("h6", "h7")); - assertThat(forwardPathIntent.path().links(), linksHasPath("h7", "h8")); - assertThat(forwardPathIntent.path().links(), linksHasPath("h8", HOST_TWO)); - } - - if (reverseResultIntent instanceof PathIntent) { - PathIntent reversePathIntent = (PathIntent) reverseResultIntent; - assertThat(reversePathIntent.path().links(), hasSize(9)); - assertThat(reversePathIntent.path().links(), linksHasPath("h1", HOST_ONE)); - assertThat(reversePathIntent.path().links(), linksHasPath("h2", "h1")); - assertThat(reversePathIntent.path().links(), linksHasPath("h3", "h2")); - assertThat(reversePathIntent.path().links(), linksHasPath("h4", "h3")); - assertThat(reversePathIntent.path().links(), linksHasPath("h5", "h4")); - assertThat(reversePathIntent.path().links(), linksHasPath("h6", "h5")); - assertThat(reversePathIntent.path().links(), linksHasPath("h7", "h6")); - assertThat(reversePathIntent.path().links(), linksHasPath("h8", "h7")); - assertThat(reversePathIntent.path().links(), linksHasPath(HOST_TWO, "h8")); - } - } -} diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompilerTest.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompilerTest.java deleted file mode 100644 index c5fa3719..00000000 --- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompilerTest.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.net.intent.impl.compiler; - -import com.google.common.collect.ImmutableSet; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onosproject.TestApplicationId; -import org.onosproject.core.ApplicationId; -import org.onosproject.core.CoreService; -import org.onosproject.core.IdGenerator; -import org.onosproject.net.ConnectPoint; -import org.onosproject.net.DefaultLink; -import org.onosproject.net.Link; -import org.onosproject.net.flow.DefaultTrafficSelector; -import org.onosproject.net.flow.DefaultTrafficTreatment; -import org.onosproject.net.flow.FlowRule; -import org.onosproject.net.flow.TrafficSelector; -import org.onosproject.net.flow.TrafficTreatment; -import org.onosproject.net.intent.FlowRuleIntent; -import org.onosproject.net.intent.Intent; -import org.onosproject.net.intent.IntentExtensionService; -import org.onosproject.net.intent.LinkCollectionIntent; -import org.onosproject.net.intent.MockIdGenerator; - -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Set; - -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.onosproject.net.Link.Type.DIRECT; -import static org.onosproject.net.NetTestTools.APP_ID; -import static org.onosproject.net.NetTestTools.PID; -import static org.onosproject.net.NetTestTools.connectPoint; - -public class LinkCollectionIntentCompilerTest { - - private final ApplicationId appId = new TestApplicationId("test"); - - private final ConnectPoint d1p1 = connectPoint("s1", 0); - private final ConnectPoint d2p0 = connectPoint("s2", 0); - private final ConnectPoint d2p1 = connectPoint("s2", 1); - private final ConnectPoint d3p1 = connectPoint("s3", 1); - private final ConnectPoint d3p0 = connectPoint("s3", 10); - private final ConnectPoint d1p0 = connectPoint("s1", 10); - - private final Set<Link> links = ImmutableSet.of( - new DefaultLink(PID, d1p1, d2p0, DIRECT), - new DefaultLink(PID, d2p1, d3p1, DIRECT), - new DefaultLink(PID, d1p1, d3p1, DIRECT)); - - private final TrafficSelector selector = DefaultTrafficSelector.builder().build(); - private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); - - private CoreService coreService; - private IntentExtensionService intentExtensionService; - private IdGenerator idGenerator = new MockIdGenerator(); - - private LinkCollectionIntent intent; - - private LinkCollectionIntentCompiler sut; - - @Before - public void setUp() { - sut = new LinkCollectionIntentCompiler(); - coreService = createMock(CoreService.class); - expect(coreService.registerApplication("org.onosproject.net.intent")) - .andReturn(appId); - sut.coreService = coreService; - - Intent.bindIdGenerator(idGenerator); - - intent = LinkCollectionIntent.builder() - .appId(APP_ID) - .selector(selector) - .treatment(treatment) - .links(links) - .ingressPoints(ImmutableSet.of(d1p1)) - .egressPoints(ImmutableSet.of(d3p1)) - .build(); - intentExtensionService = createMock(IntentExtensionService.class); - intentExtensionService.registerCompiler(LinkCollectionIntent.class, sut); - intentExtensionService.unregisterCompiler(LinkCollectionIntent.class); - sut.intentManager = intentExtensionService; - - replay(coreService, intentExtensionService); - } - - @After - public void tearDown() { - Intent.unbindIdGenerator(idGenerator); - } - - @Test - public void testCompile() { - sut.activate(); - - List<Intent> compiled = sut.compile(intent, Collections.emptyList(), Collections.emptySet()); - assertThat(compiled, hasSize(1)); - - Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); - assertThat(rules, hasSize(links.size())); - - // if not found, get() raises an exception - FlowRule rule1 = rules.stream() - .filter(rule -> rule.deviceId().equals(d1p0.deviceId())) - .findFirst() - .get(); - assertThat(rule1.selector(), is( - DefaultTrafficSelector.builder(intent.selector()).matchInPort(d1p1.port()).build() - )); - assertThat(rule1.treatment(), is( - DefaultTrafficTreatment.builder(intent.treatment()).setOutput(d1p1.port()).build() - )); - assertThat(rule1.priority(), is(intent.priority())); - - FlowRule rule2 = rules.stream() - .filter(rule -> rule.deviceId().equals(d2p0.deviceId())) - .findFirst() - .get(); - assertThat(rule2.selector(), is( - DefaultTrafficSelector.builder(intent.selector()).matchInPort(d2p0.port()).build() - )); - assertThat(rule2.treatment(), is( - DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build() - )); - assertThat(rule2.priority(), is(intent.priority())); - - FlowRule rule3 = rules.stream() - .filter(rule -> rule.deviceId().equals(d3p0.deviceId())) - .findFirst() - .get(); - assertThat(rule3.selector(), is( - DefaultTrafficSelector.builder(intent.selector()).matchInPort(d3p1.port()).build() - )); - assertThat(rule3.treatment(), is( - DefaultTrafficTreatment.builder().setOutput(d3p1.port()).build() - )); - assertThat(rule3.priority(), is(intent.priority())); - - sut.deactivate(); - } -} diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MockResourceService.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MockResourceService.java deleted file mode 100644 index f5d3d0f3..00000000 --- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MockResourceService.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.net.intent.impl.compiler; - -import com.google.common.collect.ImmutableList; -import org.onlab.packet.MplsLabel; -import org.onosproject.net.newresource.ResourceAllocation; -import org.onosproject.net.newresource.ResourceConsumer; -import org.onosproject.net.newresource.ResourceListener; -import org.onosproject.net.newresource.ResourcePath; -import org.onosproject.net.newresource.ResourceService; - -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.stream.Collectors; - -class MockResourceService implements ResourceService { - - private final Map<ResourcePath, ResourceConsumer> assignment = new HashMap<>(); - - @Override - public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<ResourcePath> resources) { - assignment.putAll( - resources.stream().collect(Collectors.toMap(x -> x, x -> consumer)) - ); - - return resources.stream() - .map(x -> new ResourceAllocation(x, consumer)) - .collect(Collectors.toList()); - } - - @Override - public boolean release(List<ResourceAllocation> allocations) { - allocations.forEach(x -> assignment.remove(x.resource())); - - return true; - } - - @Override - public boolean release(ResourceConsumer consumer) { - List<ResourcePath> resources = assignment.entrySet().stream() - .filter(x -> x.getValue().equals(consumer)) - .map(Map.Entry::getKey) - .collect(Collectors.toList()); - List<ResourceAllocation> allocations = resources.stream() - .map(x -> new ResourceAllocation(x, consumer)) - .collect(Collectors.toList()); - - return release(allocations); - } - - @Override - public Optional<ResourceAllocation> getResourceAllocation(ResourcePath resource) { - return Optional.ofNullable(assignment.get(resource)) - .map(x -> new ResourceAllocation(resource, x)); - } - - @Override - public <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls) { - return assignment.entrySet().stream() - .filter(x -> x.getKey().parent().isPresent()) - .filter(x -> x.getKey().parent().get().equals(parent)) - .map(x -> new ResourceAllocation(x.getKey(), x.getValue())) - .collect(Collectors.toList()); - } - - @Override - public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) { - return assignment.entrySet().stream() - .filter(x -> x.getValue().equals(consumer)) - .map(x -> new ResourceAllocation(x.getKey(), x.getValue())) - .collect(Collectors.toList()); - } - - @Override - public Collection<ResourcePath> getAvailableResources(ResourcePath parent) { - ResourcePath resource = parent.child(MplsLabel.mplsLabel(10)); - return ImmutableList.of(resource); - } - - @Override - public boolean isAvailable(ResourcePath resource) { - return true; - } - - @Override - public void addListener(ResourceListener listener) {} - - @Override - public void removeListener(ResourceListener listener) {} -} diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MplsIntentCompilerTest.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MplsIntentCompilerTest.java deleted file mode 100644 index 03a38a5a..00000000 --- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MplsIntentCompilerTest.java +++ /dev/null @@ -1,203 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.net.intent.impl.compiler; - -import java.util.List; -import java.util.Optional; - -import org.hamcrest.Matchers; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - - -import org.onlab.packet.MplsLabel; -import org.onosproject.TestApplicationId; -import org.onosproject.core.ApplicationId; -import org.onosproject.net.ConnectPoint; -import org.onosproject.net.Link; -import org.onosproject.net.Path; -import org.onosproject.net.flow.TrafficSelector; -import org.onosproject.net.flow.TrafficTreatment; -import org.onosproject.net.intent.AbstractIntentTest; -import org.onosproject.net.intent.Intent; -import org.onosproject.net.intent.IntentTestsMocks; -import org.onosproject.net.intent.MplsIntent; -import org.onosproject.net.intent.MplsPathIntent; - -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; -import static org.onosproject.net.DeviceId.deviceId; -import static org.onosproject.net.NetTestTools.APP_ID; -import static org.onosproject.net.NetTestTools.connectPoint; -import static org.onosproject.net.PortNumber.portNumber; -import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath; - -/** - * Unit tests for the HostToHost intent compiler. - */ -public class MplsIntentCompilerTest extends AbstractIntentTest { - - private static final ApplicationId APPID = new TestApplicationId("foo"); - - private TrafficSelector selector = new IntentTestsMocks.MockSelector(); - private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); - - /** - * Creates a PointToPoint intent based on ingress and egress device Ids. - * - * @param ingressIdString string for id of ingress device - * @param egressIdString string for id of egress device - * @return PointToPointIntent for the two devices - */ - private MplsIntent makeIntent(String ingressIdString, Optional<MplsLabel> ingressLabel, - String egressIdString, Optional<MplsLabel> egressLabel) { - - return MplsIntent.builder() - .appId(APPID) - .selector(selector) - .treatment(treatment) - .ingressPoint(connectPoint(ingressIdString, 1)) - .ingressLabel(ingressLabel) - .egressPoint(connectPoint(egressIdString, 1)) - .egressLabel(egressLabel).build(); - } - /** - * Creates a compiler for HostToHost intents. - * - * @param hops string array describing the path hops to use when compiling - * @return HostToHost intent compiler - */ - private MplsIntentCompiler makeCompiler(String[] hops) { - MplsIntentCompiler compiler = - new MplsIntentCompiler(); - compiler.pathService = new IntentTestsMocks.MockPathService(hops); - return compiler; - } - - - /** - * Tests a pair of devices in an 8 hop path, forward direction. - */ - @Test - public void testForwardPathCompilation() { - Optional<MplsLabel> ingressLabel = Optional.of(MplsLabel.mplsLabel(10)); - Optional<MplsLabel> egressLabel = Optional.of(MplsLabel.mplsLabel(20)); - - MplsIntent intent = makeIntent("d1", ingressLabel, "d8", egressLabel); - assertThat(intent, is(notNullValue())); - - String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"}; - MplsIntentCompiler compiler = makeCompiler(hops); - assertThat(compiler, is(notNullValue())); - - List<Intent> result = compiler.compile(intent, null, null); - assertThat(result, is(Matchers.notNullValue())); - assertThat(result, hasSize(1)); - Intent forwardResultIntent = result.get(0); - assertThat(forwardResultIntent instanceof MplsPathIntent, is(true)); - - // if statement suppresses static analysis warnings about unchecked cast - if (forwardResultIntent instanceof MplsPathIntent) { - MplsPathIntent forwardPathIntent = (MplsPathIntent) forwardResultIntent; - // 7 links for the hops, plus one default lnk on ingress and egress - assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1)); - assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2")); - assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3")); - assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4")); - assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5")); - assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6")); - assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7")); - assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8")); - assertEquals(forwardPathIntent.egressLabel(), egressLabel); - assertEquals(forwardPathIntent.ingressLabel(), ingressLabel); - } - } - - /** - * Tests a pair of devices in an 8 hop path, forward direction. - */ - @Test - public void testReversePathCompilation() { - Optional<MplsLabel> ingressLabel = Optional.of(MplsLabel.mplsLabel(10)); - Optional<MplsLabel> egressLabel = Optional.of(MplsLabel.mplsLabel(20)); - - MplsIntent intent = makeIntent("d8", ingressLabel, "d1", egressLabel); - assertThat(intent, is(notNullValue())); - - String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"}; - MplsIntentCompiler compiler = makeCompiler(hops); - assertThat(compiler, is(notNullValue())); - - List<Intent> result = compiler.compile(intent, null, null); - assertThat(result, is(Matchers.notNullValue())); - assertThat(result, hasSize(1)); - Intent reverseResultIntent = result.get(0); - assertThat(reverseResultIntent instanceof MplsPathIntent, is(true)); - - // if statement suppresses static analysis warnings about unchecked cast - if (reverseResultIntent instanceof MplsPathIntent) { - MplsPathIntent reversePathIntent = (MplsPathIntent) reverseResultIntent; - assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1)); - assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1")); - assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2")); - assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3")); - assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4")); - assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5")); - assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6")); - assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7")); - assertEquals(reversePathIntent.egressLabel(), egressLabel); - assertEquals(reversePathIntent.ingressLabel(), ingressLabel); - } - } - - /** - * Tests compilation of the intent which designates two different ports on the same switch. - */ - @Test - public void testSameSwitchDifferentPortsIntentCompilation() { - ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1)); - ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2)); - MplsIntent intent = MplsIntent.builder() - .appId(APP_ID) - .selector(selector) - .treatment(treatment) - .ingressPoint(src) - .ingressLabel(Optional.empty()) - .egressPoint(dst) - .egressLabel(Optional.empty()) - .build(); - - String[] hops = {"1"}; - MplsIntentCompiler sut = makeCompiler(hops); - - List<Intent> compiled = sut.compile(intent, null, null); - - assertThat(compiled, hasSize(1)); - assertThat(compiled.get(0), is(instanceOf(MplsPathIntent.class))); - Path path = ((MplsPathIntent) compiled.get(0)).path(); - - assertThat(path.links(), hasSize(2)); - Link firstLink = path.links().get(0); - assertThat(firstLink, is(createEdgeLink(src, true))); - Link secondLink = path.links().get(1); - assertThat(secondLink, is(createEdgeLink(dst, false))); - } -} diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompilerTest.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompilerTest.java deleted file mode 100644 index 6cceee12..00000000 --- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompilerTest.java +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.net.intent.impl.compiler; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Optional; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onlab.packet.MplsLabel; -import org.onosproject.TestApplicationId; -import org.onosproject.core.ApplicationId; -import org.onosproject.core.CoreService; -import org.onosproject.core.IdGenerator; -import org.onosproject.net.ConnectPoint; -import org.onosproject.net.DefaultLink; -import org.onosproject.net.DefaultPath; -import org.onosproject.net.Link; -import org.onosproject.net.flow.DefaultTrafficSelector; -import org.onosproject.net.flow.DefaultTrafficTreatment; -import org.onosproject.net.flow.FlowRule; -import org.onosproject.net.flow.TrafficSelector; -import org.onosproject.net.flow.TrafficTreatment; -import org.onosproject.net.intent.FlowRuleIntent; -import org.onosproject.net.intent.Intent; -import org.onosproject.net.intent.IntentExtensionService; -import org.onosproject.net.intent.MockIdGenerator; -import org.onosproject.net.intent.MplsPathIntent; - -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; -import static org.onosproject.net.Link.Type.DIRECT; -import static org.onosproject.net.NetTestTools.APP_ID; -import static org.onosproject.net.NetTestTools.PID; -import static org.onosproject.net.NetTestTools.connectPoint; - -public class MplsPathIntentCompilerTest { - - private final ApplicationId appId = new TestApplicationId("test"); - - private final ConnectPoint d1pi = connectPoint("s1", 100); - private final ConnectPoint d1p1 = connectPoint("s1", 0); - private final ConnectPoint d2p0 = connectPoint("s2", 0); - private final ConnectPoint d2p1 = connectPoint("s2", 1); - private final ConnectPoint d3p1 = connectPoint("s3", 1); - private final ConnectPoint d3pe = connectPoint("s3", 100); - - private final TrafficSelector selector = DefaultTrafficSelector.builder().build(); - private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); - - private final Optional<MplsLabel> ingressLabel = - Optional.of(MplsLabel.mplsLabel(10)); - private final Optional<MplsLabel> egressLabel = - Optional.of(MplsLabel.mplsLabel(20)); - - private final List<Link> links = Arrays.asList( - createEdgeLink(d1pi, true), - new DefaultLink(PID, d1p1, d2p0, DIRECT), - new DefaultLink(PID, d2p1, d3p1, DIRECT), - createEdgeLink(d3pe, false) - ); - - private IdGenerator idGenerator = new MockIdGenerator(); - - private final int hops = links.size() - 1; - private MplsPathIntent intent; - private MplsPathIntentCompiler sut; - - @Before - public void setUp() { - sut = new MplsPathIntentCompiler(); - CoreService coreService = createMock(CoreService.class); - expect(coreService.registerApplication("org.onosproject.net.intent")) - .andReturn(appId); - sut.coreService = coreService; - sut.resourceService = new MockResourceService(); - - Intent.bindIdGenerator(idGenerator); - - intent = MplsPathIntent.builder() - .appId(APP_ID) - .selector(selector) - .treatment(treatment) - .path(new DefaultPath(PID, links, hops)) - .ingressLabel(ingressLabel) - .egressLabel(egressLabel) - .priority(55) - .build(); - - IntentExtensionService intentExtensionService = createMock(IntentExtensionService.class); - intentExtensionService.registerCompiler(MplsPathIntent.class, sut); - intentExtensionService.unregisterCompiler(MplsPathIntent.class); - sut.intentExtensionService = intentExtensionService; - - replay(coreService, intentExtensionService); - } - - @After - public void tearDown() { - Intent.unbindIdGenerator(idGenerator); - } - - @Test - public void testCompile() { - sut.activate(); - - List<Intent> compiled = sut.compile(intent, Collections.emptyList(), Collections.emptySet()); - assertThat(compiled, hasSize(1)); - - Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); - assertThat(rules, hasSize(3)); - - FlowRule rule = rules.stream() - .filter(x -> x.deviceId().equals(d2p0.deviceId())) - .findFirst() - .get(); - assertThat(rule.deviceId(), is(d2p0.deviceId())); - - sut.deactivate(); - - } - -} diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MultiPointToSinglePointIntentCompilerTest.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MultiPointToSinglePointIntentCompilerTest.java deleted file mode 100644 index 03d664d3..00000000 --- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MultiPointToSinglePointIntentCompilerTest.java +++ /dev/null @@ -1,270 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.net.intent.impl.compiler; - -import org.hamcrest.Matchers; -import org.junit.Test; -import org.onosproject.TestApplicationId; -import org.onosproject.core.ApplicationId; -import org.onosproject.net.ConnectPoint; -import org.onosproject.net.DeviceId; -import org.onosproject.net.ElementId; -import org.onosproject.net.Path; -import org.onosproject.net.device.DeviceServiceAdapter; -import org.onosproject.net.flow.TrafficSelector; -import org.onosproject.net.flow.TrafficTreatment; -import org.onosproject.net.intent.AbstractIntentTest; -import org.onosproject.net.intent.Intent; -import org.onosproject.net.intent.IntentTestsMocks; -import org.onosproject.net.intent.LinkCollectionIntent; -import org.onosproject.net.intent.MultiPointToSinglePointIntent; -import org.onosproject.net.topology.PathServiceAdapter; - -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.onosproject.net.NetTestTools.connectPoint; -import static org.onosproject.net.NetTestTools.createPath; -import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath; - -/** - * Unit tests for the MultiPointToSinglePoint intent compiler. - */ -public class MultiPointToSinglePointIntentCompilerTest extends AbstractIntentTest { - - private static final ApplicationId APPID = new TestApplicationId("foo"); - - private TrafficSelector selector = new IntentTestsMocks.MockSelector(); - private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); - - /** - * Mock path service for creating paths within the test. - */ - private static class MockPathService extends PathServiceAdapter { - - final String[] pathHops; - - /** - * Constructor that provides a set of hops to mock. - * - * @param pathHops path hops to mock - */ - MockPathService(String[] pathHops) { - this.pathHops = pathHops; - } - - @Override - public Set<Path> getPaths(ElementId src, ElementId dst) { - Set<Path> result = new HashSet<>(); - - String[] allHops = new String[pathHops.length + 1]; - allHops[0] = src.toString(); - if (pathHops.length != 0) { - System.arraycopy(pathHops, 0, allHops, 1, pathHops.length); - } - result.add(createPath(allHops)); - - return result; - } - } - - /** - * Mocks the device service so that a device appears available in the test. - */ - private static class MockDeviceService extends DeviceServiceAdapter { - @Override - public boolean isAvailable(DeviceId deviceId) { - return true; - } - } - - /** - * Creates a MultiPointToSinglePoint intent for a group of ingress points - * and an egress point. - * - * @param ingressIds array of ingress device ids - * @param egressId device id of the egress point - * @return MultiPointToSinglePoint intent - */ - private MultiPointToSinglePointIntent makeIntent(String[] ingressIds, String egressId) { - Set<ConnectPoint> ingressPoints = new HashSet<>(); - ConnectPoint egressPoint = connectPoint(egressId, 2); - - for (String ingressId : ingressIds) { - ingressPoints.add(connectPoint(ingressId, 1)); - } - - return MultiPointToSinglePointIntent.builder() - .appId(APPID) - .selector(selector) - .treatment(treatment) - .ingressPoints(ingressPoints) - .egressPoint(egressPoint) - .build(); - } - - /** - * Creates a compiler for MultiPointToSinglePoint intents. - * - * @param hops hops to use while computing paths for this intent - * @return MultiPointToSinglePoint intent - */ - private MultiPointToSinglePointIntentCompiler makeCompiler(String[] hops) { - MultiPointToSinglePointIntentCompiler compiler = - new MultiPointToSinglePointIntentCompiler(); - compiler.pathService = new MockPathService(hops); - compiler.deviceService = new MockDeviceService(); - return compiler; - } - - /** - * Tests a single ingress point with 8 hops to its egress point. - */ - @Test - public void testSingleLongPathCompilation() { - - String[] ingress = {"ingress"}; - String egress = "egress"; - - MultiPointToSinglePointIntent intent = makeIntent(ingress, egress); - assertThat(intent, is(notNullValue())); - - String[] hops = {"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", - egress}; - MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); - assertThat(compiler, is(notNullValue())); - - List<Intent> result = compiler.compile(intent, null, null); - assertThat(result, is(Matchers.notNullValue())); - assertThat(result, hasSize(1)); - Intent resultIntent = result.get(0); - assertThat(resultIntent instanceof LinkCollectionIntent, is(true)); - - if (resultIntent instanceof LinkCollectionIntent) { - LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; - assertThat(linkIntent.links(), hasSize(9)); - assertThat(linkIntent.links(), linksHasPath("ingress", "h1")); - assertThat(linkIntent.links(), linksHasPath("h1", "h2")); - assertThat(linkIntent.links(), linksHasPath("h2", "h3")); - assertThat(linkIntent.links(), linksHasPath("h4", "h5")); - assertThat(linkIntent.links(), linksHasPath("h5", "h6")); - assertThat(linkIntent.links(), linksHasPath("h7", "h8")); - assertThat(linkIntent.links(), linksHasPath("h8", "egress")); - } - } - - /** - * Tests a simple topology where two ingress points share some path segments - * and some path segments are not shared. - */ - @Test - public void testTwoIngressCompilation() { - String[] ingress = {"ingress1", "ingress2"}; - String egress = "egress"; - - MultiPointToSinglePointIntent intent = makeIntent(ingress, egress); - assertThat(intent, is(notNullValue())); - - final String[] hops = {"inner1", "inner2", egress}; - MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); - assertThat(compiler, is(notNullValue())); - - List<Intent> result = compiler.compile(intent, null, null); - assertThat(result, is(notNullValue())); - assertThat(result, hasSize(1)); - Intent resultIntent = result.get(0); - assertThat(resultIntent instanceof LinkCollectionIntent, is(true)); - - if (resultIntent instanceof LinkCollectionIntent) { - LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; - assertThat(linkIntent.links(), hasSize(4)); - assertThat(linkIntent.links(), linksHasPath("ingress1", "inner1")); - assertThat(linkIntent.links(), linksHasPath("ingress2", "inner1")); - assertThat(linkIntent.links(), linksHasPath("inner1", "inner2")); - assertThat(linkIntent.links(), linksHasPath("inner2", "egress")); - } - } - - /** - * Tests a large number of ingress points that share a common path to the - * egress point. - */ - @Test - public void testMultiIngressCompilation() { - String[] ingress = {"i1", "i2", "i3", "i4", "i5", - "i6", "i7", "i8", "i9", "i10"}; - String egress = "e"; - - MultiPointToSinglePointIntent intent = makeIntent(ingress, egress); - assertThat(intent, is(notNullValue())); - - final String[] hops = {"n1", egress}; - MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); - assertThat(compiler, is(notNullValue())); - - List<Intent> result = compiler.compile(intent, null, null); - assertThat(result, is(notNullValue())); - assertThat(result, hasSize(1)); - Intent resultIntent = result.get(0); - assertThat(resultIntent instanceof LinkCollectionIntent, is(true)); - - if (resultIntent instanceof LinkCollectionIntent) { - LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; - assertThat(linkIntent.links(), hasSize(ingress.length + 1)); - for (String ingressToCheck : ingress) { - assertThat(linkIntent.links(), - linksHasPath(ingressToCheck, - "n1")); - } - assertThat(linkIntent.links(), linksHasPath("n1", egress)); - } - } - - /** - * Tests ingress and egress on the same device. - */ - @Test - public void testSameDeviceCompilation() { - String[] ingress = {"i1", "i2"}; - String egress = "i1"; - - MultiPointToSinglePointIntent intent = makeIntent(ingress, egress); - assertThat(intent, is(notNullValue())); - - final String[] hops = {"i1", "i2"}; - MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); - assertThat(compiler, is(notNullValue())); - - List<Intent> result = compiler.compile(intent, null, null); - assertThat(result, is(notNullValue())); - assertThat(result, hasSize(1)); - Intent resultIntent = result.get(0); - assertThat(resultIntent, instanceOf(LinkCollectionIntent.class)); - - if (resultIntent instanceof LinkCollectionIntent) { - LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; - assertThat(linkIntent.links(), hasSize(ingress.length)); - - assertThat(linkIntent.links(), linksHasPath("i2", "i1")); - } - } -} diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/OpticalPathIntentCompilerTest.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/OpticalPathIntentCompilerTest.java deleted file mode 100644 index 38a116dd..00000000 --- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/OpticalPathIntentCompilerTest.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.net.intent.impl.compiler; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onosproject.TestApplicationId; -import org.onosproject.core.ApplicationId; -import org.onosproject.core.CoreService; -import org.onosproject.core.IdGenerator; -import org.onosproject.net.ConnectPoint; -import org.onosproject.net.DefaultLink; -import org.onosproject.net.DefaultPath; -import org.onosproject.net.Link; -import org.onosproject.net.OchSignalType; -import org.onosproject.net.flow.FlowRule; -import org.onosproject.net.intent.FlowRuleIntent; -import org.onosproject.net.intent.Intent; -import org.onosproject.net.intent.IntentExtensionService; -import org.onosproject.net.intent.MockIdGenerator; -import org.onosproject.net.intent.OpticalPathIntent; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasSize; -import static org.junit.Assert.assertEquals; -import static org.onosproject.net.Link.Type.DIRECT; -import static org.onosproject.net.NetTestTools.PID; -import static org.onosproject.net.NetTestTools.connectPoint; -import static org.onosproject.net.NetTestTools.createLambda; - -public class OpticalPathIntentCompilerTest { - - private CoreService coreService; - private IntentExtensionService intentExtensionService; - private final IdGenerator idGenerator = new MockIdGenerator(); - private OpticalPathIntentCompiler sut; - - private final ApplicationId appId = new TestApplicationId("test"); - private final ConnectPoint d1p1 = connectPoint("s1", 0); - private final ConnectPoint d2p0 = connectPoint("s2", 0); - private final ConnectPoint d2p1 = connectPoint("s2", 1); - private final ConnectPoint d3p1 = connectPoint("s3", 1); - - private final List<Link> links = Arrays.asList( - new DefaultLink(PID, d1p1, d2p0, DIRECT), - new DefaultLink(PID, d2p1, d3p1, DIRECT) - ); - private final int hops = links.size() + 1; - private OpticalPathIntent intent; - - @Before - public void setUp() { - sut = new OpticalPathIntentCompiler(); - coreService = createMock(CoreService.class); - expect(coreService.registerApplication("org.onosproject.net.intent")) - .andReturn(appId); - sut.coreService = coreService; - - Intent.bindIdGenerator(idGenerator); - - intent = OpticalPathIntent.builder() - .appId(appId) - .src(d1p1) - .dst(d3p1) - .path(new DefaultPath(PID, links, hops)) - .lambda(createLambda()) - .signalType(OchSignalType.FIXED_GRID) - .build(); - intentExtensionService = createMock(IntentExtensionService.class); - intentExtensionService.registerCompiler(OpticalPathIntent.class, sut); - intentExtensionService.unregisterCompiler(OpticalPathIntent.class); - sut.intentManager = intentExtensionService; - - replay(coreService, intentExtensionService); - } - - @After - public void tearDown() { - Intent.unbindIdGenerator(idGenerator); - } - - @Test - public void testCompiler() { - sut.activate(); - - List<Intent> compiled = sut.compile(intent, Collections.emptyList(), Collections.emptySet()); - assertThat(compiled, hasSize(1)); - - Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); - rules.stream() - .filter(x -> x.deviceId().equals(d1p1.deviceId())) - .findFirst() - .get(); - - rules.stream() - .filter(x -> x.deviceId().equals(d2p1.deviceId())) - .findFirst() - .get(); - - rules.stream() - .filter(x -> x.deviceId().equals(d3p1.deviceId())) - .findFirst() - .get(); - - rules.forEach(rule -> assertEquals("FlowRule priority is incorrect", - intent.priority(), rule.priority())); - - sut.deactivate(); - } - - //TODO test bidirectional optical paths and verify rules - -} diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/PathIntentCompilerTest.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/PathIntentCompilerTest.java deleted file mode 100644 index f07bf42c..00000000 --- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/PathIntentCompilerTest.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.net.intent.impl.compiler; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onosproject.TestApplicationId; -import org.onosproject.core.ApplicationId; -import org.onosproject.core.CoreService; -import org.onosproject.core.IdGenerator; -import org.onosproject.net.ConnectPoint; -import org.onosproject.net.DefaultLink; -import org.onosproject.net.DefaultPath; -import org.onosproject.net.Link; -import org.onosproject.net.flow.DefaultTrafficSelector; -import org.onosproject.net.flow.DefaultTrafficTreatment; -import org.onosproject.net.flow.FlowRule; -import org.onosproject.net.flow.TrafficSelector; -import org.onosproject.net.flow.TrafficTreatment; -import org.onosproject.net.intent.FlowRuleIntent; -import org.onosproject.net.intent.Intent; -import org.onosproject.net.intent.IntentExtensionService; -import org.onosproject.net.intent.MockIdGenerator; -import org.onosproject.net.intent.PathIntent; -import org.onosproject.net.provider.ProviderId; - -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; -import static org.onosproject.net.Link.Type.DIRECT; -import static org.onosproject.net.NetTestTools.APP_ID; -import static org.onosproject.net.NetTestTools.PID; -import static org.onosproject.net.NetTestTools.connectPoint; - -/** - * Unit tests for PathIntentCompiler. - */ -public class PathIntentCompilerTest { - - private CoreService coreService; - private IntentExtensionService intentExtensionService; - private IdGenerator idGenerator = new MockIdGenerator(); - private PathIntentCompiler sut; - - private final TrafficSelector selector = DefaultTrafficSelector.builder().build(); - private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); - private final ApplicationId appId = new TestApplicationId("test"); - private final ProviderId pid = new ProviderId("of", "test"); - private final ConnectPoint d1p1 = connectPoint("s1", 0); - private final ConnectPoint d2p0 = connectPoint("s2", 0); - private final ConnectPoint d2p1 = connectPoint("s2", 1); - private final ConnectPoint d3p1 = connectPoint("s3", 1); - private final ConnectPoint d3p0 = connectPoint("s3", 10); - private final ConnectPoint d1p0 = connectPoint("s1", 10); - private static final int PRIORITY = 555; - - private final List<Link> links = Arrays.asList( - createEdgeLink(d1p0, true), - new DefaultLink(PID, d1p1, d2p0, DIRECT), - new DefaultLink(PID, d2p1, d3p1, DIRECT), - createEdgeLink(d3p0, false) - ); - private final int hops = links.size() - 1; - private PathIntent intent; - - /** - * Configures objects used in all the test cases. - */ - @Before - public void setUp() { - sut = new PathIntentCompiler(); - coreService = createMock(CoreService.class); - expect(coreService.registerApplication("org.onosproject.net.intent")) - .andReturn(appId); - sut.coreService = coreService; - - Intent.bindIdGenerator(idGenerator); - - intent = PathIntent.builder() - .appId(APP_ID) - .selector(selector) - .treatment(treatment) - .priority(PRIORITY) - .path(new DefaultPath(pid, links, hops)) - .build(); - intentExtensionService = createMock(IntentExtensionService.class); - intentExtensionService.registerCompiler(PathIntent.class, sut); - intentExtensionService.unregisterCompiler(PathIntent.class); - sut.intentManager = intentExtensionService; - - replay(coreService, intentExtensionService); - } - - /** - * Tears down objects used in all the test cases. - */ - @After - public void tearDown() { - Intent.unbindIdGenerator(idGenerator); - } - - /** - * Tests the compilation behavior of the path intent compiler. - */ - @Test - public void testCompile() { - sut.activate(); - - List<Intent> compiled = sut.compile(intent, Collections.emptyList(), Collections.emptySet()); - assertThat(compiled, hasSize(1)); - - Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); - - FlowRule rule1 = rules.stream() - .filter(x -> x.deviceId().equals(d1p0.deviceId())) - .findFirst() - .get(); - assertThat(rule1.deviceId(), is(d1p0.deviceId())); - assertThat(rule1.selector(), - is(DefaultTrafficSelector.builder(selector).matchInPort(d1p0.port()).build())); - assertThat(rule1.treatment(), - is(DefaultTrafficTreatment.builder().setOutput(d1p1.port()).build())); - assertThat(rule1.priority(), is(intent.priority())); - - FlowRule rule2 = rules.stream() - .filter(x -> x.deviceId().equals(d2p0.deviceId())) - .findFirst() - .get(); - assertThat(rule2.deviceId(), is(d2p0.deviceId())); - assertThat(rule2.selector(), - is(DefaultTrafficSelector.builder(selector).matchInPort(d2p0.port()).build())); - assertThat(rule2.treatment(), - is(DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build())); - assertThat(rule2.priority(), is(intent.priority())); - - FlowRule rule3 = rules.stream() - .filter(x -> x.deviceId().equals(d3p0.deviceId())) - .findFirst() - .get(); - assertThat(rule3.deviceId(), is(d3p1.deviceId())); - assertThat(rule3.selector(), - is(DefaultTrafficSelector.builder(selector).matchInPort(d3p1.port()).build())); - assertThat(rule3.treatment(), - is(DefaultTrafficTreatment.builder(treatment).setOutput(d3p0.port()).build())); - assertThat(rule3.priority(), is(intent.priority())); - - sut.deactivate(); - } -} diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/PointToPointIntentCompilerTest.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/PointToPointIntentCompilerTest.java deleted file mode 100644 index 5d7b5c8d..00000000 --- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/PointToPointIntentCompilerTest.java +++ /dev/null @@ -1,319 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.net.intent.impl.compiler; - -import org.hamcrest.Matchers; -import org.junit.Test; -import org.onlab.util.Bandwidth; -import org.onosproject.TestApplicationId; -import org.onosproject.core.ApplicationId; -import org.onosproject.net.ConnectPoint; -import org.onosproject.net.IndexedLambda; -import org.onosproject.net.Link; -import org.onosproject.net.Path; -import org.onosproject.net.flow.TrafficSelector; -import org.onosproject.net.flow.TrafficTreatment; -import org.onosproject.net.intent.AbstractIntentTest; -import org.onosproject.net.intent.Constraint; -import org.onosproject.net.intent.Intent; -import org.onosproject.net.intent.IntentTestsMocks; -import org.onosproject.net.intent.PathIntent; -import org.onosproject.net.intent.PointToPointIntent; -import org.onosproject.net.intent.constraint.BandwidthConstraint; -import org.onosproject.net.intent.constraint.LambdaConstraint; -import org.onosproject.net.intent.impl.PathNotFoundException; -import org.onosproject.net.resource.link.LinkResourceService; - -import java.util.Collections; -import java.util.List; - -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.fail; -import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; -import static org.onosproject.net.DeviceId.deviceId; -import static org.onosproject.net.NetTestTools.APP_ID; -import static org.onosproject.net.NetTestTools.connectPoint; -import static org.onosproject.net.PortNumber.portNumber; -import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath; - -/** - * Unit tests for the HostToHost intent compiler. - */ -public class PointToPointIntentCompilerTest extends AbstractIntentTest { - - private static final ApplicationId APPID = new TestApplicationId("foo"); - - private TrafficSelector selector = new IntentTestsMocks.MockSelector(); - private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); - - /** - * Creates a PointToPoint intent based on ingress and egress device Ids. - * - * @param ingressIdString string for id of ingress device - * @param egressIdString string for id of egress device - * @return PointToPointIntent for the two devices - */ - private PointToPointIntent makeIntent(String ingressIdString, - String egressIdString) { - return PointToPointIntent.builder() - .appId(APPID) - .selector(selector) - .treatment(treatment) - .ingressPoint(connectPoint(ingressIdString, 1)) - .egressPoint(connectPoint(egressIdString, 1)) - .build(); - } - - /** - * Creates a PointToPoint intent based on ingress and egress deviceIds and constraints. - * - * @param ingressIdString string for id of ingress device - * @param egressIdString string for id of egress device - * @param constraints constraints - * @return PointToPointIntent for the two device with constraints - */ - private PointToPointIntent makeIntent(String ingressIdString, - String egressIdString, List<Constraint> constraints) { - return PointToPointIntent.builder() - .appId(APPID) - .selector(selector) - .treatment(treatment) - .ingressPoint(connectPoint(ingressIdString, 1)) - .egressPoint(connectPoint(egressIdString, 1)) - .constraints(constraints) - .build(); - } - - /** - * Creates a compiler for HostToHost intents. - * - * @param hops string array describing the path hops to use when compiling - * @return HostToHost intent compiler - */ - private PointToPointIntentCompiler makeCompiler(String[] hops) { - PointToPointIntentCompiler compiler = new PointToPointIntentCompiler(); - compiler.pathService = new IntentTestsMocks.MockPathService(hops); - return compiler; - } - - /** - * Creates a point to point intent compiler for a three switch linear - * topology. - * - * @param resourceService service to use for resource allocation requests - * @return point to point compiler - */ - private PointToPointIntentCompiler makeCompiler(String[] hops, LinkResourceService resourceService) { - final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler(); - compiler.resourceService = resourceService; - compiler.pathService = new IntentTestsMocks.MockPathService(hops); - return compiler; - } - - /** - * Tests a pair of devices in an 8 hop path, forward direction. - */ - @Test - public void testForwardPathCompilation() { - - PointToPointIntent intent = makeIntent("d1", "d8"); - - String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"}; - PointToPointIntentCompiler compiler = makeCompiler(hops); - - List<Intent> result = compiler.compile(intent, null, null); - assertThat(result, is(Matchers.notNullValue())); - assertThat(result, hasSize(1)); - Intent forwardResultIntent = result.get(0); - assertThat(forwardResultIntent instanceof PathIntent, is(true)); - - if (forwardResultIntent instanceof PathIntent) { - PathIntent forwardPathIntent = (PathIntent) forwardResultIntent; - // 7 links for the hops, plus one default lnk on ingress and egress - assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1)); - assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2")); - assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3")); - assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4")); - assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5")); - assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6")); - assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7")); - assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8")); - } - } - - /** - * Tests a pair of devices in an 8 hop path, forward direction. - */ - @Test - public void testReversePathCompilation() { - - PointToPointIntent intent = makeIntent("d8", "d1"); - - String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"}; - PointToPointIntentCompiler compiler = makeCompiler(hops); - - List<Intent> result = compiler.compile(intent, null, null); - assertThat(result, is(Matchers.notNullValue())); - assertThat(result, hasSize(1)); - Intent reverseResultIntent = result.get(0); - assertThat(reverseResultIntent instanceof PathIntent, is(true)); - - if (reverseResultIntent instanceof PathIntent) { - PathIntent reversePathIntent = (PathIntent) reverseResultIntent; - assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1)); - assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1")); - assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2")); - assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3")); - assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4")); - assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5")); - assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6")); - assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7")); - } - } - - /** - * Tests compilation of the intent which designates two different ports on the same switch. - */ - @Test - public void testSameSwitchDifferentPortsIntentCompilation() { - ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1)); - ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2)); - PointToPointIntent intent = PointToPointIntent.builder() - .appId(APP_ID) - .selector(selector) - .treatment(treatment) - .ingressPoint(src) - .egressPoint(dst) - .build(); - - String[] hops = {"1"}; - PointToPointIntentCompiler sut = makeCompiler(hops); - - List<Intent> compiled = sut.compile(intent, null, null); - - assertThat(compiled, hasSize(1)); - assertThat(compiled.get(0), is(instanceOf(PathIntent.class))); - Path path = ((PathIntent) compiled.get(0)).path(); - - assertThat(path.links(), hasSize(2)); - Link firstLink = path.links().get(0); - assertThat(firstLink, is(createEdgeLink(src, true))); - Link secondLink = path.links().get(1); - assertThat(secondLink, is(createEdgeLink(dst, false))); - } - - /** - * Tests that requests with sufficient available bandwidth succeed. - */ - @Test - public void testBandwidthConstrainedIntentSuccess() { - - final LinkResourceService resourceService = - IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0); - final List<Constraint> constraints = - Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(100.0))); - - final PointToPointIntent intent = makeIntent("s1", "s3", constraints); - - String[] hops = {"s1", "s2", "s3"}; - final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService); - - final List<Intent> compiledIntents = compiler.compile(intent, null, null); - - assertThat(compiledIntents, Matchers.notNullValue()); - assertThat(compiledIntents, hasSize(1)); - } - - /** - * Tests that requests with insufficient available bandwidth fail. - */ - @Test - public void testBandwidthConstrainedIntentFailure() { - - final LinkResourceService resourceService = - IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0); - final List<Constraint> constraints = - Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(100.0))); - - try { - final PointToPointIntent intent = makeIntent("s1", "s3", constraints); - - String[] hops = {"s1", "s2", "s3"}; - final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService); - - compiler.compile(intent, null, null); - - fail("Point to Point compilation with insufficient bandwidth does " - + "not throw exception."); - } catch (PathNotFoundException noPath) { - assertThat(noPath.getMessage(), containsString("No path")); - } - } - - /** - * Tests that requests for available lambdas are successful. - */ - @Test - public void testLambdaConstrainedIntentSuccess() { - - final List<Constraint> constraints = - Collections.singletonList(new LambdaConstraint(new IndexedLambda(1))); - final LinkResourceService resourceService = - IntentTestsMocks.MockResourceService.makeLambdaResourceService(1); - - final PointToPointIntent intent = makeIntent("s1", "s3", constraints); - - String[] hops = {"s1", "s2", "s3"}; - final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService); - - final List<Intent> compiledIntents = - compiler.compile(intent, null, null); - - assertThat(compiledIntents, Matchers.notNullValue()); - assertThat(compiledIntents, hasSize(1)); - } - - /** - * Tests that requests for lambdas when there are no available lambdas - * fail. - */ - @Test - public void testLambdaConstrainedIntentFailure() { - - final List<Constraint> constraints = - Collections.singletonList(new LambdaConstraint(new IndexedLambda(1))); - final LinkResourceService resourceService = - IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0); - try { - final PointToPointIntent intent = makeIntent("s1", "s3", constraints); - - String[] hops = {"s1", "s2", "s3"}; - final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService); - - compiler.compile(intent, null, null); - - fail("Point to Point compilation with no available lambda does " - + "not throw exception."); - } catch (PathNotFoundException noPath) { - assertThat(noPath.getMessage(), containsString("No path")); - } - } - -} diff --git a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/phase/CompilingTest.java b/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/phase/CompilingTest.java deleted file mode 100644 index c15ecaec..00000000 --- a/framework/src/onos/core/net/src/test/java/org/onosproject/net/intent/impl/phase/CompilingTest.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.net.intent.impl.phase; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onosproject.TestApplicationId; -import org.onosproject.core.ApplicationId; -import org.onosproject.core.IdGenerator; -import org.onosproject.net.ConnectPoint; -import org.onosproject.net.DefaultLink; -import org.onosproject.net.DefaultPath; -import org.onosproject.net.Link; -import org.onosproject.net.Path; -import org.onosproject.net.flow.DefaultTrafficSelector; -import org.onosproject.net.flow.DefaultTrafficTreatment; -import org.onosproject.net.flow.TrafficSelector; -import org.onosproject.net.flow.TrafficTreatment; -import org.onosproject.net.intent.Intent; -import org.onosproject.net.intent.IntentData; -import org.onosproject.net.intent.MockIdGenerator; -import org.onosproject.net.intent.PathIntent; -import org.onosproject.net.intent.PointToPointIntent; -import org.onosproject.net.intent.impl.IntentCompilationException; -import org.onosproject.net.intent.impl.IntentProcessor; -import org.onosproject.net.provider.ProviderId; -import org.onosproject.store.Timestamp; - -import java.util.Collections; -import java.util.List; -import java.util.Optional; - -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.onosproject.net.DeviceId.deviceId; -import static org.onosproject.net.Link.Type.DIRECT; -import static org.onosproject.net.PortNumber.portNumber; -import static org.onosproject.net.intent.IntentState.INSTALL_REQ; - -/** - * Unit tests for Compiling phase. - */ -public class CompilingTest { - - private final ApplicationId appId = new TestApplicationId("test"); - private final ProviderId pid = new ProviderId("of", "test"); - private final TrafficSelector selector = DefaultTrafficSelector.emptySelector(); - private final TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); - private final ConnectPoint cp1 = new ConnectPoint(deviceId("1"), portNumber(1)); - private final ConnectPoint cp2 = new ConnectPoint(deviceId("1"), portNumber(2)); - private final ConnectPoint cp3 = new ConnectPoint(deviceId("2"), portNumber(1)); - private final ConnectPoint cp4 = new ConnectPoint(deviceId("2"), portNumber(2)); - - private final List<Link> links = Collections.singletonList(new DefaultLink(pid, cp2, cp4, DIRECT)); - private final Path path = new DefaultPath(pid, links, 10); - - private PointToPointIntent input; - private PathIntent compiled; - - private IdGenerator idGenerator; - private IntentProcessor processor; - private Timestamp version; - - @Before - public void setUp() { - processor = createMock(IntentProcessor.class); - version = createMock(Timestamp.class); - - idGenerator = new MockIdGenerator(); - - Intent.bindIdGenerator(idGenerator); - - // Intent creation should be placed after binding an ID generator - input = PointToPointIntent.builder() - .appId(appId) - .selector(selector) - .treatment(treatment) - .ingressPoint(cp1) - .egressPoint(cp3) - .build(); - compiled = PathIntent.builder() - .appId(appId) - .selector(selector) - .treatment(treatment) - .path(path) - .build(); - } - - - @After - public void tearDown() { - Intent.unbindIdGenerator(idGenerator); - } - - /** - * Tests a next phase when no exception occurs. - */ - @Test - public void testMoveToNextPhaseWithoutError() { - IntentData pending = new IntentData(input, INSTALL_REQ, version); - - expect(processor.compile(input, null)).andReturn(Collections.singletonList(compiled)); - replay(processor); - - Compiling sut = new Compiling(processor, pending, Optional.empty()); - - Optional<IntentProcessPhase> output = sut.execute(); - - verify(processor); - assertThat(output.get(), is(instanceOf(Installing.class))); - } - - /** - * Tests a next phase when IntentCompilationException occurs. - */ - @Test - public void testWhenIntentCompilationExceptionOccurs() { - IntentData pending = new IntentData(input, INSTALL_REQ, version); - - expect(processor.compile(input, null)).andThrow(new IntentCompilationException()); - replay(processor); - - Compiling sut = new Compiling(processor, pending, Optional.empty()); - - Optional<IntentProcessPhase> output = sut.execute(); - - verify(processor); - assertThat(output.get(), is(instanceOf(Failed.class))); - } -} |