remove ODL/karaf from common
[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 import org.onap.ccsdk.sli.core.dblib.DbLibService;
28 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicDblibStore;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicGraph;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;
35 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicPropertiesProvider;
36 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicResolver;
37 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceImplBase;
38 import org.onap.logging.ref.slf4j.ONAPLogConstants;
39 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.slf4j.MDC;
43
44 public class SvcLogicServiceImpl extends SvcLogicServiceImplBase implements SvcLogicService {
45
46     private static final Logger LOG = LoggerFactory.getLogger(SvcLogicServiceImpl.class);
47
48     public SvcLogicServiceImpl(SvcLogicPropertiesProvider resourceProvider, SvcLogicResolver resolver)
49             throws SvcLogicException {
50         super(null);
51         this.resolver = resolver;
52         properties = resourceProvider.getProperties();
53         this.store = getStore();
54     }
55
56     public SvcLogicServiceImpl(SvcLogicPropertiesProvider resourceProvider, DbLibService dbSvc,
57             SvcLogicResolver resolver) throws SvcLogicException {
58         super(null);
59         this.resolver = resolver;
60         properties = resourceProvider.getProperties();
61         this.store = new SvcLogicDblibStore(dbSvc);
62     }
63
64     @Override
65     public Properties execute(String module, String rpc, String version, String mode, Properties props)
66             throws SvcLogicException {
67         SvcLogicGraph graph = store.fetch(module, rpc, version, mode);
68
69         if (graph == null) {
70             Properties retProps = new Properties();
71             retProps.setProperty("error-code", "401");
72             retProps.setProperty("error-message",
73                     "No service logic found for [" + module + "," + rpc + "," + version + "," + mode + "]");
74             return (retProps);
75         }
76
77         SvcLogicContext ctx = new SvcLogicContext(props);
78         ctx.setAttribute(CURRENT_GRAPH, graph.toString());
79         // To support legacy code we should not stop populating X-ECOMP-RequestID
80         ctx.setAttribute("X-ECOMP-RequestID", MDC.get(ONAPLogConstants.MDCs.REQUEST_ID));
81         execute(graph, ctx);
82         return (ctx.toProperties());
83     }
84
85     @Override
86     @Deprecated
87     // DomDataBroker is not being used, this should be removed eventually
88     public Properties execute(String module, String rpc, String version, String mode, Properties props,
89             DOMDataBroker domDataBroker) throws SvcLogicException {
90         return (execute(module, rpc, version, mode, props));
91     }
92
93     @Override
94     public SvcLogicStore getStore() throws SvcLogicException {
95         // Create and initialize SvcLogicStore object - used to access
96         // saved service logic.
97         if (store != null) {
98             return store;
99         }
100
101         try {
102             store = SvcLogicStoreFactory.getSvcLogicStore(properties);
103         } catch (Exception e) {
104             throw new ConfigurationException("Could not get service logic store", e);
105
106         }
107
108         try {
109             store.init(properties);
110         } catch (SvcLogicException e) {
111             throw new ConfigurationException("Could not get service logic store", e);
112         }
113
114         return store;
115     }
116
117 }