0c497a22b96bf9ba7976713ecc8cfd9fe39d9e52
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / openecomp / appc / provider / AppcProviderClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.provider;
24
25 import org.openecomp.appc.util.StringHelper;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.att.eelf.i18n.EELFResourceManager;
29 import org.openecomp.sdnc.sli.SvcLogicException;
30 import org.openecomp.sdnc.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 static com.att.eelf.configuration.Configuration.*;
37
38 import java.util.Properties;
39 import java.text.DateFormat;
40 import java.text.SimpleDateFormat;
41 import java.util.Date;
42 import java.util.TimeZone;
43
44 public class AppcProviderClient {
45
46         private static EELFLogger LOG = EELFManager.getInstance().getApplicationLogger();
47     private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
48
49     private SvcLogicService svcLogic = null;
50
51     public AppcProviderClient() {
52         BundleContext bctx = FrameworkUtil.getBundle(SvcLogicService.class).getBundleContext();
53         //Handle BundleContext returning null
54         if (bctx == null){
55                 LOG.warn("Cannot find bundle context for " + SvcLogicService.NAME);
56         }
57         else{
58                 // Get SvcLogicService reference
59                 ServiceReference sref = bctx.getServiceReference(SvcLogicService.NAME);
60                 if (sref != null) {
61                     svcLogic = (SvcLogicService) bctx.getService(sref);
62         
63                 } else {
64                     LOG.warn("Cannot find service reference for " + SvcLogicService.NAME);
65         
66                 }
67         }
68     }
69
70     public boolean hasGraph(String module, String rpc, String version, String mode) throws SvcLogicException {
71         LOG.debug(String.format("Checking for graph. %s %s %s %s", module, rpc, version, mode));
72         return (svcLogic.hasGraph(module, rpc, version, mode));
73     }
74
75     public Properties execute(String module, String rpc, String version, String mode, Properties parms)
76         throws SvcLogicException {
77
78         /*
79          * Set End time for Metrics Logger
80          */
81         long startTime = System.currentTimeMillis();
82         TimeZone tz = TimeZone.getTimeZone("UTC");
83         DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
84         df.setTimeZone(tz);
85         String startTimeStr = df.format(new Date());
86         long endTime = System.currentTimeMillis();
87         long duration = endTime - startTime;
88         String endTimeStr = String.valueOf(endTime);
89         String durationStr = String.valueOf(duration);
90         String endTimeStrUTC = df.format(new Date());
91         MDC.put("EndTimestamp", endTimeStrUTC);
92         MDC.put("ElapsedTime", durationStr);
93         MDC.put("TargetEntity", "sli");
94         MDC.put("TargetServiceName", "execute");
95         MDC.put("ClassName", "org.openecomp.appc.provider.AppcProviderClient"); 
96
97         LOG.debug("Parameters passed to SLI: " + StringHelper.propertiesToString(parms));
98         metricsLogger.info("Parameters passed to SLI: " + StringHelper.propertiesToString(parms));
99
100         Properties respProps = svcLogic.execute(module, rpc, version, mode, parms);
101         
102         /*
103          * Set End time for Metrics Logger
104          */
105         endTime = System.currentTimeMillis();
106         duration = endTime - startTime;
107         endTimeStr = String.valueOf(endTime);
108         durationStr = String.valueOf(duration);
109         endTimeStrUTC = df.format(new Date());
110         MDC.put("EndTimestamp", endTimeStrUTC);
111         MDC.put("ElapsedTime", durationStr);
112
113         LOG.debug("Parameters returned by SLI: " + StringHelper.propertiesToString(respProps));
114         metricsLogger.info("Parameters returned by SLI: " + StringHelper.propertiesToString(respProps));
115
116         return respProps;
117     }
118 }