blob: c28adf5fffd0a70be12fa5c2fc6823a3443a0b3e [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;
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;
Brian O'Connor66630c82014-10-02 21:08:19 -070023import org.onlab.onos.net.intent.Intent;
24import org.onlab.onos.net.intent.IntentEvent;
25import org.onlab.onos.net.intent.IntentId;
26import org.onlab.onos.net.intent.IntentState;
27import org.onlab.onos.net.intent.IntentStore;
28import org.onlab.onos.net.intent.IntentStoreDelegate;
29import org.onlab.onos.store.AbstractStore;
30import org.slf4j.Logger;
31
Thomas Vachuskac96058a2014-10-20 23:00:16 -070032import java.util.List;
33import java.util.Map;
34import java.util.concurrent.ConcurrentHashMap;
35
36import static org.onlab.onos.net.intent.IntentState.*;
37import static org.slf4j.LoggerFactory.getLogger;
Brian O'Connor66630c82014-10-02 21:08:19 -070038
39@Component(immediate = true)
40@Service
41public class SimpleIntentStore
tom85258ee2014-10-07 00:10:02 -070042 extends AbstractStore<IntentEvent, IntentStoreDelegate>
43 implements IntentStore {
Brian O'Connor66630c82014-10-02 21:08:19 -070044
45 private final Logger log = getLogger(getClass());
Jonathan Hart11096402014-10-20 17:31:49 -070046 private final Map<IntentId, Intent> intents = new ConcurrentHashMap<>();
47 private final Map<IntentId, IntentState> states = new ConcurrentHashMap<>();
Thomas Vachuskac96058a2014-10-20 23:00:16 -070048 private final Map<IntentId, List<Intent>> installable =
Jonathan Hart11096402014-10-20 17:31:49 -070049 new ConcurrentHashMap<>();
Brian O'Connor66630c82014-10-02 21:08:19 -070050
51 @Activate
52 public void activate() {
53 log.info("Started");
54 }
55
56 @Deactivate
57 public void deactivate() {
58 log.info("Stopped");
59 }
60
61 @Override
62 public IntentEvent createIntent(Intent intent) {
tom85258ee2014-10-07 00:10:02 -070063 intents.put(intent.id(), intent);
Brian O'Connor66630c82014-10-02 21:08:19 -070064 return this.setState(intent, IntentState.SUBMITTED);
65 }
66
67 @Override
68 public IntentEvent removeIntent(IntentId intentId) {
69 Intent intent = intents.remove(intentId);
70 installable.remove(intentId);
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -070071 if (intent == null) {
72 // was already removed
73 return null;
74 }
tom85258ee2014-10-07 00:10:02 -070075 IntentEvent event = this.setState(intent, WITHDRAWN);
Brian O'Connor66630c82014-10-02 21:08:19 -070076 states.remove(intentId);
77 return event;
78 }
79
80 @Override
81 public long getIntentCount() {
82 return intents.size();
83 }
84
85 @Override
86 public Iterable<Intent> getIntents() {
87 return ImmutableSet.copyOf(intents.values());
88 }
89
90 @Override
91 public Intent getIntent(IntentId intentId) {
92 return intents.get(intentId);
93 }
94
95 @Override
96 public IntentState getIntentState(IntentId id) {
97 return states.get(id);
98 }
99
Brian O'Connor66630c82014-10-02 21:08:19 -0700100 @Override
tom85258ee2014-10-07 00:10:02 -0700101 public IntentEvent setState(Intent intent, IntentState state) {
102 IntentId id = intent.id();
103 states.put(id, state);
Pavlin Radoslavov2ca9cf22014-10-22 10:39:40 -0700104 IntentEvent.Type type = null;
105
106 switch (state) {
107 case SUBMITTED:
108 type = IntentEvent.Type.SUBMITTED;
109 break;
110 case INSTALLED:
111 type = IntentEvent.Type.INSTALLED;
112 break;
113 case FAILED:
114 type = IntentEvent.Type.FAILED;
115 break;
116 case WITHDRAWN:
117 type = IntentEvent.Type.WITHDRAWN;
118 break;
119 default:
120 break;
121 }
122 if (type == null) {
123 return null;
124 }
125 return new IntentEvent(type, intent);
Brian O'Connor66630c82014-10-02 21:08:19 -0700126 }
127
128 @Override
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700129 public void setInstallableIntents(IntentId intentId, List<Intent> result) {
Brian O'Connor66630c82014-10-02 21:08:19 -0700130 installable.put(intentId, result);
Brian O'Connor66630c82014-10-02 21:08:19 -0700131 }
132
133 @Override
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700134 public List<Intent> getInstallableIntents(IntentId intentId) {
Brian O'Connor66630c82014-10-02 21:08:19 -0700135 return installable.get(intentId);
136 }
137
138 @Override
139 public void removeInstalledIntents(IntentId intentId) {
140 installable.remove(intentId);
141 }
142
143}