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