e7b3f1be3e7a1f27b1828bcf26d20ae78faceff6
[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.logging.LoggingConstants;
29 import org.onap.appc.logging.LoggingUtils;
30 import org.onap.appc.util.StringHelper;
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 java.text.DateFormat;
39 import java.text.SimpleDateFormat;
40 import java.time.Instant;
41 import java.time.temporal.ChronoUnit;
42 import java.util.Date;
43 import java.util.Properties;
44 import java.util.TimeZone;
45
46 public class AppcProviderClient {
47
48     private final EELFLogger LOG = EELFManager.getInstance().getApplicationLogger();
49     private final 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         } else {
59             // Get SvcLogicService reference
60             ServiceReference sref = bctx.getServiceReference(SvcLogicService.NAME);
61             if (sref != null) {
62                 svcLogic = (SvcLogicService) bctx.getService(sref);
63
64             } else {
65                 LOG.warn("Cannot find service reference for " + SvcLogicService.NAME);
66
67             }
68         }
69     }
70
71     public boolean hasGraph(String module, String rpc, String version, String mode) throws SvcLogicException {
72         LOG.debug(String.format("Checking for graph. %s %s %s %s", module, rpc, version, mode));
73         return (svcLogic.hasGraph(module, rpc, version, mode));
74     }
75
76     public Properties execute(String module, String rpc, String version, String mode, Properties parms)
77         throws SvcLogicException {
78
79         /*
80          * Set properties for Metrics Logger
81          */
82         Date startTimestamp = new Date();
83         Instant startTimestampInstant = startTimestamp.toInstant();
84         String startTimeStr = LoggingUtils.generateTimestampStr(startTimestampInstant);
85         MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, "sli");
86         MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME, "execute");
87         MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, "org.onap.appc.provider.AppcProviderClient");
88
89         LOG.debug("Parameters passed to SLI: " + StringHelper.propertiesToString(parms));
90         metricsLogger.info("Parameters passed to SLI: " + StringHelper.propertiesToString(parms));
91
92         Properties respProps = svcLogic.execute(module, rpc, version, mode, parms);
93
94         /*
95          * Set End time for Metrics Logger
96          */
97         Date endTimestamp = new Date();
98         Instant endTimestampInstant = endTimestamp.toInstant();
99         String endTimeStr = LoggingUtils.generateTimestampStr(endTimestampInstant);
100         long duration = ChronoUnit.MILLIS.between(startTimestampInstant, endTimestampInstant);
101         String durationStr = String.valueOf(duration);
102         MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, startTimeStr);
103         MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, endTimeStr);
104         MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, durationStr);
105
106         LOG.debug("Parameters returned by SLI: " + StringHelper.propertiesToString(respProps));
107         metricsLogger.info("Parameters returned by SLI: " + StringHelper.propertiesToString(respProps));
108
109         return respProps;
110     }
111 }