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