blob: 537c62999bbbada85cf6e3bd659af2e295196475 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Madan Jampani7d2fab22015-03-18 17:21:57 -070019import org.onlab.packet.IpAddress;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.cluster.ClusterEvent;
21import org.onosproject.cluster.ClusterStore;
22import org.onosproject.cluster.ClusterStoreDelegate;
23import org.onosproject.cluster.ControllerNode;
24import org.onosproject.cluster.DefaultControllerNode;
Jordan Halterman00e92da2018-05-22 23:05:52 -070025import org.onosproject.cluster.Node;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.cluster.NodeId;
Jordan Haltermanf70bf462017-07-29 13:12:00 -070027import org.onosproject.core.Version;
28import org.onosproject.core.VersionService;
Brian O'Connor69d6ac72015-05-29 16:24:06 -070029import org.onosproject.event.EventDeliveryService;
30import org.onosproject.event.ListenerRegistry;
Madan Jampani3b8101a2016-09-15 13:22:01 -070031import org.onosproject.net.intent.WorkPartitionEvent;
32import org.onosproject.net.intent.WorkPartitionEventListener;
33import org.onosproject.net.intent.WorkPartitionService;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.store.AbstractStore;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070035import org.osgi.service.component.annotations.Activate;
36import org.osgi.service.component.annotations.Component;
37import org.osgi.service.component.annotations.Deactivate;
38import org.osgi.service.component.annotations.Reference;
39import org.osgi.service.component.annotations.ReferenceCardinality;
tome4729872014-09-23 00:37:37 -070040import org.slf4j.Logger;
41
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070042import java.time.Instant;
Thomas Vachuskade563cf2015-04-01 00:28:50 -070043import java.util.Set;
Madan Jampani3b8101a2016-09-15 13:22:01 -070044import java.util.function.Function;
Thomas Vachuskade563cf2015-04-01 00:28:50 -070045
Heedo Kang4a47a302016-02-29 17:40:23 +090046import static org.onosproject.security.AppGuard.checkPermission;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070047import static org.onosproject.security.AppPermission.Type.INTENT_EVENT;
48import static org.onosproject.security.AppPermission.Type.INTENT_READ;
Thomas Vachuskade563cf2015-04-01 00:28:50 -070049import static org.slf4j.LoggerFactory.getLogger;
tome4729872014-09-23 00:37:37 -070050
51/**
tomc749ae02014-09-29 13:53:51 -070052 * Manages inventory of infrastructure devices using trivial in-memory
tome4729872014-09-23 00:37:37 -070053 * structures implementation.
54 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070055@Component(immediate = true, service = { ClusterStore.class, WorkPartitionService.class })
tom0755a362014-09-24 11:54:43 -070056public class SimpleClusterStore
57 extends AbstractStore<ClusterEvent, ClusterStoreDelegate>
Madan Jampani3b8101a2016-09-15 13:22:01 -070058 implements ClusterStore, WorkPartitionService {
tome4729872014-09-23 00:37:37 -070059
Pavlin Radoslavov444b5192014-10-28 10:45:19 -070060 public static final IpAddress LOCALHOST = IpAddress.valueOf("127.0.0.1");
tome4729872014-09-23 00:37:37 -070061
62 private final Logger log = getLogger(getClass());
63
64 private ControllerNode instance;
65
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070066 private final Instant creationTime = Instant.now();
Madan Jampani7d2fab22015-03-18 17:21:57 -070067
Ray Milkeyd84f89b2018-08-17 14:54:17 -070068 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Brian O'Connor69d6ac72015-05-29 16:24:06 -070069 protected EventDeliveryService eventDispatcher;
70
Ray Milkeyd84f89b2018-08-17 14:54:17 -070071 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jordan Haltermanf70bf462017-07-29 13:12:00 -070072 protected VersionService versionService;
73
Madan Jampani3b8101a2016-09-15 13:22:01 -070074 private ListenerRegistry<WorkPartitionEvent, WorkPartitionEventListener> listenerRegistry;
Thomas Vachuska7a8de842016-03-07 20:56:35 -080075 private boolean started = false;
Brian O'Connor69d6ac72015-05-29 16:24:06 -070076
tome4729872014-09-23 00:37:37 -070077 @Activate
78 public void activate() {
79 instance = new DefaultControllerNode(new NodeId("local"), LOCALHOST);
Brian O'Connor69d6ac72015-05-29 16:24:06 -070080
81 listenerRegistry = new ListenerRegistry<>();
Madan Jampani3b8101a2016-09-15 13:22:01 -070082 eventDispatcher.addSink(WorkPartitionEvent.class, listenerRegistry);
Brian O'Connor69d6ac72015-05-29 16:24:06 -070083
tome4729872014-09-23 00:37:37 -070084 log.info("Started");
85 }
86
87 @Deactivate
88 public void deactivate() {
Madan Jampani3b8101a2016-09-15 13:22:01 -070089 eventDispatcher.removeSink(WorkPartitionEvent.class);
tome4729872014-09-23 00:37:37 -070090 log.info("Stopped");
91 }
92
93
94 @Override
95 public ControllerNode getLocalNode() {
96 return instance;
97 }
98
99 @Override
100 public Set<ControllerNode> getNodes() {
101 return ImmutableSet.of(instance);
102 }
103
104 @Override
Jordan Halterman00e92da2018-05-22 23:05:52 -0700105 public Set<Node> getStorageNodes() {
106 return ImmutableSet.of();
107 }
108
109 @Override
tome4729872014-09-23 00:37:37 -0700110 public ControllerNode getNode(NodeId nodeId) {
111 return instance.id().equals(nodeId) ? instance : null;
112 }
113
114 @Override
115 public ControllerNode.State getState(NodeId nodeId) {
116 return ControllerNode.State.ACTIVE;
117 }
118
tomb41d1ac2014-09-24 01:51:24 -0700119 @Override
Jordan Haltermanf70bf462017-07-29 13:12:00 -0700120 public Version getVersion(NodeId nodeId) {
121 return instance.id().equals(nodeId) ? versionService.version() : null;
122 }
123
124 @Override
Thomas Vachuska7a8de842016-03-07 20:56:35 -0800125 public void markFullyStarted(boolean started) {
126 this.started = started;
127 }
128
129 @Override
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700130 public Instant getLastUpdatedInstant(NodeId nodeId) {
Madan Jampani7d2fab22015-03-18 17:21:57 -0700131 return creationTime;
132 }
133
134 @Override
Pavlin Radoslavov444b5192014-10-28 10:45:19 -0700135 public ControllerNode addNode(NodeId nodeId, IpAddress ip, int tcpPort) {
tomee49c372014-09-26 15:14:50 -0700136 return null;
137 }
138
139 @Override
tomb41d1ac2014-09-24 01:51:24 -0700140 public void removeNode(NodeId nodeId) {
141 }
142
Thomas Vachuska7648d662015-03-16 11:58:30 -0700143 @Override
Madan Jampani3b8101a2016-09-15 13:22:01 -0700144 public <K> boolean isMine(K key, Function<K, Long> hasher) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900145 checkPermission(INTENT_READ);
Thomas Vachuska7648d662015-03-16 11:58:30 -0700146 return true;
147 }
148
149 @Override
Madan Jampani3b8101a2016-09-15 13:22:01 -0700150 public <K> NodeId getLeader(K key, Function<K, Long> hasher) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900151 checkPermission(INTENT_READ);
Thomas Vachuska7648d662015-03-16 11:58:30 -0700152 return instance.id();
153 }
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700154
155 @Override
Madan Jampani3b8101a2016-09-15 13:22:01 -0700156 public void addListener(WorkPartitionEventListener listener) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900157 checkPermission(INTENT_EVENT);
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700158 listenerRegistry.addListener(listener);
159 }
160
161 @Override
Madan Jampani3b8101a2016-09-15 13:22:01 -0700162 public void removeListener(WorkPartitionEventListener listener) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900163 checkPermission(INTENT_EVENT);
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700164 listenerRegistry.removeListener(listener);
165 }
tome4729872014-09-23 00:37:37 -0700166}