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