TCA: Support for VES/A&AI enrichment
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / main / java / org / openecomp / dcae / apod / analytics / cdap / tca / flowlet / TCAVESAAIEnrichmentFlowlet.java
1 /*\r
2  * ===============================LICENSE_START======================================\r
3  *  dcae-analytics\r
4  * ================================================================================\r
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  *  Licensed under the Apache License, Version 2.0 (the "License");\r
8  *  you may not use this file except in compliance with the License.\r
9  *   You may obtain a copy of the License at\r
10  *\r
11  *          http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  *  Unless required by applicable law or agreed to in writing, software\r
14  *  distributed under the License is distributed on an "AS IS" BASIS,\r
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  *  See the License for the specific language governing permissions and\r
17  *  limitations under the License.\r
18  *  ============================LICENSE_END===========================================\r
19  */\r
20 \r
21 package org.openecomp.dcae.apod.analytics.cdap.tca.flowlet;\r
22 \r
23 import co.cask.cdap.api.annotation.Output;\r
24 import co.cask.cdap.api.annotation.ProcessInput;\r
25 import co.cask.cdap.api.flow.flowlet.AbstractFlowlet;\r
26 import co.cask.cdap.api.flow.flowlet.FlowletContext;\r
27 import co.cask.cdap.api.flow.flowlet.OutputEmitter;\r
28 import org.openecomp.dcae.apod.analytics.aai.AAIClientFactory;\r
29 import org.openecomp.dcae.apod.analytics.aai.domain.config.AAIHttpClientConfig;\r
30 import org.openecomp.dcae.apod.analytics.aai.service.AAIEnrichmentClient;\r
31 import org.openecomp.dcae.apod.analytics.cdap.common.CDAPComponentsConstants;\r
32 import org.openecomp.dcae.apod.analytics.cdap.common.exception.CDAPSettingsException;\r
33 import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCAAppPreferences;\r
34 import org.openecomp.dcae.apod.analytics.cdap.tca.utils.CDAPTCAUtils;\r
35 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.ClosedLoopEventStatus;\r
36 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.ControlLoopSchemaType;\r
37 import org.openecomp.dcae.apod.analytics.model.facade.tca.TCAVESResponse;\r
38 import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils;\r
39 import org.slf4j.Logger;\r
40 import org.slf4j.LoggerFactory;\r
41 \r
42 import java.io.IOException;\r
43 \r
44 /**\r
45  * Flowlet responsible for doing A&AI Enrichment\r
46  *\r
47  * @author Rajiv Singla . Creation Date: 9/20/2017.\r
48  */\r
49 public class TCAVESAAIEnrichmentFlowlet extends AbstractFlowlet {\r
50 \r
51     private static final Logger LOG = LoggerFactory.getLogger(TCAVESAAIEnrichmentFlowlet.class);\r
52 \r
53     @Output(CDAPComponentsConstants.TCA_FIXED_VES_AAI_ENRICHMENT_NAME_OUTPUT)\r
54     protected OutputEmitter<String> aaiEnrichmentOutputEmitter;\r
55 \r
56     private TCAAppPreferences tcaAppPreferences;\r
57     private AAIEnrichmentClient aaiEnrichmentClient;\r
58 \r
59     @Override\r
60     public void configure() {\r
61         setName(CDAPComponentsConstants.TCA_FIXED_VES_AAI_ENRICHMENT_NAME_FLOWLET);\r
62         setDescription(CDAPComponentsConstants.TCA_FIXED_VES_AAI_ENRICHMENT_DESCRIPTION_FLOWLET);\r
63     }\r
64 \r
65     @Override\r
66     public void initialize(FlowletContext flowletContext) throws Exception {\r
67         super.initialize(flowletContext);\r
68         tcaAppPreferences = CDAPTCAUtils.getValidatedTCAAppPreferences(flowletContext);\r
69         if (tcaAppPreferences.getEnableAAIEnrichment()) {\r
70             final AAIHttpClientConfig aaiHttpClientConfig =\r
71                     CDAPTCAUtils.createAAIEnrichmentClientConfig(tcaAppPreferences);\r
72             aaiEnrichmentClient = AAIClientFactory.create().getEnrichmentClient(aaiHttpClientConfig);\r
73         }\r
74     }\r
75 \r
76     @ProcessInput(CDAPComponentsConstants.TCA_FIXED_VES_ALERTS_ABATEMENT_NAME_OUTPUT)\r
77     public void performAAIEnrichment(final String alertMessageString) throws IOException {\r
78 \r
79         // if A&AI enrichment is disabled - no A&AI lookups are required\r
80         if (!tcaAppPreferences.getEnableAAIEnrichment()) {\r
81 \r
82             LOG.debug("A&AI Enrichment is disabled. Skip A&AI Enrichment for alert: {}", alertMessageString);\r
83             aaiEnrichmentOutputEmitter.emit(alertMessageString);\r
84 \r
85         } else {\r
86 \r
87             // determine closed Loop Event Status\r
88             final TCAVESResponse tcavesResponse = TCAUtils.readValue(alertMessageString, TCAVESResponse.class);\r
89             final ClosedLoopEventStatus closedLoopEventStatus =\r
90                     ClosedLoopEventStatus.valueOf(tcavesResponse.getClosedLoopEventStatus());\r
91 \r
92             if (closedLoopEventStatus == ClosedLoopEventStatus.ONSET) {\r
93                 LOG.debug("Performing A&AI Enrichment of ONSET Alert: {}", alertMessageString);\r
94                 final ControlLoopSchemaType controlLoopSchemaType =\r
95                         TCAUtils.determineControlLoopSchemaType(tcavesResponse);\r
96                 final String sourceName = TCAUtils.determineSourceName(tcavesResponse);\r
97                 LOG.debug("A&AI Source Name: {}, Control Loop Schema Type: {} for ONSET Alert: {}",\r
98                         sourceName, controlLoopSchemaType, alertMessageString);\r
99 \r
100                 if (controlLoopSchemaType == ControlLoopSchemaType.VM) {\r
101                     final String aaiVMEnrichmentAPIPath = tcaAppPreferences.getAaiVMEnrichmentAPIPath();\r
102                     TCAUtils.doAAIVMEnrichment(tcavesResponse, aaiEnrichmentClient, aaiVMEnrichmentAPIPath,\r
103                             alertMessageString, sourceName);\r
104                 } else {\r
105                     final String aaiVNFEnrichmentAPIPath = tcaAppPreferences.getAaiVNFEnrichmentAPIPath();\r
106                     TCAUtils.doAAIVNFEnrichment(tcavesResponse, aaiEnrichmentClient, aaiVNFEnrichmentAPIPath,\r
107                             alertMessageString, sourceName);\r
108                 }\r
109 \r
110                 final String aaiEnrichedAlert = TCAUtils.writeValueAsString(tcavesResponse);\r
111                 LOG.debug("Emitting Alert after A&AI Enrichment: {}", aaiEnrichedAlert);\r
112                 aaiEnrichmentOutputEmitter.emit(alertMessageString);\r
113 \r
114                 // skip A&AI Enrichment of alerts with closed Loop Event Status - ABATED\r
115             } else if (closedLoopEventStatus == ClosedLoopEventStatus.ABATED) {\r
116                 LOG.debug("Skipping Enrichment of Abated Alert: {}", alertMessageString);\r
117                 aaiEnrichmentOutputEmitter.emit(alertMessageString);\r
118 \r
119             } else {\r
120                 // unsupported closed loop event status\r
121                 final String errorMessage = String.format(\r
122                         "Unexpected ClosedLoopEventStatus: %s. Only ONSET and ABATED are supported." +\r
123                                 "Ignoring alert: %s", closedLoopEventStatus, alertMessageString);\r
124                 throw new CDAPSettingsException(errorMessage, LOG, new IllegalStateException(errorMessage));\r
125             }\r
126         }\r
127     }\r
128 }\r