b84fa3efe90ae383374112eb0fe31c160d79f45b
[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     static final String SKIP_MDSAL_UPDATE_PROP = "skip-mdsal-update";
37
38     private final Logger LOG = LoggerFactory
39             .getLogger(GenericResourceApiSvcLogicServiceClient.class);
40
41     private SvcLogicService svcLogic = null;
42
43     public GenericResourceApiSvcLogicServiceClient(SvcLogicService svcLogic)
44     {
45         this.svcLogic = svcLogic;
46     }
47
48     public boolean hasGraph(String module, String rpc, String version, String mode) throws SvcLogicException
49     {
50         return svcLogic.hasGraph(module, rpc, version, mode);
51     }
52
53     public Properties execute(String module, String rpc, String version, String mode, ServiceDataBuilder serviceData)
54             throws SvcLogicException {
55         return execute(module, rpc, version, mode, serviceData, new Properties());
56     }
57
58     public Properties execute(String module, String rpc, String version, String mode, PreloadDataBuilder serviceData)
59             throws SvcLogicException {
60         return execute(module, rpc, version, mode, serviceData, new Properties());
61     }
62
63     public Properties execute(String module,
64                               String rpc,
65                               String version,
66                               String mode,
67                               ServiceDataBuilder serviceData,
68                               Properties properties)
69             throws SvcLogicException {
70
71         Properties props = GenericResourceApiUtil.toProperties(properties, serviceData);
72         printPropsDebugLogs(props, "Parameters passed to SLI");
73
74         Properties respProps = svcLogic.execute(module, rpc, version, mode, props);
75         printPropsDebugLogs(respProps, "Parameters returned by SLI");
76         if (respProps == null
77                 || FAILURE_RESULT.equalsIgnoreCase(respProps.getProperty(SVC_LOGIC_STATUS_KEY))) {
78             return respProps;
79         }
80
81         String skipMdsalUpdate = respProps.getProperty(SKIP_MDSAL_UPDATE_PROP);
82         if ((skipMdsalUpdate == null) || !skipMdsalUpdate.equals("Y")) {
83             GenericResourceApiUtil.toBuilder(respProps, serviceData);
84         } else {
85             LOG.debug("Skipping call to MdsalHelper.toBuilder");
86         }
87
88         return respProps;
89     }
90
91
92     public Properties execute(String module,
93                               String rpc,
94                               String version,
95                               String mode,
96                               PreloadDataBuilder serviceData,
97                               Properties properties)
98             throws SvcLogicException {
99
100         Properties props = GenericResourceApiUtil.toProperties(properties, serviceData);
101         printPropsDebugLogs(props, "Parameters passed to SLI");
102
103         Properties respProps = svcLogic.execute(module, rpc, version, mode, props);
104         printPropsDebugLogs(respProps, "Parameters returned by SLI");
105         if (respProps == null
106                 || FAILURE_RESULT.equalsIgnoreCase(respProps.getProperty(SVC_LOGIC_STATUS_KEY))) {
107             return (respProps);
108         }
109
110         GenericResourceApiUtil.toBuilder(respProps, serviceData);
111
112         return respProps;
113     }
114
115         public Properties execute(String module, String rpc, String version, String mode, Properties properties)
116                         throws SvcLogicException {
117
118                 printPropsDebugLogs(properties, "Parameters passed to SLI");
119
120                 Properties respProps = svcLogic.execute(module, rpc, version, mode, properties);
121                 printPropsDebugLogs(respProps, "Parameters returned by SLI");
122                 if (respProps == null || FAILURE_RESULT.equalsIgnoreCase(respProps.getProperty(SVC_LOGIC_STATUS_KEY))) {
123                         return (respProps);
124                 }
125
126                 return respProps;
127         }
128
129
130     private void printPropsDebugLogs(Properties properties, String msg) {
131         if (!LOG.isDebugEnabled()) {
132             return;
133         }
134         if (properties == null) {
135             LOG.debug(msg, "properties is null");
136             return;
137         }
138
139         LOG.debug(msg);
140         for (Object key : properties.keySet()) {
141             String paramName = (String) key;
142             LOG.debug(paramName + " = " + properties.getProperty(paramName));
143         }
144     }
145 }