enahance Sonar coverage rate
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / onap / appc / provider / AppcProviderClient.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.provider;
26
27 import org.onap.appc.util.StringHelper;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
31 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
32 import org.osgi.framework.BundleContext;
33 import org.osgi.framework.FrameworkUtil;
34 import org.osgi.framework.ServiceReference;
35 import org.slf4j.MDC;
36
37 import static com.att.eelf.configuration.Configuration.*;
38
39 import java.util.Properties;
40 import java.text.DateFormat;
41 import java.text.SimpleDateFormat;
42 import java.util.Date;
43 import java.util.TimeZone;
44
45 public class AppcProviderClient {
46
47         private static EELFLogger LOG = EELFManager.getInstance().getApplicationLogger();
48     private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
49
50     private SvcLogicService svcLogic = null;
51
52     public AppcProviderClient() {
53         BundleContext bctx = FrameworkUtil.getBundle(SvcLogicService.class).getBundleContext();
54         //Handle BundleContext returning null
55         if (bctx == null){
56                 LOG.warn("Cannot find bundle context for " + SvcLogicService.NAME);
57         }
58         else{
59                 // Get SvcLogicService reference
60                 ServiceReference sref = bctx.getServiceReference(SvcLogicService.NAME);
61                 if (sref != null) {
62                     svcLogic = (SvcLogicService) bctx.getService(sref);
63         
64                 } else {
65                     LOG.warn("Cannot find service reference for " + SvcLogicService.NAME);
66         
67                 }
68         }
69     }
70
71     public boolean hasGraph(String module, String rpc, String version, String mode) throws SvcLogicException {
72         LOG.debug(String.format("Checking for graph. %s %s %s %s", module, rpc, version, mode));
73         return (svcLogic.hasGraph(module, rpc, version, mode));
74     }
75
76     public Properties execute(String module, String rpc, String version, String mode, Properties parms)
77         throws SvcLogicException {
78
79         /*
80          * Set End time for Metrics Logger
81          */
82         long startTime = System.currentTimeMillis();
83         TimeZone tz = TimeZone.getTimeZone("UTC");
84         DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
85         df.setTimeZone(tz);
86         String startTimeStr = df.format(new Date());
87         long endTime = System.currentTimeMillis();
88         long duration = endTime - startTime;
89         String endTimeStr = String.valueOf(endTime);
90         String durationStr = String.valueOf(duration);
91         String endTimeStrUTC = df.format(new Date());
92         MDC.put("EndTimestamp", endTimeStrUTC);
93         MDC.put("ElapsedTime", durationStr);
94         MDC.put("TargetEntity", "sli");
95         MDC.put("TargetServiceName", "execute");
96         MDC.put("ClassName", "org.onap.appc.provider.AppcProviderClient"); 
97
98         LOG.debug("Parameters passed to SLI: " + StringHelper.propertiesToString(parms));
99         metricsLogger.info("Parameters passed to SLI: " + StringHelper.propertiesToString(parms));
100
101         Properties respProps = svcLogic.execute(module, rpc, version, mode, parms);
102         
103         /*
104          * Set End time for Metrics Logger
105          */
106         endTime = System.currentTimeMillis();
107         duration = endTime - startTime;
108         endTimeStr = String.valueOf(endTime);
109         durationStr = String.valueOf(duration);
110         endTimeStrUTC = df.format(new Date());
111         MDC.put("EndTimestamp", endTimeStrUTC);
112         MDC.put("ElapsedTime", durationStr);
113
114         LOG.debug("Parameters returned by SLI: " + StringHelper.propertiesToString(respProps));
115         metricsLogger.info("Parameters returned by SLI: " + StringHelper.propertiesToString(respProps));
116
117         return respProps;
118     }
119 }