blob: 5eea3cc8b33ce034a9128600112ec7f7c2abefb2 [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;
Brian O'Connor69d6ac72015-05-29 16:24:06 -070036import org.onosproject.net.intent.PartitionEvent;
37import org.onosproject.net.intent.PartitionEventListener;
Thomas Vachuska7648d662015-03-16 11:58:30 -070038import org.onosproject.net.intent.PartitionService;
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
44import static org.slf4j.LoggerFactory.getLogger;
tome4729872014-09-23 00:37:37 -070045
46/**
tomc749ae02014-09-29 13:53:51 -070047 * Manages inventory of infrastructure devices using trivial in-memory
tome4729872014-09-23 00:37:37 -070048 * structures implementation.
49 */
50@Component(immediate = true)
51@Service
tom0755a362014-09-24 11:54:43 -070052public class SimpleClusterStore
53 extends AbstractStore<ClusterEvent, ClusterStoreDelegate>
Thomas Vachuska7648d662015-03-16 11:58:30 -070054 implements ClusterStore, PartitionService {
tome4729872014-09-23 00:37:37 -070055
Pavlin Radoslavov444b5192014-10-28 10:45:19 -070056 public static final IpAddress LOCALHOST = IpAddress.valueOf("127.0.0.1");
tome4729872014-09-23 00:37:37 -070057
58 private final Logger log = getLogger(getClass());
59
60 private ControllerNode instance;
61
Madan Jampani7d2fab22015-03-18 17:21:57 -070062 private final DateTime creationTime = DateTime.now();
63
Brian O'Connor69d6ac72015-05-29 16:24:06 -070064 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected EventDeliveryService eventDispatcher;
66
67 private ListenerRegistry<PartitionEvent, PartitionEventListener> listenerRegistry;
68
tome4729872014-09-23 00:37:37 -070069 @Activate
70 public void activate() {
71 instance = new DefaultControllerNode(new NodeId("local"), LOCALHOST);
Brian O'Connor69d6ac72015-05-29 16:24:06 -070072
73 listenerRegistry = new ListenerRegistry<>();
74 eventDispatcher.addSink(PartitionEvent.class, listenerRegistry);
75
tome4729872014-09-23 00:37:37 -070076 log.info("Started");
77 }
78
79 @Deactivate
80 public void deactivate() {
Brian O'Connor69d6ac72015-05-29 16:24:06 -070081 eventDispatcher.removeSink(PartitionEvent.class);
tome4729872014-09-23 00:37:37 -070082 log.info("Stopped");
83 }
84
85
86 @Override
87 public ControllerNode getLocalNode() {
88 return instance;
89 }
90
91 @Override
92 public Set<ControllerNode> getNodes() {
93 return ImmutableSet.of(instance);
94 }
95
96 @Override
97 public ControllerNode getNode(NodeId nodeId) {
98 return instance.id().equals(nodeId) ? instance : null;
99 }
100
101 @Override
102 public ControllerNode.State getState(NodeId nodeId) {
103 return ControllerNode.State.ACTIVE;
104 }
105
tomb41d1ac2014-09-24 01:51:24 -0700106 @Override
Madan Jampani7d2fab22015-03-18 17:21:57 -0700107 public DateTime getLastUpdated(NodeId nodeId) {
108 return creationTime;
109 }
110
111 @Override
Pavlin Radoslavov444b5192014-10-28 10:45:19 -0700112 public ControllerNode addNode(NodeId nodeId, IpAddress ip, int tcpPort) {
tomee49c372014-09-26 15:14:50 -0700113 return null;
114 }
115
116 @Override
tomb41d1ac2014-09-24 01:51:24 -0700117 public void removeNode(NodeId nodeId) {
118 }
119
Thomas Vachuska7648d662015-03-16 11:58:30 -0700120 @Override
121 public boolean isMine(Key intentKey) {
122 return true;
123 }
124
125 @Override
126 public NodeId getLeader(Key intentKey) {
127 return instance.id();
128 }
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700129
130 @Override
131 public void addListener(PartitionEventListener listener) {
132 listenerRegistry.addListener(listener);
133 }
134
135 @Override
136 public void removeListener(PartitionEventListener listener) {
137 listenerRegistry.removeListener(listener);
138 }
tome4729872014-09-23 00:37:37 -0700139}