f850bc4c8eb005b56b36211cd17b2a433d743f0f
[ccsdk/sli.git] /
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.base;
25
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Properties;
29
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;
39 import org.slf4j.MDC;
40
41 public class SvcLogicServiceImplBase implements SvcLogicServiceBase {
42         protected SvcLogicResolver resolver;
43     protected static final Map<String, AbstractSvcLogicNodeExecutor> BUILTIN_NODES = new HashMap<String, AbstractSvcLogicNodeExecutor>() {
44         {
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());
66         }
67     };
68
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";
74
75     public SvcLogicServiceImplBase(SvcLogicStore store) {
76         this.store = store;
77     }
78
79     protected void registerExecutors() {
80
81         LOG.info("Entered register executors");
82         for (String nodeType : BUILTIN_NODES.keySet()) {
83             LOG.info("SLI - registering node executor for node type " + nodeType);
84             registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));
85         }
86         LOG.info("Done registerExecutors");
87     }
88
89     public void registerExecutor(String nodeName, AbstractSvcLogicNodeExecutor executor) {
90         if (nodeExecutors == null) {
91             nodeExecutors = new HashMap<>();
92         }
93         LOG.info("SLI - registering executor for node type {}", nodeName);
94         executor.setResolver(resolver);
95         nodeExecutors.put(nodeName, executor);
96     }
97
98     public void unregisterExecutor(String nodeName) {
99         LOG.info("SLI - unregistering executor for node type {}", nodeName);
100         nodeExecutors.remove(nodeName);
101     }
102
103     public SvcLogicContext execute(SvcLogicGraph graph, SvcLogicContext ctx) throws SvcLogicException {
104         if (nodeExecutors == null) {
105             registerExecutors();
106         }
107
108         // Set service name in MDC to reference current working directed graph
109         MDC.put(MetricLogger.SERVICE_NAME, graph.getModule() + ":" + graph.getRpc() + "/v" + graph.getVersion());
110
111         MDC.put(CURRENT_GRAPH, graph.toString());
112
113         SvcLogicNode curNode = graph.getRootNode();
114         LOG.info("About to execute graph {}", graph.toString());
115                 try {
116                         while (curNode != null) {
117                                 SvcLogicNode nextNode = executeNode(curNode, ctx);
118                                 curNode = nextNode;
119                         }
120                 } catch (ExitNodeException e) {
121             LOG.debug("SvcLogicServiceImpl caught ExitNodeException");
122                 }
123         MDC.remove("nodeId");
124         MDC.remove(CURRENT_GRAPH);
125
126         return (ctx);
127     }
128     
129         public SvcLogicNode executeNode(SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException {
130         if (node == null) {
131             return (null);
132         }
133
134                 LOG.info("About to execute node # {} ({})", node.getNodeId(), node.getNodeType());
135
136         if (LOG.isDebugEnabled()) {
137             LOG.debug("Executing node " + node.getNodeId() + " of " + node.getGraph().getRpc());
138         }
139
140         AbstractSvcLogicNodeExecutor executor = nodeExecutors.get(node.getNodeType());
141
142         if (executor != null) {
143             LOG.debug("Executing node executor for node type {} - {}", node.getNodeType(),
144                     executor.getClass().getName());
145
146                 MDC.put("nodeId", node.getNodeId() + " (" + node.getNodeType() + ")");
147             return (executor.execute(this, node, ctx));
148         } else {
149             throw new SvcLogicException("Attempted to execute a node of type " + node.getNodeType() + ", but no executor was registered for this type");
150         }
151     }
152
153     @Override
154     public boolean hasGraph(String module, String rpc, String version, String mode) throws SvcLogicException {
155         return (store.hasGraph(module, rpc, version, mode));
156     }
157
158     @Override
159         public Properties execute(String module, String rpc, String version, String mode, Properties props)
160                         throws SvcLogicException {
161                 LOG.info("Fetching service logic from data store");
162                 SvcLogicGraph graph = store.fetch(module, rpc, version, mode);
163
164                 if (graph == null) {
165                         Properties retProps = new Properties();
166                         retProps.setProperty("error-code", "401");
167                         retProps.setProperty("error-message",
168                                         "No service logic found for [" + module + "," + rpc + "," + version + "," + mode + "]");
169                         return (retProps);
170                 }
171
172                 SvcLogicContext ctx = new SvcLogicContext(props);
173                 ctx.setAttribute(CURRENT_GRAPH, graph.toString());
174                 ctx.setAttribute("X-ECOMP-RequestID", MDC.get("X-ECOMP-RequestID"));
175                 execute(graph, ctx);
176                 return (ctx.toProperties());
177         }
178
179         @Override
180         public SvcLogicStore getStore() throws SvcLogicException {
181                 return this.store;
182         }
183
184 }