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