blob: 95adebcd02b01b7a4f04666959d8ad04f78011a7 [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;
Ray Milkey239f96b2018-11-28 09:31:47 -080044 private static final Object CACHE_LOCK = new Object();
Ramon Casellas03f194f2018-11-15 16:06:02 +010045
Ray Milkey239f96b2018-11-28 09:31:47 -080046 //banning public construction
Ramon Casellas03f194f2018-11-15 16:06:02 +010047 private OpenConfigConnectionCache() {
48 }
49
50 /**
51 * Initializes the cache if not already present.
52 * If present returns the existing one.
53 *
54 * @return single instance of cache
55 */
56 public static OpenConfigConnectionCache init() {
Ray Milkey239f96b2018-11-28 09:31:47 -080057 synchronized (CACHE_LOCK) {
58 if (cache == null) {
59 cache = new OpenConfigConnectionCache();
60 }
Ramon Casellas03f194f2018-11-15 16:06:02 +010061 }
62 return cache;
63 }
64
65 /**
66 * Returns the number of rules stored for a given device.
67 *
68 * @param did the device
69 * @return number of flows stored
70 */
71 public int size(DeviceId did) {
72 synchronized (smp) {
73 if (!smp.containsKey(did)) {
74 return 0;
75 }
76 return smp.get(did).size();
77 }
78 }
79
80 /**
81 * Returns the flow with given Id for the specific device.
82 *
83 * @param did device id
84 * @param flowId flow id
85 * @return the flow rule
86 */
87 public FlowRule get(DeviceId did, FlowId flowId) {
88 synchronized (smp) {
89 if (!smp.containsKey(did)) {
90 return null;
91 }
92 Set<FlowRule> set = smp.get(did);
93 return set.stream()
94 .filter(r -> r.id() == flowId)
95 .findFirst()
96 .orElse(null);
97 }
98 }
99
100 /**
101 * Returns all the flows for the specific device.
102 *
103 * @param did device id
104 * @return Set of flow rules
105 */
106 public Set<FlowRule> get(DeviceId did) {
107 synchronized (smp) {
108 if (!smp.containsKey(did)) {
109 return null;
110 }
111 return smp.get(did);
112 }
113 }
114
115 /**
116 * Add a flows for the specific device.
117 *
118 * @param did device id
119 * @param flowRule the flow rule
120 */
121 public void add(DeviceId did, FlowRule flowRule) {
122 synchronized (smp) {
123 Set<FlowRule> set;
124 if (smp.containsKey(did)) {
125 set = smp.get(did);
126 } else {
127 set = new HashSet<FlowRule>();
128 log.warn("OpenConfigConnectionCache created for {}", did);
129 smp.put(did, set);
130 }
131 set.add(flowRule);
132 }
133 }
134
135 /**
136 * Add a flows for the specific device.
137 *
138 * @param did device id
139 * @param flowRule the flow rule
140 */
141 public void remove(DeviceId did, FlowRule flowRule) {
142 synchronized (smp) {
143 if (!smp.containsKey(did)) {
144 return;
145 }
146 Set<FlowRule> set = smp.get(did);
147 set.removeIf(r2 -> r2.id() == flowRule.id());
148 }
149 }
150}