Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 16 | package org.onosproject.store.trivial; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 19 | import org.onlab.packet.IpAddress; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.cluster.ClusterEvent; |
| 21 | import org.onosproject.cluster.ClusterStore; |
| 22 | import org.onosproject.cluster.ClusterStoreDelegate; |
| 23 | import org.onosproject.cluster.ControllerNode; |
| 24 | import org.onosproject.cluster.DefaultControllerNode; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 25 | import org.onosproject.cluster.Node; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.cluster.NodeId; |
Jordan Halterman | f70bf46 | 2017-07-29 13:12:00 -0700 | [diff] [blame] | 27 | import org.onosproject.core.Version; |
| 28 | import org.onosproject.core.VersionService; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 29 | import org.onosproject.event.EventDeliveryService; |
| 30 | import org.onosproject.event.ListenerRegistry; |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 31 | import org.onosproject.net.intent.WorkPartitionEvent; |
| 32 | import org.onosproject.net.intent.WorkPartitionEventListener; |
| 33 | import org.onosproject.net.intent.WorkPartitionService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.store.AbstractStore; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 35 | import org.osgi.service.component.annotations.Activate; |
| 36 | import org.osgi.service.component.annotations.Component; |
| 37 | import org.osgi.service.component.annotations.Deactivate; |
| 38 | import org.osgi.service.component.annotations.Reference; |
| 39 | import org.osgi.service.component.annotations.ReferenceCardinality; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 42 | import java.time.Instant; |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 43 | import java.util.Set; |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 44 | import java.util.function.Function; |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 45 | |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 46 | import static org.onosproject.security.AppGuard.checkPermission; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 47 | import static org.onosproject.security.AppPermission.Type.INTENT_EVENT; |
| 48 | import static org.onosproject.security.AppPermission.Type.INTENT_READ; |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 49 | import static org.slf4j.LoggerFactory.getLogger; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
tom | c749ae0 | 2014-09-29 13:53:51 -0700 | [diff] [blame] | 52 | * Manages inventory of infrastructure devices using trivial in-memory |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 53 | * structures implementation. |
| 54 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 55 | @Component(immediate = true, service = { ClusterStore.class, WorkPartitionService.class }) |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 56 | public class SimpleClusterStore |
| 57 | extends AbstractStore<ClusterEvent, ClusterStoreDelegate> |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 58 | implements ClusterStore, WorkPartitionService { |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 59 | |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 60 | public static final IpAddress LOCALHOST = IpAddress.valueOf("127.0.0.1"); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 61 | |
| 62 | private final Logger log = getLogger(getClass()); |
| 63 | |
| 64 | private ControllerNode instance; |
| 65 | |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 66 | private final Instant creationTime = Instant.now(); |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 67 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 69 | protected EventDeliveryService eventDispatcher; |
| 70 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jordan Halterman | f70bf46 | 2017-07-29 13:12:00 -0700 | [diff] [blame] | 72 | protected VersionService versionService; |
| 73 | |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 74 | private ListenerRegistry<WorkPartitionEvent, WorkPartitionEventListener> listenerRegistry; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 75 | private boolean started = false; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 76 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 77 | @Activate |
| 78 | public void activate() { |
| 79 | instance = new DefaultControllerNode(new NodeId("local"), LOCALHOST); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 80 | |
| 81 | listenerRegistry = new ListenerRegistry<>(); |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 82 | eventDispatcher.addSink(WorkPartitionEvent.class, listenerRegistry); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 83 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 84 | log.info("Started"); |
| 85 | } |
| 86 | |
| 87 | @Deactivate |
| 88 | public void deactivate() { |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 89 | eventDispatcher.removeSink(WorkPartitionEvent.class); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 90 | 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 Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 105 | public Set<Node> getStorageNodes() { |
| 106 | return ImmutableSet.of(); |
| 107 | } |
| 108 | |
| 109 | @Override |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 110 | 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 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 119 | @Override |
Jordan Halterman | f70bf46 | 2017-07-29 13:12:00 -0700 | [diff] [blame] | 120 | public Version getVersion(NodeId nodeId) { |
| 121 | return instance.id().equals(nodeId) ? versionService.version() : null; |
| 122 | } |
| 123 | |
| 124 | @Override |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 125 | public void markFullyStarted(boolean started) { |
| 126 | this.started = started; |
| 127 | } |
| 128 | |
| 129 | @Override |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 130 | public Instant getLastUpdatedInstant(NodeId nodeId) { |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 131 | return creationTime; |
| 132 | } |
| 133 | |
| 134 | @Override |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 135 | public ControllerNode addNode(NodeId nodeId, IpAddress ip, int tcpPort) { |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 136 | return null; |
| 137 | } |
| 138 | |
| 139 | @Override |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 140 | public void removeNode(NodeId nodeId) { |
| 141 | } |
| 142 | |
Thomas Vachuska | 7648d66 | 2015-03-16 11:58:30 -0700 | [diff] [blame] | 143 | @Override |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 144 | public <K> boolean isMine(K key, Function<K, Long> hasher) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 145 | checkPermission(INTENT_READ); |
Thomas Vachuska | 7648d66 | 2015-03-16 11:58:30 -0700 | [diff] [blame] | 146 | return true; |
| 147 | } |
| 148 | |
| 149 | @Override |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 150 | public <K> NodeId getLeader(K key, Function<K, Long> hasher) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 151 | checkPermission(INTENT_READ); |
Thomas Vachuska | 7648d66 | 2015-03-16 11:58:30 -0700 | [diff] [blame] | 152 | return instance.id(); |
| 153 | } |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 154 | |
| 155 | @Override |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 156 | public void addListener(WorkPartitionEventListener listener) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 157 | checkPermission(INTENT_EVENT); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 158 | listenerRegistry.addListener(listener); |
| 159 | } |
| 160 | |
| 161 | @Override |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 162 | public void removeListener(WorkPartitionEventListener listener) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 163 | checkPermission(INTENT_EVENT); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 164 | listenerRegistry.removeListener(listener); |
| 165 | } |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 166 | } |