f1c9f61d41abb8863a8b5f559038569a5ce782b2
[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.osgi.framework.BundleContext;
35 import org.osgi.framework.FrameworkUtil;
36 import org.osgi.framework.ServiceReference;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class SaveNodeExecutor extends SvcLogicNodeExecutor {
41
42         private static final Logger LOG = LoggerFactory
43                         .getLogger(SaveNodeExecutor.class);
44
45         @Override
46         public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node,
47                         SvcLogicContext ctx) throws SvcLogicException {
48
49                 String plugin = SvcLogicExpressionResolver.evaluate(
50                                 node.getAttribute("plugin"), node, ctx);
51                 String resourceType = SvcLogicExpressionResolver.evaluate(
52                                 node.getAttribute("resource"), node, ctx);
53                 String key = SvcLogicExpressionResolver.evaluateAsKey(
54                                 node.getAttribute("key"), node, ctx);
55                 String forceStr = SvcLogicExpressionResolver.evaluate(
56                                 node.getAttribute("force"), node, ctx);
57                 String localOnlyStr = SvcLogicExpressionResolver.evaluate(
58                                 node.getAttribute("local-only"), node, ctx);
59                 String pfx = SvcLogicExpressionResolver.evaluate(
60                                 node.getAttribute("pfx"), node, ctx);
61
62                 boolean force = "true".equalsIgnoreCase(forceStr);
63                 boolean localOnly = "true".equalsIgnoreCase(localOnlyStr);
64
65                 Map<String, String> parmMap = new HashMap<String, String>();
66
67                 Set<Map.Entry<String, SvcLogicExpression>> parmSet = node
68                                 .getParameterSet();
69                 boolean hasParms = false;
70
71                 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = parmSet
72                                 .iterator(); iter.hasNext();) {
73                         hasParms = true;
74                         Map.Entry<String, SvcLogicExpression> curEnt = iter.next();
75                         String curName = curEnt.getKey();
76                         SvcLogicExpression curExpr = curEnt.getValue();
77                         if (curExpr != null) {
78                                 String curExprValue = SvcLogicExpressionResolver.evaluate(
79                                                 curExpr, node, ctx);
80
81                                 LOG.debug("Parameter " + curName + " = "
82                                                 + curExpr.asParsedExpr() + " resolves to "
83                                                 + curExprValue);
84
85                                 parmMap.put(curName, curExprValue);
86                         }
87                 }
88
89                 String outValue = "failure";
90
91                 if (LOG.isDebugEnabled()) {
92                         LOG.debug("save node encountered - looking for resource class "
93                                         + plugin);
94                 }
95
96                 BundleContext bctx = FrameworkUtil.getBundle(this.getClass())
97                                 .getBundleContext();
98
99                 ServiceReference sref = bctx.getServiceReference(plugin);
100
101                 if (sref != null) {
102                         SvcLogicResource resourcePlugin = (SvcLogicResource) bctx
103                                         .getService(sref);
104
105                         if (resourcePlugin != null) {
106
107                                 try {
108                                         switch (resourcePlugin.save(resourceType, force, localOnly, key,
109                                                         parmMap, pfx, ctx)) {
110                                         case SUCCESS:
111                                                 outValue = "success";
112                                                 break;
113                                         case NOT_FOUND:
114                                                 outValue = "not-found";
115                                                 break;
116                                         case FAILURE:
117                                         default:
118                                                 outValue = "failure";
119                                         }
120                                 } catch (SvcLogicException e) {
121                                         LOG.error("Caught exception from resource plugin", e);
122                                         outValue = "failure";
123                                 }
124                         } else {
125                                 LOG.warn("Could not find SvcLogicResource object for plugin "
126                                                 + plugin);
127                         }
128                 } else {
129                         LOG.warn("Could not find service reference object for plugin "
130                                         + plugin);
131                 }
132
133                 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
134                 if (nextNode != null) {
135                         if (LOG.isDebugEnabled()) {
136                                 LOG.debug("about to execute " + outValue + " branch");
137                         }
138                         return (nextNode);
139                 }
140
141                 nextNode = node.getOutcomeValue("Other");
142                 if (nextNode != null) {
143                         if (LOG.isDebugEnabled()) {
144                                 LOG.debug("about to execute Other branch");
145                         }
146                 } else {
147                         if (LOG.isDebugEnabled()) {
148                                 LOG.debug("no "+outValue+" or Other branch found");
149                         }
150                 }
151                 return (nextNode);
152         }
153
154 }