aboutsummaryrefslogtreecommitdiffstats
path: root/feature-server-pool/src/test/java/org/onap/policy/drools/serverpool/AdapterImpl.java
blob: 044067a3ff0b222fd195f16f546d019d6c4000a6 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
 * ============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 static org.awaitility.Awaitility.await;

import java.io.PrintStream;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.kie.api.runtime.KieSession;
import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
import org.onap.policy.common.endpoints.event.comm.TopicListener;
import org.onap.policy.drools.core.PolicyContainer;
import org.onap.policy.drools.core.PolicySession;
import org.onap.policy.drools.core.PolicySessionFeatureApiConstants;
import org.onap.policy.drools.serverpooltest.Adapter;
import org.onap.policy.drools.serverpooltest.BucketWrapper;
import org.onap.policy.drools.serverpooltest.ServerWrapper;
import org.onap.policy.drools.serverpooltest.TargetLockWrapper;
import org.onap.policy.drools.system.PolicyController;
import org.onap.policy.drools.system.PolicyEngineConstants;
import org.onap.policy.drools.util.KieUtils;
import org.onap.policy.drools.utils.PropertyUtil;
import org.powermock.reflect.Whitebox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class implements the 'Adapter' interface. There is one 'AdapterImpl'
 * class for each simulated host, and one instance of each 'AdapterImpl' class.
 */
public class AdapterImpl extends Adapter {
    private static Logger logger = LoggerFactory.getLogger(AdapterImpl.class);
    /*
     * Each 'AdapterImpl' instance has it's own class object, making it a
     * singleton. There is only a single 'Adapter' class object, and all
     * 'AdapterImpl' classes are derived from it.
     */
    private static AdapterImpl adapter = null;

    // this is the adapter index
    private int index;

    // this will refer to the Drools session 'PolicyController' instance
    private PolicyController policyController = null;

    // this will refer to the Drools session 'PolicySession' instance
    private PolicySession policySession = null;

    // used by Drools session to signal back to Junit tests
    private LinkedBlockingQueue<String> inotificationQueue =
        new LinkedBlockingQueue<>();

    // provides indirect references to a select set of static 'Server' methods
    private static ServerWrapper.Static serverStatic =
        new ServerWrapperImpl.Static();

    // provides indirect references to a select set of static 'Bucket' methods
    private static BucketWrapper.Static bucketStatic =
        new BucketWrapperImpl.Static();

    /**
     * {@inheritDoc}
     */
    @Override
    public void init(int index) throws Exception {
        adapter = this;
        this.index = index;

        PolicyEngineConstants.getManager().configure(new Properties());
        PolicyEngineConstants.getManager().start();
        /*
         * Note that this method does basically what
         * 'FeatureServerPool.afterStart(PolicyEngine)' does, but allows us to
         * specify different properties for each of the 6 simulated hosts
         */
        logger.info("{}: Running: AdapterImpl.init({}), class hash code = {}",
                    this, index, AdapterImpl.class.hashCode());
        final String propertyFile = "src/test/resources/feature-server-pool-test.properties";
        Properties prop = PropertyUtil.getProperties(propertyFile);
        if (System.getProperty("os.name").toLowerCase().indexOf("mac") < 0) {
            // Window, Unix
            String[] ipComponent = prop.getProperty("server.pool.server.ipAddress").split("[.]");
            String serverIP = ipComponent[0] + "." + ipComponent[1] + "." + ipComponent[2] + "."
                + (Integer.parseInt(ipComponent[3]) + index);
            prop.setProperty("server.pool.server.ipAddress", serverIP);
        } else {
            // Mac, use localhost and different ports
            String port = Integer.toString(Integer.parseInt(
                prop.getProperty("server.pool.server.port")) + index);
            prop.setProperty("server.pool.server.port", port);
        }
        logger.info("server={}, serverIP={}, port={}", index,
            prop.getProperty("server.pool.server.ipAddress"),
            prop.getProperty("server.pool.server.port"));

        TargetLock.startup();
        Server.startup(prop);

        // use reflection to set private static field
        // 'FeatureServerPool.droolsTimeoutMillis'
        Whitebox.setInternalState(FeatureServerPool.class, "droolsTimeoutMillis",
            ServerPoolProperties.DEFAULT_BUCKET_DROOLS_TIMEOUT);

        // use reflection to set private static field
        // 'FeatureServerPool.timeToLiveSecond'
        Whitebox.setInternalState(FeatureServerPool.class, "timeToLiveSecond",
            String.valueOf(ServerPoolProperties.DEFAULT_BUCKET_TIME_TO_LIVE));

        // use reflection to call private static method
        // 'FeatureServerPool.buildKeywordTable()'
        Whitebox.invokeMethod(FeatureServerPool.class, "buildKeywordTable");

        Bucket.Backup.register(new FeatureServerPool.DroolsSessionBackup());
        Bucket.Backup.register(new TargetLock.LockBackup());

        // dump out feature lists
        logger.info("{}: ServerPoolApi features list: {}",
                    this, ServerPoolApi.impl.getList());
        logger.info("{}: PolicySessionFeatureApi features list: {}",
                    this, PolicySessionFeatureApiConstants.getImpl().getList());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void shutdown() {
        policyController.stop();
        Server.shutdown();

        PolicyEngineConstants.getManager().stop();
        PolicyEngineConstants.getManager().getExecutorService().shutdown();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public LinkedBlockingQueue<String> notificationQueue() {
        return inotificationQueue;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean waitForInit(long endTime) throws InterruptedException {
        try {
            // wait until a leader is elected
            await().atMost(endTime - System.currentTimeMillis(),
                    TimeUnit.MILLISECONDS).until(() -> Leader.getLeader() != null);

            // wait for each bucket to have an owner
            for (int i = 0; i < Bucket.BUCKETCOUNT; i += 1) {
                Bucket bucket = Bucket.getBucket(i);
                while (bucket.getOwner() == null) {
                    await().atMost(Math.min(endTime - System.currentTimeMillis(), 100L), TimeUnit.MILLISECONDS);
                }
            }
        } catch (IllegalArgumentException e) {
            // 'Thread.sleep()' was passed a negative time-out value --
            // time is up
            logger.debug("AdapterImpl waitForInit error", e);
            return false;
        }
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ServerWrapper.Static getServerStatic() {
        return serverStatic;
    }

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

    /**
     * {@inheritDoc}
     */
    @Override
    public BucketWrapper.Static getBucketStatic() {
        return bucketStatic;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public TargetLockWrapper newTargetLock(
        String key, String ownerKey, TargetLockWrapper.Owner owner, boolean waitForLock) {

        return TargetLockWrapperImpl.newTargetLock(key, ownerKey, owner, waitForLock);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public TargetLockWrapper newTargetLock(String key, String ownerKey, TargetLockWrapper.Owner owner) {
        return TargetLockWrapperImpl.newTargetLock(key, ownerKey, owner);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void dumpLocks(PrintStream out, boolean detail) {
        try {
            TargetLock.DumpLocks.dumpLocks(out, detail);
        } catch (Exception e) {
            logger.error("{}: Exception in 'dumpLocks'", this, e);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String createController() {
        Properties properties;
        /*
         * set the thread class loader to be the same as the one associated
         * with the 'AdapterImpl' instance, so it will be inherited by any
         * new threads created (the Drools session thread, in particular)
         */
        ClassLoader saveClassLoader =
            Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(AdapterImpl.class.getClassLoader());

        try {
            // build and install Drools artifact
            KieUtils.installArtifact(
                Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/META-INF/kmodule.xml").toFile(),
                Paths.get("src/test/resources/drools-artifact-1.1/pom.xml").toFile(),
                "src/main/resources/rules/org/onap/policy/drools/core/test/rules.drl",
                Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/rules.drl").toFile());

            // load properties from file
            properties = PropertyUtil.getProperties("src/test/resources/TestController-controller.properties");
        } catch (Exception e) {
            e.printStackTrace();
            Thread.currentThread().setContextClassLoader(saveClassLoader);
            return e.toString();
        }

        StringBuilder sb = new StringBuilder();
        try {
            // create and start 'PolicyController'
            policyController = PolicyEngineConstants.getManager()
                .createPolicyController("TestController", properties);
            policyController.start();

            // dump out container information (used for debugging tests)
            sb.append("PolicyContainer count: ")
            .append(PolicyContainer.getPolicyContainers().size()).append('\n');
            for (PolicyContainer policyContainer :
                    PolicyContainer.getPolicyContainers()) {
                sb.append("    name = ")
                    .append(policyContainer.getName())
                    .append('\n')
                    .append("    session count = ")
                    .append(policyContainer.getPolicySessions().size())
                    .append('\n');
                for (PolicySession pc : policyContainer.getPolicySessions()) {
                    policySession = pc;
                }
            }
        } finally {
            Thread.currentThread().setContextClassLoader(saveClassLoader);
        }
        return sb.toString();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void sendEvent(String key) {
        /*
         * Note: the dumping out of package information was useful in tracking
         * down strange Drools behavior that was eventually tied to the
         * Drools class loader.
         */
        logger.info("{}: Calling 'sendEvent': packages = {}", this,
                    policySession.getKieSession().getKieBase().getKiePackages());
        ((TopicListener) policyController).onTopicEvent(
            CommInfrastructure.UEB, "JUNIT-TEST-TOPIC",
            "{\"key\":\"" + key + "\"}");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public KieSession getKieSession() {
        return policySession == null ? null : policySession.getKieSession();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void insertDrools(Object object) {
        if (policySession != null) {
            /*
             * this will eventually be changed to use the
             * 'PolicySession.insertObject(...)' method
             */
            new FeatureServerPool().insertDrools(policySession, object);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isForeign(Object... objects) {
        boolean rval = false;
        ClassLoader myClassLoader = AdapterImpl.class.getClassLoader();
        for (Object o : objects) {
            Class clazz = o.getClass();
            ClassLoader objClassLoader = clazz.getClassLoader();

            try {
                if (myClassLoader != objClassLoader
                        && clazz != myClassLoader.loadClass(clazz.getName())) {
                    rval = true;
                    logger.info("{}: FOREIGN OBJECT ({}) - {}",
                                this, getAdapter(objClassLoader), o);
                }
            } catch (ClassNotFoundException e) {
                rval = true;
                logger.error("{}: FOREIGN OBJECT -- CLASS NOT FOUND ({}) - {}",
                             this, getAdapter(objClassLoader), o);
            }
        }
        return rval;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String findKey(String prefix, int startingIndex, ServerWrapper host) {
        String rval = null;

        // try up to 10000 numeric values to locate one on a particular host
        for (int i = 0; i < 10000; i += 1) {
            // generate key, and see if it is on the desired server
            String testString = prefix + (startingIndex + i);
            if (ServerWrapperImpl.getWrapper(
                Bucket.bucketToServer(Bucket.bucketNumber(testString))) == host) {
                // we have one that works
                rval = testString;
                break;
            }
        }
        return rval;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String findKey(String prefix, int startingIndex) {
        return findKey(prefix, startingIndex, serverStatic.getThisServer());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String findKey(String prefix) {
        return findKey(prefix, 1, serverStatic.getThisServer());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "AdapterImpl[" + index + "]";
    }

    /**
     * Return an Adapter.
     *
     * @return the 'Adapter' instance associated with the ClassLoader associated
     *     with the current thread
     */
    public static Adapter getAdapter() {
        /*
         * Note that 'return(adapter)' doesn't work as expected when called from
         * within a 'Drools' session, because of the strange way that the Drools
         * 'ClassLoader' works -- it bypasses 'AdapterClassLoader' when doing
         * class lookups, even though it is the immediate parent of the Drools
         * session class loader.
         */
        return getAdapter(Thread.currentThread().getContextClassLoader());
    }

    /**
     * Return an Adapter.
     *
     * @param classLoader a ClassLoader instance
     * @return the 'Adapter' instance associated with the specified ClassLoader
     */
    public static Adapter getAdapter(ClassLoader classLoader) {
        try {
            // locate the 'AdapterImpl' class associated with a particular
            // 'ClassLoader' (which may be different from the current one)
            Class<?> thisAdapterClass =
                classLoader.loadClass("org.onap.policy.drools.serverpool.AdapterImpl");

            // return the 'adapter' field value
            return Whitebox.getInternalState(thisAdapterClass, "adapter");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}