blob: 64d6575880cbd7d609a8caf497fddfb7890f7be4 [file] [log] [blame]
Seyeon Jeong8d3cad22020-02-28 01:17:34 -08001/*
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
17package org.onosproject.t3.api;
18
19import com.google.common.collect.ImmutableSet;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.group.Group;
22
23import java.util.Set;
24import 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 */
31public class GroupNib {
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 /**
59 * Returns all groups associated with the given device.
60 *
61 * @param deviceId device ID to get groups for
62 * @return iterable of device's groups
63 */
64 public Iterable<Group> getGroups(DeviceId deviceId) {
65 Set<Group> groupsFiltered = groups.stream()
66 .filter(g -> g.deviceId().equals(deviceId))
67 .collect(Collectors.toSet());
68 return groupsFiltered != null ? ImmutableSet.copyOf(groupsFiltered) : ImmutableSet.of();
69 }
70
71 /**
72 * Returns the singleton instance of groups NIB.
73 *
74 * @return instance of groups NIB
75 */
76 public static GroupNib getInstance() {
77 return GroupNib.SingletonHelper.INSTANCE;
78 }
79
80 private static class SingletonHelper {
81 private static final GroupNib INSTANCE = new GroupNib();
82 }
83
84}