4034d7f853932f97393ddc57138c4e0bacf24535
[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 import java.io.Serializable;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.Map;
17 import java.util.Set;
18 import org.opendaylight.controller.remote.rpc.registry.gossip.Messages.BucketStoreMessages.ContainsBucketVersions;
19 import org.opendaylight.controller.remote.rpc.registry.gossip.Messages.BucketStoreMessages.ContainsBuckets;
20
21
22 /**
23  * These messages are used by {@link org.opendaylight.controller.remote.rpc.registry.gossip.BucketStore} and
24  * {@link org.opendaylight.controller.remote.rpc.registry.gossip.Gossiper} actors.
25  */
26 public class Messages {
27
28     public static class BucketStoreMessages{
29
30         public static class GetAllBuckets implements Serializable {
31             private static final long serialVersionUID = 1L;
32         }
33
34         public static class GetBucketsByMembers implements Serializable{
35             private static final long serialVersionUID = 1L;
36             private final Set<Address> members;
37
38             public GetBucketsByMembers(Set<Address> members){
39                 Preconditions.checkArgument(members != null, "members can not be null");
40                 this.members = members;
41             }
42
43             public Set<Address> getMembers() {
44                 return new HashSet<>(members);
45             }
46         }
47
48         public static class ContainsBuckets<T extends Copier<T>> implements Serializable{
49             private static final long serialVersionUID = -4940160367495308286L;
50
51             private final Map<Address, Bucket<T>> buckets;
52
53             public ContainsBuckets(Map<Address, Bucket<T>> buckets){
54                 Preconditions.checkArgument(buckets != null, "buckets can not be null");
55                 this.buckets = buckets;
56             }
57
58             public Map<Address, Bucket<T>> getBuckets() {
59                 Map<Address, Bucket<T>> copy = new HashMap<>(buckets.size());
60
61                 for (Map.Entry<Address, Bucket<T>> entry : buckets.entrySet()){
62                     //ignore null entries
63                     if ( (entry.getKey() == null) || (entry.getValue() == null) ) {
64                         continue;
65                     }
66                     copy.put(entry.getKey(), entry.getValue());
67                 }
68                 return copy;
69             }
70         }
71
72         public static class GetAllBucketsReply<T extends Copier<T>> extends ContainsBuckets<T> implements Serializable{
73             private static final long serialVersionUID = 1L;
74             public GetAllBucketsReply(Map<Address, Bucket<T>> buckets) {
75                 super(buckets);
76             }
77         }
78
79         public static class GetBucketsByMembersReply<T extends Copier<T>> extends ContainsBuckets<T> implements Serializable{
80             private static final long serialVersionUID = 1L;
81             public GetBucketsByMembersReply(Map<Address, Bucket<T>> buckets) {
82                 super(buckets);
83             }
84         }
85
86         public static class GetBucketVersions implements Serializable {
87             private static final long serialVersionUID = 1L;
88         }
89
90         public static class ContainsBucketVersions implements Serializable{
91             private static final long serialVersionUID = -8172148925383801613L;
92
93             Map<Address, Long> versions;
94
95             public ContainsBucketVersions(Map<Address, Long> versions) {
96                 Preconditions.checkArgument(versions != null, "versions can not be null or empty");
97
98                 this.versions = versions;
99             }
100
101             public Map<Address, Long> getVersions() {
102                 return Collections.unmodifiableMap(versions);
103             }
104
105         }
106
107         public static class GetBucketVersionsReply extends ContainsBucketVersions implements Serializable{
108             private static final long serialVersionUID = 1L;
109             public GetBucketVersionsReply(Map<Address, Long> versions) {
110                 super(versions);
111             }
112         }
113
114         public static class UpdateRemoteBuckets<T extends Copier<T>> extends ContainsBuckets<T> implements Serializable{
115             private static final long serialVersionUID = 1L;
116             public UpdateRemoteBuckets(Map<Address, Bucket<T>> buckets) {
117                 super(buckets);
118             }
119         }
120     }
121
122     public static class GossiperMessages{
123         public static class Tick implements Serializable {
124             private static final long serialVersionUID = -4770935099506366773L;
125         }
126
127         public static final class GossipTick extends Tick {
128             private static final long serialVersionUID = 5803354404380026143L;
129         }
130
131         public static final class GossipStatus extends ContainsBucketVersions implements Serializable{
132             private static final long serialVersionUID = -593037395143883265L;
133
134             private final Address from;
135
136             public GossipStatus(Address from, Map<Address, Long> versions) {
137                 super(versions);
138                 this.from = from;
139             }
140
141             public Address from() {
142                 return from;
143             }
144         }
145
146         public static final class GossipEnvelope<T extends Copier<T>> extends ContainsBuckets<T> implements Serializable {
147             private static final long serialVersionUID = 8346634072582438818L;
148
149             private final Address from;
150             private final Address to;
151
152             public GossipEnvelope(Address from, Address to, Map<Address, Bucket<T>> buckets) {
153                 super(buckets);
154                 Preconditions.checkArgument(to != null, "Recipient of message must not be null");
155                 this.to = to;
156                 this.from = from;
157             }
158
159             public Address from() {
160                 return from;
161             }
162
163             public Address to() {
164                 return to;
165             }
166         }
167     }
168 }