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 | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.trivial.impl; |
| 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; |
| 20 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.intent.Intent; |
| 26 | import org.onlab.onos.net.intent.IntentEvent; |
| 27 | import org.onlab.onos.net.intent.IntentId; |
| 28 | import org.onlab.onos.net.intent.IntentState; |
| 29 | import org.onlab.onos.net.intent.IntentStore; |
| 30 | import org.onlab.onos.net.intent.IntentStoreDelegate; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 31 | import org.onlab.onos.net.intent.IntentStore.BatchWrite.Operation; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 32 | import org.onlab.onos.store.AbstractStore; |
| 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; |
| 37 | import java.util.concurrent.ConcurrentHashMap; |
| 38 | |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 39 | import static com.google.common.base.Preconditions.checkArgument; |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 40 | import static com.google.common.base.Preconditions.checkState; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 41 | import static org.onlab.onos.net.intent.IntentState.WITHDRAWN; |
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()); |
Jonathan Hart | 1109640 | 2014-10-20 17:31:49 -0700 | [diff] [blame] | 51 | private final Map<IntentId, Intent> intents = new ConcurrentHashMap<>(); |
| 52 | private final Map<IntentId, IntentState> states = new ConcurrentHashMap<>(); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 53 | private final Map<IntentId, List<Intent>> installable = new ConcurrentHashMap<>(); |
| 54 | |
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 | |
| 66 | @Override |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 67 | public void createIntent(Intent intent) { |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 68 | if (intents.containsKey(intent.id())) { |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 69 | return; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 70 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 71 | intents.put(intent.id(), intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame^] | 72 | this.setState(intent, IntentState.INSTALL_REQ); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 76 | public void removeIntent(IntentId intentId) { |
| 77 | checkState(getIntentState(intentId) == WITHDRAWN, |
| 78 | "Intent state for {} is not WITHDRAWN.", intentId); |
| 79 | intents.remove(intentId); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 80 | installable.remove(intentId); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 81 | states.remove(intentId); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public long getIntentCount() { |
| 86 | return intents.size(); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public Iterable<Intent> getIntents() { |
| 91 | return ImmutableSet.copyOf(intents.values()); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public Intent getIntent(IntentId intentId) { |
| 96 | return intents.get(intentId); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public IntentState getIntentState(IntentId id) { |
| 101 | return states.get(id); |
| 102 | } |
| 103 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 104 | @Override |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 105 | public void setState(Intent intent, IntentState state) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 106 | IntentId id = intent.id(); |
| 107 | states.put(id, state); |
Pavlin Radoslavov | 2ca9cf2 | 2014-10-22 10:39:40 -0700 | [diff] [blame] | 108 | IntentEvent.Type type = null; |
| 109 | |
| 110 | switch (state) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame^] | 111 | case INSTALL_REQ: |
| 112 | type = IntentEvent.Type.INSTALL_REQ; |
Pavlin Radoslavov | 2ca9cf2 | 2014-10-22 10:39:40 -0700 | [diff] [blame] | 113 | break; |
| 114 | case INSTALLED: |
| 115 | type = IntentEvent.Type.INSTALLED; |
| 116 | break; |
| 117 | case FAILED: |
| 118 | type = IntentEvent.Type.FAILED; |
| 119 | break; |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame^] | 120 | case WITHDRAW_REQ: |
| 121 | type = IntentEvent.Type.WITHDRAW_REQ; |
| 122 | break; |
Pavlin Radoslavov | 2ca9cf2 | 2014-10-22 10:39:40 -0700 | [diff] [blame] | 123 | case WITHDRAWN: |
| 124 | type = IntentEvent.Type.WITHDRAWN; |
| 125 | break; |
| 126 | default: |
| 127 | break; |
| 128 | } |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 129 | if (type != null) { |
| 130 | notifyDelegate(new IntentEvent(type, intent)); |
Pavlin Radoslavov | 2ca9cf2 | 2014-10-22 10:39:40 -0700 | [diff] [blame] | 131 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 135 | public 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 | |
| 144 | @Override |
| 145 | public void removeInstalledIntents(IntentId intentId) { |
| 146 | installable.remove(intentId); |
| 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) { |
| 156 | List<Operation> failed = Lists.newArrayList(); |
| 157 | for (Operation op : batch.operations()) { |
| 158 | switch (op.type()) { |
| 159 | case CREATE_INTENT: |
| 160 | checkArgument(op.args().size() == 1, |
| 161 | "CREATE_INTENT takes 1 argument. %s", op); |
| 162 | Intent intent = (Intent) op.args().get(0); |
| 163 | // TODO: what if it failed? |
| 164 | createIntent(intent); |
| 165 | break; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 166 | |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 167 | case REMOVE_INTENT: |
| 168 | checkArgument(op.args().size() == 1, |
| 169 | "REMOVE_INTENT takes 1 argument. %s", op); |
| 170 | IntentId intentId = (IntentId) op.args().get(0); |
| 171 | removeIntent(intentId); |
| 172 | break; |
| 173 | |
| 174 | case REMOVE_INSTALLED: |
| 175 | checkArgument(op.args().size() == 1, |
| 176 | "REMOVE_INSTALLED takes 1 argument. %s", op); |
| 177 | intentId = (IntentId) op.args().get(0); |
| 178 | removeInstalledIntents(intentId); |
| 179 | break; |
| 180 | |
| 181 | case SET_INSTALLABLE: |
| 182 | checkArgument(op.args().size() == 2, |
| 183 | "SET_INSTALLABLE takes 2 arguments. %s", op); |
| 184 | intentId = (IntentId) op.args().get(0); |
| 185 | @SuppressWarnings("unchecked") |
| 186 | List<Intent> installableIntents = (List<Intent>) op.args().get(1); |
| 187 | setInstallableIntents(intentId, installableIntents); |
| 188 | break; |
| 189 | |
| 190 | case SET_STATE: |
| 191 | checkArgument(op.args().size() == 2, |
| 192 | "SET_STATE takes 2 arguments. %s", op); |
| 193 | intent = (Intent) op.args().get(0); |
| 194 | IntentState newState = (IntentState) op.args().get(1); |
| 195 | setState(intent, newState); |
| 196 | break; |
| 197 | |
| 198 | default: |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | return failed; |
| 203 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 204 | } |