b000ed478aa37207918ac3e4bf0c612ddde9d7b7
[sdnc/northbound.git] / generic-resource-api / provider / src / main / java / org / onap / sdnc / northbound / GenericResourceApiSvcLogicServiceClient.java
1 /*-
2 z * ============LICENSE_START=======================================================
3  * openECOMP : SDN-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.onap.sdnc.northbound;
23
24 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
25 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
26 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.preload.data.PreloadDataBuilder;
27 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.data.ServiceDataBuilder;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.util.Properties;
32
33 public class GenericResourceApiSvcLogicServiceClient {
34     static final String FAILURE_RESULT = "failure";
35     static final String SVC_LOGIC_STATUS_KEY = "SvcLogic.status";
36
37     private final Logger LOG = LoggerFactory
38             .getLogger(GenericResourceApiSvcLogicServiceClient.class);
39
40     private SvcLogicService svcLogic = null;
41
42     public GenericResourceApiSvcLogicServiceClient(SvcLogicService svcLogic)
43     {
44         this.svcLogic = svcLogic;
45     }
46
47     public boolean hasGraph(String module, String rpc, String version, String mode) throws SvcLogicException
48     {
49         return svcLogic.hasGraph(module, rpc, version, mode);
50     }
51
52     public Properties execute(String module, String rpc, String version, String mode, ServiceDataBuilder serviceData)
53             throws SvcLogicException {
54         return execute(module, rpc, version, mode, serviceData, new Properties());
55     }
56
57     public Properties execute(String module, String rpc, String version, String mode, PreloadDataBuilder serviceData)
58             throws SvcLogicException {
59         return execute(module, rpc, version, mode, serviceData, new Properties());
60     }
61
62     public Properties execute(String module,
63                               String rpc,
64                               String version,
65                               String mode,
66                               ServiceDataBuilder serviceData,
67                               Properties properties)
68             throws SvcLogicException {
69
70         Properties props = GenericResourceApiUtil.toProperties(properties, serviceData);
71         printPropsDebugLogs(props, "Parameters passed to SLI");
72
73         Properties respProps = svcLogic.execute(module, rpc, version, mode, props);
74         printPropsDebugLogs(respProps, "Parameters returned by SLI");
75         if (respProps == null
76                 || FAILURE_RESULT.equalsIgnoreCase(respProps.getProperty(SVC_LOGIC_STATUS_KEY))) {
77             return respProps;
78         }
79
80         GenericResourceApiUtil.toBuilder(respProps, serviceData);
81
82         return respProps;
83     }
84
85
86     public Properties execute(String module,
87                               String rpc,
88                               String version,
89                               String mode,
90                               PreloadDataBuilder serviceData,
91                               Properties properties)
92             throws SvcLogicException {
93
94         Properties props = GenericResourceApiUtil.toProperties(properties, serviceData);
95         printPropsDebugLogs(props, "Parameters passed to SLI");
96
97         Properties respProps = svcLogic.execute(module, rpc, version, mode, props);
98         printPropsDebugLogs(respProps, "Parameters returned by SLI");
99         if (respProps == null
100                 || FAILURE_RESULT.equalsIgnoreCase(respProps.getProperty(SVC_LOGIC_STATUS_KEY))) {
101             return (respProps);
102         }
103
104         GenericResourceApiUtil.toBuilder(respProps, serviceData);
105
106         return respProps;
107     }
108
109         public Properties execute(String module, String rpc, String version, String mode, Properties properties)
110                         throws SvcLogicException {
111
112                 printPropsDebugLogs(properties, "Parameters passed to SLI");
113
114                 Properties respProps = svcLogic.execute(module, rpc, version, mode, properties);
115                 printPropsDebugLogs(respProps, "Parameters returned by SLI");
116                 if (respProps == null || FAILURE_RESULT.equalsIgnoreCase(respProps.getProperty(SVC_LOGIC_STATUS_KEY))) {
117                         return (respProps);
118                 }
119
120                 return respProps;
121         }
122
123
124     private void printPropsDebugLogs(Properties properties, String msg) {
125         if (!LOG.isDebugEnabled()) {
126             return;
127         }
128         if (properties == null) {
129             LOG.debug(msg, "properties is null");
130             return;
131         }
132
133         LOG.debug(msg);
134         for (Object key : properties.keySet()) {
135             String paramName = (String) key;
136             LOG.debug(paramName, " = ", properties.getProperty(paramName));
137         }
138     }
139 }