Remove jackson from feature-pooling-dmaap
[policy/drools-pdp.git] / feature-pooling-dmaap / src / main / java / org / onap / policy / drools / pooling / message / BucketAssignments.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.drools.pooling.message;
22
23 import java.util.Arrays;
24 import java.util.HashSet;
25 import java.util.Set;
26 import org.onap.policy.drools.pooling.PoolingFeatureException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Bucket assignments, which is simply an array of host names.
32  */
33 public class BucketAssignments {
34
35     private static final Logger logger = LoggerFactory.getLogger(BucketAssignments.class);
36
37     /**
38      * The number of bits in the maximum number of buckets.
39      */
40     private static final int MAX_BUCKET_BITS = 10;
41
42     /**
43      * Maximum number of buckets. Must be a power of two.
44      */
45     public static final int MAX_BUCKETS = 1 << MAX_BUCKET_BITS;
46
47     /**
48      * Used to ensure that a hash code is not negative.
49      */
50     private static final int MAX_BUCKETS_MASK = MAX_BUCKETS - 1;
51
52     /**
53      * Identifies the host serving a particular bucket.
54      */
55     private String[] hostArray = null;
56
57     /**
58      * Constructor.
59      */
60     public BucketAssignments() {
61         super();
62     }
63
64     /**
65      * Constructor.
66      * 
67      * @param hostArray maps a bucket number (i.e., array index) to a host. All values
68      *        must be non-null
69      */
70     public BucketAssignments(String[] hostArray) {
71         this.hostArray = hostArray;
72     }
73
74     public String[] getHostArray() {
75         return hostArray;
76     }
77
78     public void setHostArray(String[] hostArray) {
79         this.hostArray = hostArray;
80     }
81
82     /**
83      * Gets the leader, which is the host with the minimum UUID.
84      * 
85      * @return the assignment leader
86      */
87     public String getLeader() {
88         if (hostArray == null) {
89             return null;
90         }
91
92         String leader = null;
93
94         for (String host : hostArray) {
95             if (host != null && (leader == null || host.compareTo(leader) < 0)) {
96                 leader = host;
97             }
98         }
99
100         return leader;
101
102     }
103
104     /**
105      * Determines if a host has an assignment.
106      * 
107      * @param host host to be checked
108      * @return {@code true} if the host has an assignment, {@code false} otherwise
109      */
110     public boolean hasAssignment(String host) {
111         if (hostArray == null) {
112             return false;
113         }
114
115         for (String host2 : hostArray) {
116             if (host.equals(host2)) {
117                 return true;
118             }
119         }
120
121         return false;
122     }
123
124     /**
125      * Gets all of the hosts that have an assignment.
126      * 
127      * @return all of the hosts that have an assignment
128      */
129     public Set<String> getAllHosts() {
130         Set<String> set = new HashSet<>();
131         if (hostArray == null) {
132             return set;
133         }
134
135         for (String host : hostArray) {
136             if (host != null) {
137                 set.add(host);
138             }
139         }
140
141         return set;
142     }
143
144     /**
145      * Gets the host assigned to a given bucket.
146      * 
147      * @param hashCode hash code of the item whose assignment is desired
148      * @return the assigned host, or {@code null} if the item has no assigned host
149      */
150     public String getAssignedHost(int hashCode) {
151         if (hostArray == null || hostArray.length == 0) {
152             logger.error("no buckets have been assigned");
153             return null;
154         }
155
156         return hostArray[(hashCode & MAX_BUCKETS_MASK) % hostArray.length];
157     }
158
159     /**
160      * Gets the number of buckets.
161      * 
162      * @return the number of buckets
163      */
164     public int size() {
165         return (hostArray != null ? hostArray.length : 0);
166     }
167
168     /**
169      * Checks the validity of the assignments, verifying that all buckets have been
170      * assigned to a host.
171      * 
172      * @throws PoolingFeatureException if the assignments are invalid
173      */
174     public void checkValidity() throws PoolingFeatureException {
175         if (hostArray == null || hostArray.length == 0) {
176             throw new PoolingFeatureException("missing hosts in message bucket assignments");
177         }
178
179         if (hostArray.length > MAX_BUCKETS) {
180             throw new PoolingFeatureException("too many hosts in message bucket assignments");
181         }
182
183         for (int x = 0; x < hostArray.length; ++x) {
184             if (hostArray[x] == null) {
185                 throw new PoolingFeatureException("bucket " + x + " has no assignment");
186             }
187         }
188     }
189
190     @Override
191     public int hashCode() {
192         final int prime = 31;
193         int result = 1;
194         result = prime * result + Arrays.hashCode(hostArray);
195         return result;
196     }
197
198     @Override
199     public boolean equals(Object obj) {
200         if (this == obj) {
201             return true;
202         }
203         if (obj == null) {
204             return false;
205         }
206         if (getClass() != obj.getClass()) {
207             return false;
208         }
209         BucketAssignments other = (BucketAssignments) obj;
210         return Arrays.equals(hostArray, other.hostArray);
211     }
212 }