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 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
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; |
Sho SHIMIZU | 64ae11c | 2014-12-03 15:17:47 -0800 | [diff] [blame] | 24 | import org.onosproject.net.intent.BatchWrite; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame^] | 25 | import org.onosproject.net.intent.BatchWrite.Operation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.net.intent.Intent; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame^] | 27 | import org.onosproject.net.intent.IntentData; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.net.intent.IntentEvent; |
| 29 | import org.onosproject.net.intent.IntentId; |
| 30 | import org.onosproject.net.intent.IntentState; |
| 31 | import org.onosproject.net.intent.IntentStore; |
| 32 | import org.onosproject.net.intent.IntentStoreDelegate; |
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 | |
Sho SHIMIZU | 2bb988b | 2015-01-20 13:45:35 -0800 | [diff] [blame] | 36 | import java.util.Collections; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 37 | import java.util.List; |
| 38 | import java.util.Map; |
| 39 | import java.util.concurrent.ConcurrentHashMap; |
| 40 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame^] | 41 | import static com.google.common.base.Preconditions.*; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import static org.onosproject.net.intent.IntentState.WITHDRAWN; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 43 | import static org.slf4j.LoggerFactory.getLogger; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 44 | |
| 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 | |
| 53 | // current state maps FIXME.. make this a IntentData map |
Jonathan Hart | 1109640 | 2014-10-20 17:31:49 -0700 | [diff] [blame] | 54 | private final Map<IntentId, Intent> intents = new ConcurrentHashMap<>(); |
| 55 | private final Map<IntentId, IntentState> states = new ConcurrentHashMap<>(); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 56 | private final Map<IntentId, List<Intent>> installable = new ConcurrentHashMap<>(); |
| 57 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame^] | 58 | private final Map<String, IntentData> pending = new ConcurrentHashMap<>(); //String is "key" |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 59 | |
| 60 | @Activate |
| 61 | public void activate() { |
| 62 | log.info("Started"); |
| 63 | } |
| 64 | |
| 65 | @Deactivate |
| 66 | public void deactivate() { |
| 67 | log.info("Stopped"); |
| 68 | } |
| 69 | |
Jonathan Hart | c036367 | 2015-01-20 16:21:08 -0800 | [diff] [blame] | 70 | private void createIntent(Intent intent) { |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 71 | if (intents.containsKey(intent.id())) { |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 72 | return; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 73 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 74 | intents.put(intent.id(), intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 75 | this.setState(intent, IntentState.INSTALL_REQ); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Jonathan Hart | c036367 | 2015-01-20 16:21:08 -0800 | [diff] [blame] | 78 | private void removeIntent(IntentId intentId) { |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 79 | checkState(getIntentState(intentId) == WITHDRAWN, |
| 80 | "Intent state for {} is not WITHDRAWN.", intentId); |
| 81 | intents.remove(intentId); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 82 | installable.remove(intentId); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 83 | states.remove(intentId); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public long getIntentCount() { |
| 88 | return intents.size(); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Iterable<Intent> getIntents() { |
| 93 | return ImmutableSet.copyOf(intents.values()); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public Intent getIntent(IntentId intentId) { |
| 98 | return intents.get(intentId); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public IntentState getIntentState(IntentId id) { |
| 103 | return states.get(id); |
| 104 | } |
| 105 | |
Jonathan Hart | dac3008 | 2015-01-22 16:02:48 -0800 | [diff] [blame] | 106 | private void setState(Intent intent, IntentState state) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 107 | IntentId id = intent.id(); |
| 108 | states.put(id, state); |
Pavlin Radoslavov | 2ca9cf2 | 2014-10-22 10:39:40 -0700 | [diff] [blame] | 109 | IntentEvent.Type type = null; |
| 110 | |
| 111 | switch (state) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 112 | case INSTALL_REQ: |
| 113 | type = IntentEvent.Type.INSTALL_REQ; |
Pavlin Radoslavov | 2ca9cf2 | 2014-10-22 10:39:40 -0700 | [diff] [blame] | 114 | break; |
| 115 | case INSTALLED: |
| 116 | type = IntentEvent.Type.INSTALLED; |
| 117 | break; |
| 118 | case FAILED: |
| 119 | type = IntentEvent.Type.FAILED; |
| 120 | break; |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 121 | case WITHDRAW_REQ: |
| 122 | type = IntentEvent.Type.WITHDRAW_REQ; |
| 123 | break; |
Pavlin Radoslavov | 2ca9cf2 | 2014-10-22 10:39:40 -0700 | [diff] [blame] | 124 | case WITHDRAWN: |
| 125 | type = IntentEvent.Type.WITHDRAWN; |
| 126 | break; |
| 127 | default: |
| 128 | break; |
| 129 | } |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 130 | if (type != null) { |
| 131 | notifyDelegate(new IntentEvent(type, intent)); |
Pavlin Radoslavov | 2ca9cf2 | 2014-10-22 10:39:40 -0700 | [diff] [blame] | 132 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Jonathan Hart | dac3008 | 2015-01-22 16:02:48 -0800 | [diff] [blame] | 135 | private void setInstallableIntents(IntentId intentId, List<Intent> result) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 136 | installable.put(intentId, result); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 140 | public List<Intent> getInstallableIntents(IntentId intentId) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 141 | return installable.get(intentId); |
| 142 | } |
| 143 | |
Jonathan Hart | dac3008 | 2015-01-22 16:02:48 -0800 | [diff] [blame] | 144 | private void removeInstalledIntents(IntentId intentId) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 145 | installable.remove(intentId); |
| 146 | } |
Jonathan Hart | dac3008 | 2015-01-22 16:02:48 -0800 | [diff] [blame] | 147 | |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 148 | /** |
| 149 | * Execute writes in a batch. |
| 150 | * |
| 151 | * @param batch BatchWrite to execute |
| 152 | * @return failed operations |
| 153 | */ |
| 154 | @Override |
| 155 | public List<Operation> batchWrite(BatchWrite batch) { |
Sho SHIMIZU | 2bb988b | 2015-01-20 13:45:35 -0800 | [diff] [blame] | 156 | if (batch.isEmpty()) { |
| 157 | return Collections.emptyList(); |
| 158 | } |
| 159 | |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 160 | List<Operation> failed = Lists.newArrayList(); |
| 161 | for (Operation op : batch.operations()) { |
| 162 | switch (op.type()) { |
| 163 | case CREATE_INTENT: |
| 164 | checkArgument(op.args().size() == 1, |
| 165 | "CREATE_INTENT takes 1 argument. %s", op); |
| 166 | Intent intent = (Intent) op.args().get(0); |
| 167 | // TODO: what if it failed? |
| 168 | createIntent(intent); |
| 169 | break; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 170 | |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 171 | case REMOVE_INTENT: |
| 172 | checkArgument(op.args().size() == 1, |
| 173 | "REMOVE_INTENT takes 1 argument. %s", op); |
| 174 | IntentId intentId = (IntentId) op.args().get(0); |
| 175 | removeIntent(intentId); |
| 176 | break; |
| 177 | |
| 178 | case REMOVE_INSTALLED: |
| 179 | checkArgument(op.args().size() == 1, |
| 180 | "REMOVE_INSTALLED takes 1 argument. %s", op); |
| 181 | intentId = (IntentId) op.args().get(0); |
| 182 | removeInstalledIntents(intentId); |
| 183 | break; |
| 184 | |
| 185 | case SET_INSTALLABLE: |
| 186 | checkArgument(op.args().size() == 2, |
| 187 | "SET_INSTALLABLE takes 2 arguments. %s", op); |
| 188 | intentId = (IntentId) op.args().get(0); |
| 189 | @SuppressWarnings("unchecked") |
| 190 | List<Intent> installableIntents = (List<Intent>) op.args().get(1); |
| 191 | setInstallableIntents(intentId, installableIntents); |
| 192 | break; |
| 193 | |
| 194 | case SET_STATE: |
| 195 | checkArgument(op.args().size() == 2, |
| 196 | "SET_STATE takes 2 arguments. %s", op); |
| 197 | intent = (Intent) op.args().get(0); |
| 198 | IntentState newState = (IntentState) op.args().get(1); |
| 199 | setState(intent, newState); |
| 200 | break; |
| 201 | |
| 202 | default: |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | return failed; |
| 207 | } |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame^] | 208 | |
| 209 | @Override |
| 210 | public void addPending(IntentData data) { |
| 211 | //FIXME need to compare versions |
| 212 | pending.put(data.key(), data); |
| 213 | checkNotNull(delegate, "Store delegate is not set") |
| 214 | .process(data); |
| 215 | } |
| 216 | // FIXME!!! pending.remove(intent.key()); // TODO check version |
| 217 | |
| 218 | |
| 219 | @Override |
| 220 | public boolean isMaster(Intent intent) { |
| 221 | return true; |
| 222 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 223 | } |