logging changes
[ccsdk/sli/core.git] / sli / provider / src / main / java / org / onap / ccsdk / sli / core / sli / provider / SvcLogicServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                         reserved.
7  * ================================================================================
8  *  Modifications Copyright (C) 2018 IBM.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.ccsdk.sli.core.sli.provider;
25
26 import java.util.Properties;
27
28 import org.onap.ccsdk.sli.core.dblib.DbLibService;
29 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicDblibStore;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicGraph;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;
36 import org.onap.ccsdk.sli.core.sli.provider.base.AbstractSvcLogicNodeExecutor;
37 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicPropertiesProvider;
38 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceImplBase;
39 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.ServiceReference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.slf4j.MDC;
45
46 public class SvcLogicServiceImpl extends SvcLogicServiceImplBase implements SvcLogicService {
47
48     private static final Logger LOG = LoggerFactory.getLogger(SvcLogicServiceImpl.class);
49     protected BundleContext bctx = null;
50
51     public SvcLogicServiceImpl(SvcLogicPropertiesProvider resourceProvider) throws SvcLogicException {
52         super(null);
53         this.resolver = SvcLogicClassResolver.getInstance();
54         properties = resourceProvider.getProperties();
55         this.store = getStore();
56     }
57
58     public SvcLogicServiceImpl(SvcLogicPropertiesProvider resourceProvider, DbLibService dbSvc)
59             throws SvcLogicException {
60         super(null);
61         this.resolver = SvcLogicClassResolver.getInstance();
62         properties = resourceProvider.getProperties();
63         this.store = new SvcLogicDblibStore(dbSvc);
64     }
65
66     public void registerExecutor(ServiceReference sr) {
67         String nodeName = (String) sr.getProperty("nodeType");
68         if (nodeName != null) {
69             AbstractSvcLogicNodeExecutor executor;
70             try {
71                 executor = (AbstractSvcLogicNodeExecutor) bctx.getService(sr);
72             } catch (Exception e) {
73                 LOG.error("Cannot get service executor for {}", nodeName, e);
74                 return;
75             }
76             registerExecutor(nodeName, executor);
77         }
78     }
79
80     public void unregisterExecutor(ServiceReference sr) {
81         String nodeName = (String) sr.getProperty("nodeType");
82
83         if (nodeName != null) {
84             unregisterExecutor(nodeName);
85         }
86     }
87
88     @Override
89     public Properties execute(String module, String rpc, String version, String mode, Properties props)
90             throws SvcLogicException {
91         return (execute(module, rpc, version, mode, props, null));
92     }
93
94     @Override
95     public Properties execute(String module, String rpc, String version, String mode, Properties props,
96             DOMDataBroker domDataBroker) throws SvcLogicException {
97         SvcLogicGraph graph = store.fetch(module, rpc, version, mode);
98
99         if (graph == null) {
100             Properties retProps = new Properties();
101             retProps.setProperty("error-code", "401");
102             retProps.setProperty("error-message",
103                     "No service logic found for [" + module + "," + rpc + "," + version + "," + mode + "]");
104             return (retProps);
105         }
106
107         SvcLogicContext ctx = new SvcLogicContext(props);
108         ctx.setAttribute(CURRENT_GRAPH, graph.toString());
109         ctx.setAttribute("X-ECOMP-RequestID", MDC.get("X-ECOMP-RequestID"));
110         ctx.setDomDataBroker(domDataBroker);
111         execute(graph, ctx);
112         return (ctx.toProperties());
113     }
114
115     @Override
116     public SvcLogicStore getStore() throws SvcLogicException {
117         // Create and initialize SvcLogicStore object - used to access
118         // saved service logic.
119         if (store != null) {
120             return store;
121         }
122
123         try {
124             store = SvcLogicStoreFactory.getSvcLogicStore(properties);
125         } catch (Exception e) {
126             throw new ConfigurationException("Could not get service logic store", e);
127
128         }
129
130         try {
131             store.init(properties);
132         } catch (SvcLogicException e) {
133             throw new ConfigurationException("Could not get service logic store", e);
134         }
135
136         return store;
137     }
138
139 }