Ray Milkey
Committed by Gerrit Code Review

Add unit test for MPLS path intent installer

- refactored common code from intent installer tests into
  a base class.

Change-Id: Iab4d01531748dd014ba73cc2fbed5930d8018977
...@@ -52,6 +52,8 @@ import org.onosproject.net.resource.LinkResourceAllocations; ...@@ -52,6 +52,8 @@ import org.onosproject.net.resource.LinkResourceAllocations;
52 import org.onosproject.net.resource.LinkResourceListener; 52 import org.onosproject.net.resource.LinkResourceListener;
53 import org.onosproject.net.resource.LinkResourceRequest; 53 import org.onosproject.net.resource.LinkResourceRequest;
54 import org.onosproject.net.resource.LinkResourceService; 54 import org.onosproject.net.resource.LinkResourceService;
55 +import org.onosproject.net.resource.MplsLabel;
56 +import org.onosproject.net.resource.MplsLabelResourceAllocation;
55 import org.onosproject.net.resource.ResourceAllocation; 57 import org.onosproject.net.resource.ResourceAllocation;
56 import org.onosproject.net.resource.ResourceRequest; 58 import org.onosproject.net.resource.ResourceRequest;
57 import org.onosproject.net.resource.ResourceType; 59 import org.onosproject.net.resource.ResourceType;
...@@ -63,6 +65,7 @@ import org.onosproject.net.topology.TopologyVertex; ...@@ -63,6 +65,7 @@ import org.onosproject.net.topology.TopologyVertex;
63 import org.onosproject.store.Timestamp; 65 import org.onosproject.store.Timestamp;
64 66
65 import com.google.common.base.MoreObjects; 67 import com.google.common.base.MoreObjects;
68 +import com.google.common.collect.ImmutableSet;
66 69
67 /** 70 /**
68 * Common mocks used by the intent framework tests. 71 * Common mocks used by the intent framework tests.
...@@ -152,7 +155,7 @@ public class IntentTestsMocks { ...@@ -152,7 +155,7 @@ public class IntentTestsMocks {
152 public static class MockLinkResourceAllocations implements LinkResourceAllocations { 155 public static class MockLinkResourceAllocations implements LinkResourceAllocations {
153 @Override 156 @Override
154 public Set<ResourceAllocation> getResourceAllocation(Link link) { 157 public Set<ResourceAllocation> getResourceAllocation(Link link) {
155 - return null; 158 + return ImmutableSet.of(new MplsLabelResourceAllocation(MplsLabel.valueOf(10)));
156 } 159 }
157 160
158 @Override 161 @Override
...@@ -254,17 +257,19 @@ public class IntentTestsMocks { ...@@ -254,17 +257,19 @@ public class IntentTestsMocks {
254 257
255 @Override 258 @Override
256 public Iterable<LinkResourceAllocations> getAllocations() { 259 public Iterable<LinkResourceAllocations> getAllocations() {
257 - return null; 260 + return ImmutableSet.of(
261 + new IntentTestsMocks.MockLinkResourceAllocations());
258 } 262 }
259 263
260 @Override 264 @Override
261 public Iterable<LinkResourceAllocations> getAllocations(Link link) { 265 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
262 - return null; 266 + return ImmutableSet.of(
267 + new IntentTestsMocks.MockLinkResourceAllocations());
263 } 268 }
264 269
265 @Override 270 @Override
266 public LinkResourceAllocations getAllocations(IntentId intentId) { 271 public LinkResourceAllocations getAllocations(IntentId intentId) {
267 - return null; 272 + return new IntentTestsMocks.MockLinkResourceAllocations();
268 } 273 }
269 274
270 @Override 275 @Override
......
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 org.junit.After;
19 +import org.junit.Before;
20 +import org.onosproject.core.ApplicationId;
21 +import org.onosproject.core.CoreService;
22 +import org.onosproject.core.CoreServiceAdapter;
23 +import org.onosproject.core.IdGenerator;
24 +import org.onosproject.net.ConnectPoint;
25 +import org.onosproject.net.DeviceId;
26 +import org.onosproject.net.flow.FlowRuleOperation;
27 +import org.onosproject.net.intent.FakeIntentManager;
28 +import org.onosproject.net.intent.Intent;
29 +import org.onosproject.net.intent.IntentInstaller;
30 +import org.onosproject.net.intent.IntentTestsMocks;
31 +import org.onosproject.net.intent.MockIdGenerator;
32 +
33 +import static org.hamcrest.MatcherAssert.assertThat;
34 +import static org.hamcrest.Matchers.equalTo;
35 +import static org.hamcrest.Matchers.is;
36 +import static org.onosproject.net.NetTestTools.APP_ID;
37 +import static org.onosproject.net.NetTestTools.connectPoint;
38 +
39 +/**
40 + * Base class for intent installer tests.
41 + */
42 +public class IntentInstallerTest {
43 +
44 + /**
45 + * Mock for core service.
46 + */
47 + static class TestCoreService extends CoreServiceAdapter {
48 +
49 + String registeredId = "";
50 +
51 + @Override
52 + public ApplicationId registerApplication(String identifier) {
53 + registeredId = identifier;
54 + return APP_ID;
55 + }
56 + }
57 +
58 + /**
59 + * Mock for intent manager service. Checks that the PathIntent
60 + * installer installs and uninstalls properly.
61 + */
62 + static class MockIntentManager extends FakeIntentManager {
63 +
64 + boolean installerRegistered = false;
65 + final Class expectedClass;
66 +
67 + private MockIntentManager() {
68 + expectedClass = null;
69 + }
70 +
71 + MockIntentManager(Class expectedInstaller) {
72 + this.expectedClass = expectedInstaller;
73 + }
74 +
75 + @Override
76 + public <T extends Intent> void registerInstaller(
77 + Class<T> cls,
78 + IntentInstaller<T> installer) {
79 + assertThat(cls, equalTo(expectedClass));
80 + installerRegistered = true;
81 + }
82 +
83 + @Override
84 + public <T extends Intent> void unregisterInstaller(Class<T> cls) {
85 + assertThat(cls, equalTo(expectedClass));
86 + assertThat(installerRegistered, is(true));
87 + }
88 +
89 + }
90 +
91 + CoreService testCoreService;
92 + IdGenerator idGenerator = new MockIdGenerator();
93 + IntentInstaller installer;
94 +
95 + final IntentTestsMocks.MockSelector selector = new IntentTestsMocks.MockSelector();
96 + final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
97 + final ConnectPoint d1p1 = connectPoint("s1", 0);
98 + final ConnectPoint d2p0 = connectPoint("s2", 0);
99 + final ConnectPoint d2p1 = connectPoint("s2", 1);
100 + final ConnectPoint d3p1 = connectPoint("s3", 1);
101 + final ConnectPoint d3p0 = connectPoint("s3", 10);
102 + final ConnectPoint d1p0 = connectPoint("s1", 10);
103 +
104 + /**
105 + * Configures objects used in all the test cases.
106 + */
107 + @Before
108 + public void setUp() {
109 + testCoreService = new TestCoreService();
110 + Intent.bindIdGenerator(idGenerator);
111 + }
112 +
113 + /**
114 + * Tears down objects used in all the test cases.
115 + */
116 + @After
117 + public void tearDown() {
118 + Intent.unbindIdGenerator(idGenerator);
119 + }
120 +
121 + /**
122 + * Checks that a flow operation contains the correct values.
123 + *
124 + * @param op flow rule operation to check
125 + * @param type type the flow rule operation should have
126 + * @param deviceId device id the flow rule operation should have
127 + */
128 + void checkFlowOperation(FlowRuleOperation op,
129 + FlowRuleOperation.Type type,
130 + DeviceId deviceId) {
131 + assertThat(op.type(), is(type));
132 + assertThat(op.rule().deviceId(), equalTo(deviceId));
133 + }
134 +
135 +}
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 +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 =
51 + Optional.ofNullable(MplsLabel.mplsLabel(10));
52 + private final Optional<MplsLabel> egressLabel =
53 + Optional.ofNullable(MplsLabel.mplsLabel(20));
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,
77 + ImmutableList.of());
78 + }
79 +
80 + /**
81 + * Tests activation and deactivation of the installer.
82 + */
83 + @Test
84 + public void activateDeactivate() {
85 + installer.activate();
86 + installer.deactivate();
87 + }
88 +
89 + /**
90 + * Tests installation operation of the MPLS path intent installer.
91 + */
92 + @Test
93 + public void install() {
94 + installer.activate();
95 +
96 + List<Collection<FlowRuleOperation>> operations =
97 + installer.install(intent);
98 + assertThat(operations, notNullValue());
99 + assertThat(operations, hasSize(1));
100 +
101 + Collection<FlowRuleOperation> flowRuleOpsCollection = operations.get(0);
102 + assertThat(flowRuleOpsCollection, hasSize(hops));
103 + FlowRuleOperation[] flowRuleOps =
104 + flowRuleOpsCollection.toArray(new FlowRuleOperation[hops]);
105 +
106 + FlowRuleOperation op0 = flowRuleOps[0];
107 + checkFlowOperation(op0, FlowRuleOperation.Type.ADD, d2p0.deviceId());
108 +
109 + installer.deactivate();
110 + }
111 +
112 + /**
113 + * Checks the uninstall operation of the path intent installer.
114 + */
115 + @Test
116 + public void uninstall() {
117 + installer.activate();
118 +
119 + List<Collection<FlowRuleOperation>> operations =
120 + installer.uninstall(intent);
121 + assertThat(operations, notNullValue());
122 + assertThat(operations, hasSize(1));
123 +
124 + Collection<FlowRuleOperation> flowRuleOpsCollection = operations.get(0);
125 + assertThat(flowRuleOpsCollection, hasSize(hops));
126 + FlowRuleOperation[] flowRuleOps =
127 + flowRuleOpsCollection.toArray(new FlowRuleOperation[hops]);
128 +
129 + FlowRuleOperation op0 = flowRuleOps[0];
130 + checkFlowOperation(op0, FlowRuleOperation.Type.REMOVE, d2p0.deviceId());
131 +
132 + installer.deactivate();
133 + }
134 +}
...@@ -19,54 +19,31 @@ import java.util.Arrays; ...@@ -19,54 +19,31 @@ import java.util.Arrays;
19 import java.util.Collection; 19 import java.util.Collection;
20 import java.util.List; 20 import java.util.List;
21 21
22 -import org.junit.After;
23 import org.junit.Before; 22 import org.junit.Before;
24 import org.junit.Test; 23 import org.junit.Test;
25 -import org.onosproject.core.ApplicationId;
26 -import org.onosproject.core.CoreService;
27 -import org.onosproject.core.CoreServiceAdapter;
28 -import org.onosproject.core.IdGenerator;
29 -import org.onosproject.net.ConnectPoint;
30 import org.onosproject.net.DefaultLink; 24 import org.onosproject.net.DefaultLink;
31 import org.onosproject.net.DefaultPath; 25 import org.onosproject.net.DefaultPath;
32 -import org.onosproject.net.DeviceId;
33 import org.onosproject.net.Link; 26 import org.onosproject.net.Link;
34 import org.onosproject.net.flow.FlowRuleOperation; 27 import org.onosproject.net.flow.FlowRuleOperation;
35 -import org.onosproject.net.intent.FakeIntentManager;
36 -import org.onosproject.net.intent.Intent;
37 -import org.onosproject.net.intent.IntentInstaller;
38 -import org.onosproject.net.intent.IntentTestsMocks;
39 -import org.onosproject.net.intent.MockIdGenerator;
40 import org.onosproject.net.intent.PathIntent; 28 import org.onosproject.net.intent.PathIntent;
41 29
42 import com.google.common.collect.ImmutableList; 30 import com.google.common.collect.ImmutableList;
43 31
44 import static org.hamcrest.MatcherAssert.assertThat; 32 import static org.hamcrest.MatcherAssert.assertThat;
45 -import static org.hamcrest.Matchers.*; 33 +import static org.hamcrest.Matchers.hasSize;
34 +import static org.hamcrest.Matchers.notNullValue;
46 import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; 35 import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
47 import static org.onosproject.net.Link.Type.DIRECT; 36 import static org.onosproject.net.Link.Type.DIRECT;
48 import static org.onosproject.net.NetTestTools.APP_ID; 37 import static org.onosproject.net.NetTestTools.APP_ID;
49 import static org.onosproject.net.NetTestTools.PID; 38 import static org.onosproject.net.NetTestTools.PID;
50 -import static org.onosproject.net.NetTestTools.connectPoint;
51 39
52 /** 40 /**
53 * Unit tests for path intent installer. 41 * Unit tests for path intent installer.
54 */ 42 */
55 -public class PathIntentInstallerTest { 43 +public class PathIntentInstallerTest extends IntentInstallerTest {
56 44
57 - CoreService testCoreService;
58 - IdGenerator idGenerator = new MockIdGenerator();
59 PathIntentInstaller installer; 45 PathIntentInstaller installer;
60 46
61 - private final IntentTestsMocks.MockSelector selector = new IntentTestsMocks.MockSelector();
62 - private final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
63 - private final ConnectPoint d1p1 = connectPoint("s1", 0);
64 - private final ConnectPoint d2p0 = connectPoint("s2", 0);
65 - private final ConnectPoint d2p1 = connectPoint("s2", 1);
66 - private final ConnectPoint d3p1 = connectPoint("s3", 1);
67 - private final ConnectPoint d3p0 = connectPoint("s3", 10);
68 - private final ConnectPoint d1p0 = connectPoint("s1", 10);
69 -
70 private final List<Link> links = Arrays.asList( 47 private final List<Link> links = Arrays.asList(
71 createEdgeLink(d1p0, true), 48 createEdgeLink(d1p0, true),
72 new DefaultLink(PID, d1p1, d2p0, DIRECT), 49 new DefaultLink(PID, d1p1, d2p0, DIRECT),
...@@ -80,65 +57,15 @@ public class PathIntentInstallerTest { ...@@ -80,65 +57,15 @@ public class PathIntentInstallerTest {
80 * Configures objects used in all the test cases. 57 * Configures objects used in all the test cases.
81 */ 58 */
82 @Before 59 @Before
83 - public void localSetup() { 60 + public void localSetUp() {
84 - testCoreService = new TestCoreService();
85 - Intent.bindIdGenerator(idGenerator);
86 installer = new PathIntentInstaller(); 61 installer = new PathIntentInstaller();
87 installer.coreService = testCoreService; 62 installer.coreService = testCoreService;
88 - installer.intentManager = new MockIntentManager(); 63 + installer.intentManager = new MockIntentManager(PathIntent.class);
89 intent = new PathIntent(APP_ID, selector, treatment, 64 intent = new PathIntent(APP_ID, selector, treatment,
90 new DefaultPath(PID, links, hops), ImmutableList.of()); 65 new DefaultPath(PID, links, hops), ImmutableList.of());
91 } 66 }
92 67
93 /** 68 /**
94 - * Tears down objects used in all the test cases.
95 - */
96 - @After
97 - public void localTearDown() {
98 - Intent.unbindIdGenerator(idGenerator);
99 - }
100 -
101 - /**
102 - * Mock for core service.
103 - */
104 - private static class TestCoreService extends CoreServiceAdapter {
105 -
106 - String registeredId = "";
107 -
108 - @Override
109 - public ApplicationId registerApplication(String identifier) {
110 - registeredId = identifier;
111 - return APP_ID;
112 - }
113 - }
114 -
115 - /**
116 - * Mock for intent manager service. Checks that the PathIntent
117 - * installer installs and uninstalls properly.
118 - */
119 - private static class MockIntentManager extends FakeIntentManager {
120 -
121 - boolean installerRegistered = false;
122 -
123 - @Override
124 - public <T extends Intent> void registerInstaller(
125 - Class<T> cls,
126 - IntentInstaller<T> installer) {
127 - assertThat(cls.getCanonicalName(),
128 - equalTo("org.onosproject.net.intent.PathIntent"));
129 - installerRegistered = true;
130 - }
131 -
132 - @Override
133 - public <T extends Intent> void unregisterInstaller(Class<T> cls) {
134 - assertThat(cls.getCanonicalName(),
135 - equalTo("org.onosproject.net.intent.PathIntent"));
136 - assertThat(installerRegistered, is(true));
137 - }
138 -
139 - }
140 -
141 - /**
142 * Tests activation and deactivation of the installer. 69 * Tests activation and deactivation of the installer.
143 */ 70 */
144 @Test 71 @Test
...@@ -148,20 +75,6 @@ public class PathIntentInstallerTest { ...@@ -148,20 +75,6 @@ public class PathIntentInstallerTest {
148 } 75 }
149 76
150 /** 77 /**
151 - * Checks that a flow operation contains the correct values.
152 - *
153 - * @param op flow rule operation to check
154 - * @param type type the flow rule operation should have
155 - * @param deviceId device id the flow rule operation should have
156 - */
157 - private void checkFlowOperation(FlowRuleOperation op,
158 - FlowRuleOperation.Type type,
159 - DeviceId deviceId) {
160 - assertThat(op.type(), is(type));
161 - assertThat(op.rule().deviceId(), equalTo(deviceId));
162 - }
163 -
164 - /**
165 * Tests installation operation of the path intent installer. 78 * Tests installation operation of the path intent installer.
166 */ 79 */
167 @Test 80 @Test
......