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