Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cluster.impl; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 17 | |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame^] | 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
| 22 | import java.util.Set; |
| 23 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 29 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame^] | 30 | import org.joda.time.DateTime; |
| 31 | import org.onlab.packet.IpAddress; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.cluster.ClusterAdminService; |
| 33 | import org.onosproject.cluster.ClusterEvent; |
| 34 | import org.onosproject.cluster.ClusterEventListener; |
| 35 | import org.onosproject.cluster.ClusterService; |
| 36 | import org.onosproject.cluster.ClusterStore; |
| 37 | import org.onosproject.cluster.ClusterStoreDelegate; |
| 38 | import org.onosproject.cluster.ControllerNode; |
| 39 | import org.onosproject.cluster.NodeId; |
| 40 | import org.onosproject.event.AbstractListenerRegistry; |
| 41 | import org.onosproject.event.EventDeliveryService; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Implementation of the cluster service. |
| 46 | */ |
Madan Jampani | dd6479f | 2015-03-02 02:23:44 +0000 | [diff] [blame] | 47 | @Component(immediate = true) |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 48 | @Service |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 49 | public class ClusterManager implements ClusterService, ClusterAdminService { |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 50 | |
| 51 | public static final String INSTANCE_ID_NULL = "Instance ID cannot be null"; |
| 52 | private final Logger log = getLogger(getClass()); |
| 53 | |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 54 | private ClusterStoreDelegate delegate = new InternalStoreDelegate(); |
| 55 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 56 | protected final AbstractListenerRegistry<ClusterEvent, ClusterEventListener> |
| 57 | listenerRegistry = new AbstractListenerRegistry<>(); |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected ClusterStore store; |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected EventDeliveryService eventDispatcher; |
| 64 | |
| 65 | @Activate |
| 66 | public void activate() { |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 67 | store.setDelegate(delegate); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 68 | eventDispatcher.addSink(ClusterEvent.class, listenerRegistry); |
| 69 | log.info("Started"); |
| 70 | } |
| 71 | |
| 72 | @Deactivate |
| 73 | public void deactivate() { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 74 | store.unsetDelegate(delegate); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 75 | eventDispatcher.removeSink(ClusterEvent.class); |
| 76 | log.info("Stopped"); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public ControllerNode getLocalNode() { |
| 81 | return store.getLocalNode(); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public Set<ControllerNode> getNodes() { |
| 86 | return store.getNodes(); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public ControllerNode getNode(NodeId nodeId) { |
| 91 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 92 | return store.getNode(nodeId); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public ControllerNode.State getState(NodeId nodeId) { |
| 97 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 98 | return store.getState(nodeId); |
| 99 | } |
| 100 | |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame^] | 101 | |
| 102 | @Override |
| 103 | public DateTime getLastUpdated(NodeId nodeId) { |
| 104 | return store.getLastUpdated(nodeId); |
| 105 | } |
| 106 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 107 | @Override |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 108 | public ControllerNode addNode(NodeId nodeId, IpAddress ip, int tcpPort) { |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 109 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 110 | checkNotNull(ip, "IP address cannot be null"); |
| 111 | checkArgument(tcpPort > 5000, "TCP port must be > 5000"); |
| 112 | return store.addNode(nodeId, ip, tcpPort); |
| 113 | } |
| 114 | |
| 115 | @Override |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 116 | public void removeNode(NodeId nodeId) { |
| 117 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 118 | store.removeNode(nodeId); |
| 119 | } |
| 120 | |
| 121 | @Override |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 122 | public void addListener(ClusterEventListener listener) { |
| 123 | listenerRegistry.addListener(listener); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void removeListener(ClusterEventListener listener) { |
| 128 | listenerRegistry.removeListener(listener); |
| 129 | } |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 130 | |
| 131 | // Store delegate to re-post events emitted from the store. |
| 132 | private class InternalStoreDelegate implements ClusterStoreDelegate { |
| 133 | @Override |
| 134 | public void notify(ClusterEvent event) { |
| 135 | checkNotNull(event, "Event cannot be null"); |
| 136 | eventDispatcher.post(event); |
| 137 | } |
| 138 | } |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 139 | } |