aboutsummaryrefslogtreecommitdiffstats
path: root/feature-server-pool/src/test/java/org/onap/policy/drools/serverpool/BucketWrapperImpl.java
blob: ca89d99a50ede6782991e28bcd7ca300bcaca4ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * ============LICENSE_START=======================================================
 * feature-server-pool
 * ================================================================================
 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.drools.serverpool;

import java.io.PrintStream;
import java.util.IdentityHashMap;

import org.onap.policy.drools.serverpooltest.BucketWrapper;
import org.onap.policy.drools.serverpooltest.ServerWrapper;

/**
 * This class implements the 'BucketWrapper' interface. There is one
 * 'BucketWrapperImpl' class for each simulated host.
 */
public class BucketWrapperImpl implements BucketWrapper {
    // this maps a 'Bucket' instance on this host to an associated wrapper
    private static IdentityHashMap<Bucket, BucketWrapperImpl> bucketToWrapper =
        new IdentityHashMap<>();

    // this is the 'Bucket' instance associated with the wrapper
    private Bucket bucket;

    /**
     * This method maps a 'Bucket' instance into a 'BucketWrapperImpl'
     * instance. The goal is to have only a single 'BucketWrapperImpl' instance
     * for each 'Bucket' instance, so that testing for identity will work
     * as expected.
     *
     * @param bucket the 'Bucket' instance
     * @return the associated 'BucketWrapperImpl' instance
     */
    static synchronized BucketWrapperImpl getWrapper(Bucket bucket) {
        if (bucket == null) {
            return null;
        }
        BucketWrapperImpl rval = bucketToWrapper.get(bucket);
        if (rval == null) {
            // a matching entry does not yet exist -- create one
            rval = new BucketWrapperImpl(bucket);
            bucketToWrapper.put(bucket, rval);
        }
        return rval;
    }

    /**
     * Constructor - initialize the 'bucket' field.
     */
    BucketWrapperImpl(Bucket bucket) {
        this.bucket = bucket;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getBucketNumber() {
        return bucket.getIndex();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ServerWrapper getOwner() {
        return ServerWrapperImpl.getWrapper(bucket.getOwner());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ServerWrapper getPrimaryBackup() {
        return ServerWrapperImpl.getWrapper(bucket.getPrimaryBackup());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ServerWrapper getSecondaryBackup() {
        return ServerWrapperImpl.getWrapper(bucket.getSecondaryBackup());
    }

    /* ============================================================ */

    /**
     * This class implements the 'BucketWrapper.Static' interface. There is
     * one 'BucketWrapperImpl.Static' class, and one instance for each
     * simulated host
     */
    public static class Static implements BucketWrapper.Static {
        /**
         * {@inheritDoc}
         */
        @Override
        public int getBucketCount() {
            return Bucket.BUCKETCOUNT;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public int bucketNumber(String value) {
            return Bucket.bucketNumber(value);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ServerWrapper bucketToServer(int bucketNumber) {
            return ServerWrapperImpl.getWrapper(Bucket.bucketToServer(bucketNumber));
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public BucketWrapper getBucket(int bucketNumber) {
            return getWrapper(Bucket.getBucket(bucketNumber));
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isKeyOnThisServer(String key) {
            return Bucket.isKeyOnThisServer(key);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void moveBucket(PrintStream out, int bucketNumber, String newHostUuid) {
            ClassLoader save = Thread.currentThread().getContextClassLoader();
            try {
                Thread.currentThread().setContextClassLoader(
                    BucketWrapperImpl.class.getClassLoader());
                Bucket.moveBucket(out, bucketNumber, newHostUuid);
            } finally {
                Thread.currentThread().setContextClassLoader(save);
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void dumpAdjuncts(PrintStream out) {
            Bucket.dumpAdjuncts(out);
        }
    }
}