Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Jonathan Hart | 6a8fd1d | 2015-02-25 15:44:37 -0800 | [diff] [blame] | 2 | * Copyright 2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.trivial.impl; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.intent.Intent; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 25 | import org.onosproject.net.intent.IntentData; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.net.intent.IntentEvent; |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 27 | import org.onosproject.net.intent.IntentState; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.net.intent.IntentStore; |
| 29 | import org.onosproject.net.intent.IntentStoreDelegate; |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 30 | import org.onosproject.net.intent.Key; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.store.AbstractStore; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 34 | import java.util.List; |
| 35 | import java.util.Map; |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 36 | import java.util.stream.Collectors; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 37 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 38 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame^] | 39 | import static org.onosproject.net.intent.IntentState.PURGE_REQ; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 40 | import static org.slf4j.LoggerFactory.getLogger; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 41 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Simple single-instance implementation of the intent store. |
| 44 | */ |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 45 | @Component(immediate = true) |
| 46 | @Service |
| 47 | public class SimpleIntentStore |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 48 | extends AbstractStore<IntentEvent, IntentStoreDelegate> |
| 49 | implements IntentStore { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 50 | |
| 51 | private final Logger log = getLogger(getClass()); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 52 | |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 53 | private final Map<Key, IntentData> current = Maps.newConcurrentMap(); |
Jonathan Hart | 6a8fd1d | 2015-02-25 15:44:37 -0800 | [diff] [blame] | 54 | private final Map<Key, IntentData> pending = Maps.newConcurrentMap(); |
| 55 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 56 | @Activate |
| 57 | public void activate() { |
| 58 | log.info("Started"); |
| 59 | } |
| 60 | |
| 61 | @Deactivate |
| 62 | public void deactivate() { |
| 63 | log.info("Stopped"); |
| 64 | } |
| 65 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 66 | @Override |
| 67 | public long getIntentCount() { |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 68 | return current.size(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public Iterable<Intent> getIntents() { |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 73 | return current.values().stream() |
| 74 | .map(IntentData::intent) |
| 75 | .collect(Collectors.toList()); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame^] | 79 | public Iterable<IntentData> getIntentData(boolean localOnly, long olderThan) { |
| 80 | if (localOnly || olderThan > 0) { |
| 81 | long older = System.nanoTime() - olderThan * 1_000_000; //convert ms to ns |
| 82 | final SystemClockTimestamp time = new SystemClockTimestamp(older); |
| 83 | return pending.values().stream() |
| 84 | .filter(data -> data.version().isOlderThan(time) && |
| 85 | (!localOnly || isMaster(data.key()))) |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 86 | .collect(Collectors.toList()); |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame^] | 87 | |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 88 | } |
| 89 | return Lists.newArrayList(current.values()); |
| 90 | } |
| 91 | |
| 92 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 93 | public IntentState getIntentState(Key intentKey) { |
| 94 | IntentData data = current.get(intentKey); |
| 95 | return (data != null) ? data.state() : null; |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 99 | public List<Intent> getInstallableIntents(Key intentKey) { |
| 100 | IntentData data = current.get(intentKey); |
Jonathan Hart | 6a8fd1d | 2015-02-25 15:44:37 -0800 | [diff] [blame] | 101 | if (data != null) { |
| 102 | return data.installables(); |
| 103 | } |
| 104 | return null; |
| 105 | } |
| 106 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 107 | @Override |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 108 | public void write(IntentData newData) { |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 109 | checkNotNull(newData); |
| 110 | |
Jonathan Hart | 6a8fd1d | 2015-02-25 15:44:37 -0800 | [diff] [blame] | 111 | synchronized (this) { |
| 112 | // TODO this could be refactored/cleaned up |
| 113 | IntentData currentData = current.get(newData.key()); |
| 114 | IntentData pendingData = pending.get(newData.key()); |
| 115 | |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 116 | if (IntentData.isUpdateAcceptable(currentData, newData)) { |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 117 | if (pendingData != null) { |
| 118 | if (pendingData.state() == PURGE_REQ) { |
| 119 | current.remove(newData.key(), newData); |
| 120 | } else { |
| 121 | current.put(newData.key(), new IntentData(newData)); |
| 122 | } |
Jonathan Hart | 6a8fd1d | 2015-02-25 15:44:37 -0800 | [diff] [blame] | 123 | |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 124 | if (pendingData.version().compareTo(newData.version()) <= 0) { |
Jonathan Hart | 6a8fd1d | 2015-02-25 15:44:37 -0800 | [diff] [blame] | 125 | // pendingData version is less than or equal to newData's |
| 126 | // Note: a new update for this key could be pending (it's version will be greater) |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 127 | pending.remove(newData.key()); |
| 128 | } |
Jonathan Hart | 6a8fd1d | 2015-02-25 15:44:37 -0800 | [diff] [blame] | 129 | } |
Jonathan Hart | 6a8fd1d | 2015-02-25 15:44:37 -0800 | [diff] [blame] | 130 | notifyDelegateIfNotNull(IntentEvent.getEvent(newData)); |
| 131 | } |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 132 | } |
Jonathan Hart | 6a8fd1d | 2015-02-25 15:44:37 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | private void notifyDelegateIfNotNull(IntentEvent event) { |
| 136 | if (event != null) { |
| 137 | notifyDelegate(event); |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void batchWrite(Iterable<IntentData> updates) { |
| 143 | for (IntentData data : updates) { |
| 144 | write(data); |
| 145 | } |
| 146 | } |
| 147 | |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 148 | @Override |
| 149 | public Intent getIntent(Key key) { |
| 150 | IntentData data = current.get(key); |
| 151 | return (data != null) ? data.intent() : null; |
| 152 | } |
| 153 | |
Jonathan Hart | 6a8fd1d | 2015-02-25 15:44:37 -0800 | [diff] [blame] | 154 | @Override |
| 155 | public IntentData getIntentData(Key key) { |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 156 | IntentData currentData = current.get(key); |
| 157 | if (currentData == null) { |
| 158 | return null; |
| 159 | } |
| 160 | return new IntentData(currentData); |
Jonathan Hart | 6a8fd1d | 2015-02-25 15:44:37 -0800 | [diff] [blame] | 161 | } |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 162 | |
| 163 | @Override |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 164 | public void addPending(IntentData data) { |
Jonathan Hart | 6a8fd1d | 2015-02-25 15:44:37 -0800 | [diff] [blame] | 165 | if (data.version() == null) { // recompiled intents will already have a version |
| 166 | data.setVersion(new SystemClockTimestamp()); |
| 167 | } |
| 168 | synchronized (this) { |
| 169 | IntentData existingData = pending.get(data.key()); |
| 170 | if (existingData == null || |
| 171 | // existing version is strictly less than data's version |
| 172 | // Note: if they are equal, we already have the update |
| 173 | // TODO maybe we should still make this <= to be safe? |
| 174 | existingData.version().compareTo(data.version()) < 0) { |
| 175 | pending.put(data.key(), data); |
| 176 | checkNotNull(delegate, "Store delegate is not set") |
| 177 | .process(data); |
| 178 | notifyDelegateIfNotNull(IntentEvent.getEvent(data)); |
| 179 | } else { |
| 180 | log.debug("IntentData {} is older than existing: {}", |
| 181 | data, existingData); |
| 182 | } |
| 183 | //TODO consider also checking the current map at this point |
| 184 | } |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 185 | } |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 186 | |
Jonathan Hart | 6a8fd1d | 2015-02-25 15:44:37 -0800 | [diff] [blame] | 187 | @Override |
| 188 | public boolean isMaster(Key intentKey) { |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public Iterable<Intent> getPending() { |
| 194 | return pending.values().stream() |
| 195 | .map(IntentData::intent) |
| 196 | .collect(Collectors.toList()); |
| 197 | } |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame^] | 198 | |
| 199 | @Override |
| 200 | public Iterable<IntentData> getPendingData() { |
| 201 | return Lists.newArrayList(pending.values()); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public Iterable<IntentData> getPendingData(boolean localOnly, long olderThan) { |
| 206 | long older = System.nanoTime() - olderThan * 1_000_000; //convert ms to ns |
| 207 | final SystemClockTimestamp time = new SystemClockTimestamp(older); |
| 208 | return pending.values().stream() |
| 209 | .filter(data -> data.version().isOlderThan(time) && |
| 210 | (!localOnly || isMaster(data.key()))) |
| 211 | .collect(Collectors.toList()); |
| 212 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 213 | } |