2 * ===============================LICENSE_START======================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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===========================================
21 package org.openecomp.dcae.apod.analytics.cdap.tca.utils;
23 import co.cask.cdap.api.RuntimeContext;
24 import com.google.common.base.Function;
25 import com.google.common.collect.Lists;
26 import org.openecomp.dcae.apod.analytics.cdap.common.exception.CDAPSettingsException;
27 import org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca.TCAVESAlertEntity;
28 import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCAAppPreferences;
29 import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCAPolicyPreferences;
30 import org.openecomp.dcae.apod.analytics.cdap.tca.validator.TCAPolicyPreferencesValidator;
31 import org.openecomp.dcae.apod.analytics.cdap.tca.validator.TCAPreferencesValidator;
32 import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants;
33 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy;
34 import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import java.io.IOException;
39 import java.util.Collection;
40 import java.util.List;
42 import java.util.TreeMap;
44 import static com.google.common.collect.Lists.newArrayList;
45 import static org.openecomp.dcae.apod.analytics.cdap.common.utils.ValidationUtils.validateSettings;
46 import static org.openecomp.dcae.apod.analytics.common.AnalyticsConstants.TCA_POLICY_METRICS_PER_FUNCTIONAL_ROLE_PATH;
49 * Utility Helper methods for CDAP TCA sub module.
52 * @author Rajiv Singla . Creation Date: 10/24/2016.
54 public abstract class CDAPTCAUtils extends TCAUtils {
56 private static final Logger LOG = LoggerFactory.getLogger(CDAPTCAUtils.class);
59 * Function that extracts alert message string from {@link TCAVESAlertEntity}
61 public static final Function<TCAVESAlertEntity, String> MAP_ALERT_ENTITY_TO_ALERT_STRING_FUNCTION =
62 new Function<TCAVESAlertEntity, String>() {
64 public String apply(TCAVESAlertEntity alertEntity) {
65 return alertEntity == null ? null : alertEntity.getAlertMessage();
71 * Parses and validates Runtime Arguments to {@link TCAAppPreferences} object
73 * @param runtimeContext Runtime Context
75 * @return validated runtime arguments as {@link TCAAppPreferences} object
77 public static TCAAppPreferences getValidatedTCAAppPreferences(final RuntimeContext runtimeContext) {
78 // Parse runtime arguments
79 final Map<String, String> runtimeArguments = runtimeContext.getRuntimeArguments();
80 final TCAAppPreferences tcaAppPreferences =
81 ANALYTICS_MODEL_OBJECT_MAPPER.convertValue(runtimeArguments, TCAAppPreferences.class);
83 // Validate runtime arguments
84 validateSettings(tcaAppPreferences, new TCAPreferencesValidator());
86 return tcaAppPreferences;
91 * Extracts alert message strings from {@link TCAVESAlertEntity}
93 * @param alertEntities collection of alert entities
95 * @return List of alert message strings
97 public static List<String> extractAlertFromAlertEntities(final Collection<TCAVESAlertEntity> alertEntities) {
98 return Lists.transform(newArrayList(alertEntities), MAP_ALERT_ENTITY_TO_ALERT_STRING_FUNCTION);
103 * Converts Runtime Arguments to {@link TCAPolicyPreferences} object
105 * @param runtimeContext CDAP Runtime Arguments
107 * @return TCA Policy Preferences
109 public static TCAPolicy getValidatedTCAPolicyPreferences(final RuntimeContext runtimeContext) {
111 final Map<String, String> runtimeArguments = runtimeContext.getRuntimeArguments();
112 final TreeMap<String, String> sortedRuntimeArguments = new TreeMap<>(runtimeArguments);
114 LOG.debug("Printing all Received Runtime Arguments:");
115 for (Map.Entry<String, String> runtimeArgsEntry : sortedRuntimeArguments.entrySet()) {
116 LOG.debug("{}:{}", runtimeArgsEntry.getKey(), runtimeArgsEntry.getValue());
119 TCAPolicyPreferences tcaPolicyPreferences = new TCAPolicyPreferences();
121 final String tcaPolicy = sortedRuntimeArguments.get(AnalyticsConstants.TCA_POLICY_JSON_KEY);
123 if (tcaPolicy != null) {
125 LOG.debug(" tcaPolicy is being read from JSON String");
127 // initialize unquotedTCAPolicy
128 String unquotedTCAPolicy = tcaPolicy;
130 //remove starting and ending quote from tcaPolicy
131 if (tcaPolicy.trim().startsWith(AnalyticsConstants.TCA_POLICY_STRING_DELIMITER) && tcaPolicy.trim().endsWith
132 (AnalyticsConstants.TCA_POLICY_STRING_DELIMITER)) {
133 unquotedTCAPolicy = tcaPolicy.trim().substring(1, tcaPolicy.trim().length() - 1);
137 tcaPolicyPreferences = readValue(unquotedTCAPolicy , TCAPolicyPreferences.class);
138 } catch (IOException e) {
139 throw new CDAPSettingsException("Invalid tca policy format", LOG, e);
142 } else { // old controller is being used. Validate preferences as received from old controller
144 // extract TCA Policy Domain from Runtime Arguments
145 final String policyDomain = sortedRuntimeArguments.get(AnalyticsConstants.TCA_POLICY_DOMAIN_PATH);
147 // create new TCA Policy object
148 tcaPolicyPreferences.setDomain(policyDomain);
150 // filter out other non relevant fields which are not related to tca policy
151 final Map<String, String> tcaPolicyMap = filterMapByKeyNamePrefix(sortedRuntimeArguments,
152 TCA_POLICY_METRICS_PER_FUNCTIONAL_ROLE_PATH);
154 // determine functional Roles
155 final Map<String, Map<String, String>> functionalRolesMap =
156 extractSubTree(tcaPolicyMap, 2, 3, AnalyticsConstants.TCA_POLICY_DELIMITER);
158 // create metrics per functional role list
159 tcaPolicyPreferences.setMetricsPerFunctionalRole(
160 createTCAPolicyMetricsPerFunctionalRoleList(functionalRolesMap));
164 // validate tca Policy Preferences
165 validateSettings(tcaPolicyPreferences, new TCAPolicyPreferencesValidator());
167 LOG.info("Printing Effective TCA Policy: {}", tcaPolicyPreferences);
169 return tcaPolicyPreferences;