blob: ba0ea76d320263c0c24a1695507d110282010a20 [file] [log] [blame]
Andrea Campanellabb66e092019-01-28 13:50:06 +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.onlab.osgi.DefaultServiceDirectory;
22import org.onlab.util.KryoNamespace;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.flow.FlowId;
25import org.onosproject.net.flow.FlowRule;
26import org.onosproject.store.serializers.KryoNamespaces;
27import org.onosproject.store.service.EventuallyConsistentMap;
28import org.onosproject.store.service.StorageService;
29import org.onosproject.store.service.WallClockTimestamp;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import java.util.HashSet;
34import java.util.Set;
35import java.util.stream.Collectors;
36
37/**
38 * Stores a set of Rules for given open config based devices in order to properly report them to the store.
39 */
40public final class DeviceConnectionCache {
41 private static final Logger log =
42 LoggerFactory.getLogger(DeviceConnectionCache.class);
43
44 private static final StorageService STORAGE_SERVICE = DefaultServiceDirectory.getService(StorageService.class);
45
46 private static final String MAP_NAME = "onos-odtn-flow-cache";
47
48 private static final KryoNamespace SERIALIZER = KryoNamespace.newBuilder()
49 .register(KryoNamespaces.API)
50 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
51 .register(DeviceConnection.class).build();
52
53 private EventuallyConsistentMap<DeviceId, Set<DeviceConnection>>
54 flowCache;
55
56 private static DeviceConnectionCache cache = null;
57 private static final Object CACHE_LOCK = new Object();
58
59 //banning public construction
60 private DeviceConnectionCache() {
61 flowCache = STORAGE_SERVICE
62 .<DeviceId, Set<DeviceConnection>>eventuallyConsistentMapBuilder()
63 .withName(MAP_NAME)
64 .withSerializer(SERIALIZER)
65 .withTimestampProvider((k, v) -> new WallClockTimestamp())
66 .build();
67 }
68
69 /**
70 * Initializes the cache if not already present.
71 * If present returns the existing one.
72 *
73 * @return single instance of cache
74 */
75 public static DeviceConnectionCache init() {
76 synchronized (CACHE_LOCK) {
77 if (cache == null) {
78 cache = new DeviceConnectionCache();
79 }
80 }
81 return cache;
82 }
83
84 /**
85 * Returns the number of rules stored for a given device.
86 *
87 * @param did the device
88 * @return number of flows stored
89 */
90 public int size(DeviceId did) {
91 if (!flowCache.containsKey(did)) {
92 return 0;
93 }
94 return flowCache.get(did).size();
95 }
96
97 /**
98 * Returns the flow with given Id for the specific device.
99 *
100 * @param did device id
101 * @param flowId flow id
102 * @return the flow rule
103 */
104 public FlowRule get(DeviceId did, FlowId flowId) {
105 if (!flowCache.containsKey(did)) {
106 return null;
107 }
108 Set<DeviceConnection> set = flowCache.get(did);
109 DeviceConnection connection = set.stream()
110 .filter(c -> c.getFlowRule().id() == flowId)
111 .findFirst()
112 .orElse(null);
113 return connection != null ? connection.getFlowRule() : null;
114 }
115
116 /**
117 * Returns the flow with given Id for the specific device.
118 *
119 * @param did device id
120 * @param connectionId the device specific connection id
121 * @return the flow rule
122 */
123 public FlowRule get(DeviceId did, String connectionId) {
124 if (!flowCache.containsKey(did)) {
125 return null;
126 }
127 Set<DeviceConnection> set = flowCache.get(did);
128 DeviceConnection connection = set.stream()
129 .filter(c -> c.getId().equals(connectionId))
130 .findFirst()
131 .orElse(null);
132 return connection != null ? connection.getFlowRule() : null;
133 }
134
135 /**
136 * Returns all the flows for the specific device.
137 *
138 * @param did device id
139 * @return Set of flow rules
140 */
141 public Set<FlowRule> get(DeviceId did) {
142 if (!flowCache.containsKey(did)) {
143 return null;
144 }
145 return flowCache.get(did).stream()
146 .map(DeviceConnection::getFlowRule)
147 .collect(Collectors.toSet());
148 }
149
150 /**
151 * Add to a specific device a flow and a device specific connection id for that flow.
152 *
153 * @param did device id
154 * @param connectionId the device specific connection identifier
155 * @param flowRule the flow rule
156 */
157 public void add(DeviceId did, String connectionId, FlowRule flowRule) {
158 Set<DeviceConnection> set;
159 if (flowCache.containsKey(did)) {
160 set = flowCache.get(did);
161 } else {
162 set = new HashSet<>();
163 log.debug("DeviceConnectionCache created for {}", did);
164 flowCache.put(did, set);
165 }
166 set.add(DeviceConnection.of(connectionId, flowRule));
167 }
168
169 /**
170 * Add a flows for the specific device.
171 *
172 * @param did device id
173 * @param flowRule the flow rule
174 */
175 public void remove(DeviceId did, FlowRule flowRule) {
176 if (!flowCache.containsKey(did)) {
177 return;
178 }
179 Set<DeviceConnection> set = flowCache.get(did);
180 set.removeIf(r2 -> r2.getFlowRule().id() == flowRule.id());
181 }
182
183 /**
184 * Add a flows for the specific device.
185 *
186 * @param did device id
187 * @param connectionId the connectionId as identified on the Device
188 */
189 public void remove(DeviceId did, String connectionId) {
190 if (!flowCache.containsKey(did)) {
191 return;
192 }
193 Set<DeviceConnection> set = flowCache.get(did);
194 set.removeIf(r2 -> r2.getId().equals(connectionId));
195 }
196}