Sonar Critical Fix
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / main / java / org / onap / dcae / apod / analytics / cdap / tca / flowlet / TCAVESAAIEnrichmentFlowlet.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.onap.dcae.apod.analytics.cdap.tca.flowlet;
22
23 import co.cask.cdap.api.annotation.Output;
24 import co.cask.cdap.api.annotation.ProcessInput;
25 import co.cask.cdap.api.flow.flowlet.AbstractFlowlet;
26 import co.cask.cdap.api.flow.flowlet.FlowletContext;
27 import co.cask.cdap.api.flow.flowlet.OutputEmitter;
28 import org.onap.dcae.apod.analytics.aai.AAIClientFactory;
29 import org.onap.dcae.apod.analytics.aai.domain.config.AAIHttpClientConfig;
30 import org.onap.dcae.apod.analytics.aai.service.AAIEnrichmentClient;
31 import org.onap.dcae.apod.analytics.cdap.common.CDAPComponentsConstants;
32 import org.onap.dcae.apod.analytics.cdap.common.exception.CDAPSettingsException;
33 import org.onap.dcae.apod.analytics.cdap.tca.settings.TCAAppPreferences;
34 import org.onap.dcae.apod.analytics.cdap.tca.utils.CDAPTCAUtils;
35 import org.onap.dcae.apod.analytics.model.domain.policy.tca.ClosedLoopEventStatus;
36 import org.onap.dcae.apod.analytics.model.domain.policy.tca.ControlLoopSchemaType;
37 import org.onap.dcae.apod.analytics.model.facade.tca.TCAVESResponse;
38 import org.onap.dcae.apod.analytics.tca.utils.TCAUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import java.io.IOException;
43
44 /**
45  * Flowlet responsible for doing A&AI Enrichment
46  *
47  * @author Rajiv Singla . Creation Date: 9/20/2017.
48  */
49 public class TCAVESAAIEnrichmentFlowlet extends AbstractFlowlet {
50
51     private static final Logger LOG = LoggerFactory.getLogger(TCAVESAAIEnrichmentFlowlet.class);
52
53     @Output(CDAPComponentsConstants.TCA_FIXED_VES_AAI_ENRICHMENT_NAME_OUTPUT)
54     protected OutputEmitter<String> aaiEnrichmentOutputEmitter;
55
56     private TCAAppPreferences tcaAppPreferences;
57     private AAIEnrichmentClient aaiEnrichmentClient;
58
59     @Override
60     public void configure() {
61         setName(CDAPComponentsConstants.TCA_FIXED_VES_AAI_ENRICHMENT_NAME_FLOWLET);
62         setDescription(CDAPComponentsConstants.TCA_FIXED_VES_AAI_ENRICHMENT_DESCRIPTION_FLOWLET);
63     }
64
65     @Override
66     public void initialize(FlowletContext flowletContext) throws Exception {
67         super.initialize(flowletContext);
68         tcaAppPreferences = CDAPTCAUtils.getValidatedTCAAppPreferences(flowletContext);
69         if (tcaAppPreferences.getEnableAAIEnrichment()) {
70             final AAIHttpClientConfig aaiHttpClientConfig =
71                     CDAPTCAUtils.createAAIEnrichmentClientConfig(tcaAppPreferences);
72             aaiEnrichmentClient = AAIClientFactory.create().getEnrichmentClient(aaiHttpClientConfig);
73         }
74     }
75
76     @ProcessInput(CDAPComponentsConstants.TCA_FIXED_VES_ALERTS_ABATEMENT_NAME_OUTPUT)
77     public void performAAIEnrichment(final String alertMessageString) throws IOException {
78
79         // if A&AI enrichment is disabled - no A&AI lookups are required
80         if (!tcaAppPreferences.getEnableAAIEnrichment()) {
81
82             LOG.debug("A&AI Enrichment is disabled. Skip A&AI Enrichment for alert: {}", alertMessageString);
83             aaiEnrichmentOutputEmitter.emit(alertMessageString);
84
85         } else {
86
87             // determine closed Loop Event Status
88             final TCAVESResponse tcavesResponse = TCAUtils.readValue(alertMessageString, TCAVESResponse.class);
89             final ClosedLoopEventStatus closedLoopEventStatus =
90                     ClosedLoopEventStatus.valueOf(tcavesResponse.getClosedLoopEventStatus());
91
92             if (closedLoopEventStatus == ClosedLoopEventStatus.ONSET) {
93                 LOG.debug("Performing A&AI Enrichment of ONSET Alert: {}", alertMessageString);
94                 final ControlLoopSchemaType controlLoopSchemaType =
95                         TCAUtils.determineControlLoopSchemaType(tcavesResponse);
96                 final String sourceName = TCAUtils.determineSourceName(tcavesResponse);
97                 LOG.debug("A&AI Source Name: {}, Control Loop Schema Type: {} for ONSET Alert: {}",
98                         sourceName, controlLoopSchemaType, alertMessageString);
99
100                 if (controlLoopSchemaType == ControlLoopSchemaType.VM) {
101                     final String aaiVMEnrichmentAPIPath = tcaAppPreferences.getAaiVMEnrichmentAPIPath();
102                     TCAUtils.doAAIVMEnrichment(tcavesResponse, aaiEnrichmentClient, aaiVMEnrichmentAPIPath,
103                             alertMessageString, sourceName);
104                 } else {
105                     final String aaiVNFEnrichmentAPIPath = tcaAppPreferences.getAaiVNFEnrichmentAPIPath();
106                     TCAUtils.doAAIVNFEnrichment(tcavesResponse, aaiEnrichmentClient, aaiVNFEnrichmentAPIPath,
107                             alertMessageString, sourceName);
108                 }
109
110                 final String aaiEnrichedAlert = TCAUtils.writeValueAsString(tcavesResponse);
111                 LOG.debug("Emitting Alert after A&AI Enrichment: {}", aaiEnrichedAlert);
112                 aaiEnrichmentOutputEmitter.emit(aaiEnrichedAlert);
113
114                 // skip A&AI Enrichment of alerts with closed Loop Event Status - ABATED
115             } else if (closedLoopEventStatus == ClosedLoopEventStatus.ABATED) {
116                 LOG.debug("Skipping Enrichment of Abated Alert: {}", alertMessageString);
117                 aaiEnrichmentOutputEmitter.emit(alertMessageString);
118
119             } else {
120                 // unsupported closed loop event status
121                 final String errorMessage = String.format(
122                         "Unexpected ClosedLoopEventStatus: %s. Only ONSET and ABATED are supported." +
123                                 "Ignoring alert: %s", closedLoopEventStatus, alertMessageString);
124                 throw new CDAPSettingsException(errorMessage, LOG, new IllegalStateException(errorMessage));
125             }
126         }
127     }
128 }