Suppression of long logs in GraphExecutor
[appc.git] / appc-config / appc-flow-controller / provider / src / main / java / org / onap / appc / flow / controller / executorImpl / GraphExecutor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Ericsson
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  * ============LICENSE_END=========================================================
22  */
23 package org.onap.appc.flow.controller.executorImpl;
24
25 import java.util.Enumeration;
26 import java.util.Map;
27 import java.util.Properties;
28 import org.onap.appc.flow.controller.data.Transaction;
29 import org.onap.appc.flow.controller.interfaces.FlowExecutorInterface;
30 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.FrameworkUtil;
36 import org.osgi.framework.ServiceReference;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39
40
41 public class GraphExecutor implements FlowExecutorInterface {
42
43     private static EELFLogger log = EELFManager.getInstance().getLogger(GraphExecutor.class);
44     static final String SVC_LOGIC_STATUS_PARAM = "SvcLogic.status";
45
46     private SvcLogicService svcLogic = null;
47
48     public GraphExecutor() {
49         BundleContext bctx = FrameworkUtil.getBundle(SvcLogicService.class).getBundleContext();
50
51         ServiceReference sref = bctx.getServiceReference(SvcLogicService.NAME);
52         if (sref != null) {
53             svcLogic = (SvcLogicService) bctx.getService(sref);
54         } else {
55             log.warn("Cannot find service reference for " + SvcLogicService.NAME);
56         }
57         log.debug("Graph Executor Initialized successfully");
58     }
59
60     public boolean hasGraph(String module, String rpc, String version, String mode) throws SvcLogicException {
61         return svcLogic.hasGraph(module, rpc, version, mode);
62     }
63
64     public Properties executeGraph(String module, String rpc, String version, String mode, Properties parms)
65         throws SvcLogicException {
66         log.debug("Parameters passed to SLI");
67
68         Properties respProps = svcLogic.execute(module, rpc, version, mode, parms);
69         if (log.isDebugEnabled()) {
70             log.debug("Parameters returned by SLI");
71             for (Object key : respProps.keySet()) {
72                 String parmName = (String) key;
73                 String parmValue = respProps.getProperty(parmName);
74
75                 log.debug(parmName + " = " + parmValue);
76             }
77         }
78         return respProps;
79     }
80
81     @Override
82     public Map<String, String> execute(Transaction transaction, SvcLogicContext ctx) throws Exception {
83
84         String fn = "GraphExecutor.execute ";
85         log.debug(fn + "About to execute graph : " + transaction.getExecutionRPC());
86
87         String payload = transaction.getPayload();
88         log.debug("payload:" + payload);
89
90         Properties parms = new Properties();
91         for (Object key : ctx.getAttributeKeySet()) {
92             String parmName = (String) key;
93             String parmValue = ctx.getAttribute(parmName);
94             parms.put(parmName, parmValue);
95             if (log.isTraceEnabled()) {
96                 log.trace(fn + "Setting Key= " + parmName + " and Value = " + parmValue);
97             }
98             else {
99                 if (parmValue.length() > 255) {
100                     log.info(fn + "Setting Key= " + parmName + " and Value = " + parmValue.substring(0, 255));
101                     log.info("\n...\n" + parmValue.length() +
102                             " characters in property, turn on TRACE logging to log entire parameter value");
103                 }
104                 else {
105                     log.info(fn + "Setting Key= " + parmName + " and Value = " + parmValue);
106                 }
107             }
108
109         }
110         Properties returnParams =
111             executeGraph(transaction.getExecutionModule(), transaction.getExecutionRPC(), null, "sync", parms);
112
113         log.debug("Returned Params from DG Module: " + transaction.getExecutionModule() + "and DG NAME: "
114             + transaction.getExecutionRPC() + returnParams.toString());
115
116         Enumeration e = returnParams.propertyNames();
117
118         while (e.hasMoreElements()) {
119             String key = (String) e.nextElement();
120             String property = returnParams.getProperty(key);
121             if (log.isTraceEnabled()) {
122                 log.trace("NEW KEY = " + key + " -- " + property);
123             }
124             else {
125                 if (property.length() > 255) {
126                     log.info("NEW KEY = " + key + " -- " + property.substring(0, 255));
127                     log.info("\n...\n" + property.length() +
128                             " characters in property, turn on TRACE logging to log entire property");
129                 }
130                 else {
131                     log.info("NEW KEY = " + key + " -- " + property);
132                 }
133             }
134
135             ctx.setAttribute(key, returnParams.getProperty(key));
136         }
137
138         if (FlowControllerConstants.FAILURE.equalsIgnoreCase(returnParams.getProperty(SVC_LOGIC_STATUS_PARAM))) {
139             transaction.setStatus(FlowControllerConstants.FAILURE);
140             ctx.setAttribute(
141                 ctx.getAttribute(FlowControllerConstants.RESPONSE_PREFIX)
142                     + FlowControllerConstants.OUTPUT_PARAM_STATUS,
143                 FlowControllerConstants.OUTPUT_STATUS_FAILURE);
144             ctx.setAttribute(ctx.getAttribute(FlowControllerConstants.RESPONSE_PREFIX)
145                 + FlowControllerConstants.OUTPUT_STATUS_MESSAGE, returnParams.getProperty("error-message"));
146             transaction.setStatusCode("401");
147             transaction.setState(ctx.getAttribute(transaction.getExecutionModule() + "." + transaction.getExecutionRPC()
148                 + "." + FlowControllerConstants.OUTPUT_STATUS_MESSAGE));
149             // Get error code from above instead setting here ...its for testing purpose
150
151         } else if (FlowControllerConstants.SUCCESS.equalsIgnoreCase(returnParams.getProperty(SVC_LOGIC_STATUS_PARAM))) {
152             transaction.setStatus(FlowControllerConstants.SUCCESS);
153             transaction.setStatusCode("400");
154             ctx.setAttribute(
155                 ctx.getAttribute(FlowControllerConstants.RESPONSE_PREFIX)
156                     + FlowControllerConstants.OUTPUT_PARAM_STATUS,
157                 FlowControllerConstants.OUTPUT_STATUS_SUCCESS);
158             transaction.setState(ctx.getAttribute(transaction.getExecutionModule() + "." + transaction.getExecutionRPC()
159                 + "." + FlowControllerConstants.OUTPUT_STATUS_MESSAGE));
160             // Get error code from above instead setting here ...its for testing purpose
161         } else {
162             transaction.setStatus(FlowControllerConstants.OTHERS);
163             ctx.setAttribute(
164                 ctx.getAttribute(FlowControllerConstants.RESPONSE_PREFIX)
165                     + FlowControllerConstants.OUTPUT_PARAM_STATUS,
166                 FlowControllerConstants.OUTPUT_STATUS_FAILURE);
167             transaction.setStatusCode("401");
168             ctx.setAttribute(ctx.getAttribute(FlowControllerConstants.RESPONSE_PREFIX)
169                 + FlowControllerConstants.OUTPUT_STATUS_MESSAGE, returnParams.getProperty("error-message"));
170         }
171
172         return null;
173         // Change null to required value if required in upper level
174     }
175 }