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