blob: 6658eff5b6751dd51223bb02e6f8ce290c565f8e [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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.trivial.impl;
Brian O'Connor66630c82014-10-02 21:08:19 -070017
Brian O'Connorb499b352015-02-03 16:46:15 -080018import com.google.common.collect.Maps;
Brian O'Connor66630c82014-10-02 21:08:19 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Service;
Sho SHIMIZU64ae11c2014-12-03 15:17:47 -080023import org.onosproject.net.intent.BatchWrite;
Brian O'Connorcff03322015-02-03 15:28:59 -080024import org.onosproject.net.intent.BatchWrite.Operation;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.intent.Intent;
Brian O'Connorcff03322015-02-03 15:28:59 -080026import org.onosproject.net.intent.IntentData;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.intent.IntentEvent;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.intent.IntentStore;
29import org.onosproject.net.intent.IntentStoreDelegate;
Ray Milkey5b3717e2015-02-05 11:44:08 -080030import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.store.AbstractStore;
Brian O'Connor66630c82014-10-02 21:08:19 -070032import org.slf4j.Logger;
33
Thomas Vachuskac96058a2014-10-20 23:00:16 -070034import java.util.List;
35import java.util.Map;
Brian O'Connorb499b352015-02-03 16:46:15 -080036import java.util.stream.Collectors;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070037
Brian O'Connorb499b352015-02-03 16:46:15 -080038import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070039import static org.slf4j.LoggerFactory.getLogger;
Brian O'Connor66630c82014-10-02 21:08:19 -070040
41@Component(immediate = true)
42@Service
43public class SimpleIntentStore
tom85258ee2014-10-07 00:10:02 -070044 extends AbstractStore<IntentEvent, IntentStoreDelegate>
45 implements IntentStore {
Brian O'Connor66630c82014-10-02 21:08:19 -070046
47 private final Logger log = getLogger(getClass());
Brian O'Connorcff03322015-02-03 15:28:59 -080048
49 // current state maps FIXME.. make this a IntentData map
Ray Milkey5b3717e2015-02-05 11:44:08 -080050 private final Map<Key, IntentData> current = Maps.newConcurrentMap();
51 private final Map<Key, IntentData> pending = Maps.newConcurrentMap(); //String is "key"
Brian O'Connor66630c82014-10-02 21:08:19 -070052
53 @Activate
54 public void activate() {
55 log.info("Started");
56 }
57
58 @Deactivate
59 public void deactivate() {
60 log.info("Stopped");
61 }
62
Brian O'Connor66630c82014-10-02 21:08:19 -070063 @Override
64 public long getIntentCount() {
Brian O'Connorb499b352015-02-03 16:46:15 -080065 return current.size();
Brian O'Connor66630c82014-10-02 21:08:19 -070066 }
67
68 @Override
69 public Iterable<Intent> getIntents() {
Brian O'Connorb499b352015-02-03 16:46:15 -080070 return current.values().stream()
71 .map(IntentData::intent)
72 .collect(Collectors.toList());
Brian O'Connor66630c82014-10-02 21:08:19 -070073 }
74
75 @Override
Ray Milkey5b3717e2015-02-05 11:44:08 -080076 public IntentData getIntentData(Key key) {
Brian O'Connorb499b352015-02-03 16:46:15 -080077 return current.get(key);
Brian O'Connor66630c82014-10-02 21:08:19 -070078 }
79
Brian O'Connor03406a42015-02-03 17:28:57 -080080 /*
alshabiba9819bf2014-11-30 18:15:52 -080081 * Execute writes in a batch.
82 *
83 * @param batch BatchWrite to execute
84 * @return failed operations
85 */
86 @Override
87 public List<Operation> batchWrite(BatchWrite batch) {
Brian O'Connor03406a42015-02-03 17:28:57 -080088 throw new UnsupportedOperationException("deprecated");
89 /*
Sho SHIMIZU2bb988b2015-01-20 13:45:35 -080090 if (batch.isEmpty()) {
91 return Collections.emptyList();
92 }
93
alshabiba9819bf2014-11-30 18:15:52 -080094 List<Operation> failed = Lists.newArrayList();
95 for (Operation op : batch.operations()) {
96 switch (op.type()) {
97 case CREATE_INTENT:
98 checkArgument(op.args().size() == 1,
99 "CREATE_INTENT takes 1 argument. %s", op);
100 Intent intent = (Intent) op.args().get(0);
101 // TODO: what if it failed?
Brian O'Connorb499b352015-02-03 16:46:15 -0800102// createIntent(intent); FIXME
alshabiba9819bf2014-11-30 18:15:52 -0800103 break;
Brian O'Connor66630c82014-10-02 21:08:19 -0700104
alshabiba9819bf2014-11-30 18:15:52 -0800105 case REMOVE_INTENT:
106 checkArgument(op.args().size() == 1,
107 "REMOVE_INTENT takes 1 argument. %s", op);
108 IntentId intentId = (IntentId) op.args().get(0);
Brian O'Connorb499b352015-02-03 16:46:15 -0800109// removeIntent(intentId); FIXME
alshabiba9819bf2014-11-30 18:15:52 -0800110 break;
111
112 case REMOVE_INSTALLED:
113 checkArgument(op.args().size() == 1,
114 "REMOVE_INSTALLED takes 1 argument. %s", op);
115 intentId = (IntentId) op.args().get(0);
116 removeInstalledIntents(intentId);
117 break;
118
119 case SET_INSTALLABLE:
120 checkArgument(op.args().size() == 2,
121 "SET_INSTALLABLE takes 2 arguments. %s", op);
122 intentId = (IntentId) op.args().get(0);
123 @SuppressWarnings("unchecked")
124 List<Intent> installableIntents = (List<Intent>) op.args().get(1);
125 setInstallableIntents(intentId, installableIntents);
126 break;
127
128 case SET_STATE:
129 checkArgument(op.args().size() == 2,
130 "SET_STATE takes 2 arguments. %s", op);
131 intent = (Intent) op.args().get(0);
132 IntentState newState = (IntentState) op.args().get(1);
133 setState(intent, newState);
134 break;
135
136 default:
137 break;
138 }
139 }
140 return failed;
Brian O'Connor03406a42015-02-03 17:28:57 -0800141 */
alshabiba9819bf2014-11-30 18:15:52 -0800142 }
Brian O'Connorcff03322015-02-03 15:28:59 -0800143
144 @Override
Brian O'Connor03406a42015-02-03 17:28:57 -0800145 public void write(IntentData newData) {
146 //FIXME need to compare the versions
147 current.put(newData.key(), newData);
148 try {
149 notifyDelegate(IntentEvent.getEvent(newData));
150 } catch (IllegalArgumentException e) {
151 //no-op
152 log.trace("ignore this exception: {}", e);
153 }
154 IntentData old = pending.get(newData.key());
155 if (old != null /* && FIXME version check */) {
156 pending.remove(newData.key());
157 }
158 }
159
160 @Override
161 public void batchWrite(Iterable<IntentData> updates) {
162 for (IntentData data : updates) {
163 write(data);
164 }
165 }
166
167
168 @Override
Brian O'Connorcff03322015-02-03 15:28:59 -0800169 public void addPending(IntentData data) {
170 //FIXME need to compare versions
171 pending.put(data.key(), data);
172 checkNotNull(delegate, "Store delegate is not set")
173 .process(data);
Brian O'Connor03406a42015-02-03 17:28:57 -0800174 notifyDelegate(IntentEvent.getEvent(data));
Brian O'Connorcff03322015-02-03 15:28:59 -0800175 }
Brian O'Connorcff03322015-02-03 15:28:59 -0800176
177
178 @Override
179 public boolean isMaster(Intent intent) {
180 return true;
181 }
Brian O'Connor66630c82014-10-02 21:08:19 -0700182}