blob: c504501029f3d3248c3ee316c4e012632ad88321 [file] [log] [blame]
Sho SHIMIZU892509a2015-02-24 14:44:31 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Ray Milkeya7cf8c82018-02-08 15:07:06 -080020import org.onlab.graph.ScalarWeight;
Sho SHIMIZU892509a2015-02-24 14:44:31 -080021import 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.TrafficSelector;
32import org.onosproject.net.flow.TrafficTreatment;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070033import org.onosproject.net.intent.AbstractIntentTest;
Yuta HIGUCHId95d5902016-06-27 00:18:45 -070034import org.onosproject.net.intent.IntentCompilationException;
Sho SHIMIZU892509a2015-02-24 14:44:31 -080035import org.onosproject.net.intent.IntentData;
Sho SHIMIZU892509a2015-02-24 14:44:31 -080036import org.onosproject.net.intent.PathIntent;
37import org.onosproject.net.intent.PointToPointIntent;
Sho SHIMIZU892509a2015-02-24 14:44:31 -080038import org.onosproject.net.intent.impl.IntentProcessor;
39import org.onosproject.net.provider.ProviderId;
40import org.onosproject.store.Timestamp;
41
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070042import java.util.Collections;
Sho SHIMIZU892509a2015-02-24 14:44:31 -080043import java.util.List;
44import java.util.Optional;
45
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070046import static org.easymock.EasyMock.*;
Sho SHIMIZU892509a2015-02-24 14:44:31 -080047import static org.hamcrest.Matchers.instanceOf;
48import static org.hamcrest.Matchers.is;
49import static org.junit.Assert.assertThat;
50import static org.onosproject.net.DeviceId.deviceId;
51import static org.onosproject.net.Link.Type.DIRECT;
52import static org.onosproject.net.PortNumber.portNumber;
53import static org.onosproject.net.intent.IntentState.INSTALL_REQ;
54
55/**
56 * Unit tests for Compiling phase.
57 */
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070058public class CompilingTest extends AbstractIntentTest {
Sho SHIMIZU892509a2015-02-24 14:44:31 -080059
60 private final ApplicationId appId = new TestApplicationId("test");
61 private final ProviderId pid = new ProviderId("of", "test");
Brian O'Connor6b528132015-03-10 16:39:52 -070062 private final TrafficSelector selector = DefaultTrafficSelector.emptySelector();
63 private final TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
Sho SHIMIZU892509a2015-02-24 14:44:31 -080064 private final ConnectPoint cp1 = new ConnectPoint(deviceId("1"), portNumber(1));
65 private final ConnectPoint cp2 = new ConnectPoint(deviceId("1"), portNumber(2));
66 private final ConnectPoint cp3 = new ConnectPoint(deviceId("2"), portNumber(1));
67 private final ConnectPoint cp4 = new ConnectPoint(deviceId("2"), portNumber(2));
68
Ray Milkey2693bda2016-01-22 16:08:14 -080069 private final List<Link> links = Collections.singletonList(
70 DefaultLink.builder().providerId(pid).src(cp2).dst(cp4).type(DIRECT).build());
Ray Milkeya7cf8c82018-02-08 15:07:06 -080071 private final Path path = new DefaultPath(pid, links, ScalarWeight.toWeight(10));
Sho SHIMIZU892509a2015-02-24 14:44:31 -080072
73 private PointToPointIntent input;
74 private PathIntent compiled;
75
76 private IdGenerator idGenerator;
77 private IntentProcessor processor;
78 private Timestamp version;
79
80 @Before
81 public void setUp() {
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070082 super.setUp();
83
Sho SHIMIZU892509a2015-02-24 14:44:31 -080084 processor = createMock(IntentProcessor.class);
85 version = createMock(Timestamp.class);
86
Sho SHIMIZU892509a2015-02-24 14:44:31 -080087 // Intent creation should be placed after binding an ID generator
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070088 input = PointToPointIntent.builder()
89 .appId(appId)
90 .selector(selector)
91 .treatment(treatment)
92 .ingressPoint(cp1)
93 .egressPoint(cp3)
94 .build();
Ray Milkeyebc5d222015-03-18 15:45:36 -070095 compiled = PathIntent.builder()
96 .appId(appId)
97 .selector(selector)
98 .treatment(treatment)
99 .path(path)
100 .build();
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800101 }
102
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800103 /**
104 * Tests a next phase when no exception occurs.
105 */
106 @Test
107 public void testMoveToNextPhaseWithoutError() {
108 IntentData pending = new IntentData(input, INSTALL_REQ, version);
109
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700110 expect(processor.compile(input, null)).andReturn(Collections.singletonList(compiled));
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800111 replay(processor);
112
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700113 Compiling sut = new Compiling(processor, pending, Optional.empty());
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800114
115 Optional<IntentProcessPhase> output = sut.execute();
116
117 verify(processor);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800118 assertThat(output.get(), is(instanceOf(Installing.class)));
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800119 }
120
121 /**
122 * Tests a next phase when IntentCompilationException occurs.
123 */
124 @Test
125 public void testWhenIntentCompilationExceptionOccurs() {
126 IntentData pending = new IntentData(input, INSTALL_REQ, version);
127
128 expect(processor.compile(input, null)).andThrow(new IntentCompilationException());
129 replay(processor);
130
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700131 Compiling sut = new Compiling(processor, pending, Optional.empty());
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800132
133 Optional<IntentProcessPhase> output = sut.execute();
134
135 verify(processor);
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700136 assertThat(output.get(), is(instanceOf(Failed.class)));
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800137 }
138}