Standalone TCA with EELF Logger
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-tca-web / src / main / java / org / onap / dcae / analytics / tca / web / util / TcaUtils.java
1 /*
2  * ================================================================================
3  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  *
18  */
19
20 package org.onap.dcae.analytics.tca.web.util;
21
22 import static org.onap.dcae.analytics.tca.model.util.json.TcaModelJsonConversion.TCA_OBJECT_MAPPER;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Optional;
29 import java.util.stream.Collectors;
30
31 import org.onap.dcae.analytics.model.TcaModelConstants;
32 import org.onap.dcae.analytics.model.cef.EventListener;
33 import org.onap.dcae.analytics.tca.model.policy.TcaPolicy;
34 import org.onap.dcae.analytics.web.exception.AnalyticsParsingException;
35 import org.onap.dcae.utils.eelf.logger.model.info.ServiceLogInfoImpl;
36
37 /**
38  * @author Rajiv Singla
39  */
40 public abstract class TcaUtils {
41
42     /**
43      * TCA Service Log Info for ECOMP Logging
44      */
45     public static final ServiceLogInfoImpl TCA_SERVICE_LOG_INFO =
46             new ServiceLogInfoImpl(TcaModelConstants.TCA_SERVICE_NAME, System.getProperty("user.name"), "");
47
48     /**
49      * Creates a deep copy of Tca Policy
50      *
51      * @param tcaPolicy source tca policy object
52      *
53      * @return deep copy of provided tca policy
54      */
55     public static TcaPolicy getTcaPolicyDeepCopy(final TcaPolicy tcaPolicy) {
56         if (tcaPolicy != null) {
57             try {
58                 return TCA_OBJECT_MAPPER.treeToValue(TCA_OBJECT_MAPPER.valueToTree(tcaPolicy), TcaPolicy.class);
59             } catch (JsonProcessingException e) {
60                 throw new AnalyticsParsingException("Unable to create deep copy of TCA Policy: " + tcaPolicy, e);
61             }
62         } else {
63             final String errorMessage = "Invalid application state. TCA Policy must not be null";
64             throw new AnalyticsParsingException(errorMessage, new IllegalStateException(errorMessage));
65         }
66     }
67
68
69     /**
70      * Converts given event Listeners to list of CEF Message String
71      *
72      * @param eventListeners event listeners object
73      *
74      * @return cef messages as string
75      */
76     public static List<String> getCefMessagesFromEventListeners(final List<EventListener> eventListeners) {
77         if (!Optional.ofNullable(eventListeners).isPresent()) {
78             return Collections.emptyList();
79         }
80         return eventListeners.stream().map(eventListener -> {
81             try {
82                 return TCA_OBJECT_MAPPER.writeValueAsString(eventListener);
83             } catch (JsonProcessingException e) {
84                 throw new AnalyticsParsingException("Unable to parse EventLister to String: " + eventListener, e);
85             }
86         }).collect(Collectors.toList());
87     }
88
89     private TcaUtils() {
90         // private constructor
91     }
92 }