Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.t3.api; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.group.Group; |
| 22 | |
| 23 | import java.util.Set; |
| 24 | import java.util.stream.Collectors; |
| 25 | |
| 26 | /** |
| 27 | * Represents Network Information Base (NIB) for groups |
| 28 | * and supports alternative functions to |
| 29 | * {@link org.onosproject.net.group.GroupService} for offline data. |
| 30 | */ |
Seyeon Jeong | 5018bdd | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 31 | public class GroupNib extends AbstractNib { |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 32 | |
| 33 | // TODO with method optimization, store into subdivided structures at the first load |
| 34 | private Set<Group> groups; |
| 35 | |
| 36 | // use the singleton helper to create the instance |
| 37 | protected GroupNib() { |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Sets a set of groups. |
| 42 | * |
| 43 | * @param groups group set |
| 44 | */ |
| 45 | public void setGroups(Set<Group> groups) { |
| 46 | this.groups = groups; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the set of groups. |
| 51 | * |
| 52 | * @return group set |
| 53 | */ |
| 54 | public Set<Group> getGroups() { |
| 55 | return ImmutableSet.copyOf(groups); |
| 56 | } |
| 57 | |
| 58 | /** |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame^] | 59 | * Returns all groups associated with the given device and filtered by the group state. |
| 60 | * |
| 61 | * @param deviceId device ID to get groups for |
| 62 | * @param groupState the group state |
| 63 | * @return iterable of device's groups |
| 64 | */ |
| 65 | public Iterable<Group> getGroupsByState(DeviceId deviceId, Group.GroupState groupState) { |
| 66 | Set<Group> groupsFiltered = groups.stream() |
| 67 | .filter(group -> group.state() == groupState |
| 68 | && group.deviceId().equals(deviceId)) |
| 69 | .collect(Collectors.toSet()); |
| 70 | return ImmutableSet.copyOf(groupsFiltered); |
| 71 | } |
| 72 | |
| 73 | /** |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 74 | * Returns all groups associated with the given device. |
| 75 | * |
| 76 | * @param deviceId device ID to get groups for |
| 77 | * @return iterable of device's groups |
| 78 | */ |
| 79 | public Iterable<Group> getGroups(DeviceId deviceId) { |
| 80 | Set<Group> groupsFiltered = groups.stream() |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame^] | 81 | .filter(group -> group.deviceId().equals(deviceId)) |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 82 | .collect(Collectors.toSet()); |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame^] | 83 | return ImmutableSet.copyOf(groupsFiltered); |
Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns the singleton instance of groups NIB. |
| 88 | * |
| 89 | * @return instance of groups NIB |
| 90 | */ |
| 91 | public static GroupNib getInstance() { |
| 92 | return GroupNib.SingletonHelper.INSTANCE; |
| 93 | } |
| 94 | |
| 95 | private static class SingletonHelper { |
| 96 | private static final GroupNib INSTANCE = new GroupNib(); |
| 97 | } |
| 98 | |
| 99 | } |