blob: ecdee64c31b4a22cd20a911663ea57475fb68a6a [file] [log] [blame]
Ray Milkeyd9905352015-02-24 16:52:42 -08001/*
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 */
16package org.onosproject.net.intent.impl;
17
18import java.util.Arrays;
19import java.util.Collection;
20import java.util.List;
21
Ray Milkeyd9905352015-02-24 16:52:42 -080022import org.junit.Before;
23import org.junit.Test;
Ray Milkeyd9905352015-02-24 16:52:42 -080024import org.onosproject.net.DefaultLink;
25import org.onosproject.net.DefaultPath;
Ray Milkeyd9905352015-02-24 16:52:42 -080026import org.onosproject.net.Link;
27import org.onosproject.net.flow.FlowRuleOperation;
Ray Milkeyd9905352015-02-24 16:52:42 -080028import org.onosproject.net.intent.PathIntent;
29
30import com.google.common.collect.ImmutableList;
31
32import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkeybb320482015-02-25 15:16:46 -080033import static org.hamcrest.Matchers.hasSize;
34import static org.hamcrest.Matchers.notNullValue;
Ray Milkeyd9905352015-02-24 16:52:42 -080035import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
36import static org.onosproject.net.Link.Type.DIRECT;
37import static org.onosproject.net.NetTestTools.APP_ID;
38import static org.onosproject.net.NetTestTools.PID;
Ray Milkeyd9905352015-02-24 16:52:42 -080039
40/**
41 * Unit tests for path intent installer.
42 */
Ray Milkeybb320482015-02-25 15:16:46 -080043public class PathIntentInstallerTest extends IntentInstallerTest {
Ray Milkeyd9905352015-02-24 16:52:42 -080044
Ray Milkeyd9905352015-02-24 16:52:42 -080045 PathIntentInstaller installer;
46
Ray Milkeyd9905352015-02-24 16:52:42 -080047 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 Milkeybb320482015-02-25 15:16:46 -080060 public void localSetUp() {
Ray Milkeyd9905352015-02-24 16:52:42 -080061 installer = new PathIntentInstaller();
62 installer.coreService = testCoreService;
Ray Milkeybb320482015-02-25 15:16:46 -080063 installer.intentManager = new MockIntentManager(PathIntent.class);
Ray Milkeyd9905352015-02-24 16:52:42 -080064 intent = new PathIntent(APP_ID, selector, treatment,
65 new DefaultPath(PID, links, hops), ImmutableList.of());
66 }
67
68 /**
Ray Milkeyd9905352015-02-24 16:52:42 -080069 * Tests activation and deactivation of the installer.
70 */
71 @Test
72 public void activateDeactivate() {
73 installer.activate();
74 installer.deactivate();
75 }
76
77 /**
Ray Milkeyd9905352015-02-24 16:52:42 -080078 * 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}