blob: 5a91276f4861add17d5424bcd3bc0abd5583f71c [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
Brian O'Connorfa81eae2014-10-30 13:20:05 -070036import static org.onlab.onos.net.intent.IntentState.WITHDRAWN;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070037import 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<>();
Brian O'Connorfa81eae2014-10-30 13:20:05 -070048 private final Map<IntentId, List<Intent>> installable = new ConcurrentHashMap<>();
49
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) {
Brian O'Connorfa81eae2014-10-30 13:20:05 -070063 if (intents.containsKey(intent.id())) {
64 return null;
65 }
tom85258ee2014-10-07 00:10:02 -070066 intents.put(intent.id(), intent);
Brian O'Connor66630c82014-10-02 21:08:19 -070067 return this.setState(intent, IntentState.SUBMITTED);
68 }
69
70 @Override
71 public IntentEvent removeIntent(IntentId intentId) {
72 Intent intent = intents.remove(intentId);
73 installable.remove(intentId);
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -070074 if (intent == null) {
75 // was already removed
76 return null;
77 }
tom85258ee2014-10-07 00:10:02 -070078 IntentEvent event = this.setState(intent, WITHDRAWN);
Brian O'Connor66630c82014-10-02 21:08:19 -070079 states.remove(intentId);
80 return event;
81 }
82
83 @Override
84 public long getIntentCount() {
85 return intents.size();
86 }
87
88 @Override
89 public Iterable<Intent> getIntents() {
90 return ImmutableSet.copyOf(intents.values());
91 }
92
93 @Override
94 public Intent getIntent(IntentId intentId) {
95 return intents.get(intentId);
96 }
97
98 @Override
99 public IntentState getIntentState(IntentId id) {
100 return states.get(id);
101 }
102
Brian O'Connor66630c82014-10-02 21:08:19 -0700103 @Override
tom85258ee2014-10-07 00:10:02 -0700104 public IntentEvent setState(Intent intent, IntentState state) {
105 IntentId id = intent.id();
106 states.put(id, state);
Pavlin Radoslavov2ca9cf22014-10-22 10:39:40 -0700107 IntentEvent.Type type = null;
108
109 switch (state) {
110 case SUBMITTED:
111 type = IntentEvent.Type.SUBMITTED;
112 break;
113 case INSTALLED:
114 type = IntentEvent.Type.INSTALLED;
115 break;
116 case FAILED:
117 type = IntentEvent.Type.FAILED;
118 break;
119 case WITHDRAWN:
120 type = IntentEvent.Type.WITHDRAWN;
121 break;
122 default:
123 break;
124 }
125 if (type == null) {
126 return null;
127 }
128 return new IntentEvent(type, intent);
Brian O'Connor66630c82014-10-02 21:08:19 -0700129 }
130
131 @Override
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700132 public void setInstallableIntents(IntentId intentId, List<Intent> result) {
Brian O'Connor66630c82014-10-02 21:08:19 -0700133 installable.put(intentId, result);
Brian O'Connor66630c82014-10-02 21:08:19 -0700134 }
135
136 @Override
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700137 public List<Intent> getInstallableIntents(IntentId intentId) {
Brian O'Connor66630c82014-10-02 21:08:19 -0700138 return installable.get(intentId);
139 }
140
141 @Override
142 public void removeInstalledIntents(IntentId intentId) {
143 installable.remove(intentId);
144 }
145
146}