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