Brian O'Connor | b488b19 | 2015-02-06 17:58:58 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 16 | package org.onosproject.store.intent.impl; |
| 17 | |
| 18 | import com.google.common.collect.Maps; |
| 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; |
| 23 | import org.onosproject.net.intent.BatchWrite; |
| 24 | import org.onosproject.net.intent.BatchWrite.Operation; |
| 25 | import org.onosproject.net.intent.Intent; |
| 26 | import org.onosproject.net.intent.IntentData; |
| 27 | import org.onosproject.net.intent.IntentEvent; |
| 28 | import org.onosproject.net.intent.IntentId; |
| 29 | import org.onosproject.net.intent.IntentState; |
| 30 | import org.onosproject.net.intent.IntentStore; |
| 31 | import org.onosproject.net.intent.IntentStoreDelegate; |
| 32 | import org.onosproject.net.intent.Key; |
| 33 | import org.onosproject.store.AbstractStore; |
| 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | import java.util.List; |
| 37 | import java.util.Map; |
| 38 | import java.util.Objects; |
| 39 | import java.util.stream.Collectors; |
| 40 | |
| 41 | import static com.google.common.base.Preconditions.checkNotNull; |
| 42 | import static org.slf4j.LoggerFactory.getLogger; |
| 43 | |
| 44 | @Component(immediate = true) |
| 45 | @Service |
| 46 | //FIXME remove this |
| 47 | public class SimpleIntentStore |
| 48 | extends AbstractStore<IntentEvent, IntentStoreDelegate> |
| 49 | implements IntentStore { |
| 50 | |
| 51 | private final Logger log = getLogger(getClass()); |
| 52 | |
| 53 | // current state maps FIXME.. make this a IntentData map |
| 54 | private final Map<Key, IntentData> current = Maps.newConcurrentMap(); |
| 55 | private final Map<Key, IntentData> pending = Maps.newConcurrentMap(); //String is "key" |
| 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 | |
| 67 | @Override |
| 68 | public long getIntentCount() { |
| 69 | return current.size(); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public Iterable<Intent> getIntents() { |
| 74 | return current.values().stream() |
| 75 | .map(IntentData::intent) |
| 76 | .collect(Collectors.toList()); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public Intent getIntent(IntentId intentId) { |
| 81 | for (IntentData data : current.values()) { |
| 82 | if (Objects.equals(data.intent().id(), intentId)) { |
| 83 | return data.intent(); |
| 84 | } |
| 85 | } |
| 86 | return null; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public IntentState getIntentState(IntentId intentId) { |
| 91 | for (IntentData data : current.values()) { |
| 92 | if (Objects.equals(data.intent().id(), intentId)) { |
| 93 | return data.state(); |
| 94 | } |
| 95 | } |
| 96 | return null; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public List<Intent> getInstallableIntents(IntentId intentId) { |
| 101 | for (IntentData data : current.values()) { |
| 102 | if (Objects.equals(data.intent().id(), intentId)) { |
| 103 | return data.installables(); |
| 104 | } |
| 105 | } |
| 106 | return null; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public IntentData getIntentData(Key key) { |
| 111 | return current.get(key); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Execute writes in a batch. |
| 116 | * |
| 117 | * @param batch BatchWrite to execute |
| 118 | * @return failed operations |
| 119 | */ |
| 120 | @Override |
| 121 | public List<Operation> batchWrite(BatchWrite batch) { |
| 122 | throw new UnsupportedOperationException("deprecated"); |
| 123 | /* |
| 124 | if (batch.isEmpty()) { |
| 125 | return Collections.emptyList(); |
| 126 | } |
| 127 | |
| 128 | List<Operation> failed = Lists.newArrayList(); |
| 129 | for (Operation op : batch.operations()) { |
| 130 | switch (op.type()) { |
| 131 | case CREATE_INTENT: |
| 132 | checkArgument(op.args().size() == 1, |
| 133 | "CREATE_INTENT takes 1 argument. %s", op); |
| 134 | Intent intent = (Intent) op.args().get(0); |
| 135 | // TODO: what if it failed? |
| 136 | // createIntent(intent); FIXME |
| 137 | break; |
| 138 | |
| 139 | case REMOVE_INTENT: |
| 140 | checkArgument(op.args().size() == 1, |
| 141 | "REMOVE_INTENT takes 1 argument. %s", op); |
| 142 | IntentId intentId = (IntentId) op.args().get(0); |
| 143 | // removeIntent(intentId); FIXME |
| 144 | break; |
| 145 | |
| 146 | case REMOVE_INSTALLED: |
| 147 | checkArgument(op.args().size() == 1, |
| 148 | "REMOVE_INSTALLED takes 1 argument. %s", op); |
| 149 | intentId = (IntentId) op.args().get(0); |
| 150 | removeInstalledIntents(intentId); |
| 151 | break; |
| 152 | |
| 153 | case SET_INSTALLABLE: |
| 154 | checkArgument(op.args().size() == 2, |
| 155 | "SET_INSTALLABLE takes 2 arguments. %s", op); |
| 156 | intentId = (IntentId) op.args().get(0); |
| 157 | @SuppressWarnings("unchecked") |
| 158 | List<Intent> installableIntents = (List<Intent>) op.args().get(1); |
| 159 | setInstallableIntents(intentId, installableIntents); |
| 160 | break; |
| 161 | |
| 162 | case SET_STATE: |
| 163 | checkArgument(op.args().size() == 2, |
| 164 | "SET_STATE takes 2 arguments. %s", op); |
| 165 | intent = (Intent) op.args().get(0); |
| 166 | IntentState newState = (IntentState) op.args().get(1); |
| 167 | setState(intent, newState); |
| 168 | break; |
| 169 | |
| 170 | default: |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | return failed; |
| 175 | */ |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void write(IntentData newData) { |
| 180 | //FIXME need to compare the versions |
| 181 | current.put(newData.key(), newData); |
| 182 | try { |
| 183 | notifyDelegate(IntentEvent.getEvent(newData)); |
| 184 | } catch (IllegalArgumentException e) { |
| 185 | //no-op |
| 186 | log.trace("ignore this exception: {}", e); |
| 187 | } |
| 188 | IntentData old = pending.get(newData.key()); |
| 189 | if (old != null /* && FIXME version check */) { |
| 190 | pending.remove(newData.key()); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public void batchWrite(Iterable<IntentData> updates) { |
| 196 | for (IntentData data : updates) { |
| 197 | write(data); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public Intent getIntent(Key key) { |
| 203 | IntentData data = current.get(key); |
| 204 | return (data != null) ? data.intent() : null; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | @Override |
| 209 | public void addPending(IntentData data) { |
| 210 | //FIXME need to compare versions |
| 211 | pending.put(data.key(), data); |
| 212 | checkNotNull(delegate, "Store delegate is not set") |
| 213 | .process(data); |
| 214 | notifyDelegate(IntentEvent.getEvent(data)); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | @Override |
| 219 | public boolean isMaster(Intent intent) { |
| 220 | return true; |
| 221 | } |
| 222 | } |