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