89c5a842adba638036d73a007c7fea4c9d153675
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / main / java / org / openecomp / dcae / apod / analytics / cdap / tca / utils / CDAPTCAUtils.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
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
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
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===========================================
19  */
20
21 package org.openecomp.dcae.apod.analytics.cdap.tca.utils;
22
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;
43
44 import java.io.IOException;
45 import java.net.MalformedURLException;
46 import java.net.URL;
47 import java.util.Collection;
48 import java.util.List;
49 import java.util.Map;
50 import java.util.TreeMap;
51
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;
55
56 /**
57  * Utility Helper methods for CDAP TCA sub module.
58  *
59  * <p>
60  * @author Rajiv Singla . Creation Date: 10/24/2016.
61  */
62 public abstract class CDAPTCAUtils extends TCAUtils {
63
64     private static final Logger LOG = LoggerFactory.getLogger(CDAPTCAUtils.class);
65
66     /**
67      * Function that extracts alert message string from {@link TCAVESAlertEntity}
68      */
69     public static final Function<TCAVESAlertEntity, String> MAP_ALERT_ENTITY_TO_ALERT_STRING_FUNCTION =
70             new Function<TCAVESAlertEntity, String>() {
71                 @Override
72                 public String apply(TCAVESAlertEntity alertEntity) {
73                     return alertEntity == null ? null : alertEntity.getAlertMessage();
74                 }
75             };
76
77
78     /**
79      * Parses and validates Runtime Arguments to {@link TCAAppPreferences} object
80      *
81      * @param runtimeContext Runtime Context
82      *
83      * @return validated runtime arguments as {@link TCAAppPreferences} object
84      */
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);
90
91         final String appConfigString = runtimeContext.getApplicationSpecification().getConfiguration();
92
93         // populate DMaaP Information from App Config String
94         populateDMaaPInfoFromAppConfiguration(appConfigString, tcaAppPreferences);
95
96         // Validate runtime arguments
97         validateSettings(tcaAppPreferences, new TCAPreferencesValidator());
98
99         return tcaAppPreferences;
100     }
101
102     /**
103      * Populated App Preferences DMaaP Information from Application Config String
104      *
105      * @param appConfigString  CDAP Application config String
106      * @param tcaAppPreferences TCA App Preferences
107      */
108     private static void populateDMaaPInfoFromAppConfiguration(final String appConfigString,
109                                                               final TCAAppPreferences tcaAppPreferences) {
110
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");
113         }
114
115         LOG.info("Fetching DMaaP information from App Configuration String: {}", appConfigString);
116
117         try {
118             final TCAControllerAppConfig tcaControllerAppConfig =
119                     readValue(appConfigString, TCAControllerAppConfig.class);
120
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) {
125
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));
136
137                 final TCAHandleIn tcaHandleIn = tcaControllerAppConfig.getStreamsSubscribes().getTcaHandleIn();
138                 tcaAppPreferences.setSubscriberUserName(tcaHandleIn.getAafUserName());
139                 tcaAppPreferences.setSubscriberUserPassword(tcaHandleIn.getAafPassword());
140             } else {
141                 LOG.warn("Unable to populate Subscriber DMaaP Information from App Config String: {}", appConfigString);
142             }
143
144
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) {
149
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));
160
161                 final TCAHandleOut tcaHandleOut = tcaControllerAppConfig.getStreamsPublishes().getTcaHandleOut();
162                 tcaAppPreferences.setPublisherUserName(tcaHandleOut.getAafUserName());
163                 tcaAppPreferences.setPublisherUserPassword(tcaHandleOut.getAafPassword());
164             } else {
165                 LOG.warn("Unable to populate Publisher DMaaP Information from App Config String: {}", appConfigString);
166             }
167
168
169         } catch (IOException e) {
170             throw new CDAPSettingsException(
171                     "Unable to parse App Config to Json Object.Invalid App Config String: " + appConfigString, LOG, e);
172         }
173     }
174
175     /**
176      * Parses provided DMaaP MR URL string to {@link URL} object
177      *
178      * @param urlString url string
179      *
180      * @return url object
181      */
182     private static URL parseURL(final String urlString) {
183         try {
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);
188         }
189     }
190
191     /**
192      * Sets up default DMaaP Port if not provided with DMaaP URL
193      *
194      * @param protocol protocol e.g. http or https
195      *
196      * @return default DMaaP MR port number
197      */
198     private static int getDefaultDMaaPPort(final String protocol) {
199         if ("http".equals(protocol)) {
200             return 3904;
201         } else if ("https".equals(protocol)) {
202             return 3905;
203         } else {
204             return 80;
205         }
206     }
207
208
209     /**
210      * Extracts alert message strings from {@link TCAVESAlertEntity}
211      *
212      * @param alertEntities collection of alert entities
213      *
214      * @return List of alert message strings
215      */
216     public static List<String> extractAlertFromAlertEntities(final Collection<TCAVESAlertEntity> alertEntities) {
217         return Lists.transform(newArrayList(alertEntities), MAP_ALERT_ENTITY_TO_ALERT_STRING_FUNCTION);
218     }
219
220
221     /**
222      * Converts Runtime Arguments to {@link TCAPolicyPreferences} object
223      *
224      * @param runtimeContext CDAP Runtime Arguments
225      *
226      * @return TCA Policy Preferences
227      */
228     public static TCAPolicy getValidatedTCAPolicyPreferences(final RuntimeContext runtimeContext) {
229
230         final Map<String, String> runtimeArguments = runtimeContext.getRuntimeArguments();
231         final TreeMap<String, String> sortedRuntimeArguments = new TreeMap<>(runtimeArguments);
232
233         LOG.debug("Printing all Received Runtime Arguments:");
234         for (Map.Entry<String, String> runtimeArgsEntry : sortedRuntimeArguments.entrySet()) {
235             LOG.debug("{}:{}", runtimeArgsEntry.getKey(), runtimeArgsEntry.getValue());
236         }
237
238         TCAPolicyPreferences tcaPolicyPreferences = new TCAPolicyPreferences();
239
240         final String tcaPolicyJsonString = sortedRuntimeArguments.get(AnalyticsConstants.TCA_POLICY_JSON_KEY);
241
242         if (StringUtils.isNotBlank(tcaPolicyJsonString)) {
243
244             LOG.info("TcaPolicy will be set from input argument name: {} as JSON String with value: {}",
245                     AnalyticsConstants.TCA_POLICY_JSON_KEY, tcaPolicyJsonString);
246
247             // initialize unquotedTCAPolicy
248             String unquotedTCAPolicy = tcaPolicyJsonString.trim();
249
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);
254             }
255
256             try {
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);
261             }
262
263         } else {  // classical controller is being used.  Validate preferences as received from classical controller
264
265             LOG.info("TcaPolicy is being parsed as key value pair from classical controller");
266
267             // extract TCA Policy Domain from Runtime Arguments
268             final String policyDomain = sortedRuntimeArguments.get(AnalyticsConstants.TCA_POLICY_DOMAIN_PATH);
269
270             // create new TCA Policy object
271             tcaPolicyPreferences.setDomain(policyDomain);
272
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);
276
277             // determine functional Roles
278             final Map<String, Map<String, String>> functionalRolesMap =
279                     extractSubTree(tcaPolicyMap, 2, 3, AnalyticsConstants.TCA_POLICY_DELIMITER);
280
281             // create metrics per functional role list
282             tcaPolicyPreferences.setMetricsPerEventName(
283                     createTCAPolicyMetricsPerEventNameList(functionalRolesMap));
284
285         }
286
287         // validate tca Policy Preferences
288         validateSettings(tcaPolicyPreferences, new TCAPolicyPreferencesValidator());
289
290         LOG.info("Printing Effective TCA Policy: {}", tcaPolicyPreferences);
291
292         return tcaPolicyPreferences;
293     }
294 }