blob: add26ad5d7db853950693a87da26f7e7a85d6507 [file] [log] [blame]
Toshio Koide32336182014-10-30 13:02:47 -07001/*
2 * Copyright 2014 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 */
16package org.onlab.onos.store.trivial.impl;
17
Toshio Koide32336182014-10-30 13:02:47 -070018import java.util.HashSet;
19import java.util.Set;
20
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
Toshio Koide8e5e91e2014-11-13 12:27:12 -080024import org.onlab.onos.net.AnnotationKeys;
25import org.onlab.onos.net.Annotations;
Toshio Koide32336182014-10-30 13:02:47 -070026import org.onlab.onos.net.ConnectPoint;
Toshio Koide8e5e91e2014-11-13 12:27:12 -080027import org.onlab.onos.net.DefaultAnnotations;
Toshio Koide32336182014-10-30 13:02:47 -070028import org.onlab.onos.net.DefaultLink;
29import org.onlab.onos.net.Link;
30import org.onlab.onos.net.provider.ProviderId;
31import org.onlab.onos.net.resource.Bandwidth;
32import org.onlab.onos.net.resource.BandwidthResourceAllocation;
33import org.onlab.onos.net.resource.LambdaResourceAllocation;
34import org.onlab.onos.net.resource.LinkResourceAllocations;
35import org.onlab.onos.net.resource.LinkResourceStore;
36import org.onlab.onos.net.resource.ResourceAllocation;
37import org.onlab.onos.net.resource.ResourceType;
38
Ray Milkeye97ede92014-11-20 10:43:12 -080039import static org.junit.Assert.assertEquals;
40import static org.junit.Assert.assertFalse;
41import static org.junit.Assert.assertNotNull;
42import static org.onlab.onos.net.DeviceId.deviceId;
43import static org.onlab.onos.net.Link.Type.DIRECT;
44import static org.onlab.onos.net.PortNumber.portNumber;
45
Toshio Koide32336182014-10-30 13:02:47 -070046/**
47 * Test of the simple LinkResourceStore implementation.
48 */
49public class SimpleLinkResourceStoreTest {
50
51 private LinkResourceStore store;
52 private SimpleLinkResourceStore simpleStore;
53 private Link link1;
54 private Link link2;
55 private Link link3;
56
57 /**
58 * Returns {@link Link} object.
59 *
60 * @param dev1 source device
61 * @param port1 source port
62 * @param dev2 destination device
63 * @param port2 destination port
64 * @return created {@link Link} object
65 */
66 private Link newLink(String dev1, int port1, String dev2, int port2) {
Toshio Koide8e5e91e2014-11-13 12:27:12 -080067 Annotations annotations = DefaultAnnotations.builder()
68 .set(AnnotationKeys.OPTICAL_WAVES, "80")
69 .set(AnnotationKeys.BANDWIDTH, "1000000")
70 .build();
Toshio Koide32336182014-10-30 13:02:47 -070071 return new DefaultLink(
72 new ProviderId("of", "foo"),
73 new ConnectPoint(deviceId(dev1), portNumber(port1)),
74 new ConnectPoint(deviceId(dev2), portNumber(port2)),
Toshio Koide8e5e91e2014-11-13 12:27:12 -080075 DIRECT, annotations);
Toshio Koide32336182014-10-30 13:02:47 -070076 }
77
78 @Before
79 public void setUp() throws Exception {
80 simpleStore = new SimpleLinkResourceStore();
81 simpleStore.activate();
82 store = simpleStore;
83
84 link1 = newLink("of:1", 1, "of:2", 2);
85 link2 = newLink("of:2", 1, "of:3", 2);
86 link3 = newLink("of:3", 1, "of:4", 2);
87 }
88
89 @After
90 public void tearDown() throws Exception {
91 simpleStore.deactivate();
92 }
93
94 /**
95 * Tests constructor and activate method.
96 */
97 @Test
98 public void testConstructorAndActivate() {
99 final Iterable<LinkResourceAllocations> allAllocations = store.getAllocations();
100 assertNotNull(allAllocations);
101 assertFalse(allAllocations.iterator().hasNext());
102
103 final Iterable<LinkResourceAllocations> linkAllocations =
104 store.getAllocations(link1);
105 assertNotNull(linkAllocations);
106 assertFalse(linkAllocations.iterator().hasNext());
107
108 final Set<ResourceAllocation> res = store.getFreeResources(link2);
109 assertNotNull(res);
110 }
111
112 /**
113 * Picks up and returns one of bandwidth allocations from a given set.
114 *
115 * @param resources the set of {@link ResourceAllocation}s
116 * @return {@link BandwidthResourceAllocation} object if found, null
117 * otherwise
118 */
119 private BandwidthResourceAllocation getBandwidthObj(Set<ResourceAllocation> resources) {
120 for (ResourceAllocation res : resources) {
121 if (res.type() == ResourceType.BANDWIDTH) {
122 return ((BandwidthResourceAllocation) res);
123 }
124 }
125 return null;
126 }
127
128 /**
129 * Returns all lambda allocations from a given set.
130 *
131 * @param resources the set of {@link ResourceAllocation}s
132 * @return a set of {@link LambdaResourceAllocation} objects
133 */
134 private Set<LambdaResourceAllocation> getLambdaObjs(Set<ResourceAllocation> resources) {
135 Set<LambdaResourceAllocation> lambdaResources = new HashSet<>();
136 for (ResourceAllocation res : resources) {
137 if (res.type() == ResourceType.LAMBDA) {
138 lambdaResources.add((LambdaResourceAllocation) res);
139 }
140 }
141 return lambdaResources;
142 }
143
144 /**
145 * Tests initial free bandwidth for a link.
146 */
147 @Test
148 public void testInitialBandwidth() {
149 final Set<ResourceAllocation> freeRes = store.getFreeResources(link1);
150 assertNotNull(freeRes);
151
152 final BandwidthResourceAllocation alloc = getBandwidthObj(freeRes);
153 assertNotNull(alloc);
154
155 assertEquals(Bandwidth.valueOf(1000000.0), alloc.bandwidth());
156 }
157
158 /**
159 * Tests initial free lambda for a link.
160 */
161 @Test
162 public void testInitialLambdas() {
163 final Set<ResourceAllocation> freeRes = store.getFreeResources(link3);
164 assertNotNull(freeRes);
165
166 final Set<LambdaResourceAllocation> res = getLambdaObjs(freeRes);
167 assertNotNull(res);
Toshio Koide8e5e91e2014-11-13 12:27:12 -0800168 assertEquals(80, res.size());
Toshio Koide32336182014-10-30 13:02:47 -0700169 }
170}