d5c7f3bfe4a59f7148cfcb803f9c943a25514653
[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         LOG.info("Fetching service logic from data store");
98         SvcLogicGraph graph = store.fetch(module, rpc, version, mode);
99
100         if (graph == null) {
101             Properties retProps = new Properties();
102             retProps.setProperty("error-code", "401");
103             retProps.setProperty("error-message",
104                     "No service logic found for [" + module + "," + rpc + "," + version + "," + mode + "]");
105             return (retProps);
106         }
107
108         SvcLogicContext ctx = new SvcLogicContext(props);
109         ctx.setAttribute(CURRENT_GRAPH, graph.toString());
110         ctx.setAttribute("X-ECOMP-RequestID", MDC.get("X-ECOMP-RequestID"));
111         ctx.setDomDataBroker(domDataBroker);
112         execute(graph, ctx);
113         return (ctx.toProperties());
114     }
115
116     @Override
117     public SvcLogicStore getStore() throws SvcLogicException {
118         // Create and initialize SvcLogicStore object - used to access
119         // saved service logic.
120         if (store != null) {
121             return store;
122         }
123
124         try {
125             store = SvcLogicStoreFactory.getSvcLogicStore(properties);
126         } catch (Exception e) {
127             throw new ConfigurationException("Could not get service logic store", e);
128
129         }
130
131         try {
132             store.init(properties);
133         } catch (SvcLogicException e) {
134             throw new ConfigurationException("Could not get service logic store", e);
135         }
136
137         return store;
138     }
139
140 }