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