blob: 4a5e69299033f8a3b84b305b90012900f92964f4 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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'Connor66630c82014-10-02 21:08:19 -070016package org.onlab.onos.store.trivial.impl;
17
Thomas Vachuskac96058a2014-10-20 23:00:16 -070018import com.google.common.collect.ImmutableSet;
alshabiba9819bf2014-11-30 18:15:52 -080019import com.google.common.collect.Lists;
20
Brian O'Connor66630c82014-10-02 21:08:19 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Service;
Brian O'Connor66630c82014-10-02 21:08:19 -070025import org.onlab.onos.net.intent.Intent;
26import org.onlab.onos.net.intent.IntentEvent;
27import org.onlab.onos.net.intent.IntentId;
28import org.onlab.onos.net.intent.IntentState;
29import org.onlab.onos.net.intent.IntentStore;
30import org.onlab.onos.net.intent.IntentStoreDelegate;
alshabiba9819bf2014-11-30 18:15:52 -080031import org.onlab.onos.net.intent.IntentStore.BatchWrite.Operation;
Brian O'Connor66630c82014-10-02 21:08:19 -070032import org.onlab.onos.store.AbstractStore;
33import org.slf4j.Logger;
34
Thomas Vachuskac96058a2014-10-20 23:00:16 -070035import java.util.List;
36import java.util.Map;
37import java.util.concurrent.ConcurrentHashMap;
38
alshabiba9819bf2014-11-30 18:15:52 -080039import static com.google.common.base.Preconditions.checkArgument;
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -080040import static com.google.common.base.Preconditions.checkState;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070041import static org.onlab.onos.net.intent.IntentState.WITHDRAWN;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070042import static org.slf4j.LoggerFactory.getLogger;
Brian O'Connor66630c82014-10-02 21:08:19 -070043
44@Component(immediate = true)
45@Service
46public class SimpleIntentStore
tom85258ee2014-10-07 00:10:02 -070047 extends AbstractStore<IntentEvent, IntentStoreDelegate>
48 implements IntentStore {
Brian O'Connor66630c82014-10-02 21:08:19 -070049
50 private final Logger log = getLogger(getClass());
Jonathan Hart11096402014-10-20 17:31:49 -070051 private final Map<IntentId, Intent> intents = new ConcurrentHashMap<>();
52 private final Map<IntentId, IntentState> states = new ConcurrentHashMap<>();
Brian O'Connorfa81eae2014-10-30 13:20:05 -070053 private final Map<IntentId, List<Intent>> installable = new ConcurrentHashMap<>();
54
Brian O'Connor66630c82014-10-02 21:08:19 -070055
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
alshabiba9819bf2014-11-30 18:15:52 -080067 public void createIntent(Intent intent) {
Brian O'Connorfa81eae2014-10-30 13:20:05 -070068 if (intents.containsKey(intent.id())) {
alshabiba9819bf2014-11-30 18:15:52 -080069 return;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070070 }
tom85258ee2014-10-07 00:10:02 -070071 intents.put(intent.id(), intent);
Brian O'Connor7a71d5d2014-12-02 00:12:27 -080072 this.setState(intent, IntentState.INSTALL_REQ);
Brian O'Connor66630c82014-10-02 21:08:19 -070073 }
74
75 @Override
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -080076 public void removeIntent(IntentId intentId) {
77 checkState(getIntentState(intentId) == WITHDRAWN,
78 "Intent state for {} is not WITHDRAWN.", intentId);
79 intents.remove(intentId);
Brian O'Connor66630c82014-10-02 21:08:19 -070080 installable.remove(intentId);
Brian O'Connor66630c82014-10-02 21:08:19 -070081 states.remove(intentId);
Brian O'Connor66630c82014-10-02 21:08:19 -070082 }
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'Connor66630c82014-10-02 21:08:19 -0700104 @Override
alshabiba9819bf2014-11-30 18:15:52 -0800105 public void setState(Intent intent, IntentState state) {
tom85258ee2014-10-07 00:10:02 -0700106 IntentId id = intent.id();
107 states.put(id, state);
Pavlin Radoslavov2ca9cf22014-10-22 10:39:40 -0700108 IntentEvent.Type type = null;
109
110 switch (state) {
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800111 case INSTALL_REQ:
112 type = IntentEvent.Type.INSTALL_REQ;
Pavlin Radoslavov2ca9cf22014-10-22 10:39:40 -0700113 break;
114 case INSTALLED:
115 type = IntentEvent.Type.INSTALLED;
116 break;
117 case FAILED:
118 type = IntentEvent.Type.FAILED;
119 break;
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800120 case WITHDRAW_REQ:
121 type = IntentEvent.Type.WITHDRAW_REQ;
122 break;
Pavlin Radoslavov2ca9cf22014-10-22 10:39:40 -0700123 case WITHDRAWN:
124 type = IntentEvent.Type.WITHDRAWN;
125 break;
126 default:
127 break;
128 }
alshabiba9819bf2014-11-30 18:15:52 -0800129 if (type != null) {
130 notifyDelegate(new IntentEvent(type, intent));
Pavlin Radoslavov2ca9cf22014-10-22 10:39:40 -0700131 }
Brian O'Connor66630c82014-10-02 21:08:19 -0700132 }
133
134 @Override
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700135 public void setInstallableIntents(IntentId intentId, List<Intent> result) {
Brian O'Connor66630c82014-10-02 21:08:19 -0700136 installable.put(intentId, result);
Brian O'Connor66630c82014-10-02 21:08:19 -0700137 }
138
139 @Override
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700140 public List<Intent> getInstallableIntents(IntentId intentId) {
Brian O'Connor66630c82014-10-02 21:08:19 -0700141 return installable.get(intentId);
142 }
143
144 @Override
145 public void removeInstalledIntents(IntentId intentId) {
146 installable.remove(intentId);
147 }
alshabiba9819bf2014-11-30 18:15:52 -0800148 /**
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'Connor66630c82014-10-02 21:08:19 -0700166
alshabiba9819bf2014-11-30 18:15:52 -0800167 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'Connor66630c82014-10-02 21:08:19 -0700204}