appc-dg-common sonar fixes part 2
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / main / java / org / onap / appc / dg / common / impl / OutputMessagePluginImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.dg.common.impl;
26
27 import static org.apache.commons.lang3.StringUtils.isEmpty;
28
29 import java.util.Map;
30 import org.onap.appc.dg.common.OutputMessagePlugin;
31 import org.onap.appc.exceptions.APPCException;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
33
34 public class OutputMessagePluginImpl implements OutputMessagePlugin {
35
36     @Override
37     public void outputMessageBuilder(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
38         String errorDescription;
39         String eventDescription;
40
41         //making output.status.message
42         errorDescription = params.get(Constants.ATTRIBUTE_ERROR_MESSAGE);
43         eventDescription = params.get(Constants.EVENT_MESSAGE);
44
45         addToContextIfNotContains(errorDescription, eventDescription, ctx);
46
47         //making event-message
48
49         if (!isEmpty(eventDescription)) {
50             ctx.setAttribute(Constants.EVENT_MESSAGE, eventDescription);
51         } else {
52             ctx.setAttribute(Constants.EVENT_MESSAGE, ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
53         }
54     }
55
56     private static void addToContextIfNotContains(String errorDescription, String eventDescription,
57         SvcLogicContext ctx) {
58         if (!isEmpty(errorDescription)) {
59             updateContext(errorDescription, ctx);
60         }
61         if (!isEmpty(eventDescription)) {
62             updateContext(eventDescription, ctx);
63         }
64     }
65
66     private static void updateContext(String eventDescription, SvcLogicContext ctx) {
67         if (isEmpty(ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE))) {
68             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, eventDescription);
69         } else if (!ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE).contains(eventDescription)) {
70             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE,
71                 ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE) + " | " + eventDescription);
72         }
73     }
74 }