blob: 475beb556b586120dcfa73544ce8652abdcc5fb1 [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);
alshabiba9819bf2014-11-30 18:15:52 -080072 this.setState(intent, IntentState.SUBMITTED);
73 return;
Brian O'Connor66630c82014-10-02 21:08:19 -070074 }
75
76 @Override
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -080077 public void removeIntent(IntentId intentId) {
78 checkState(getIntentState(intentId) == WITHDRAWN,
79 "Intent state for {} is not WITHDRAWN.", intentId);
80 intents.remove(intentId);
Brian O'Connor66630c82014-10-02 21:08:19 -070081 installable.remove(intentId);
Brian O'Connor66630c82014-10-02 21:08:19 -070082 states.remove(intentId);
Brian O'Connor66630c82014-10-02 21:08:19 -070083 }
84
85 @Override
86 public long getIntentCount() {
87 return intents.size();
88 }
89
90 @Override
91 public Iterable<Intent> getIntents() {
92 return ImmutableSet.copyOf(intents.values());
93 }
94
95 @Override
96 public Intent getIntent(IntentId intentId) {
97 return intents.get(intentId);
98 }
99
100 @Override
101 public IntentState getIntentState(IntentId id) {
102 return states.get(id);
103 }
104
Brian O'Connor66630c82014-10-02 21:08:19 -0700105 @Override
alshabiba9819bf2014-11-30 18:15:52 -0800106 public void setState(Intent intent, IntentState state) {
tom85258ee2014-10-07 00:10:02 -0700107 IntentId id = intent.id();
108 states.put(id, state);
Pavlin Radoslavov2ca9cf22014-10-22 10:39:40 -0700109 IntentEvent.Type type = null;
110
111 switch (state) {
112 case SUBMITTED:
113 type = IntentEvent.Type.SUBMITTED;
114 break;
115 case INSTALLED:
116 type = IntentEvent.Type.INSTALLED;
117 break;
118 case FAILED:
119 type = IntentEvent.Type.FAILED;
120 break;
121 case WITHDRAWN:
122 type = IntentEvent.Type.WITHDRAWN;
123 break;
124 default:
125 break;
126 }
alshabiba9819bf2014-11-30 18:15:52 -0800127 if (type != null) {
128 notifyDelegate(new IntentEvent(type, intent));
Pavlin Radoslavov2ca9cf22014-10-22 10:39:40 -0700129 }
Brian O'Connor66630c82014-10-02 21:08:19 -0700130 }
131
132 @Override
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700133 public void setInstallableIntents(IntentId intentId, List<Intent> result) {
Brian O'Connor66630c82014-10-02 21:08:19 -0700134 installable.put(intentId, result);
Brian O'Connor66630c82014-10-02 21:08:19 -0700135 }
136
137 @Override
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700138 public List<Intent> getInstallableIntents(IntentId intentId) {
Brian O'Connor66630c82014-10-02 21:08:19 -0700139 return installable.get(intentId);
140 }
141
142 @Override
143 public void removeInstalledIntents(IntentId intentId) {
144 installable.remove(intentId);
145 }
alshabiba9819bf2014-11-30 18:15:52 -0800146 /**
147 * Execute writes in a batch.
148 *
149 * @param batch BatchWrite to execute
150 * @return failed operations
151 */
152 @Override
153 public List<Operation> batchWrite(BatchWrite batch) {
154 List<Operation> failed = Lists.newArrayList();
155 for (Operation op : batch.operations()) {
156 switch (op.type()) {
157 case CREATE_INTENT:
158 checkArgument(op.args().size() == 1,
159 "CREATE_INTENT takes 1 argument. %s", op);
160 Intent intent = (Intent) op.args().get(0);
161 // TODO: what if it failed?
162 createIntent(intent);
163 break;
Brian O'Connor66630c82014-10-02 21:08:19 -0700164
alshabiba9819bf2014-11-30 18:15:52 -0800165 case REMOVE_INTENT:
166 checkArgument(op.args().size() == 1,
167 "REMOVE_INTENT takes 1 argument. %s", op);
168 IntentId intentId = (IntentId) op.args().get(0);
169 removeIntent(intentId);
170 break;
171
172 case REMOVE_INSTALLED:
173 checkArgument(op.args().size() == 1,
174 "REMOVE_INSTALLED takes 1 argument. %s", op);
175 intentId = (IntentId) op.args().get(0);
176 removeInstalledIntents(intentId);
177 break;
178
179 case SET_INSTALLABLE:
180 checkArgument(op.args().size() == 2,
181 "SET_INSTALLABLE takes 2 arguments. %s", op);
182 intentId = (IntentId) op.args().get(0);
183 @SuppressWarnings("unchecked")
184 List<Intent> installableIntents = (List<Intent>) op.args().get(1);
185 setInstallableIntents(intentId, installableIntents);
186 break;
187
188 case SET_STATE:
189 checkArgument(op.args().size() == 2,
190 "SET_STATE takes 2 arguments. %s", op);
191 intent = (Intent) op.args().get(0);
192 IntentState newState = (IntentState) op.args().get(1);
193 setState(intent, newState);
194 break;
195
196 default:
197 break;
198 }
199 }
200 return failed;
201 }
Brian O'Connor66630c82014-10-02 21:08:19 -0700202}