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.quartz.Job;
29 import org.quartz.JobDataMap;
30 import org.quartz.JobExecutionContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import java.net.MalformedURLException;
35 import java.util.Optional;
37 public class EventJob implements Job {
39 private static final Logger LOGGER = LoggerFactory.getLogger(EventJob.class);
41 static final String TEMPLATE_NAME = "TEMPLATE_NAME";
42 static final String VES_URL = "VES_URL";
43 static final String BODY = "BODY";
44 static final String CLIENT_ADAPTER = "CLIENT_ADAPTER";
45 static final String KEYWORDS_HANDLER = "KEYWORDS_HANDLER";
46 static final String EVENT_ID = "EVENT_ID";
49 public void execute(JobExecutionContext jobExecutionContext) {
50 JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
51 String templateName = jobDataMap.getString(TEMPLATE_NAME);
52 String vesUrl = jobDataMap.getString(VES_URL);
53 JsonObject body = (JsonObject) jobDataMap.get(BODY);
54 String id = jobDataMap.getString(EVENT_ID);
55 Optional<HttpClientAdapter> httpClientAdapter = getHttpClientAdapter(jobDataMap, vesUrl);
57 if (httpClientAdapter.isPresent()) {
58 KeywordsHandler keywordsHandler = (KeywordsHandler) jobDataMap.get(KEYWORDS_HANDLER);
59 JsonElement processedBody = keywordsHandler.substituteKeywords(body, id);
60 String processedBodyString = processedBody.toString();
61 String jobKey = jobExecutionContext.getJobDetail().getKey().toString();
63 logEventDetails(templateName, vesUrl, body.toString(), jobKey);
64 httpClientAdapter.get().send(processedBodyString);
66 LOGGER.error("Could not send event as client is not available");
69 private Optional<HttpClientAdapter> getHttpClientAdapter(JobDataMap jobDataMap, String vesUrl) {
70 HttpClientAdapter adapter = null;
72 adapter = (HttpClientAdapter) jobDataMap
73 .getOrDefault(CLIENT_ADAPTER, new HttpClientAdapterImpl(vesUrl));
74 } catch (MalformedURLException e) {
75 LOGGER.error("Invalid format of vesServerUr: {}", vesUrl);
77 return Optional.ofNullable(adapter);
80 private void logEventDetails(String templateName, String vesUrl, String body, String jobKey) {
81 LOGGER.info(String.format("Job %s:Sending event to %s from template %s",
82 jobKey, vesUrl, templateName));
83 if (LOGGER.isDebugEnabled()) {
84 LOGGER.debug(String.format("Job %s: Request body %s", jobKey, body));