2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.onap.ccsdk.sli.core.sli.provider.base;
26 import java.util.HashMap;
28 import java.util.Properties;
30 import org.onap.ccsdk.sli.core.sli.ExitNodeException;
31 import org.onap.ccsdk.sli.core.sli.MetricLogger;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicGraph;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicNode;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 public class SvcLogicServiceImplBase implements SvcLogicServiceBase {
42 protected SvcLogicResolver resolver;
43 protected static final Map<String, AbstractSvcLogicNodeExecutor> BUILTIN_NODES = new HashMap<String, AbstractSvcLogicNodeExecutor>() {
45 put("block", new BlockNodeExecutor());
46 put("call", new CallNodeExecutor());
47 put("configure", new ConfigureNodeExecutor());
48 put("delete", new DeleteNodeExecutor());
49 put("execute", new ExecuteNodeExecutor());
50 put("exists", new ExistsNodeExecutor());
51 put("for", new ForNodeExecutor());
52 put("get-resource", new GetResourceNodeExecutor());
53 put("is-available", new IsAvailableNodeExecutor());
54 put("notify", new NotifyNodeExecutor());
55 put("record", new RecordNodeExecutor());
56 put("release", new ReleaseNodeExecutor());
57 put("reserve", new ReserveNodeExecutor());
58 put("return", new ReturnNodeExecutor());
59 put("save", new SaveNodeExecutor());
60 put("set", new SetNodeExecutor());
61 put("switch", new SwitchNodeExecutor());
62 put("update", new UpdateNodeExecutor());
63 put("break", new BreakNodeExecutor());
64 put("while", new WhileNodeExecutor());
65 put("exit", new ExitNodeExecutor());
69 private static final Logger LOG = LoggerFactory.getLogger(SvcLogicServiceImplBase.class);
70 protected HashMap<String, AbstractSvcLogicNodeExecutor> nodeExecutors = null;
71 protected Properties properties;
72 protected SvcLogicStore store;
73 protected static final String CURRENT_GRAPH="currentGraph";
75 public SvcLogicServiceImplBase(SvcLogicStore store) {
79 protected void registerExecutors() {
80 for (String nodeType : BUILTIN_NODES.keySet()) {
81 registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));
85 public void registerExecutor(String nodeName, AbstractSvcLogicNodeExecutor executor) {
86 if (nodeExecutors == null) {
87 nodeExecutors = new HashMap<>();
89 executor.setResolver(resolver);
90 nodeExecutors.put(nodeName, executor);
93 public void unregisterExecutor(String nodeName) {
94 nodeExecutors.remove(nodeName);
97 public SvcLogicContext execute(SvcLogicGraph graph, SvcLogicContext ctx) throws SvcLogicException {
98 if (nodeExecutors == null) {
102 MDC.put(CURRENT_GRAPH, graph.toString());
104 SvcLogicNode curNode = graph.getRootNode();
105 LOG.info("About to execute graph {}", graph.toString());
107 while (curNode != null) {
108 SvcLogicNode nextNode = executeNode(curNode, ctx);
111 } catch (ExitNodeException e) {
112 LOG.debug("SvcLogicServiceImpl caught ExitNodeException");
114 MDC.remove("nodeId");
115 MDC.remove(CURRENT_GRAPH);
120 public SvcLogicNode executeNode(SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException {
125 LOG.info("About to execute node # {} ({})", node.getNodeId(), node.getNodeType());
127 if (LOG.isDebugEnabled()) {
128 LOG.debug("Executing node " + node.getNodeId() + " of " + node.getGraph().getRpc());
131 AbstractSvcLogicNodeExecutor executor = nodeExecutors.get(node.getNodeType());
133 if (executor != null) {
134 MDC.put("nodeId", node.getNodeId() + " (" + node.getNodeType() + ")");
135 return (executor.execute(this, node, ctx));
137 throw new SvcLogicException("Attempted to execute a node of type " + node.getNodeType() + ", but no executor was registered for this type");
142 public boolean hasGraph(String module, String rpc, String version, String mode) throws SvcLogicException {
143 return (store.hasGraph(module, rpc, version, mode));
147 public Properties execute(String module, String rpc, String version, String mode, Properties props)
148 throws SvcLogicException {
149 LOG.info("Fetching service logic from data store");
150 SvcLogicGraph graph = store.fetch(module, rpc, version, mode);
153 Properties retProps = new Properties();
154 retProps.setProperty("error-code", "401");
155 retProps.setProperty("error-message",
156 "No service logic found for [" + module + "," + rpc + "," + version + "," + mode + "]");
160 SvcLogicContext ctx = new SvcLogicContext(props);
161 ctx.setAttribute(CURRENT_GRAPH, graph.toString());
162 ctx.setAttribute("X-ECOMP-RequestID", MDC.get("X-ECOMP-RequestID"));
164 return (ctx.toProperties());
168 public SvcLogicStore getStore() throws SvcLogicException {