refactor sli-provider
[ccsdk/sli/core.git] / sli / provider-base / src / main / java / org / onap / ccsdk / sli / core / sli / provider / base / SaveNodeExecutor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
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.onap.ccsdk.sli.core.sli.provider.base;
23
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.Set;
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 AbstractSvcLogicNodeExecutor {
37
38     private static final Logger LOG = LoggerFactory.getLogger(SaveNodeExecutor.class);
39
40     @Override
41     public SvcLogicNode execute(SvcLogicServiceBase svc, SvcLogicNode node, SvcLogicContext ctx)
42             throws SvcLogicException {
43
44         String plugin = SvcLogicExpressionResolver.evaluate(node.getAttribute("plugin"), node, ctx);
45         String resourceType = SvcLogicExpressionResolver.evaluate(node.getAttribute("resource"), node, ctx);
46         String key = SvcLogicExpressionResolver.evaluateAsKey(node.getAttribute("key"), node, ctx);
47         String forceStr = SvcLogicExpressionResolver.evaluate(node.getAttribute("force"), node, ctx);
48         String localOnlyStr = SvcLogicExpressionResolver.evaluate(node.getAttribute("local-only"), node, ctx);
49         String pfx = SvcLogicExpressionResolver.evaluate(node.getAttribute("pfx"), node, ctx);
50
51         boolean force = "true".equalsIgnoreCase(forceStr);
52         boolean localOnly = "true".equalsIgnoreCase(localOnlyStr);
53
54         Map<String, String> parmMap = new HashMap<String, String>();
55
56         Set<Map.Entry<String, SvcLogicExpression>> parmSet = node.getParameterSet();
57         boolean hasParms = false;
58
59         for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = parmSet.iterator(); iter.hasNext();) {
60             hasParms = true;
61             Map.Entry<String, SvcLogicExpression> curEnt = iter.next();
62             String curName = curEnt.getKey();
63             SvcLogicExpression curExpr = curEnt.getValue();
64             if (curExpr != null) {
65                 String curExprValue = SvcLogicExpressionResolver.evaluate(curExpr, node, ctx);
66
67                 LOG.debug("Parameter " + curName + " = " + curExpr.asParsedExpr() + " resolves to " + curExprValue);
68
69                 parmMap.put(curName, curExprValue);
70             }
71         }
72
73         String outValue = "failure";
74
75         if (LOG.isDebugEnabled()) {
76             LOG.debug("save node encountered - looking for resource class " + plugin);
77         }
78
79
80
81         SvcLogicResource resourcePlugin = getSvcLogicResource(plugin);
82
83         if (resourcePlugin != null) {
84
85             try {
86                 switch (resourcePlugin.save(resourceType, force, localOnly, key, parmMap, pfx, ctx)) {
87                     case SUCCESS:
88                         outValue = "success";
89                         break;
90                     case NOT_FOUND:
91                         outValue = "not-found";
92                         break;
93                     case FAILURE:
94                     default:
95                         outValue = "failure";
96                 }
97             } catch (SvcLogicException e) {
98                 LOG.error("Caught exception from resource plugin", e);
99                 outValue = "failure";
100             }
101         } else {
102             LOG.warn("Could not find SvcLogicResource object for plugin " + plugin);
103         }
104         return (getNextNode(node, outValue));
105     }
106
107 }