99a94e70e9114bc2f6e320240bf75d150034c84a
[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
75             public GetAllBucketsReply(Map<Address, Bucket<T>> buckets) {
76                 super(buckets);
77             }
78         }
79
80         public static class GetBucketsByMembersReply<T extends Copier<T>> extends ContainsBuckets<T>
81                 implements Serializable {
82             private static final long serialVersionUID = 1L;
83
84             public GetBucketsByMembersReply(Map<Address, Bucket<T>> buckets) {
85                 super(buckets);
86             }
87         }
88
89         public static class GetBucketVersions implements Serializable {
90             private static final long serialVersionUID = 1L;
91         }
92
93         public static class ContainsBucketVersions implements Serializable {
94             private static final long serialVersionUID = -8172148925383801613L;
95
96             Map<Address, Long> versions;
97
98             public ContainsBucketVersions(Map<Address, Long> versions) {
99                 Preconditions.checkArgument(versions != null, "versions can not be null or empty");
100
101                 this.versions = versions;
102             }
103
104             public Map<Address, Long> getVersions() {
105                 return Collections.unmodifiableMap(versions);
106             }
107
108         }
109
110         public static class GetBucketVersionsReply extends ContainsBucketVersions implements Serializable {
111             private static final long serialVersionUID = 1L;
112
113             public GetBucketVersionsReply(Map<Address, Long> versions) {
114                 super(versions);
115             }
116         }
117
118         public static class UpdateRemoteBuckets<T extends Copier<T>> extends ContainsBuckets<T>
119                 implements Serializable {
120             private static final long serialVersionUID = 1L;
121
122             public UpdateRemoteBuckets(Map<Address, Bucket<T>> buckets) {
123                 super(buckets);
124             }
125         }
126     }
127
128     public static class GossiperMessages {
129         public static class Tick implements Serializable {
130             private static final long serialVersionUID = -4770935099506366773L;
131         }
132
133         public static final class GossipTick extends Tick {
134             private static final long serialVersionUID = 5803354404380026143L;
135         }
136
137         public static final class GossipStatus extends ContainsBucketVersions implements Serializable {
138             private static final long serialVersionUID = -593037395143883265L;
139
140             private final Address from;
141
142             public GossipStatus(Address from, Map<Address, Long> versions) {
143                 super(versions);
144                 this.from = from;
145             }
146
147             public Address from() {
148                 return from;
149             }
150         }
151
152         public static final class GossipEnvelope<T extends Copier<T>> extends ContainsBuckets<T>
153                 implements Serializable {
154             private static final long serialVersionUID = 8346634072582438818L;
155
156             private final Address from;
157             private final Address to;
158
159             public GossipEnvelope(Address from, Address to, Map<Address, Bucket<T>> buckets) {
160                 super(buckets);
161                 Preconditions.checkArgument(to != null, "Recipient of message must not be null");
162                 this.to = to;
163                 this.from = from;
164             }
165
166             public Address from() {
167                 return from;
168             }
169
170             public Address to() {
171                 return to;
172             }
173         }
174     }
175 }