c1c656490529487a484ab783612cf1c96261f0b3
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.common.model.java;
23
24 import java.security.NoSuchAlgorithmException;
25 import java.security.SecureRandom;
26
27 import org.onap.policy.apex.core.engine.executor.context.TaskExecutionContext;
28
29 /**
30  * The Class DefaultTask_Logic is default task logic in Java.
31  */
32 public class DefaultTaskLogic {
33     private static final int BOUND_FOR_RANDOM_INT = 4;
34
35     /**
36      * Gets the event.
37      *
38      * @param executor the executor
39      * @return true , if the event exists
40      */
41     public boolean getEvent(final TaskExecutionContext executor) {
42         String idString = executor.subject.getId();
43         executor.logger.debug(idString);
44
45         String albumNameString = executor.getContextAlbum("GlobalContextAlbum").getName();
46         executor.logger.debug(albumNameString);
47
48         String inFieldsString = executor.inFields.toString();
49         executor.logger.debug(inFieldsString);
50
51         try {
52             SecureRandom rand = SecureRandom.getInstanceStrong();
53             if (executor.inFields.containsKey("TestDecideCaseSelected")) {
54                 executor.outFields.put("TestActCaseSelected", (byte) rand.nextInt(BOUND_FOR_RANDOM_INT));
55                 executor.outFields.put("TestActStateTime", System.nanoTime());
56             } else if (executor.inFields.containsKey("TestEstablishCaseSelected")) {
57                 executor.outFields.put("TestDecideCaseSelected", (byte) rand.nextInt(BOUND_FOR_RANDOM_INT));
58                 executor.outFields.put("TestDecideStateTime", System.nanoTime());
59             } else if (executor.inFields.containsKey("TestMatchCaseSelected")) {
60                 executor.outFields.put("TestEstablishCaseSelected", (byte) rand.nextInt(BOUND_FOR_RANDOM_INT));
61                 executor.outFields.put("TestEstablishStateTime", System.nanoTime());
62             } else {
63                 executor.outFields.put("TestMatchCaseSelected", (byte) rand.nextInt(BOUND_FOR_RANDOM_INT));
64                 executor.outFields.put("TestMatchStateTime", System.nanoTime());
65             }
66         } catch (NoSuchAlgorithmException e) {
67             executor.logger.error("Exception during Random number generation ", e);
68             return false;
69         }
70        return true;
71     }
72 }