SDNC-12 refactoring to support easy mocking
[sdnc/core.git] / sli / provider / src / main / java / org / openecomp / sdnc / sli / provider / SaveNodeExecutor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdnc.sli.provider;
23
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.openecomp.sdnc.sli.SvcLogicContext;
30 import org.openecomp.sdnc.sli.SvcLogicException;
31 import org.openecomp.sdnc.sli.SvcLogicExpression;
32 import org.openecomp.sdnc.sli.SvcLogicNode;
33 import org.openecomp.sdnc.sli.SvcLogicResource;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class SaveNodeExecutor extends SvcLogicNodeExecutor {
38
39         private static final Logger LOG = LoggerFactory
40                         .getLogger(SaveNodeExecutor.class);
41
42         @Override
43         public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node,
44                         SvcLogicContext ctx) throws SvcLogicException {
45
46                 String plugin = SvcLogicExpressionResolver.evaluate(
47                                 node.getAttribute("plugin"), node, ctx);
48                 String resourceType = SvcLogicExpressionResolver.evaluate(
49                                 node.getAttribute("resource"), node, ctx);
50                 String key = SvcLogicExpressionResolver.evaluateAsKey(
51                                 node.getAttribute("key"), node, ctx);
52                 String forceStr = SvcLogicExpressionResolver.evaluate(
53                                 node.getAttribute("force"), node, ctx);
54                 String localOnlyStr = SvcLogicExpressionResolver.evaluate(
55                                 node.getAttribute("local-only"), node, ctx);
56                 String pfx = SvcLogicExpressionResolver.evaluate(
57                                 node.getAttribute("pfx"), node, ctx);
58
59                 boolean force = "true".equalsIgnoreCase(forceStr);
60                 boolean localOnly = "true".equalsIgnoreCase(localOnlyStr);
61
62                 Map<String, String> parmMap = new HashMap<String, String>();
63
64                 Set<Map.Entry<String, SvcLogicExpression>> parmSet = node
65                                 .getParameterSet();
66                 boolean hasParms = false;
67
68                 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = parmSet
69                                 .iterator(); iter.hasNext();) {
70                         hasParms = true;
71                         Map.Entry<String, SvcLogicExpression> curEnt = iter.next();
72                         String curName = curEnt.getKey();
73                         SvcLogicExpression curExpr = curEnt.getValue();
74                         if (curExpr != null) {
75                                 String curExprValue = SvcLogicExpressionResolver.evaluate(
76                                                 curExpr, node, ctx);
77
78                                 LOG.debug("Parameter " + curName + " = "
79                                                 + curExpr.asParsedExpr() + " resolves to "
80                                                 + curExprValue);
81
82                                 parmMap.put(curName, curExprValue);
83                         }
84                 }
85
86                 String outValue = "failure";
87
88                 if (LOG.isDebugEnabled()) {
89                         LOG.debug("save node encountered - looking for resource class "
90                                         + plugin);
91                 }
92
93
94
95         SvcLogicResource resourcePlugin = getSvcLogicResource(plugin);
96
97                         if (resourcePlugin != null) {
98
99                                 try {
100                                         switch (resourcePlugin.save(resourceType, force, localOnly, key,
101                                                         parmMap, pfx, ctx)) {
102                                         case SUCCESS:
103                                                 outValue = "success";
104                                                 break;
105                                         case NOT_FOUND:
106                                                 outValue = "not-found";
107                                                 break;
108                                         case FAILURE:
109                                         default:
110                                                 outValue = "failure";
111                                         }
112                                 } catch (SvcLogicException e) {
113                                         LOG.error("Caught exception from resource plugin", e);
114                                         outValue = "failure";
115                                 }
116                         } else {
117                                 LOG.warn("Could not find SvcLogicResource object for plugin "
118                                                 + plugin);
119                         }
120
121                 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
122                 if (nextNode != null) {
123                         if (LOG.isDebugEnabled()) {
124                                 LOG.debug("about to execute " + outValue + " branch");
125                         }
126                         return (nextNode);
127                 }
128
129                 nextNode = node.getOutcomeValue("Other");
130                 if (nextNode != null) {
131                         if (LOG.isDebugEnabled()) {
132                                 LOG.debug("about to execute Other branch");
133                         }
134                 } else {
135                         if (LOG.isDebugEnabled()) {
136                                 LOG.debug("no "+outValue+" or Other branch found");
137                         }
138                 }
139                 return (nextNode);
140         }
141
142 }