Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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; |
Sho SHIMIZU | 64ae11c | 2014-12-03 15:17:47 -0800 | [diff] [blame] | 23 | import org.onosproject.net.intent.BatchWrite; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 24 | import org.onosproject.net.intent.BatchWrite.Operation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.intent.Intent; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 26 | import org.onosproject.net.intent.IntentData; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.intent.IntentEvent; |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame^] | 28 | import org.onosproject.net.intent.IntentId; |
| 29 | import org.onosproject.net.intent.IntentState; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.intent.IntentStore; |
| 31 | import org.onosproject.net.intent.IntentStoreDelegate; |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 32 | import org.onosproject.net.intent.Key; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.store.AbstractStore; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 36 | import java.util.List; |
| 37 | import java.util.Map; |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame^] | 38 | import java.util.Objects; |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 39 | import java.util.stream.Collectors; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 40 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 41 | import static com.google.common.base.Preconditions.checkNotNull; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 42 | import static org.slf4j.LoggerFactory.getLogger; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 43 | |
| 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 | |
| 52 | // current state maps FIXME.. make this a IntentData map |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 53 | private final Map<Key, IntentData> current = Maps.newConcurrentMap(); |
| 54 | private final Map<Key, IntentData> pending = Maps.newConcurrentMap(); //String is "key" |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 55 | |
| 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 | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame^] | 79 | public Intent getIntent(IntentId intentId) { |
| 80 | for (IntentData data : current.values()) { |
| 81 | if (Objects.equals(data.intent().id(), intentId)) { |
| 82 | return data.intent(); |
| 83 | } |
| 84 | } |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public IntentState getIntentState(IntentId intentId) { |
| 90 | for (IntentData data : current.values()) { |
| 91 | if (Objects.equals(data.intent().id(), intentId)) { |
| 92 | return data.state(); |
| 93 | } |
| 94 | } |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public List<Intent> getInstallableIntents(IntentId intentId) { |
| 100 | for (IntentData data : current.values()) { |
| 101 | if (Objects.equals(data.intent().id(), intentId)) { |
| 102 | return data.installables(); |
| 103 | } |
| 104 | } |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | @Override |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 109 | public IntentData getIntentData(Key key) { |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 110 | return current.get(key); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 113 | /* |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 114 | * Execute writes in a batch. |
| 115 | * |
| 116 | * @param batch BatchWrite to execute |
| 117 | * @return failed operations |
| 118 | */ |
| 119 | @Override |
| 120 | public List<Operation> batchWrite(BatchWrite batch) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 121 | throw new UnsupportedOperationException("deprecated"); |
| 122 | /* |
Sho SHIMIZU | 2bb988b | 2015-01-20 13:45:35 -0800 | [diff] [blame] | 123 | if (batch.isEmpty()) { |
| 124 | return Collections.emptyList(); |
| 125 | } |
| 126 | |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 127 | List<Operation> failed = Lists.newArrayList(); |
| 128 | for (Operation op : batch.operations()) { |
| 129 | switch (op.type()) { |
| 130 | case CREATE_INTENT: |
| 131 | checkArgument(op.args().size() == 1, |
| 132 | "CREATE_INTENT takes 1 argument. %s", op); |
| 133 | Intent intent = (Intent) op.args().get(0); |
| 134 | // TODO: what if it failed? |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 135 | // createIntent(intent); FIXME |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 136 | break; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 137 | |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 138 | case REMOVE_INTENT: |
| 139 | checkArgument(op.args().size() == 1, |
| 140 | "REMOVE_INTENT takes 1 argument. %s", op); |
| 141 | IntentId intentId = (IntentId) op.args().get(0); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 142 | // removeIntent(intentId); FIXME |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 143 | break; |
| 144 | |
| 145 | case REMOVE_INSTALLED: |
| 146 | checkArgument(op.args().size() == 1, |
| 147 | "REMOVE_INSTALLED takes 1 argument. %s", op); |
| 148 | intentId = (IntentId) op.args().get(0); |
| 149 | removeInstalledIntents(intentId); |
| 150 | break; |
| 151 | |
| 152 | case SET_INSTALLABLE: |
| 153 | checkArgument(op.args().size() == 2, |
| 154 | "SET_INSTALLABLE takes 2 arguments. %s", op); |
| 155 | intentId = (IntentId) op.args().get(0); |
| 156 | @SuppressWarnings("unchecked") |
| 157 | List<Intent> installableIntents = (List<Intent>) op.args().get(1); |
| 158 | setInstallableIntents(intentId, installableIntents); |
| 159 | break; |
| 160 | |
| 161 | case SET_STATE: |
| 162 | checkArgument(op.args().size() == 2, |
| 163 | "SET_STATE takes 2 arguments. %s", op); |
| 164 | intent = (Intent) op.args().get(0); |
| 165 | IntentState newState = (IntentState) op.args().get(1); |
| 166 | setState(intent, newState); |
| 167 | break; |
| 168 | |
| 169 | default: |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | return failed; |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 174 | */ |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 175 | } |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 176 | |
| 177 | @Override |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 178 | public void write(IntentData newData) { |
| 179 | //FIXME need to compare the versions |
| 180 | current.put(newData.key(), newData); |
| 181 | try { |
| 182 | notifyDelegate(IntentEvent.getEvent(newData)); |
| 183 | } catch (IllegalArgumentException e) { |
| 184 | //no-op |
| 185 | log.trace("ignore this exception: {}", e); |
| 186 | } |
| 187 | IntentData old = pending.get(newData.key()); |
| 188 | if (old != null /* && FIXME version check */) { |
| 189 | pending.remove(newData.key()); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void batchWrite(Iterable<IntentData> updates) { |
| 195 | for (IntentData data : updates) { |
| 196 | write(data); |
| 197 | } |
| 198 | } |
| 199 | |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame^] | 200 | @Override |
| 201 | public Intent getIntent(Key key) { |
| 202 | IntentData data = current.get(key); |
| 203 | return (data != null) ? data.intent() : null; |
| 204 | } |
| 205 | |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 206 | |
| 207 | @Override |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 208 | public void addPending(IntentData data) { |
| 209 | //FIXME need to compare versions |
| 210 | pending.put(data.key(), data); |
| 211 | checkNotNull(delegate, "Store delegate is not set") |
| 212 | .process(data); |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 213 | notifyDelegate(IntentEvent.getEvent(data)); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 214 | } |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 215 | |
| 216 | |
| 217 | @Override |
| 218 | public boolean isMaster(Intent intent) { |
| 219 | return true; |
| 220 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 221 | } |