blob: 56d005ac53eef1213dcf55a1ddffaf31f1eaafa2 [file] [log] [blame]
Sho SHIMIZU68470162016-07-05 11:54:57 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
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 */
16
17package org.onosproject.store.resource.impl;
18
19import com.google.common.collect.ImmutableList;
20import org.junit.Test;
21import org.onlab.util.Bandwidth;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.PortNumber;
24import org.onosproject.net.intent.IntentId;
25import org.onosproject.net.resource.ContinuousResource;
26import org.onosproject.net.resource.ResourceAllocation;
27import org.onosproject.net.resource.ResourceConsumer;
28import org.onosproject.net.resource.Resources;
29
Sho SHIMIZU81416d02016-07-06 14:53:28 -070030import static org.hamcrest.Matchers.hasItem;
Sho SHIMIZU68470162016-07-05 11:54:57 -070031import static org.hamcrest.Matchers.is;
32import static org.junit.Assert.assertThat;
33
34/**
35 * Unit tests for ContinuousResourceAllocation.
36 */
37public class ContinuousResourceAllocationTest {
38
39 private static final DeviceId DID = DeviceId.deviceId("a");
40 private static final PortNumber PN1 = PortNumber.portNumber(1);
41
42 @Test
Sho SHIMIZU709a1792016-07-05 11:55:53 -070043 public void testNoAllocationHasEnoughResource() {
44 ContinuousResource original =
45 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
46
47 ContinuousResourceAllocation sut = ContinuousResourceAllocation.empty(original);
48
49 ContinuousResource request =
50 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(100).bps());
51
52 assertThat(sut.hasEnoughResource(request), is(true));
53 }
54
55 @Test
56 public void testHasEnoughResourceWhenSmallResourceIsRequested() {
57 ContinuousResource original =
58 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
59 ContinuousResource allocated =
60 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
61 ResourceConsumer consumer = IntentId.valueOf(1);
62
63 ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
64 ImmutableList.of(new ResourceAllocation(allocated, consumer)));
65
66 ContinuousResource request =
67 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(200).bps());
68 assertThat(sut.hasEnoughResource(request), is(true));
69 }
70
71 @Test
72 public void testHasEnoughResourceWhenLargeResourceIsRequested() {
73 ContinuousResource original =
74 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
75 ContinuousResource allocated =
76 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
77 ResourceConsumer consumer = IntentId.valueOf(1);
78
79 ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
80 ImmutableList.of(new ResourceAllocation(allocated, consumer)));
81
82 ContinuousResource request =
83 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(600).bps());
84 assertThat(sut.hasEnoughResource(request), is(false));
85 }
86
87 @Test
88 public void testHasEnoughResourceWhenExactResourceIsRequested() {
89 ContinuousResource original =
90 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
91 ContinuousResource allocated =
92 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
93 ResourceConsumer consumer = IntentId.valueOf(1);
94
95 ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
96 ImmutableList.of(new ResourceAllocation(allocated, consumer)));
97
98 ContinuousResource request =
99 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
100 assertThat(sut.hasEnoughResource(request), is(true));
101 }
102
103 @Test
Sho SHIMIZU68470162016-07-05 11:54:57 -0700104 public void testReleaseWhenAllocatedResourceIsRequested() {
105 ContinuousResource original =
106 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
107 ContinuousResource allocated =
108 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
109 ResourceConsumer consumer = IntentId.valueOf(1);
110
111 ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
112 ImmutableList.of(new ResourceAllocation(allocated, consumer)));
113
114 ContinuousResource request =
115 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
116
117 ContinuousResourceAllocation released = sut.release(request, consumer.consumerId());
118
119 assertThat(released.allocations().isEmpty(), is(true));
120 }
121
122 @Test
123 public void testReleaseWhenDifferentConsumerIsSpecified() {
124 ContinuousResource original =
125 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
126 ContinuousResource allocated =
127 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
128 ResourceConsumer consumer = IntentId.valueOf(1);
129 ResourceConsumer otherConsumer = IntentId.valueOf(2);
130
131 ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
132 ImmutableList.of(new ResourceAllocation(allocated, consumer)));
133
134 ContinuousResource request =
135 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
136
137 ImmutableList<ResourceAllocation> allocations = sut.release(request, otherConsumer.consumerId()).allocations();
138
139 assertThat(allocations.size(), is(1));
140 assertThat(allocations.get(0).resource().equals(allocated), is(true));
141 }
Sho SHIMIZU81416d02016-07-06 14:53:28 -0700142
143 @Test
144 public void testAllocateDifferentValue() {
145 ContinuousResource original =
146 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
147 ContinuousResource allocated =
148 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
149 ResourceConsumer consumer = IntentId.valueOf(1);
150
151 ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
152 ImmutableList.of(new ResourceAllocation(allocated, consumer)));
153
154 ContinuousResource request =
155 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(200).bps());
156
157 ContinuousResourceAllocation newValue = sut.allocate(new ResourceAllocation(request, consumer));
158
159 assertThat(newValue.allocations().size(), is(2));
160 assertThat(newValue.allocations(), hasItem(new ResourceAllocation(allocated, consumer)));
161 assertThat(newValue.allocations(), hasItem(new ResourceAllocation(request, consumer)));
162 }
163
164 @Test
165 public void testAllocateSameValue() {
166 ContinuousResource original =
167 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
168 ContinuousResource allocated =
169 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(300).bps());
170 ResourceConsumer consumer = IntentId.valueOf(1);
171
172 ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
173 ImmutableList.of(new ResourceAllocation(allocated, consumer)));
174
175 ContinuousResource request =
176 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(300).bps());
177
178 ContinuousResourceAllocation newValue = sut.allocate(new ResourceAllocation(request, consumer));
179
180 assertThat(newValue.allocations().size(), is(2));
181 assertThat(newValue.allocations()
182 .stream()
183 .allMatch(x -> x.equals(new ResourceAllocation(allocated, consumer))), is(true));
184 }
Sho SHIMIZU68470162016-07-05 11:54:57 -0700185}