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.apache.commons.lang3.StringUtils;
27 import org.openecomp.dcae.apod.analytics.cdap.common.exception.CDAPSettingsException;
28 import org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca.TCAVESAlertEntity;
29 import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCAAppPreferences;
30 import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCAPolicyPreferences;
31 import org.openecomp.dcae.apod.analytics.cdap.tca.validator.TCAPolicyPreferencesValidator;
32 import org.openecomp.dcae.apod.analytics.cdap.tca.validator.TCAPreferencesValidator;
33 import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants;
34 import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
35 import org.openecomp.dcae.apod.analytics.model.config.tca.DMAAPInfo;
36 import org.openecomp.dcae.apod.analytics.model.config.tca.TCAControllerAppConfig;
37 import org.openecomp.dcae.apod.analytics.model.config.tca.TCAHandleIn;
38 import org.openecomp.dcae.apod.analytics.model.config.tca.TCAHandleOut;
39 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy;
40 import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 import java.io.IOException;
45 import java.net.MalformedURLException;
47 import java.util.Collection;
48 import java.util.List;
50 import java.util.TreeMap;
52 import static com.google.common.collect.Lists.newArrayList;
53 import static org.openecomp.dcae.apod.analytics.cdap.common.utils.ValidationUtils.validateSettings;
54 import static org.openecomp.dcae.apod.analytics.common.AnalyticsConstants.TCA_POLICY_METRICS_PER_FUNCTIONAL_ROLE_PATH;
57 * Utility Helper methods for CDAP TCA sub module.
60 * @author Rajiv Singla . Creation Date: 10/24/2016.
62 public abstract class CDAPTCAUtils extends TCAUtils {
64 private static final Logger LOG = LoggerFactory.getLogger(CDAPTCAUtils.class);
67 * Function that extracts alert message string from {@link TCAVESAlertEntity}
69 public static final Function<TCAVESAlertEntity, String> MAP_ALERT_ENTITY_TO_ALERT_STRING_FUNCTION =
70 new Function<TCAVESAlertEntity, String>() {
72 public String apply(TCAVESAlertEntity alertEntity) {
73 return alertEntity == null ? null : alertEntity.getAlertMessage();
79 * Parses and validates Runtime Arguments to {@link TCAAppPreferences} object
81 * @param runtimeContext Runtime Context
83 * @return validated runtime arguments as {@link TCAAppPreferences} object
85 public static TCAAppPreferences getValidatedTCAAppPreferences(final RuntimeContext runtimeContext) {
86 // Parse runtime arguments
87 final Map<String, String> runtimeArguments = runtimeContext.getRuntimeArguments();
88 final TCAAppPreferences tcaAppPreferences =
89 ANALYTICS_MODEL_OBJECT_MAPPER.convertValue(runtimeArguments, TCAAppPreferences.class);
91 final String appConfigString = runtimeContext.getApplicationSpecification().getConfiguration();
93 // populate DMaaP Information from App Config String
94 populateDMaaPInfoFromAppConfiguration(appConfigString, tcaAppPreferences);
96 // Validate runtime arguments
97 validateSettings(tcaAppPreferences, new TCAPreferencesValidator());
99 return tcaAppPreferences;
103 * Populated App Preferences DMaaP Information from Application Config String
105 * @param appConfigString CDAP Application config String
106 * @param tcaAppPreferences TCA App Preferences
108 private static void populateDMaaPInfoFromAppConfiguration(final String appConfigString,
109 final TCAAppPreferences tcaAppPreferences) {
111 if (null != tcaAppPreferences.getSubscriberHostName() || null != tcaAppPreferences.getPublisherHostName()) {
112 LOG.info("DMaaP Information is set from runtime preferences. Skipping getting DMaaP info from App Config");
115 LOG.info("Fetching DMaaP information from App Configuration String: {}", appConfigString);
118 final TCAControllerAppConfig tcaControllerAppConfig =
119 readValue(appConfigString, TCAControllerAppConfig.class);
121 // Parse Subscriber DMaaP information from App Config String
122 if (tcaControllerAppConfig.getStreamsSubscribes() != null &&
123 tcaControllerAppConfig.getStreamsSubscribes().getTcaHandleIn() != null &&
124 tcaControllerAppConfig.getStreamsSubscribes().getTcaHandleIn().getDmaapInfo() != null) {
126 final DMAAPInfo subscriberDmaapInfo =
127 tcaControllerAppConfig.getStreamsSubscribes().getTcaHandleIn().getDmaapInfo();
128 LOG.debug("App Config Subscriber Host URL: {}", subscriberDmaapInfo.getTopicUrl());
129 final URL subscriberUrl = parseURL(subscriberDmaapInfo.getTopicUrl());
130 tcaAppPreferences.setSubscriberProtocol(subscriberUrl.getProtocol());
131 tcaAppPreferences.setSubscriberHostName(subscriberUrl.getHost());
132 final int subscriberUrlPort = subscriberUrl.getPort() != -1 ?
133 subscriberUrl.getPort() : getDefaultDMaaPPort(subscriberUrl.getProtocol());
134 tcaAppPreferences.setSubscriberHostPort(subscriberUrlPort);
135 tcaAppPreferences.setSubscriberTopicName(subscriberUrl.getPath().substring(8));
137 final TCAHandleIn tcaHandleIn = tcaControllerAppConfig.getStreamsSubscribes().getTcaHandleIn();
138 tcaAppPreferences.setSubscriberUserName(tcaHandleIn.getAafUserName());
139 tcaAppPreferences.setSubscriberUserPassword(tcaHandleIn.getAafPassword());
141 LOG.warn("Unable to populate Subscriber DMaaP Information from App Config String: {}", appConfigString);
145 // Parse Publisher DMaaP information from App Config String
146 if (tcaControllerAppConfig.getStreamsPublishes() != null &&
147 tcaControllerAppConfig.getStreamsPublishes().getTcaHandleOut() != null &&
148 tcaControllerAppConfig.getStreamsPublishes().getTcaHandleOut().getDmaapInfo() != null) {
150 final DMAAPInfo publisherDmaapInfo =
151 tcaControllerAppConfig.getStreamsPublishes().getTcaHandleOut().getDmaapInfo();
152 LOG.debug("App Config Publisher Host URL: {}", publisherDmaapInfo.getTopicUrl());
153 final URL publisherUrl = parseURL(publisherDmaapInfo.getTopicUrl());
154 tcaAppPreferences.setPublisherProtocol(publisherUrl.getProtocol());
155 tcaAppPreferences.setPublisherHostName(publisherUrl.getHost());
156 final int publisherUrlPort = publisherUrl.getPort() != -1 ?
157 publisherUrl.getPort() : getDefaultDMaaPPort(publisherUrl.getProtocol());
158 tcaAppPreferences.setPublisherHostPort(publisherUrlPort);
159 tcaAppPreferences.setPublisherTopicName(publisherUrl.getPath().substring(8));
161 final TCAHandleOut tcaHandleOut = tcaControllerAppConfig.getStreamsPublishes().getTcaHandleOut();
162 tcaAppPreferences.setPublisherUserName(tcaHandleOut.getAafUserName());
163 tcaAppPreferences.setPublisherUserPassword(tcaHandleOut.getAafPassword());
165 LOG.warn("Unable to populate Publisher DMaaP Information from App Config String: {}", appConfigString);
169 } catch (IOException e) {
170 throw new CDAPSettingsException(
171 "Unable to parse App Config to Json Object.Invalid App Config String: " + appConfigString, LOG, e);
176 * Parses provided DMaaP MR URL string to {@link URL} object
178 * @param urlString url string
182 private static URL parseURL(final String urlString) {
184 return new URL(urlString);
185 } catch (MalformedURLException e) {
186 final String errorMessage = String.format("Invalid URL format: %s", urlString);
187 throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
192 * Sets up default DMaaP Port if not provided with DMaaP URL
194 * @param protocol protocol e.g. http or https
196 * @return default DMaaP MR port number
198 private static int getDefaultDMaaPPort(final String protocol) {
199 if ("http".equals(protocol)) {
201 } else if ("https".equals(protocol)) {
210 * Extracts alert message strings from {@link TCAVESAlertEntity}
212 * @param alertEntities collection of alert entities
214 * @return List of alert message strings
216 public static List<String> extractAlertFromAlertEntities(final Collection<TCAVESAlertEntity> alertEntities) {
217 return Lists.transform(newArrayList(alertEntities), MAP_ALERT_ENTITY_TO_ALERT_STRING_FUNCTION);
222 * Converts Runtime Arguments to {@link TCAPolicyPreferences} object
224 * @param runtimeContext CDAP Runtime Arguments
226 * @return TCA Policy Preferences
228 public static TCAPolicy getValidatedTCAPolicyPreferences(final RuntimeContext runtimeContext) {
230 final Map<String, String> runtimeArguments = runtimeContext.getRuntimeArguments();
231 final TreeMap<String, String> sortedRuntimeArguments = new TreeMap<>(runtimeArguments);
233 LOG.debug("Printing all Received Runtime Arguments:");
234 for (Map.Entry<String, String> runtimeArgsEntry : sortedRuntimeArguments.entrySet()) {
235 LOG.debug("{}:{}", runtimeArgsEntry.getKey(), runtimeArgsEntry.getValue());
238 TCAPolicyPreferences tcaPolicyPreferences = new TCAPolicyPreferences();
240 final String tcaPolicyJsonString = sortedRuntimeArguments.get(AnalyticsConstants.TCA_POLICY_JSON_KEY);
242 if (StringUtils.isNotBlank(tcaPolicyJsonString)) {
244 LOG.info("TcaPolicy will be set from input argument name: {} as JSON String with value: {}",
245 AnalyticsConstants.TCA_POLICY_JSON_KEY, tcaPolicyJsonString);
247 // initialize unquotedTCAPolicy
248 String unquotedTCAPolicy = tcaPolicyJsonString.trim();
250 //remove starting and ending quote from passed tca policy Json string if present
251 if (tcaPolicyJsonString.trim().startsWith(AnalyticsConstants.TCA_POLICY_STRING_DELIMITER) &&
252 tcaPolicyJsonString.trim().endsWith(AnalyticsConstants.TCA_POLICY_STRING_DELIMITER)) {
253 unquotedTCAPolicy = tcaPolicyJsonString.trim().substring(1, tcaPolicyJsonString.trim().length() - 1);
257 tcaPolicyPreferences = readValue(unquotedTCAPolicy , TCAPolicyPreferences.class);
258 } catch (IOException e) {
259 throw new CDAPSettingsException(
260 "Input tca_policy string format is not correct. tca_policy: " + tcaPolicyJsonString, LOG, e);
263 } else { // classical controller is being used. Validate preferences as received from classical controller
265 LOG.info("TcaPolicy is being parsed as key value pair from classical controller");
267 // extract TCA Policy Domain from Runtime Arguments
268 final String policyDomain = sortedRuntimeArguments.get(AnalyticsConstants.TCA_POLICY_DOMAIN_PATH);
270 // create new TCA Policy object
271 tcaPolicyPreferences.setDomain(policyDomain);
273 // filter out other non relevant fields which are not related to tca policy
274 final Map<String, String> tcaPolicyMap = filterMapByKeyNamePrefix(sortedRuntimeArguments,
275 TCA_POLICY_METRICS_PER_FUNCTIONAL_ROLE_PATH);
277 // determine functional Roles
278 final Map<String, Map<String, String>> functionalRolesMap =
279 extractSubTree(tcaPolicyMap, 2, 3, AnalyticsConstants.TCA_POLICY_DELIMITER);
281 // create metrics per functional role list
282 tcaPolicyPreferences.setMetricsPerEventName(
283 createTCAPolicyMetricsPerEventNameList(functionalRolesMap));
287 // validate tca Policy Preferences
288 validateSettings(tcaPolicyPreferences, new TCAPolicyPreferencesValidator());
290 LOG.info("Printing Effective TCA Policy: {}", tcaPolicyPreferences);
292 return tcaPolicyPreferences;