Added UniversalVesAdapter in the Mapper
[dcaegen2/services/mapper.git] / UniversalVesAdapter / src / main / java / org / onap / universalvesadapter / adapter / UniversalEventAdapter.java
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : DCAE
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
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 * ============LICENSE_END=========================================================
19 */
20 package org.onap.universalvesadapter.adapter;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.IOException;
24 import java.nio.charset.StandardCharsets;
25 import java.util.Map;
26 import java.util.concurrent.ConcurrentHashMap;
27
28 import javax.annotation.PreDestroy;
29 import javax.annotation.Resource;
30
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;
44
45 import com.fasterxml.jackson.core.JsonProcessingException;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47
48 /**
49  * Default implementation of the Generic Adapter
50  * 
51  * @author kmalbari
52  *
53  */
54 @Component
55 public class UniversalEventAdapter implements GenericAdapter{
56         
57         private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
58         
59         @Autowired
60         private UniversalEventConfiguration configuration;
61         
62         @Resource(name="diskRepoConfigFileService")
63         private ConfigFileService configFileService;
64         
65 //      private Smooks smooks;
66         
67         private Map<String, Smooks> eventToSmooksMapping = new ConcurrentHashMap<>();
68
69         /*public String transform(String incomingJsonString) throws ConfigFileReadException {
70                 String result = "";
71                 try {
72                 //reading config file.. for now, looking at it as just one time operation
73                         if(null == smooks){
74                                 String configFileData = configFileService.readConfigFile(configuration.getConfigFile());
75                                 smooks = new Smooks(new ByteArrayInputStream(configFileData.getBytes(StandardCharsets.UTF_8)));
76                         }
77                 
78                         VesEvent vesEvent = SmooksUtils.getTransformedObjectForInput(smooks, incomingJsonString);
79                         ObjectMapper objectMapper = new ObjectMapper();
80                         result = objectMapper.writeValueAsString(vesEvent); 
81                 } catch (IOException | SAXException e) {
82                         e.printStackTrace();
83                 }
84                 
85                 return result;
86         }*/
87         
88
89
90         @Override
91         public String transform(String incomingJsonString, String eventType) throws ConfigFileReadException, 
92                                                                         ConfigFileSmooksConversionException, VesException {
93                 String result = "";
94                 try {
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);
102                         }
103                 
104                         
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());
117                 }
118                 return result;
119         }
120         
121         
122         /**
123          * Closes all open smooks' instances before bean is destroyed
124          */
125         @PreDestroy
126         public void destroy(){
127 //              if(null != smooks)
128 //                      smooks.close();
129                 
130                 for(Smooks smooks : eventToSmooksMapping.values())
131                         smooks.close();
132                 
133                 LOGGER.debug("All Smooks objects closed");
134         }       
135
136 }