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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.analytics.tca.web.util;
22 import static org.onap.dcae.analytics.tca.model.util.json.TcaModelJsonConversion.TCA_OBJECT_MAPPER;
24 import com.fasterxml.jackson.core.JsonProcessingException;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Optional;
29 import java.util.stream.Collectors;
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;
38 * @author Rajiv Singla
40 public abstract class TcaUtils {
43 * TCA Service Log Info for ECOMP Logging
45 public static final ServiceLogInfoImpl TCA_SERVICE_LOG_INFO =
46 new ServiceLogInfoImpl(TcaModelConstants.TCA_SERVICE_NAME, System.getProperty("user.name"), "");
49 * Creates a deep copy of Tca Policy
51 * @param tcaPolicy source tca policy object
53 * @return deep copy of provided tca policy
55 public static TcaPolicy getTcaPolicyDeepCopy(final TcaPolicy tcaPolicy) {
56 if (tcaPolicy != null) {
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);
63 final String errorMessage = "Invalid application state. TCA Policy must not be null";
64 throw new AnalyticsParsingException(errorMessage, new IllegalStateException(errorMessage));
70 * Converts given event Listeners to list of CEF Message String
72 * @param eventListeners event listeners object
74 * @return cef messages as string
76 public static List<String> getCefMessagesFromEventListeners(final List<EventListener> eventListeners) {
77 if (!Optional.ofNullable(eventListeners).isPresent()) {
78 return Collections.emptyList();
80 return eventListeners.stream().map(eventListener -> {
82 return TCA_OBJECT_MAPPER.writeValueAsString(eventListener);
83 } catch (JsonProcessingException e) {
84 throw new AnalyticsParsingException("Unable to parse EventLister to String: " + eventListener, e);
86 }).collect(Collectors.toList());
90 // private constructor