blob: 393e56baa99fe9e81c1e1c63225c339a533eb985 [file] [log] [blame]
Sho SHIMIZU892509a2015-02-24 14:44:31 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZU892509a2015-02-24 14:44:31 -08003 *
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
Sho SHIMIZU892509a2015-02-24 14:44:31 -080018import org.junit.Before;
19import org.junit.Test;
20import org.onosproject.TestApplicationId;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.core.IdGenerator;
23import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.DefaultLink;
25import org.onosproject.net.DefaultPath;
26import org.onosproject.net.Link;
27import org.onosproject.net.Path;
28import org.onosproject.net.flow.DefaultTrafficSelector;
29import org.onosproject.net.flow.DefaultTrafficTreatment;
30import org.onosproject.net.flow.TrafficSelector;
31import org.onosproject.net.flow.TrafficTreatment;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070032import org.onosproject.net.intent.AbstractIntentTest;
Yuta HIGUCHId95d5902016-06-27 00:18:45 -070033import org.onosproject.net.intent.IntentCompilationException;
Sho SHIMIZU892509a2015-02-24 14:44:31 -080034import org.onosproject.net.intent.IntentData;
Sho SHIMIZU892509a2015-02-24 14:44:31 -080035import org.onosproject.net.intent.PathIntent;
36import org.onosproject.net.intent.PointToPointIntent;
Sho SHIMIZU892509a2015-02-24 14:44:31 -080037import org.onosproject.net.intent.impl.IntentProcessor;
38import org.onosproject.net.provider.ProviderId;
39import org.onosproject.store.Timestamp;
40
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070041import java.util.Collections;
Sho SHIMIZU892509a2015-02-24 14:44:31 -080042import java.util.List;
43import java.util.Optional;
44
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070045import static org.easymock.EasyMock.*;
Sho SHIMIZU892509a2015-02-24 14:44:31 -080046import static org.hamcrest.Matchers.instanceOf;
47import static org.hamcrest.Matchers.is;
48import static org.junit.Assert.assertThat;
49import static org.onosproject.net.DeviceId.deviceId;
50import static org.onosproject.net.Link.Type.DIRECT;
51import static org.onosproject.net.PortNumber.portNumber;
52import static org.onosproject.net.intent.IntentState.INSTALL_REQ;
53
54/**
55 * Unit tests for Compiling phase.
56 */
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070057public class CompilingTest extends AbstractIntentTest {
Sho SHIMIZU892509a2015-02-24 14:44:31 -080058
59 private final ApplicationId appId = new TestApplicationId("test");
60 private final ProviderId pid = new ProviderId("of", "test");
Brian O'Connor6b528132015-03-10 16:39:52 -070061 private final TrafficSelector selector = DefaultTrafficSelector.emptySelector();
62 private final TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
Sho SHIMIZU892509a2015-02-24 14:44:31 -080063 private final ConnectPoint cp1 = new ConnectPoint(deviceId("1"), portNumber(1));
64 private final ConnectPoint cp2 = new ConnectPoint(deviceId("1"), portNumber(2));
65 private final ConnectPoint cp3 = new ConnectPoint(deviceId("2"), portNumber(1));
66 private final ConnectPoint cp4 = new ConnectPoint(deviceId("2"), portNumber(2));
67
Ray Milkey2693bda2016-01-22 16:08:14 -080068 private final List<Link> links = Collections.singletonList(
69 DefaultLink.builder().providerId(pid).src(cp2).dst(cp4).type(DIRECT).build());
Sho SHIMIZU892509a2015-02-24 14:44:31 -080070 private final Path path = new DefaultPath(pid, links, 10);
71
72 private PointToPointIntent input;
73 private PathIntent compiled;
74
75 private IdGenerator idGenerator;
76 private IntentProcessor processor;
77 private Timestamp version;
78
79 @Before
80 public void setUp() {
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070081 super.setUp();
82
Sho SHIMIZU892509a2015-02-24 14:44:31 -080083 processor = createMock(IntentProcessor.class);
84 version = createMock(Timestamp.class);
85
Sho SHIMIZU892509a2015-02-24 14:44:31 -080086 // Intent creation should be placed after binding an ID generator
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070087 input = PointToPointIntent.builder()
88 .appId(appId)
89 .selector(selector)
90 .treatment(treatment)
91 .ingressPoint(cp1)
92 .egressPoint(cp3)
93 .build();
Ray Milkeyebc5d222015-03-18 15:45:36 -070094 compiled = PathIntent.builder()
95 .appId(appId)
96 .selector(selector)
97 .treatment(treatment)
98 .path(path)
99 .build();
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800100 }
101
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800102 /**
103 * Tests a next phase when no exception occurs.
104 */
105 @Test
106 public void testMoveToNextPhaseWithoutError() {
107 IntentData pending = new IntentData(input, INSTALL_REQ, version);
108
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700109 expect(processor.compile(input, null)).andReturn(Collections.singletonList(compiled));
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800110 replay(processor);
111
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700112 Compiling sut = new Compiling(processor, pending, Optional.empty());
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800113
114 Optional<IntentProcessPhase> output = sut.execute();
115
116 verify(processor);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800117 assertThat(output.get(), is(instanceOf(Installing.class)));
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800118 }
119
120 /**
121 * Tests a next phase when IntentCompilationException occurs.
122 */
123 @Test
124 public void testWhenIntentCompilationExceptionOccurs() {
125 IntentData pending = new IntentData(input, INSTALL_REQ, version);
126
127 expect(processor.compile(input, null)).andThrow(new IntentCompilationException());
128 replay(processor);
129
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700130 Compiling sut = new Compiling(processor, pending, Optional.empty());
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800131
132 Optional<IntentProcessPhase> output = sut.execute();
133
134 verify(processor);
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700135 assertThat(output.get(), is(instanceOf(Failed.class)));
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800136 }
137}