2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2018 Nokia. 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
21 package org.onap.pnfsimulator.simulator.scheduler;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
25 import org.onap.pnfsimulator.simulator.KeywordsHandler;
26 import org.onap.pnfsimulator.simulator.client.HttpClientAdapter;
27 import org.onap.pnfsimulator.simulator.client.HttpClientAdapterImpl;
28 import org.onap.pnfsimulator.simulator.client.utils.ssl.SSLAuthenticationHelper;
29 import org.quartz.Job;
30 import org.quartz.JobDataMap;
31 import org.quartz.JobExecutionContext;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import java.io.IOException;
36 import java.net.MalformedURLException;
37 import java.security.GeneralSecurityException;
38 import java.util.Optional;
40 public class EventJob implements Job {
42 private static final Logger LOGGER = LoggerFactory.getLogger(EventJob.class);
44 static final String TEMPLATE_NAME = "TEMPLATE_NAME";
45 static final String VES_URL = "VES_URL";
46 static final String BODY = "BODY";
47 static final String CLIENT_ADAPTER = "CLIENT_ADAPTER";
48 static final String KEYWORDS_HANDLER = "KEYWORDS_HANDLER";
49 static final String EVENT_ID = "EVENT_ID";
52 public void execute(JobExecutionContext jobExecutionContext) {
53 JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
54 String templateName = jobDataMap.getString(TEMPLATE_NAME);
55 String vesUrl = jobDataMap.getString(VES_URL);
56 JsonObject body = (JsonObject) jobDataMap.get(BODY);
57 String id = jobDataMap.getString(EVENT_ID);
58 Optional<HttpClientAdapter> httpClientAdapter = getHttpClientAdapter(jobDataMap, vesUrl);
60 if (httpClientAdapter.isPresent()) {
61 KeywordsHandler keywordsHandler = (KeywordsHandler) jobDataMap.get(KEYWORDS_HANDLER);
62 JsonElement processedBody = keywordsHandler.substituteKeywords(body, id);
63 String processedBodyString = processedBody.toString();
64 String jobKey = jobExecutionContext.getJobDetail().getKey().toString();
66 logEventDetails(templateName, vesUrl, body.toString(), jobKey);
67 httpClientAdapter.get().send(processedBodyString);
69 LOGGER.error("Could not send event as client is not available");
72 private Optional<HttpClientAdapter> getHttpClientAdapter(JobDataMap jobDataMap, String vesUrl) {
73 HttpClientAdapter adapter = null;
75 adapter = (HttpClientAdapter) (jobDataMap.containsKey(CLIENT_ADAPTER) ? jobDataMap.get(CLIENT_ADAPTER) :
76 new HttpClientAdapterImpl(vesUrl, new SSLAuthenticationHelper()));
77 } catch (MalformedURLException e) {
78 LOGGER.error("Invalid format of vesServerUr: {}", vesUrl);
79 } catch (IOException | GeneralSecurityException e){
80 LOGGER.error("Invalid configuration of client certificate");
82 return Optional.ofNullable(adapter);
85 private void logEventDetails(String templateName, String vesUrl, String body, String jobKey) {
86 LOGGER.info(String.format("Job %s:Sending event to %s from template %s",
87 jobKey, vesUrl, templateName));
88 if (LOGGER.isDebugEnabled()) {
89 LOGGER.debug(String.format("Job %s: Request body %s", jobKey, body));