b22b258837e2ea241120e9e0c8f2df760b1d6457
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
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.uservice.executionproperties;
23
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.util.EnumMap;
28 import java.util.Map;
29 import java.util.Properties;
30 import lombok.Getter;
31 import org.onap.policy.apex.service.engine.event.ApexEventException;
32 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
33 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
34 import org.onap.policy.apex.service.engine.event.PeeredReference;
35 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
36 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
37 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * Dummy Apex event producer for testing event properties.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class DummyApexEventProducer implements ApexEventProducer {
48     // Get a reference to the logger
49     private static final Logger LOGGER = LoggerFactory.getLogger(DummyApexEventProducer.class);
50
51     // The parameters read from the parameter service
52     private DummyCarrierTechnologyParameters dummyProducerProperties;
53
54     // The name for this producer
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     @Override
62     public void init(final String producerName, final EventHandlerParameters producerParameters)
63             throws ApexEventException {
64         this.name = producerName;
65
66         // Check and get the Properties
67         if (!(producerParameters.getCarrierTechnologyParameters() instanceof DummyCarrierTechnologyParameters)) {
68             String message = "specified producer properties are not applicable to a dummy producer (" + this.name + ")";
69             LOGGER.warn(message);
70             throw new ApexEventException(message);
71         }
72         dummyProducerProperties =
73                 (DummyCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
74
75         new File(dummyProducerProperties.getPropertyFileName()).delete();
76     }
77
78     /**
79      * {@inheritDoc}.
80      */
81     @Override
82     public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
83         return peerReferenceMap.get(peeredMode);
84     }
85
86     /**
87      * {@inheritDoc}.
88      */
89     @Override
90     public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
91         peerReferenceMap.put(peeredMode, peeredReference);
92     }
93
94     /**
95      * {@inheritDoc}.
96      */
97     @Override
98     public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
99             final Object eventAsJsonString) {
100         // Check if this is a synchronized event, if so we have received a reply
101         final SynchronousEventCache synchronousEventCache =
102                 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
103         if (synchronousEventCache != null) {
104             synchronousEventCache.removeCachedEventToApexIfExists(executionId);
105         }
106
107         RunTestEvent testEvent = new RunTestEvent();
108         try {
109             testEvent.fromJson((String) eventAsJsonString);
110         } catch (CoderException ce) {
111             String message = "could not decode event from JSON";
112             LOGGER.warn(message, ce);
113             throw new ApexEventRuntimeException(message, ce);
114         }
115         if (!dummyProducerProperties.getTestToRun().equals(testEvent.getTestToRun())) {
116             String message = "tests in received test event and parameters do not match " + testEvent.getTestToRun()
117                     + ":" + dummyProducerProperties.getTestToRun();
118             LOGGER.warn(message);
119             throw new ApexEventRuntimeException(message);
120         }
121
122         try {
123             executionProperties.store(new FileOutputStream(new File(dummyProducerProperties.getPropertyFileName())),
124                     "");
125         } catch (IOException ioe) {
126             String message = "writing of executor properties for testing failed from file: "
127                     + dummyProducerProperties.getPropertyFileName();
128             LOGGER.warn(message, ioe);
129             throw new ApexEventRuntimeException(message, ioe);
130         }
131     }
132
133     /**
134      * {@inheritDoc}.
135      */
136     @Override
137     public void stop() {
138         // Not used
139     }
140 }