8ec09bd1d0ee5ce32783a4b0581bf84d8c95e63c
[onosfw.git] /
1 /*
2  * Copyright 2015 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 package org.onosproject.net.intent.impl.compiler;
17
18 import com.google.common.collect.ImmutableList;
19 import org.onlab.packet.MplsLabel;
20 import org.onosproject.net.newresource.ResourceAllocation;
21 import org.onosproject.net.newresource.ResourceConsumer;
22 import org.onosproject.net.newresource.ResourceListener;
23 import org.onosproject.net.newresource.ResourcePath;
24 import org.onosproject.net.newresource.ResourceService;
25
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Optional;
31 import java.util.stream.Collectors;
32
33 class MockResourceService implements ResourceService {
34
35     private final Map<ResourcePath, ResourceConsumer> assignment = new HashMap<>();
36
37     @Override
38     public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<ResourcePath> resources) {
39         assignment.putAll(
40                 resources.stream().collect(Collectors.toMap(x -> x, x -> consumer))
41         );
42
43         return resources.stream()
44                 .map(x -> new ResourceAllocation(x, consumer))
45                 .collect(Collectors.toList());
46     }
47
48     @Override
49     public boolean release(List<ResourceAllocation> allocations) {
50         allocations.forEach(x -> assignment.remove(x.resource()));
51
52         return true;
53     }
54
55     @Override
56     public boolean release(ResourceConsumer consumer) {
57         List<ResourcePath> resources = assignment.entrySet().stream()
58                 .filter(x -> x.getValue().equals(consumer))
59                 .map(Map.Entry::getKey)
60                 .collect(Collectors.toList());
61         List<ResourceAllocation> allocations = resources.stream()
62                 .map(x -> new ResourceAllocation(x, consumer))
63                 .collect(Collectors.toList());
64
65         return release(allocations);
66     }
67
68     @Override
69     public Optional<ResourceAllocation> getResourceAllocation(ResourcePath resource) {
70         return Optional.ofNullable(assignment.get(resource))
71                 .map(x -> new ResourceAllocation(resource, x));
72     }
73
74     @Override
75     public <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls) {
76         return assignment.entrySet().stream()
77                 .filter(x -> x.getKey().parent().isPresent())
78                 .filter(x -> x.getKey().parent().get().equals(parent))
79                 .map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
80                 .collect(Collectors.toList());
81     }
82
83     @Override
84     public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
85         return assignment.entrySet().stream()
86                 .filter(x -> x.getValue().equals(consumer))
87                 .map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
88                 .collect(Collectors.toList());
89     }
90
91     @Override
92     public Collection<ResourcePath> getAvailableResources(ResourcePath parent) {
93         ResourcePath resource = ResourcePath.child(parent, MplsLabel.mplsLabel(10));
94         return ImmutableList.of(resource);
95     }
96
97     @Override
98     public boolean isAvailable(ResourcePath resource) {
99         return true;
100     }
101
102     @Override
103     public void addListener(ResourceListener listener) {}
104
105     @Override
106     public void removeListener(ResourceListener listener) {}
107 }