Gossip based eventually consistent RPC Registry.
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / registry / gossip / Messages.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.remote.rpc.registry.gossip;
9
10 import akka.actor.Address;
11 import com.google.common.base.Preconditions;
12
13 import java.io.Serializable;
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19
20 /**
21  * These messages are used by {@link org.opendaylight.controller.remote.rpc.registry.gossip.BucketStore} and
22  * {@link org.opendaylight.controller.remote.rpc.registry.gossip.Gossiper} actors.
23  */
24 public class Messages {
25
26     public static class BucketStoreMessages{
27
28         public static class GetLocalBucket implements Serializable{}
29
30         public static class ContainsBucket implements Serializable {
31             final private Bucket bucket;
32
33             public ContainsBucket(Bucket bucket){
34                 Preconditions.checkArgument(bucket != null, "bucket can not be null");
35                 this.bucket = bucket;
36             }
37
38             public Bucket getBucket(){
39                 return bucket;
40             }
41
42         }
43
44         public static class UpdateBucket extends ContainsBucket implements Serializable {
45             public UpdateBucket(Bucket bucket){
46                 super(bucket);
47             }
48         }
49
50         public static class GetLocalBucketReply extends ContainsBucket implements Serializable {
51             public GetLocalBucketReply(Bucket bucket){
52                 super(bucket);
53             }
54         }
55
56         public static class GetAllBuckets implements Serializable{}
57
58         public static class GetBucketsByMembers implements Serializable{
59             private Set<Address> members;
60
61             public GetBucketsByMembers(Set<Address> members){
62                 Preconditions.checkArgument(members != null, "members can not be null");
63                 this.members = members;
64             }
65
66             public Set<Address> getMembers() {
67                 return new HashSet<>(members);
68             }
69         }
70
71         public static class ContainsBuckets implements Serializable{
72             private Map<Address, Bucket> buckets;
73
74             public ContainsBuckets(Map<Address, Bucket> buckets){
75                 Preconditions.checkArgument(buckets != null, "buckets can not be null");
76                 this.buckets = buckets;
77             }
78
79             public Map<Address, Bucket> getBuckets() {
80                 Map<Address, Bucket> copy = new HashMap<>(buckets.size());
81
82                 for (Map.Entry<Address, Bucket> entry : buckets.entrySet()){
83                     //ignore null entries
84                     if ( (entry.getKey() == null) || (entry.getValue() == null) )
85                         continue;
86                     copy.put(entry.getKey(), entry.getValue());
87                 }
88                 return new HashMap<>(copy);
89             }
90         }
91
92         public static class GetAllBucketsReply extends ContainsBuckets implements Serializable{
93             public GetAllBucketsReply(Map<Address, Bucket> buckets) {
94                 super(buckets);
95             }
96         }
97
98         public static class GetBucketsByMembersReply extends ContainsBuckets implements Serializable{
99             public GetBucketsByMembersReply(Map<Address, Bucket> buckets) {
100                 super(buckets);
101             }
102         }
103
104         public static class GetBucketVersions implements Serializable{}
105
106         public static class ContainsBucketVersions implements Serializable{
107             Map<Address, Long> versions;
108
109             public ContainsBucketVersions(Map<Address, Long> versions) {
110                 Preconditions.checkArgument(versions != null, "versions can not be null");
111                 this.versions = versions;
112             }
113
114             public Map<Address, Long> getVersions() {
115                 return Collections.unmodifiableMap(versions);
116             }
117
118         }
119
120         public static class GetBucketVersionsReply extends ContainsBucketVersions implements Serializable{
121             public GetBucketVersionsReply(Map<Address, Long> versions) {
122                 super(versions);
123             }
124         }
125
126         public static class UpdateRemoteBuckets extends ContainsBuckets implements Serializable{
127             public UpdateRemoteBuckets(Map<Address, Bucket> buckets) {
128                 super(buckets);
129             }
130         }
131     }
132
133     public static class GossiperMessages{
134         public static class Tick implements Serializable {}
135
136         public static final class GossipTick extends Tick {}
137
138         public static final class GossipStatus extends BucketStoreMessages.ContainsBucketVersions implements Serializable{
139             private Address from;
140
141             public GossipStatus(Address from, Map<Address, Long> versions) {
142                 super(versions);
143                 this.from = from;
144             }
145
146             public Address from() {
147                 return from;
148             }
149         }
150
151         public static final class GossipEnvelope extends BucketStoreMessages.ContainsBuckets implements Serializable {
152             private final Address from;
153             private final Address to;
154
155             public GossipEnvelope(Address from, Address to, Map<Address, Bucket> buckets) {
156                 super(buckets);
157                 this.to = to;
158                 this.from = from;
159             }
160
161             public Address from() {
162                 return from;
163             }
164
165             public Address to() {
166                 return to;
167             }
168         }
169     }
170 }