blob: ab41805eaa53c65bdcd059b405138e132dbc93a7 [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 HIGUCHI406a5652014-10-20 22:18:16 -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;
Yuta HIGUCHI406a5652014-10-20 22:18:16 -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 Vachuskab97cf282014-10-20 23:31:12 -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;
Yuta HIGUCHI406a5652014-10-20 22:18:16 -070038
39//FIXME: I LIE I AM NOT DISTRIBUTED
40@Component(immediate = true)
41@Service
42public class DistributedIntentStore
43 extends AbstractStore<IntentEvent, IntentStoreDelegate>
44 implements IntentStore {
45
46 private final Logger log = getLogger(getClass());
47 private final Map<IntentId, Intent> intents = new ConcurrentHashMap<>();
48 private final Map<IntentId, IntentState> states = new ConcurrentHashMap<>();
Thomas Vachuskab97cf282014-10-20 23:31:12 -070049 private final Map<IntentId, List<Intent>> installable = new ConcurrentHashMap<>();
Yuta HIGUCHI406a5652014-10-20 22:18:16 -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) {
63 intents.put(intent.id(), intent);
64 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);
71 IntentEvent event = this.setState(intent, WITHDRAWN);
72 states.remove(intentId);
73 return event;
74 }
75
76 @Override
77 public long getIntentCount() {
78 return intents.size();
79 }
80
81 @Override
82 public Iterable<Intent> getIntents() {
83 return ImmutableSet.copyOf(intents.values());
84 }
85
86 @Override
87 public Intent getIntent(IntentId intentId) {
88 return intents.get(intentId);
89 }
90
91 @Override
92 public IntentState getIntentState(IntentId id) {
93 return states.get(id);
94 }
95
96 @Override
97 public IntentEvent setState(Intent intent, IntentState state) {
98 IntentId id = intent.id();
99 states.put(id, state);
Pavlin Radoslavovc8ccbd92014-10-22 09:59:37 -0700100 IntentEvent.Type type = null;
101
102 switch (state) {
103 case SUBMITTED:
104 type = IntentEvent.Type.SUBMITTED;
105 break;
106 case INSTALLED:
107 type = IntentEvent.Type.INSTALLED;
108 break;
109 case FAILED:
110 type = IntentEvent.Type.FAILED;
111 break;
112 case WITHDRAWN:
113 type = IntentEvent.Type.WITHDRAWN;
114 break;
115 default:
116 break;
117 }
118 if (type == null) {
119 return null;
120 }
121 return new IntentEvent(type, intent);
Yuta HIGUCHI406a5652014-10-20 22:18:16 -0700122 }
123
124 @Override
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700125 public void addInstallableIntents(IntentId intentId, List<Intent> result) {
Yuta HIGUCHI406a5652014-10-20 22:18:16 -0700126 installable.put(intentId, result);
127 }
128
129 @Override
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700130 public List<Intent> getInstallableIntents(IntentId intentId) {
Yuta HIGUCHI406a5652014-10-20 22:18:16 -0700131 return installable.get(intentId);
132 }
133
134 @Override
135 public void removeInstalledIntents(IntentId intentId) {
136 installable.remove(intentId);
137 }
138
139}