blob: 656d9408954167bf52a5b271ac21df740d1c9037 [file] [log] [blame]
Sho SHIMIZU64d48672015-02-25 08:59:37 -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.phase;
17
18import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
21import org.onosproject.TestApplicationId;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.core.IdGenerator;
24import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DefaultLink;
26import org.onosproject.net.DefaultPath;
27import org.onosproject.net.Link;
28import org.onosproject.net.Path;
29import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.DefaultTrafficTreatment;
31import org.onosproject.net.flow.FlowRuleOperations;
32import org.onosproject.net.flow.TrafficSelector;
33import org.onosproject.net.flow.TrafficTreatment;
34import org.onosproject.net.intent.Intent;
35import org.onosproject.net.intent.IntentData;
36import org.onosproject.net.intent.MockIdGenerator;
37import org.onosproject.net.intent.PathIntent;
38import org.onosproject.net.intent.PointToPointIntent;
39import org.onosproject.net.intent.impl.IntentInstallationException;
40import org.onosproject.net.intent.impl.IntentProcessor;
41import org.onosproject.net.provider.ProviderId;
42import org.onosproject.store.Timestamp;
43
44import java.util.Arrays;
45import java.util.List;
46import java.util.Optional;
47
48import static org.easymock.EasyMock.createMock;
49import static org.easymock.EasyMock.expectLastCall;
50import static org.easymock.EasyMock.replay;
51import static org.easymock.EasyMock.verify;
52import static org.hamcrest.Matchers.instanceOf;
53import static org.hamcrest.Matchers.is;
54import static org.junit.Assert.assertThat;
55import static org.onosproject.net.DeviceId.deviceId;
56import static org.onosproject.net.Link.Type.DIRECT;
57import static org.onosproject.net.PortNumber.portNumber;
58import static org.onosproject.net.intent.IntentState.INSTALL_REQ;
59
60/**
61 * Unit tests for Installing phase.
62 */
63public class InstallingTest {
64
65 private final ApplicationId appId = new TestApplicationId("test");
66 private final ProviderId pid = new ProviderId("of", "test");
67 private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
68 private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
69 private final ConnectPoint cp1 = new ConnectPoint(deviceId("1"), portNumber(1));
70 private final ConnectPoint cp2 = new ConnectPoint(deviceId("1"), portNumber(2));
71 private final ConnectPoint cp3 = new ConnectPoint(deviceId("2"), portNumber(1));
72 private final ConnectPoint cp4 = new ConnectPoint(deviceId("2"), portNumber(2));
73
74 private final List<Link> links = Arrays.asList(new DefaultLink(pid, cp2, cp4, DIRECT));
75 private final Path path = new DefaultPath(pid, links, 10);
76
77 private PointToPointIntent input;
78 private PathIntent compiled;
79
80 private IdGenerator idGenerator;
81 private IntentProcessor processor;
82 private Timestamp version;
83
84 @Before
85 public void setUp() {
86 processor = createMock(IntentProcessor.class);
87 version = createMock(Timestamp.class);
88
89 idGenerator = new MockIdGenerator();
90
91 Intent.bindIdGenerator(idGenerator);
92
93 // Intent creation should be placed after binding an ID generator
94 input = new PointToPointIntent(appId, selector, treatment, cp1, cp3);
95 compiled = new PathIntent(appId, selector, treatment, path);
96 }
97
98
99 @After
100 public void tearDown() {
101 Intent.unbindIdGenerator(idGenerator);
102 }
103
104 /**
105 * Tests a next phase when no exception occurs.
106 */
107 @Test
108 public void testMoveToNextPhaseWithoutError() {
109 IntentData pending = new IntentData(input, INSTALL_REQ, version);
110 pending.setInstallables(Arrays.asList(compiled));
111
112 FlowRuleOperations operations = createMock(FlowRuleOperations.class);
113
114 processor.applyFlowRules(operations);
115 replay(processor);
116
117 Installing sut = new Installing(processor, pending, operations);
118
119 Optional<IntentProcessPhase> executed = sut.execute();
120 verify(processor);
121 assertThat(executed.get(), is(instanceOf(Installed.class)));
122 }
123
124 /**
125 * Test a next phase when IntentInstallationException occurs.
126 */
127 @Test
128 public void testWhenIntentInstallationExceptionOccurs() {
129 IntentData pending = new IntentData(input, INSTALL_REQ, version);
130 pending.setInstallables(Arrays.asList(compiled));
131
132 FlowRuleOperations operations = createMock(FlowRuleOperations.class);
133
134 processor.applyFlowRules(operations);
135 expectLastCall().andThrow(new IntentInstallationException());
136 replay(processor);
137
138 Installing sut = new Installing(processor, pending, operations);
139
140 Optional<IntentProcessPhase> executed = sut.execute();
141 verify(processor);
142 assertThat(executed.get(), is(instanceOf(InstallingFailed.class)));
143 }
144}