aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/core/api/src/test/java/org/onosproject/net/flowobjective/ObjectiveTest.java
blob: 850582b0272dc0d43bd442a60b2c577652582135 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.net.flowobjective;

import org.junit.Test;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.flow.criteria.Criteria;
import org.onosproject.net.flow.criteria.Criterion;

import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
import static org.onosproject.net.NetTestTools.APP_ID;
import static org.onosproject.net.flowobjective.FilteringObjective.Type.DENY;
import static org.onosproject.net.flowobjective.ForwardingObjective.Flag.SPECIFIC;
import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
import static org.onosproject.net.flowobjective.Objective.Operation.ADD;
import static org.onosproject.net.flowobjective.Objective.Operation.REMOVE;

/**
 * Unit tests for forwarding objective class.
 */
public class ObjectiveTest {

    private final TrafficTreatment treatment =
            DefaultTrafficTreatment.emptyTreatment();
    private final TrafficSelector selector =
            DefaultTrafficSelector.emptySelector();
    private final Criterion criterion = Criteria.dummy();
    private final Criterion key = Criteria.dummy();

    /**
     * Mock objective context.
     */
    private static class MockObjectiveContext implements ObjectiveContext {
        @Override
        public void onSuccess(Objective objective) {
            // stub
        }

        @Override
        public void onError(Objective objective, ObjectiveError error) {
            // stub
        }
    }

    /**
     * Checks immutability of objective classes.
     */
    @Test
    public void testImmutability() {
        assertThatClassIsImmutable(DefaultFilteringObjective.class);
        assertThatClassIsImmutable(DefaultForwardingObjective.class);
        assertThatClassIsImmutable(DefaultNextObjective.class);
    }

    //  Forwarding Objectives

    /**
     * Makes a forwarding objective builder with a set of default values.
     *
     * @return forwarding objective builder
     */
    private ForwardingObjective.Builder baseForwardingBuilder() {
        return DefaultForwardingObjective.builder()
                .withSelector(selector)
                .withTreatment(treatment)
                .withFlag(SPECIFIC)
                .fromApp(APP_ID)
                .withPriority(22)
                .makeTemporary(5)
                .nextStep(33);
    }

    /**
     * Checks the default values of a forwarding objective object.
     *
     * @param objective forwarding objective to check
     */
    private void checkForwardingBase(ForwardingObjective objective,
                                     Objective.Operation op,
                                     ObjectiveContext expectedContext) {
        assertThat(objective.permanent(), is(false));
        assertThat(objective.timeout(), is(5));
        assertThat(objective.selector(), is(selector));
        assertThat(objective.treatment(), is(treatment));
        assertThat(objective.flag(), is(SPECIFIC));
        assertThat(objective.appId(), is(APP_ID));
        assertThat(objective.nextId(), is(33));
        assertThat(objective.id(), is(not(0)));
        assertThat(objective.priority(), is(22));
        assertThat(objective.op(), is(op));
        if (objective.context().isPresent()) {
            assertThat(objective.context().get(), is(expectedContext));
        } else {
            assertThat(expectedContext, nullValue());
        }
    }

    /**
     * Tests that forwarding objective objects are built correctly using the
     * add() method.
     */
    @Test
    public void testForwardingAdd() {
        checkForwardingBase(baseForwardingBuilder().add(), ADD, null);
    }

    /**
     * Tests that forwarding objective objects are built correctly using the
     * add(context) method.
     */
    @Test
    public void testForwardingAddWithContext() {
        ObjectiveContext context = new MockObjectiveContext();
        checkForwardingBase(baseForwardingBuilder().add(context), ADD, context);
    }

    /**
     * Tests that forwarding objective objects are built correctly using the
     * remove() method.
     */
    @Test
    public void testForwardingRemove() {
        checkForwardingBase(baseForwardingBuilder().remove(), REMOVE, null);
    }

    /**
     * Tests that forwarding objective objects are built correctly using the
     * remove(context) method.
     */
    @Test
    public void testForwardingRemoveWithContext() {
        ObjectiveContext context = new MockObjectiveContext();
        checkForwardingBase(baseForwardingBuilder().remove(context), REMOVE, context);
    }

    // Filtering objectives

    /**
     * Makes a filtering objective builder with a set of default values.
     *
     * @return filtering objective builder
     */
    private FilteringObjective.Builder baseFilteringBuilder() {
        return DefaultFilteringObjective.builder()
                .withKey(key)
                .withPriority(5)
                .addCondition(criterion)
                .fromApp(APP_ID)
                .makeTemporary(2)
                .deny();
    }

    /**
     * Checks the default values of a filtering objective object.
     *
     * @param objective filtering objective to check
     */
    private void checkFilteringBase(FilteringObjective objective,
                                    Objective.Operation op,
                                    ObjectiveContext expectedContext) {
        assertThat(objective.key(), is(key));
        assertThat(objective.conditions(), hasItem(criterion));
        assertThat(objective.permanent(), is(false));
        assertThat(objective.timeout(), is(2));
        assertThat(objective.priority(), is(5));
        assertThat(objective.appId(), is(APP_ID));
        assertThat(objective.type(), is(DENY));
        assertThat(objective.id(), is(not(0)));
        assertThat(objective.op(), is(op));
        if (objective.context().isPresent()) {
            assertThat(objective.context().get(), is(expectedContext));
        } else {
            assertThat(expectedContext, nullValue());
        }
    }

    /**
     * Tests that forwarding objective objects are built correctly using the
     * add() method.
     */
    @Test
    public void testFilteringAdd() {
        checkFilteringBase(baseFilteringBuilder().add(), ADD, null);
    }

    /**
     * Tests that forwarding objective objects are built correctly using the
     * add(context) method.
     */
    @Test
    public void testFilteringAddWithContext() {
        ObjectiveContext context = new MockObjectiveContext();
        checkFilteringBase(baseFilteringBuilder().add(context), ADD, context);
    }

    /**
     * Tests that forwarding objective objects are built correctly using the
     * remove() method.
     */
    @Test
    public void testFilteringRemove() {
        checkFilteringBase(baseFilteringBuilder().remove(), REMOVE, null);
    }

    /**
     * Tests that forwarding objective objects are built correctly using the
     * remove(context) method.
     */
    @Test
    public void testFilteringRemoveWithContext() {
        ObjectiveContext context = new MockObjectiveContext();
        checkFilteringBase(baseFilteringBuilder().remove(context), REMOVE, context);
    }

    // Next objectives

    /**
     * Makes a next objective builder with a set of default values.
     *
     * @return next objective builder
     */
    private NextObjective.Builder baseNextBuilder() {
        return DefaultNextObjective.builder()
                .addTreatment(treatment)
                .withId(12)
                .withType(HASHED)
                .makeTemporary(777)
                .withPriority(33)
                .fromApp(APP_ID);
    }

    /**
     * Checks the default values of a next objective object.
     *
     * @param objective next objective to check
     */
    private void checkNextBase(NextObjective objective,
                               Objective.Operation op,
                               ObjectiveContext expectedContext) {
        assertThat(objective.id(), is(12));
        assertThat(objective.appId(), is(APP_ID));
        assertThat(objective.type(), is(HASHED));
        assertThat(objective.next(), hasItem(treatment));
        assertThat(objective.permanent(), is(false));
        assertThat(objective.timeout(), is(0));
        assertThat(objective.priority(), is(0));
        assertThat(objective.op(), is(op));
        if (objective.context().isPresent()) {
            assertThat(objective.context().get(), is(expectedContext));
        } else {
            assertThat(expectedContext, nullValue());
        }
    }

    /**
     * Tests that forwarding objective objects are built correctly using the
     * add() method.
     */
    @Test
    public void testNextAdd() {
        checkNextBase(baseNextBuilder().add(), ADD, null);
    }

    /**
     * Tests that forwarding objective objects are built correctly using the
     * add(context) method.
     */
    @Test
    public void testNextAddWithContext() {
        ObjectiveContext context = new MockObjectiveContext();
        checkNextBase(baseNextBuilder().add(context), ADD, context);
    }

    /**
     * Tests that forwarding objective objects are built correctly using the
     * remove() method.
     */
    @Test
    public void testNextRemove() {
        checkNextBase(baseNextBuilder().remove(), REMOVE, null);
    }

    /**
     * Tests that forwarding objective objects are built correctly using the
     * remove(context) method.
     */
    @Test
    public void testNextRemoveWithContext() {
        ObjectiveContext context = new MockObjectiveContext();
        checkNextBase(baseNextBuilder().remove(context), REMOVE, context);
    }
}