226756b1c32b5722f788d203330b3578c208e1df
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
5  *  Modifications Copyright (C) 2021 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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.testsuites.integration.uservice.executionproperties;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.util.EnumMap;
29 import java.util.Map;
30 import java.util.Properties;
31 import lombok.Getter;
32 import org.onap.policy.apex.service.engine.event.ApexEventConsumer;
33 import org.onap.policy.apex.service.engine.event.ApexEventException;
34 import org.onap.policy.apex.service.engine.event.ApexEventReceiver;
35 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
36 import org.onap.policy.apex.service.engine.event.PeeredReference;
37 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
38 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * Dummy Apex event consumer for testing event properties.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class DummyApexEventConsumer implements ApexEventConsumer {
48     // Get a reference to the logger
49     private static final Logger LOGGER = LoggerFactory.getLogger(DummyApexEventConsumer.class);
50
51     // The event receiver that will receive events from this consumer
52     private ApexEventReceiver eventReceiver;
53
54     // The name for this consumer
55     @Getter
56     private String name = null;
57
58     // The peer references for this event handler
59     private Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class);
60
61     private DummyCarrierTechnologyParameters dummyConsumerProperties = null;
62
63     @Override
64     public void init(final String consumerName, final EventHandlerParameters consumerParameters,
65                     final ApexEventReceiver incomingEventReceiver) throws ApexEventException {
66         this.eventReceiver = incomingEventReceiver;
67         this.name = consumerName;
68
69         // Check and get the properties
70         if (!(consumerParameters.getCarrierTechnologyParameters() instanceof DummyCarrierTechnologyParameters)) {
71             String message = "specified consumer properties of type \""
72                             + consumerParameters.getCarrierTechnologyParameters().getClass().getName()
73                             + "\" are not applicable to a dummy consumer";
74             LOGGER.warn(message);
75             throw new ApexEventException(message);
76         }
77
78         dummyConsumerProperties = (DummyCarrierTechnologyParameters) consumerParameters
79                         .getCarrierTechnologyParameters();
80     }
81
82     @Override
83     public void start() {
84         new Thread(new RunTestEventSender()).start();
85     }
86
87     /**
88      * {@inheritDoc}.
89      */
90     @Override
91     public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
92         return peerReferenceMap.get(peeredMode);
93     }
94
95     /**
96      * {@inheritDoc}.
97      */
98     @Override
99     public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
100         peerReferenceMap.put(peeredMode, peeredReference);
101     }
102
103     @Override
104     public void stop() {
105     }
106
107     private class RunTestEventSender implements Runnable {
108         @Override
109         public void run() {
110             Properties executionProperties = new Properties();
111             try {
112                 executionProperties.load(new FileInputStream(new File(dummyConsumerProperties.getPropertyFileName())));
113             } catch (IOException e1) {
114                 String message = "reading of executor properties for testing failed from file: "
115                                 + dummyConsumerProperties.getPropertyFileName();
116                 LOGGER.warn(message);
117                 throw new ApexEventRuntimeException(message);
118             }
119
120             RunTestEvent event = new RunTestEvent();
121             event.setTestToRun(dummyConsumerProperties.getTestToRun());
122             try {
123                 eventReceiver.receiveEvent(1, executionProperties, event.toJson());
124             } catch (Exception e) {
125                 String message = "event processing for executor properties testing failed: " + e.getMessage();
126                 LOGGER.warn(message, e);
127                 throw new ApexEventRuntimeException(message, e);
128             }
129         }
130     }
131 }