Ray Milkey | d990535 | 2015-02-24 16:52:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.net.intent.impl; |
| 17 | |
| 18 | import java.util.Arrays; |
| 19 | import java.util.Collection; |
| 20 | import java.util.List; |
| 21 | |
Ray Milkey | d990535 | 2015-02-24 16:52:42 -0800 | [diff] [blame] | 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
Ray Milkey | d990535 | 2015-02-24 16:52:42 -0800 | [diff] [blame] | 24 | import org.onosproject.net.DefaultLink; |
| 25 | import org.onosproject.net.DefaultPath; |
Ray Milkey | d990535 | 2015-02-24 16:52:42 -0800 | [diff] [blame] | 26 | import org.onosproject.net.Link; |
| 27 | import org.onosproject.net.flow.FlowRuleOperation; |
Ray Milkey | d990535 | 2015-02-24 16:52:42 -0800 | [diff] [blame] | 28 | import org.onosproject.net.intent.PathIntent; |
| 29 | |
| 30 | import com.google.common.collect.ImmutableList; |
| 31 | |
| 32 | import static org.hamcrest.MatcherAssert.assertThat; |
Ray Milkey | bb32048 | 2015-02-25 15:16:46 -0800 | [diff] [blame] | 33 | import static org.hamcrest.Matchers.hasSize; |
| 34 | import static org.hamcrest.Matchers.notNullValue; |
Ray Milkey | d990535 | 2015-02-24 16:52:42 -0800 | [diff] [blame] | 35 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
| 36 | import static org.onosproject.net.Link.Type.DIRECT; |
| 37 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 38 | import static org.onosproject.net.NetTestTools.PID; |
Ray Milkey | d990535 | 2015-02-24 16:52:42 -0800 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Unit tests for path intent installer. |
| 42 | */ |
Ray Milkey | bb32048 | 2015-02-25 15:16:46 -0800 | [diff] [blame] | 43 | public class PathIntentInstallerTest extends IntentInstallerTest { |
Ray Milkey | d990535 | 2015-02-24 16:52:42 -0800 | [diff] [blame] | 44 | |
Ray Milkey | d990535 | 2015-02-24 16:52:42 -0800 | [diff] [blame] | 45 | PathIntentInstaller installer; |
| 46 | |
Ray Milkey | d990535 | 2015-02-24 16:52:42 -0800 | [diff] [blame] | 47 | private final List<Link> links = Arrays.asList( |
| 48 | createEdgeLink(d1p0, true), |
| 49 | new DefaultLink(PID, d1p1, d2p0, DIRECT), |
| 50 | new DefaultLink(PID, d2p1, d3p1, DIRECT), |
| 51 | createEdgeLink(d3p0, false) |
| 52 | ); |
| 53 | private final int hops = links.size() - 1; |
| 54 | private PathIntent intent; |
| 55 | |
| 56 | /** |
| 57 | * Configures objects used in all the test cases. |
| 58 | */ |
| 59 | @Before |
Ray Milkey | bb32048 | 2015-02-25 15:16:46 -0800 | [diff] [blame] | 60 | public void localSetUp() { |
Ray Milkey | d990535 | 2015-02-24 16:52:42 -0800 | [diff] [blame] | 61 | installer = new PathIntentInstaller(); |
| 62 | installer.coreService = testCoreService; |
Ray Milkey | bb32048 | 2015-02-25 15:16:46 -0800 | [diff] [blame] | 63 | installer.intentManager = new MockIntentManager(PathIntent.class); |
Ray Milkey | d990535 | 2015-02-24 16:52:42 -0800 | [diff] [blame] | 64 | intent = new PathIntent(APP_ID, selector, treatment, |
| 65 | new DefaultPath(PID, links, hops), ImmutableList.of()); |
| 66 | } |
| 67 | |
| 68 | /** |
Ray Milkey | d990535 | 2015-02-24 16:52:42 -0800 | [diff] [blame] | 69 | * Tests activation and deactivation of the installer. |
| 70 | */ |
| 71 | @Test |
| 72 | public void activateDeactivate() { |
| 73 | installer.activate(); |
| 74 | installer.deactivate(); |
| 75 | } |
| 76 | |
| 77 | /** |
Ray Milkey | d990535 | 2015-02-24 16:52:42 -0800 | [diff] [blame] | 78 | * Tests installation operation of the path intent installer. |
| 79 | */ |
| 80 | @Test |
| 81 | public void install() { |
| 82 | installer.activate(); |
| 83 | |
| 84 | List<Collection<FlowRuleOperation>> operations = |
| 85 | installer.install(intent); |
| 86 | assertThat(operations, notNullValue()); |
| 87 | assertThat(operations, hasSize(1)); |
| 88 | |
| 89 | Collection<FlowRuleOperation> flowRuleOpsCollection = operations.get(0); |
| 90 | assertThat(flowRuleOpsCollection, hasSize(hops)); |
| 91 | FlowRuleOperation[] flowRuleOps = |
| 92 | flowRuleOpsCollection.toArray(new FlowRuleOperation[hops]); |
| 93 | |
| 94 | FlowRuleOperation op0 = flowRuleOps[0]; |
| 95 | checkFlowOperation(op0, FlowRuleOperation.Type.ADD, d1p0.deviceId()); |
| 96 | |
| 97 | FlowRuleOperation op1 = flowRuleOps[1]; |
| 98 | checkFlowOperation(op1, FlowRuleOperation.Type.ADD, d2p0.deviceId()); |
| 99 | |
| 100 | FlowRuleOperation op2 = flowRuleOps[2]; |
| 101 | checkFlowOperation(op2, FlowRuleOperation.Type.ADD, d3p0.deviceId()); |
| 102 | |
| 103 | installer.deactivate(); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Checks the uninstall operation of the path intent installer. |
| 108 | */ |
| 109 | @Test |
| 110 | public void uninstall() { |
| 111 | installer.activate(); |
| 112 | |
| 113 | List<Collection<FlowRuleOperation>> operations = |
| 114 | installer.uninstall(intent); |
| 115 | assertThat(operations, notNullValue()); |
| 116 | assertThat(operations, hasSize(1)); |
| 117 | |
| 118 | Collection<FlowRuleOperation> flowRuleOpsCollection = operations.get(0); |
| 119 | assertThat(flowRuleOpsCollection, hasSize(hops)); |
| 120 | FlowRuleOperation[] flowRuleOps = |
| 121 | flowRuleOpsCollection.toArray(new FlowRuleOperation[hops]); |
| 122 | |
| 123 | FlowRuleOperation op0 = flowRuleOps[0]; |
| 124 | checkFlowOperation(op0, FlowRuleOperation.Type.REMOVE, d1p0.deviceId()); |
| 125 | |
| 126 | FlowRuleOperation op1 = flowRuleOps[1]; |
| 127 | checkFlowOperation(op1, FlowRuleOperation.Type.REMOVE, d2p0.deviceId()); |
| 128 | |
| 129 | FlowRuleOperation op2 = flowRuleOps[2]; |
| 130 | checkFlowOperation(op2, FlowRuleOperation.Type.REMOVE, d3p0.deviceId()); |
| 131 | |
| 132 | installer.deactivate(); |
| 133 | } |
| 134 | } |