blob: d282fbc3e6c9a8f0670f42794b22901df5d13e66 [file] [log] [blame]
Ray Milkeyb82c42b2015-06-30 09:42:20 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkeyb82c42b2015-06-30 09:42:20 -07003 *
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.codec.impl;
17
18import java.io.IOException;
19import java.io.InputStream;
20
21import org.junit.Assert;
22import org.junit.Before;
23import org.junit.Test;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.core.CoreService;
26import org.onosproject.net.Link;
27import org.onosproject.net.intent.Constraint;
28import org.onosproject.net.intent.constraint.AnnotationConstraint;
29import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
30import org.onosproject.net.intent.constraint.BandwidthConstraint;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070031import org.onosproject.net.intent.constraint.LatencyConstraint;
32import org.onosproject.net.intent.constraint.LinkTypeConstraint;
33import org.onosproject.net.intent.constraint.ObstacleConstraint;
34import org.onosproject.net.intent.constraint.WaypointConstraint;
35
36import com.fasterxml.jackson.databind.JsonNode;
37import com.fasterxml.jackson.databind.node.ObjectNode;
38
39import static com.google.common.base.Preconditions.checkNotNull;
40import static org.easymock.EasyMock.createMock;
41import static org.easymock.EasyMock.expect;
42import static org.easymock.EasyMock.replay;
43import static org.hamcrest.MatcherAssert.assertThat;
44import static org.hamcrest.Matchers.hasItem;
45import static org.hamcrest.Matchers.hasSize;
46import static org.hamcrest.Matchers.instanceOf;
47import static org.hamcrest.Matchers.is;
48import static org.hamcrest.Matchers.notNullValue;
49import static org.onosproject.net.NetTestTools.APP_ID;
50import static org.onosproject.net.NetTestTools.did;
51
52/**
53 * Unit tests for Constraint codec.
54 */
55public class ConstraintCodecTest {
56
57 MockCodecContext context;
58 JsonCodec<Constraint> constraintCodec;
59 final CoreService mockCoreService = createMock(CoreService.class);
60
61 /**
62 * Sets up for each test. Creates a context and fetches the flow rule
63 * codec.
64 */
65 @Before
66 public void setUp() {
67 context = new MockCodecContext();
68 constraintCodec = context.codec(Constraint.class);
69 assertThat(constraintCodec, notNullValue());
70
71 expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID))
72 .andReturn(APP_ID).anyTimes();
73 replay(mockCoreService);
74 context.registerService(CoreService.class, mockCoreService);
75 }
76
77 /**
78 * Reads in a constraint from the given resource and decodes it.
79 *
80 * @param resourceName resource to use to read the JSON for the constraint
81 * @return decoded constraint
82 */
83 private Constraint getConstraint(String resourceName) {
84 InputStream jsonStream = ConstraintCodecTest.class
85 .getResourceAsStream(resourceName);
86 try {
87 JsonNode json = context.mapper().readTree(jsonStream);
88 assertThat(json, notNullValue());
89 Constraint constraint = constraintCodec.decode((ObjectNode) json, context);
90 assertThat(constraint, notNullValue());
91 return checkNotNull(constraint);
92 } catch (IOException ioe) {
93 Assert.fail(ioe.getMessage());
94 throw new IllegalStateException("cannot happen");
95 }
96 }
97
98
99 /**
100 * Tests link type constraint.
101 */
102 @Test
103 public void linkTypeConstraint() {
104 Constraint constraint = getConstraint("LinkTypeConstraint.json");
105 assertThat(constraint, instanceOf(LinkTypeConstraint.class));
106
107 LinkTypeConstraint linkTypeConstraint = (LinkTypeConstraint) constraint;
108 assertThat(linkTypeConstraint.isInclusive(), is(false));
109 assertThat(linkTypeConstraint.types(), hasSize(2));
110 assertThat(linkTypeConstraint.types(), hasItem(Link.Type.OPTICAL));
111 assertThat(linkTypeConstraint.types(), hasItem(Link.Type.DIRECT));
112 }
113
114 /**
115 * Tests annotation constraint.
116 */
117 @Test
118 public void annotationConstraint() {
119 Constraint constraint = getConstraint("AnnotationConstraint.json");
120 assertThat(constraint, instanceOf(AnnotationConstraint.class));
121
122 AnnotationConstraint annotationConstraint = (AnnotationConstraint) constraint;
123 assertThat(annotationConstraint.key(), is("key"));
124 assertThat(annotationConstraint.threshold(), is(123.0D));
125 }
126
127 /**
128 * Tests bandwidth constraint.
129 */
130 @Test
131 public void bandwidthConstraint() {
132 Constraint constraint = getConstraint("BandwidthConstraint.json");
133 assertThat(constraint, instanceOf(BandwidthConstraint.class));
134
135 BandwidthConstraint bandwidthConstraint = (BandwidthConstraint) constraint;
Sho SHIMIZUa88db492015-11-23 13:21:04 -0800136 assertThat(bandwidthConstraint.bandwidth().bps(), is(345.678D));
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700137 }
138
139 /**
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700140 * Tests latency constraint.
141 */
142 @Test
143 public void latencyConstraint() {
144 Constraint constraint = getConstraint("LatencyConstraint.json");
145 assertThat(constraint, instanceOf(LatencyConstraint.class));
146
147 LatencyConstraint latencyConstraint = (LatencyConstraint) constraint;
148 assertThat(latencyConstraint.latency().toMillis(), is(111L));
149 }
150
151 /**
152 * Tests obstacle constraint.
153 */
154 @Test
155 public void obstacleConstraint() {
156 Constraint constraint = getConstraint("ObstacleConstraint.json");
157 assertThat(constraint, instanceOf(ObstacleConstraint.class));
158
159 ObstacleConstraint obstacleConstraint = (ObstacleConstraint) constraint;
160
161 assertThat(obstacleConstraint.obstacles(), hasItem(did("dev1")));
162 assertThat(obstacleConstraint.obstacles(), hasItem(did("dev2")));
163 assertThat(obstacleConstraint.obstacles(), hasItem(did("dev3")));
164 }
165
166 /**
167 * Tests waypoint constaint.
168 */
169 @Test
170 public void waypointConstraint() {
171 Constraint constraint = getConstraint("WaypointConstraint.json");
172 assertThat(constraint, instanceOf(WaypointConstraint.class));
173
174 WaypointConstraint waypointConstraint = (WaypointConstraint) constraint;
175
176 assertThat(waypointConstraint.waypoints(), hasItem(did("devA")));
177 assertThat(waypointConstraint.waypoints(), hasItem(did("devB")));
178 assertThat(waypointConstraint.waypoints(), hasItem(did("devC")));
179 }
180
181 /**
182 * Tests asymmetric path constraint.
183 */
184 @Test
185 public void asymmetricPathConstraint() {
186 Constraint constraint = getConstraint("AsymmetricPathConstraint.json");
187 assertThat(constraint, instanceOf(AsymmetricPathConstraint.class));
188 }
189}