Support 7.2.1 VES in TCAGEN2
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-tca-core / src / main / java / org / onap / dcae / analytics / tca / core / util / TcaUtils.java
1 /*
2  * ==========LICENSE_START=========================================================
3  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4  * Copyright (c) 2022 Wipro Limited 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  * ============LICENSE_END=========================================================
18  *
19  */
20
21 package org.onap.dcae.analytics.tca.core.util;
22
23 import static org.onap.dcae.analytics.tca.model.util.json.TcaModelJsonConversion.TCA_OBJECT_MAPPER;
24
25 import com.fasterxml.jackson.core.JsonProcessingException;
26
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Optional;
31 import java.util.stream.Collectors;
32
33 import org.onap.dcae.analytics.model.TcaModelConstants;
34 import org.onap.dcae.analytics.model.cef.EventListener;
35 import org.onap.dcae.analytics.tca.core.exception.AnalyticsParsingException;
36 import org.onap.dcae.analytics.tca.model.policy.TcaPolicy;
37 import org.onap.dcae.utils.eelf.logger.model.info.ServiceLogInfoImpl;
38
39 /**
40  * @author Rajiv Singla
41  */
42 public abstract class TcaUtils {
43
44     /**
45      * TCA Service Log Info for ECOMP Logging
46      */
47     public static final ServiceLogInfoImpl TCA_SERVICE_LOG_INFO =
48             new ServiceLogInfoImpl(TcaModelConstants.TCA_SERVICE_NAME, System.getProperty("user.name"), "");
49
50     /**
51      * Creates a deep copy of Tca Policy
52      *
53      * @param tcaPolicy source tca policy object
54      *
55      * @return deep copy of provided tca policy
56      */
57     public static List<TcaPolicy> getTcaPolicyDeepCopy(final List<TcaPolicy> tcaPolicy) {
58         if (tcaPolicy != null) {
59           List<TcaPolicy> tcaList =  new ArrayList<TcaPolicy>();    
60           for( TcaPolicy tcaPol : tcaPolicy) {    
61              try {
62                 tcaList.add(TCA_OBJECT_MAPPER.treeToValue(TCA_OBJECT_MAPPER.valueToTree(tcaPol), TcaPolicy.class));
63             } catch (JsonProcessingException e) {
64                 throw new AnalyticsParsingException("Unable to create deep copy of TCA Policy: " + tcaPol, e);
65             }
66           }
67           return tcaList;    
68         } 
69         else {
70             final String errorMessage = "Invalid application state. TCA Policy must not be null";
71             throw new AnalyticsParsingException(errorMessage, new IllegalStateException(errorMessage));
72         }
73     }
74
75
76     /**
77      * Converts given event Listeners to list of CEF Message String
78      *
79      * @param eventListeners event listeners object
80      *
81      * @return cef messages as string
82      */
83     public static List<String> getCefMessagesFromEventListeners(final List<EventListener> eventListeners) {
84         if (!Optional.ofNullable(eventListeners).isPresent()) {
85             return Collections.emptyList();
86         }
87         return eventListeners.stream().map(eventListener -> {
88             try {
89                 return TCA_OBJECT_MAPPER.writeValueAsString(eventListener);
90             } catch (JsonProcessingException e) {
91                 throw new AnalyticsParsingException("Unable to parse EventLister to String: " + eventListener, e);
92             }
93         }).collect(Collectors.toList());
94     }
95
96     private TcaUtils() {
97         // private constructor
98     }
99 }