blob: 093020abae761d492de1efd1ad84931164cac53b [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 */
Thomas Vachuskac97aa612015-06-23 16:00:18 -070016package org.onosproject.store.trivial;
tome4729872014-09-23 00:37:37 -070017
Thomas Vachuskade563cf2015-04-01 00:28:50 -070018import com.google.common.collect.ImmutableSet;
tome4729872014-09-23 00:37:37 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
Brian O'Connor69d6ac72015-05-29 16:24:06 -070022import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
tome4729872014-09-23 00:37:37 -070024import org.apache.felix.scr.annotations.Service;
Madan Jampani7d2fab22015-03-18 17:21:57 -070025import org.joda.time.DateTime;
26import org.onlab.packet.IpAddress;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.cluster.ClusterEvent;
28import org.onosproject.cluster.ClusterStore;
29import org.onosproject.cluster.ClusterStoreDelegate;
30import org.onosproject.cluster.ControllerNode;
31import org.onosproject.cluster.DefaultControllerNode;
32import org.onosproject.cluster.NodeId;
Brian O'Connor69d6ac72015-05-29 16:24:06 -070033import org.onosproject.event.EventDeliveryService;
34import org.onosproject.event.ListenerRegistry;
Thomas Vachuska7648d662015-03-16 11:58:30 -070035import org.onosproject.net.intent.Key;
Madan Jampani1c965102016-01-13 14:34:16 -080036import org.onosproject.net.intent.IntentPartitionEvent;
37import org.onosproject.net.intent.IntentPartitionEventListener;
38import org.onosproject.net.intent.IntentPartitionService;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.store.AbstractStore;
tome4729872014-09-23 00:37:37 -070040import org.slf4j.Logger;
41
Thomas Vachuskade563cf2015-04-01 00:28:50 -070042import java.util.Set;
43
Heedo Kang4a47a302016-02-29 17:40:23 +090044import static org.onosproject.security.AppGuard.checkPermission;
45import static org.onosproject.security.AppPermission.Type.*;
Thomas Vachuskade563cf2015-04-01 00:28:50 -070046import static org.slf4j.LoggerFactory.getLogger;
tome4729872014-09-23 00:37:37 -070047
48/**
tomc749ae02014-09-29 13:53:51 -070049 * Manages inventory of infrastructure devices using trivial in-memory
tome4729872014-09-23 00:37:37 -070050 * structures implementation.
51 */
52@Component(immediate = true)
53@Service
tom0755a362014-09-24 11:54:43 -070054public class SimpleClusterStore
55 extends AbstractStore<ClusterEvent, ClusterStoreDelegate>
Madan Jampani1c965102016-01-13 14:34:16 -080056 implements ClusterStore, IntentPartitionService {
tome4729872014-09-23 00:37:37 -070057
Pavlin Radoslavov444b5192014-10-28 10:45:19 -070058 public static final IpAddress LOCALHOST = IpAddress.valueOf("127.0.0.1");
tome4729872014-09-23 00:37:37 -070059
60 private final Logger log = getLogger(getClass());
61
62 private ControllerNode instance;
63
Madan Jampani7d2fab22015-03-18 17:21:57 -070064 private final DateTime creationTime = DateTime.now();
65
Brian O'Connor69d6ac72015-05-29 16:24:06 -070066 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected EventDeliveryService eventDispatcher;
68
Madan Jampani1c965102016-01-13 14:34:16 -080069 private ListenerRegistry<IntentPartitionEvent, IntentPartitionEventListener> listenerRegistry;
Thomas Vachuska7a8de842016-03-07 20:56:35 -080070 private boolean started = false;
Brian O'Connor69d6ac72015-05-29 16:24:06 -070071
tome4729872014-09-23 00:37:37 -070072 @Activate
73 public void activate() {
74 instance = new DefaultControllerNode(new NodeId("local"), LOCALHOST);
Brian O'Connor69d6ac72015-05-29 16:24:06 -070075
76 listenerRegistry = new ListenerRegistry<>();
Madan Jampani1c965102016-01-13 14:34:16 -080077 eventDispatcher.addSink(IntentPartitionEvent.class, listenerRegistry);
Brian O'Connor69d6ac72015-05-29 16:24:06 -070078
tome4729872014-09-23 00:37:37 -070079 log.info("Started");
80 }
81
82 @Deactivate
83 public void deactivate() {
Madan Jampani1c965102016-01-13 14:34:16 -080084 eventDispatcher.removeSink(IntentPartitionEvent.class);
tome4729872014-09-23 00:37:37 -070085 log.info("Stopped");
86 }
87
88
89 @Override
90 public ControllerNode getLocalNode() {
91 return instance;
92 }
93
94 @Override
95 public Set<ControllerNode> getNodes() {
96 return ImmutableSet.of(instance);
97 }
98
99 @Override
100 public ControllerNode getNode(NodeId nodeId) {
101 return instance.id().equals(nodeId) ? instance : null;
102 }
103
104 @Override
105 public ControllerNode.State getState(NodeId nodeId) {
106 return ControllerNode.State.ACTIVE;
107 }
108
tomb41d1ac2014-09-24 01:51:24 -0700109 @Override
Thomas Vachuska7a8de842016-03-07 20:56:35 -0800110 public void markFullyStarted(boolean started) {
111 this.started = started;
112 }
113
114 @Override
Madan Jampani7d2fab22015-03-18 17:21:57 -0700115 public DateTime getLastUpdated(NodeId nodeId) {
116 return creationTime;
117 }
118
119 @Override
Pavlin Radoslavov444b5192014-10-28 10:45:19 -0700120 public ControllerNode addNode(NodeId nodeId, IpAddress ip, int tcpPort) {
tomee49c372014-09-26 15:14:50 -0700121 return null;
122 }
123
124 @Override
tomb41d1ac2014-09-24 01:51:24 -0700125 public void removeNode(NodeId nodeId) {
126 }
127
Thomas Vachuska7648d662015-03-16 11:58:30 -0700128 @Override
129 public boolean isMine(Key intentKey) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900130 checkPermission(INTENT_READ);
Thomas Vachuska7648d662015-03-16 11:58:30 -0700131 return true;
132 }
133
134 @Override
135 public NodeId getLeader(Key intentKey) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900136 checkPermission(INTENT_READ);
Thomas Vachuska7648d662015-03-16 11:58:30 -0700137 return instance.id();
138 }
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700139
140 @Override
Madan Jampani1c965102016-01-13 14:34:16 -0800141 public void addListener(IntentPartitionEventListener listener) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900142 checkPermission(INTENT_EVENT);
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700143 listenerRegistry.addListener(listener);
144 }
145
146 @Override
Madan Jampani1c965102016-01-13 14:34:16 -0800147 public void removeListener(IntentPartitionEventListener listener) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900148 checkPermission(INTENT_EVENT);
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700149 listenerRegistry.removeListener(listener);
150 }
tome4729872014-09-23 00:37:37 -0700151}