blob: aecb57c1a5b3ce5d5d48b7d44d106385c1817170 [file] [log] [blame]
Seyeon Jeong357bcec2020-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.flow.FlowEntry;
22
23import java.util.Set;
24import java.util.stream.Collectors;
25
26/**
27 * Represents Network Information Base (NIB) for flows
28 * and supports alternative functions to
29 * {@link org.onosproject.net.flow.FlowRuleService} for offline data.
30 */
31public class FlowNib {
32
33 // TODO with method optimization, store into subdivided structures at the first load
34 private Set<FlowEntry> flows;
35
36 // use the singleton helper to create the instance
37 protected FlowNib() {
38 }
39
40 /**
41 * Sets a set of flows.
42 *
43 * @param flows flow set
44 */
45 public void setFlows(Set<FlowEntry> flows) {
46 this.flows = flows;
47 }
48
49 /**
50 * Returns the set of flows.
51 *
52 * @return flow set
53 */
54 public Set<FlowEntry> getFlows() {
55 return ImmutableSet.copyOf(flows);
56 }
57
58 /**
59 * Returns a list of rules filtered by device id and flow state.
60 *
61 * @param deviceId the device id to lookup
62 * @param flowState the flow state to lookup
63 * @return collection of flow entries
64 */
65 public Iterable<FlowEntry> getFlowEntriesByState(DeviceId deviceId, FlowEntry.FlowEntryState flowState) {
66 Set<FlowEntry> flowsFiltered = flows.stream()
67 .filter(flow -> flow.state() == flowState
68 && flow.deviceId().equals(deviceId))
69 .collect(Collectors.toSet());
70 return flowsFiltered != null ? ImmutableSet.copyOf(flowsFiltered) : ImmutableSet.of();
71 }
72
73 /**
74 * Returns the singleton instance of flows NIB.
75 *
76 * @return instance of flows NIB
77 */
78 public static FlowNib getInstance() {
79 return SingletonHelper.INSTANCE;
80 }
81
82 private static class SingletonHelper {
83 private static final FlowNib INSTANCE = new FlowNib();
84 }
85
86}