blob: 366ae3e66df36c997d52d527580a13afb2860a4c [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 */
Yuta HIGUCHI406a5652014-10-20 22:18:16 -070016package org.onlab.onos.store.intent.impl;
17
Thomas Vachuskab97cf282014-10-20 23:31:12 -070018import com.google.common.collect.ImmutableSet;
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -070019import com.hazelcast.core.IMap;
20
Yuta HIGUCHI406a5652014-10-20 22:18:16 -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;
Yuta HIGUCHI406a5652014-10-20 22:18:16 -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;
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -070031import org.onlab.onos.store.hz.AbstractHazelcastStore;
32import org.onlab.onos.store.hz.SMap;
Yuta HIGUCHI406a5652014-10-20 22:18:16 -070033import org.slf4j.Logger;
34
Thomas Vachuskab97cf282014-10-20 23:31:12 -070035import java.util.List;
36import java.util.Map;
37import java.util.concurrent.ConcurrentHashMap;
38
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -070039import static com.google.common.base.Verify.verify;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070040import static org.onlab.onos.net.intent.IntentState.*;
41import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI406a5652014-10-20 22:18:16 -070042
Yuta HIGUCHI406a5652014-10-20 22:18:16 -070043@Component(immediate = true)
44@Service
45public class DistributedIntentStore
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -070046 extends AbstractHazelcastStore<IntentEvent, IntentStoreDelegate>
Yuta HIGUCHI406a5652014-10-20 22:18:16 -070047 implements IntentStore {
48
49 private final Logger log = getLogger(getClass());
Yuta HIGUCHI406a5652014-10-20 22:18:16 -070050
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -070051 // Assumption: IntentId will not have synonyms
52 private SMap<IntentId, Intent> intents;
53 private SMap<IntentId, IntentState> states;
54
55 // Map to store instance local intermediate state transition
56 private transient Map<IntentId, IntentState> transientStates = new ConcurrentHashMap<>();
57
58 private SMap<IntentId, List<Intent>> installable;
59
60 @Override
Yuta HIGUCHI406a5652014-10-20 22:18:16 -070061 @Activate
62 public void activate() {
Yuta HIGUCHI71fa4932014-10-28 22:27:49 -070063 // FIXME: We need a way to add serializer for intents which has been plugged-in.
64 // TODO: As a short term workaround, relax Kryo config to
65 // registrationRequired=false?
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -070066 super.activate();
67
68 // TODO: enable near cache, allow read from backup for this IMap
69 IMap<byte[], byte[]> rawIntents = super.theInstance.getMap("intents");
70 intents = new SMap<>(rawIntents , super.serializer);
71
72 // TODO: disable near cache, disable read from backup for this IMap
73 IMap<byte[], byte[]> rawStates = super.theInstance.getMap("intent-states");
74 states = new SMap<>(rawStates , super.serializer);
75
76 transientStates.clear();
77
78 // TODO: disable near cache, disable read from backup for this IMap
79 IMap<byte[], byte[]> rawInstallables = super.theInstance.getMap("installable-intents");
80 installable = new SMap<>(rawInstallables , super.serializer);
81
Yuta HIGUCHI406a5652014-10-20 22:18:16 -070082 log.info("Started");
83 }
84
85 @Deactivate
86 public void deactivate() {
87 log.info("Stopped");
88 }
89
90 @Override
91 public IntentEvent createIntent(Intent intent) {
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -070092 Intent existing = intents.putIfAbsent(intent.id(), intent);
93 if (existing != null) {
94 // duplicate, ignore
95 return null;
96 } else {
97 return this.setState(intent, IntentState.SUBMITTED);
98 }
Yuta HIGUCHI406a5652014-10-20 22:18:16 -070099 }
100
101 @Override
102 public IntentEvent removeIntent(IntentId intentId) {
103 Intent intent = intents.remove(intentId);
104 installable.remove(intentId);
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700105 if (intent == null) {
106 // was already removed
107 return null;
108 }
Yuta HIGUCHI406a5652014-10-20 22:18:16 -0700109 IntentEvent event = this.setState(intent, WITHDRAWN);
110 states.remove(intentId);
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700111 transientStates.remove(intentId);
112 // TODO: Should we callremoveInstalledIntents if this Intent was
Yuta HIGUCHI406a5652014-10-20 22:18:16 -0700113 return event;
114 }
115
116 @Override
117 public long getIntentCount() {
118 return intents.size();
119 }
120
121 @Override
122 public Iterable<Intent> getIntents() {
123 return ImmutableSet.copyOf(intents.values());
124 }
125
126 @Override
127 public Intent getIntent(IntentId intentId) {
128 return intents.get(intentId);
129 }
130
131 @Override
132 public IntentState getIntentState(IntentId id) {
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700133 final IntentState localState = transientStates.get(id);
134 if (localState != null) {
135 return localState;
136 }
Yuta HIGUCHI406a5652014-10-20 22:18:16 -0700137 return states.get(id);
138 }
139
140 @Override
141 public IntentEvent setState(Intent intent, IntentState state) {
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700142 final IntentId id = intent.id();
Pavlin Radoslavovc8ccbd92014-10-22 09:59:37 -0700143 IntentEvent.Type type = null;
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700144 IntentState prev = null;
145 boolean transientStateChangeOnly = false;
Pavlin Radoslavovc8ccbd92014-10-22 09:59:37 -0700146
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700147 // TODO: enable sanity checking if Debug enabled, etc.
Pavlin Radoslavovc8ccbd92014-10-22 09:59:37 -0700148 switch (state) {
149 case SUBMITTED:
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700150 prev = states.putIfAbsent(id, SUBMITTED);
151 verify(prev == null, "Illegal state transition attempted from %s to SUBMITTED", prev);
Pavlin Radoslavovc8ccbd92014-10-22 09:59:37 -0700152 type = IntentEvent.Type.SUBMITTED;
153 break;
154 case INSTALLED:
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700155 // parking state transition
156 prev = states.replace(id, INSTALLED);
157 verify(prev != null, "Illegal state transition attempted from non-SUBMITTED to INSTALLED");
Pavlin Radoslavovc8ccbd92014-10-22 09:59:37 -0700158 type = IntentEvent.Type.INSTALLED;
159 break;
160 case FAILED:
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700161 prev = states.replace(id, FAILED);
Pavlin Radoslavovc8ccbd92014-10-22 09:59:37 -0700162 type = IntentEvent.Type.FAILED;
163 break;
164 case WITHDRAWN:
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700165 prev = states.replace(id, WITHDRAWN);
166 verify(prev != null, "Illegal state transition attempted from non-WITHDRAWING to WITHDRAWN");
Pavlin Radoslavovc8ccbd92014-10-22 09:59:37 -0700167 type = IntentEvent.Type.WITHDRAWN;
168 break;
169 default:
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700170 transientStateChangeOnly = true;
Pavlin Radoslavovc8ccbd92014-10-22 09:59:37 -0700171 break;
172 }
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700173 if (!transientStateChangeOnly) {
174 log.debug("Parking State change: {} {}=>{}", id, prev, state);
175 }
176 // Update instance local state, which includes non-parking state transition
177 prev = transientStates.put(id, state);
178 log.debug("Transient State change: {} {}=>{}", id, prev, state);
179
Pavlin Radoslavovc8ccbd92014-10-22 09:59:37 -0700180 if (type == null) {
181 return null;
182 }
183 return new IntentEvent(type, intent);
Yuta HIGUCHI406a5652014-10-20 22:18:16 -0700184 }
185
186 @Override
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700187 public void setInstallableIntents(IntentId intentId, List<Intent> result) {
Yuta HIGUCHI406a5652014-10-20 22:18:16 -0700188 installable.put(intentId, result);
189 }
190
191 @Override
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700192 public List<Intent> getInstallableIntents(IntentId intentId) {
Yuta HIGUCHI406a5652014-10-20 22:18:16 -0700193 return installable.get(intentId);
194 }
195
196 @Override
197 public void removeInstalledIntents(IntentId intentId) {
198 installable.remove(intentId);
199 }
200
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700201 // FIXME add handler to react to remote event
Yuta HIGUCHI406a5652014-10-20 22:18:16 -0700202}