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