blob: 2a5593135b65fc92f3e038c5db7b9caf076e386a [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
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.TrafficSelector;
32import org.onosproject.net.flow.TrafficTreatment;
33import org.onosproject.net.intent.Intent;
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;
36import org.onosproject.net.intent.MockIdGenerator;
37import org.onosproject.net.intent.PathIntent;
38import org.onosproject.net.intent.PointToPointIntent;
Sho SHIMIZU892509a2015-02-24 14:44:31 -080039import org.onosproject.net.intent.impl.IntentProcessor;
40import org.onosproject.net.provider.ProviderId;
41import org.onosproject.store.Timestamp;
42
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070043import java.util.Collections;
Sho SHIMIZU892509a2015-02-24 14:44:31 -080044import java.util.List;
45import java.util.Optional;
46
47import static org.easymock.EasyMock.createMock;
48import static org.easymock.EasyMock.expect;
49import static org.easymock.EasyMock.replay;
50import static org.easymock.EasyMock.verify;
51import static org.hamcrest.Matchers.instanceOf;
52import static org.hamcrest.Matchers.is;
53import static org.junit.Assert.assertThat;
54import static org.onosproject.net.DeviceId.deviceId;
55import static org.onosproject.net.Link.Type.DIRECT;
56import static org.onosproject.net.PortNumber.portNumber;
57import static org.onosproject.net.intent.IntentState.INSTALL_REQ;
58
59/**
60 * Unit tests for Compiling phase.
61 */
62public class CompilingTest {
63
64 private final ApplicationId appId = new TestApplicationId("test");
65 private final ProviderId pid = new ProviderId("of", "test");
Brian O'Connor6b528132015-03-10 16:39:52 -070066 private final TrafficSelector selector = DefaultTrafficSelector.emptySelector();
67 private final TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
Sho SHIMIZU892509a2015-02-24 14:44:31 -080068 private final ConnectPoint cp1 = new ConnectPoint(deviceId("1"), portNumber(1));
69 private final ConnectPoint cp2 = new ConnectPoint(deviceId("1"), portNumber(2));
70 private final ConnectPoint cp3 = new ConnectPoint(deviceId("2"), portNumber(1));
71 private final ConnectPoint cp4 = new ConnectPoint(deviceId("2"), portNumber(2));
72
Ray Milkey2693bda2016-01-22 16:08:14 -080073 private final List<Link> links = Collections.singletonList(
74 DefaultLink.builder().providerId(pid).src(cp2).dst(cp4).type(DIRECT).build());
Sho SHIMIZU892509a2015-02-24 14:44:31 -080075 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
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070094 input = PointToPointIntent.builder()
95 .appId(appId)
96 .selector(selector)
97 .treatment(treatment)
98 .ingressPoint(cp1)
99 .egressPoint(cp3)
100 .build();
Ray Milkeyebc5d222015-03-18 15:45:36 -0700101 compiled = PathIntent.builder()
102 .appId(appId)
103 .selector(selector)
104 .treatment(treatment)
105 .path(path)
106 .build();
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800107 }
108
109
110 @After
111 public void tearDown() {
112 Intent.unbindIdGenerator(idGenerator);
113 }
114
115 /**
116 * Tests a next phase when no exception occurs.
117 */
118 @Test
119 public void testMoveToNextPhaseWithoutError() {
120 IntentData pending = new IntentData(input, INSTALL_REQ, version);
121
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700122 expect(processor.compile(input, null)).andReturn(Collections.singletonList(compiled));
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800123 replay(processor);
124
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700125 Compiling sut = new Compiling(processor, pending, Optional.empty());
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800126
127 Optional<IntentProcessPhase> output = sut.execute();
128
129 verify(processor);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800130 assertThat(output.get(), is(instanceOf(Installing.class)));
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800131 }
132
133 /**
134 * Tests a next phase when IntentCompilationException occurs.
135 */
136 @Test
137 public void testWhenIntentCompilationExceptionOccurs() {
138 IntentData pending = new IntentData(input, INSTALL_REQ, version);
139
140 expect(processor.compile(input, null)).andThrow(new IntentCompilationException());
141 replay(processor);
142
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700143 Compiling sut = new Compiling(processor, pending, Optional.empty());
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800144
145 Optional<IntentProcessPhase> output = sut.execute();
146
147 verify(processor);
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700148 assertThat(output.get(), is(instanceOf(Failed.class)));
Sho SHIMIZU892509a2015-02-24 14:44:31 -0800149 }
150}