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