2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright 2018 TechMahindra
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=========================================================
20 package org.onap.universalvesadapter.adapter;
22 import java.io.ByteArrayInputStream;
23 import java.io.IOException;
24 import java.nio.charset.StandardCharsets;
26 import java.util.concurrent.ConcurrentHashMap;
28 import javax.annotation.PreDestroy;
29 import javax.annotation.Resource;
31 import org.milyn.Smooks;
32 import org.onap.dcaegen2.ves.domain.VesEvent;
33 import org.onap.universalvesadapter.configs.UniversalEventConfiguration;
34 import org.onap.universalvesadapter.exception.ConfigFileReadException;
35 import org.onap.universalvesadapter.exception.ConfigFileSmooksConversionException;
36 import org.onap.universalvesadapter.exception.VesException;
37 import org.onap.universalvesadapter.service.ConfigFileService;
38 import org.onap.universalvesadapter.utils.SmooksUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Component;
43 import org.xml.sax.SAXException;
45 import com.fasterxml.jackson.core.JsonProcessingException;
46 import com.fasterxml.jackson.databind.ObjectMapper;
49 * Default implementation of the Generic Adapter
55 public class UniversalEventAdapter implements GenericAdapter{
57 private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
60 private UniversalEventConfiguration configuration;
62 @Resource(name="diskRepoConfigFileService")
63 private ConfigFileService configFileService;
65 // private Smooks smooks;
67 private Map<String, Smooks> eventToSmooksMapping = new ConcurrentHashMap<>();
69 /*public String transform(String incomingJsonString) throws ConfigFileReadException {
72 //reading config file.. for now, looking at it as just one time operation
74 String configFileData = configFileService.readConfigFile(configuration.getConfigFile());
75 smooks = new Smooks(new ByteArrayInputStream(configFileData.getBytes(StandardCharsets.UTF_8)));
78 VesEvent vesEvent = SmooksUtils.getTransformedObjectForInput(smooks, incomingJsonString);
79 ObjectMapper objectMapper = new ObjectMapper();
80 result = objectMapper.writeValueAsString(vesEvent);
81 } catch (IOException | SAXException e) {
91 public String transform(String incomingJsonString, String eventType) throws ConfigFileReadException,
92 ConfigFileSmooksConversionException, VesException {
95 if(null == eventToSmooksMapping.get(eventType)){
96 LOGGER.debug("No smooks mapping for this event type " + eventType + ".. reading config file");
97 String configFileData = configFileService.readConfigFile(configuration.getConfigForEvent(eventType));
98 LOGGER.debug("Read config file " + configFileData);
99 Smooks smooksTemp = new Smooks(new ByteArrayInputStream(configFileData.getBytes(StandardCharsets.UTF_8)));
100 eventToSmooksMapping.put(eventType, smooksTemp);
101 LOGGER.debug("Added smooks mapping for event type" + eventType);
105 LOGGER.debug("Read smooks mapping for event type" + eventType);
106 LOGGER.debug("Transforming incoming json now");
107 VesEvent vesEvent = SmooksUtils.getTransformedObjectForInput(eventToSmooksMapping.get(eventType), incomingJsonString);
108 LOGGER.debug("Incoming json transformed to VES format successfully");
109 ObjectMapper objectMapper = new ObjectMapper();
110 result = objectMapper.writeValueAsString(vesEvent);
111 LOGGER.debug("Serialized VES json");
112 } catch (JsonProcessingException exception) {
113 throw new VesException("Unable to convert pojo to VES format" + "\n Reason :" + exception.getMessage());
114 } catch (SAXException | IOException exception) {
115 throw new ConfigFileSmooksConversionException("Unable to convert config file into smooks for event type " + eventType
116 + "\n Reason :" + exception.getMessage());
123 * Closes all open smooks' instances before bean is destroyed
126 public void destroy(){
127 // if(null != smooks)
130 for(Smooks smooks : eventToSmooksMapping.values())
133 LOGGER.debug("All Smooks objects closed");