Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
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; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 25 | import org.joda.time.DateTime; |
| 26 | import org.onlab.packet.IpAddress; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.cluster.ClusterEvent; |
| 28 | import org.onosproject.cluster.ClusterStore; |
| 29 | import org.onosproject.cluster.ClusterStoreDelegate; |
| 30 | import org.onosproject.cluster.ControllerNode; |
| 31 | import org.onosproject.cluster.DefaultControllerNode; |
| 32 | import org.onosproject.cluster.NodeId; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 33 | import org.onosproject.event.EventDeliveryService; |
| 34 | import org.onosproject.event.ListenerRegistry; |
Thomas Vachuska | 7648d66 | 2015-03-16 11:58:30 -0700 | [diff] [blame] | 35 | import org.onosproject.net.intent.Key; |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 36 | import org.onosproject.net.intent.IntentPartitionEvent; |
| 37 | import org.onosproject.net.intent.IntentPartitionEventListener; |
| 38 | import org.onosproject.net.intent.IntentPartitionService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.store.AbstractStore; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 42 | import java.util.Set; |
| 43 | |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
tom | c749ae0 | 2014-09-29 13:53:51 -0700 | [diff] [blame] | 47 | * Manages inventory of infrastructure devices using trivial in-memory |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 48 | * structures implementation. |
| 49 | */ |
| 50 | @Component(immediate = true) |
| 51 | @Service |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 52 | public class SimpleClusterStore |
| 53 | extends AbstractStore<ClusterEvent, ClusterStoreDelegate> |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 54 | implements ClusterStore, IntentPartitionService { |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 55 | |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 56 | public static final IpAddress LOCALHOST = IpAddress.valueOf("127.0.0.1"); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 57 | |
| 58 | private final Logger log = getLogger(getClass()); |
| 59 | |
| 60 | private ControllerNode instance; |
| 61 | |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 62 | private final DateTime creationTime = DateTime.now(); |
| 63 | |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected EventDeliveryService eventDispatcher; |
| 66 | |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 67 | private ListenerRegistry<IntentPartitionEvent, IntentPartitionEventListener> listenerRegistry; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 68 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 69 | @Activate |
| 70 | public void activate() { |
| 71 | instance = new DefaultControllerNode(new NodeId("local"), LOCALHOST); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 72 | |
| 73 | listenerRegistry = new ListenerRegistry<>(); |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 74 | eventDispatcher.addSink(IntentPartitionEvent.class, listenerRegistry); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 75 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 76 | log.info("Started"); |
| 77 | } |
| 78 | |
| 79 | @Deactivate |
| 80 | public void deactivate() { |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 81 | eventDispatcher.removeSink(IntentPartitionEvent.class); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 82 | 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 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 106 | @Override |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 107 | public DateTime getLastUpdated(NodeId nodeId) { |
| 108 | return creationTime; |
| 109 | } |
| 110 | |
| 111 | @Override |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 112 | public ControllerNode addNode(NodeId nodeId, IpAddress ip, int tcpPort) { |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 113 | return null; |
| 114 | } |
| 115 | |
| 116 | @Override |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 117 | public void removeNode(NodeId nodeId) { |
| 118 | } |
| 119 | |
Thomas Vachuska | 7648d66 | 2015-03-16 11:58:30 -0700 | [diff] [blame] | 120 | @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'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 129 | |
| 130 | @Override |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 131 | public void addListener(IntentPartitionEventListener listener) { |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 132 | listenerRegistry.addListener(listener); |
| 133 | } |
| 134 | |
| 135 | @Override |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 136 | public void removeListener(IntentPartitionEventListener listener) { |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 137 | listenerRegistry.removeListener(listener); |
| 138 | } |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 139 | } |