blob: 5d2c7e742ca20e6731c659e5572b903f35483ebc [file] [log] [blame]
Ramon Casellas03f194f2018-11-15 16:06:02 +01001/*
2 * Copyright 2018-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 * This work was partially supported by EC H2020 project METRO-HAUL (761727).
17 */
18
19package org.onosproject.drivers.odtn.impl;
20
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.flow.FlowId;
23import org.onosproject.net.flow.FlowRule;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import java.util.Collections;
28import java.util.HashMap;
29import java.util.HashSet;
30import java.util.Map;
31import java.util.Set;
32
33/**
34 * Stores a set of Rules for given open config based devices in order to properly report them to the store.
35 */
36public final class OpenConfigConnectionCache {
37 private static final Logger log =
38 LoggerFactory.getLogger(OpenConfigConnectionCache.class);
39
40 private Map<DeviceId, Set<FlowRule>> mp = new HashMap<>();
41 private Map<DeviceId, Set<FlowRule>> smp = Collections.synchronizedMap(mp);
42
43 private static OpenConfigConnectionCache cache = null;
44
45 //banning public contraction
46 private OpenConfigConnectionCache() {
47 }
48
49 /**
50 * Initializes the cache if not already present.
51 * If present returns the existing one.
52 *
53 * @return single instance of cache
54 */
55 public static OpenConfigConnectionCache init() {
56 if (cache == null) {
57 cache = new OpenConfigConnectionCache();
58 }
59 return cache;
60 }
61
62 /**
63 * Returns the number of rules stored for a given device.
64 *
65 * @param did the device
66 * @return number of flows stored
67 */
68 public int size(DeviceId did) {
69 synchronized (smp) {
70 if (!smp.containsKey(did)) {
71 return 0;
72 }
73 return smp.get(did).size();
74 }
75 }
76
77 /**
78 * Returns the flow with given Id for the specific device.
79 *
80 * @param did device id
81 * @param flowId flow id
82 * @return the flow rule
83 */
84 public FlowRule get(DeviceId did, FlowId flowId) {
85 synchronized (smp) {
86 if (!smp.containsKey(did)) {
87 return null;
88 }
89 Set<FlowRule> set = smp.get(did);
90 return set.stream()
91 .filter(r -> r.id() == flowId)
92 .findFirst()
93 .orElse(null);
94 }
95 }
96
97 /**
98 * Returns all the flows for the specific device.
99 *
100 * @param did device id
101 * @return Set of flow rules
102 */
103 public Set<FlowRule> get(DeviceId did) {
104 synchronized (smp) {
105 if (!smp.containsKey(did)) {
106 return null;
107 }
108 return smp.get(did);
109 }
110 }
111
112 /**
113 * Add a flows for the specific device.
114 *
115 * @param did device id
116 * @param flowRule the flow rule
117 */
118 public void add(DeviceId did, FlowRule flowRule) {
119 synchronized (smp) {
120 Set<FlowRule> set;
121 if (smp.containsKey(did)) {
122 set = smp.get(did);
123 } else {
124 set = new HashSet<FlowRule>();
125 log.warn("OpenConfigConnectionCache created for {}", did);
126 smp.put(did, set);
127 }
128 set.add(flowRule);
129 }
130 }
131
132 /**
133 * Add a flows for the specific device.
134 *
135 * @param did device id
136 * @param flowRule the flow rule
137 */
138 public void remove(DeviceId did, FlowRule flowRule) {
139 synchronized (smp) {
140 if (!smp.containsKey(did)) {
141 return;
142 }
143 Set<FlowRule> set = smp.get(did);
144 set.removeIf(r2 -> r2.id() == flowRule.id());
145 }
146 }
147}