Ray Milkey

Add unit test for link collection intent installer

Change-Id: Ifd89b9868b320546bc55ba78e207993e35aaf50c
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.Collection;
19 +import java.util.List;
20 +import java.util.Set;
21 +
22 +import org.junit.Before;
23 +import org.junit.Test;
24 +import org.onosproject.net.DefaultLink;
25 +import org.onosproject.net.DeviceId;
26 +import org.onosproject.net.Link;
27 +import org.onosproject.net.flow.FlowRuleOperation;
28 +import org.onosproject.net.intent.LinkCollectionIntent;
29 +
30 +import com.google.common.collect.ImmutableSet;
31 +
32 +import static org.hamcrest.MatcherAssert.assertThat;
33 +import static org.hamcrest.Matchers.hasSize;
34 +import static org.hamcrest.Matchers.notNullValue;
35 +import static org.onosproject.net.Link.Type.DIRECT;
36 +import static org.onosproject.net.NetTestTools.APP_ID;
37 +import static org.onosproject.net.NetTestTools.PID;
38 +
39 +public class LinkCollectionIntentInstallerTest extends IntentInstallerTest {
40 +
41 + LinkCollectionIntentInstaller installer;
42 +
43 + private final Set<Link> links = ImmutableSet.of(
44 + new DefaultLink(PID, d1p1, d2p0, DIRECT),
45 + new DefaultLink(PID, d2p1, d3p1, DIRECT),
46 + new DefaultLink(PID, d1p1, d3p1, DIRECT));
47 +
48 + private LinkCollectionIntent intent;
49 +
50 + /**
51 + * Configures objects used in all the test cases.
52 + */
53 + @Before
54 + public void localSetUp() {
55 + installer = new LinkCollectionIntentInstaller();
56 + installer.coreService = testCoreService;
57 + installer.intentManager =
58 + new IntentInstallerTest.MockIntentManager(LinkCollectionIntent.class);
59 + intent = new LinkCollectionIntent(APP_ID, selector, treatment, links, d1p1, d3p1);
60 + }
61 +
62 + private FlowRuleOperation findOperation(Collection<FlowRuleOperation> ops,
63 + DeviceId deviceId) {
64 + for (FlowRuleOperation op : ops) {
65 + if (op.rule().deviceId().equals(deviceId)) {
66 + return op;
67 + }
68 + }
69 + return null;
70 + }
71 +
72 + /**
73 + * Tests activation and deactivation of the installer.
74 + */
75 + @Test
76 + public void activateDeactivate() {
77 + installer.activate();
78 + installer.deactivate();
79 + }
80 +
81 + /**
82 + * Tests installation operation of the path intent installer.
83 + */
84 + @Test
85 + public void install() {
86 + installer.activate();
87 +
88 + List<Collection<FlowRuleOperation>> operations =
89 + installer.install(intent);
90 + assertThat(operations, notNullValue());
91 + assertThat(operations, hasSize(1));
92 +
93 + Collection<FlowRuleOperation> flowRuleOpsCollection = operations.get(0);
94 + assertThat(flowRuleOpsCollection, hasSize(links.size()));
95 +
96 + FlowRuleOperation op0 = findOperation(flowRuleOpsCollection,
97 + d1p0.deviceId());
98 + checkFlowOperation(op0, FlowRuleOperation.Type.ADD, d1p0.deviceId());
99 +
100 + FlowRuleOperation op1 = findOperation(flowRuleOpsCollection,
101 + d2p0.deviceId());
102 + checkFlowOperation(op1, FlowRuleOperation.Type.ADD, d2p0.deviceId());
103 +
104 + FlowRuleOperation op2 = findOperation(flowRuleOpsCollection,
105 + d3p0.deviceId());
106 + checkFlowOperation(op2, FlowRuleOperation.Type.ADD, d3p0.deviceId());
107 +
108 + installer.deactivate();
109 + }
110 +
111 + /**
112 + * Checks the uninstall operation of the path intent installer.
113 + */
114 + @Test
115 + public void uninstall() {
116 + installer.activate();
117 +
118 + List<Collection<FlowRuleOperation>> operations =
119 + installer.uninstall(intent);
120 + assertThat(operations, notNullValue());
121 + assertThat(operations, hasSize(1));
122 +
123 + Collection<FlowRuleOperation> flowRuleOpsCollection = operations.get(0);
124 + assertThat(flowRuleOpsCollection, hasSize(links.size()));
125 +
126 + FlowRuleOperation op0 = findOperation(flowRuleOpsCollection,
127 + d1p0.deviceId());
128 + checkFlowOperation(op0, FlowRuleOperation.Type.REMOVE, d1p0.deviceId());
129 +
130 + FlowRuleOperation op1 = findOperation(flowRuleOpsCollection,
131 + d2p0.deviceId());
132 + checkFlowOperation(op1, FlowRuleOperation.Type.REMOVE, d2p0.deviceId());
133 +
134 + FlowRuleOperation op2 = findOperation(flowRuleOpsCollection,
135 + d3p0.deviceId());
136 + checkFlowOperation(op2, FlowRuleOperation.Type.REMOVE, d3p0.deviceId());
137 +
138 + installer.deactivate();
139 + }
140 +}