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