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