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