da8386c049657a523d4142aaa3934ec83e18a4bc
[sdnc/core.git] / sli / provider / src / main / java / org / openecomp / sdnc / sli / provider / DeleteNodeExecutor.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 org.openecomp.sdnc.sli.SvcLogicContext;
25 import org.openecomp.sdnc.sli.SvcLogicException;
26 import org.openecomp.sdnc.sli.SvcLogicNode;
27 import org.openecomp.sdnc.sli.SvcLogicResource;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.FrameworkUtil;
30 import org.osgi.framework.ServiceReference;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class DeleteNodeExecutor extends SvcLogicNodeExecutor {
35
36         private static final Logger LOG = LoggerFactory
37                         .getLogger(DeleteNodeExecutor.class);
38         @Override
39         public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node,
40                         SvcLogicContext ctx) throws SvcLogicException {
41
42                 String plugin = SvcLogicExpressionResolver.evaluate(
43                                 node.getAttribute("plugin"), node, ctx);
44                 String resourceType = SvcLogicExpressionResolver.evaluate(
45                                 node.getAttribute("resource"), node, ctx);
46                 String key = SvcLogicExpressionResolver.evaluateAsKey(
47                                 node.getAttribute("key"), node, ctx);
48
49                 String outValue = "failure";
50
51                 if (LOG.isDebugEnabled()) {
52                         LOG.debug("delete node encountered - looking for resource class "
53                                         + plugin);
54                 }
55
56                 BundleContext bctx = FrameworkUtil.getBundle(this.getClass())
57                                 .getBundleContext();
58
59                 ServiceReference sref = bctx.getServiceReference(plugin);
60
61                 if (sref != null) {
62                         SvcLogicResource resourcePlugin = (SvcLogicResource) bctx
63                                         .getService(sref);
64
65                         if (resourcePlugin != null) {
66
67                                 try {
68
69                                         switch (resourcePlugin.delete(resourceType, key, ctx)) {
70                                         case SUCCESS:
71                                                 outValue = "success";
72                                                 break;
73                                         case NOT_FOUND:
74                                                 outValue = "not-found";
75                                                 break;
76                                         case FAILURE:
77                                         default:
78                                                 outValue = "failure";
79                                         }
80                                 } catch (SvcLogicException e) {
81                                         LOG.error("Caught exception from resource plugin", e);
82                                         outValue = "failure";
83                                 }
84                         } else {
85                                 LOG.warn("Could not find SvcLogicResource object for plugin "
86                                                 + plugin);
87                         }
88                 } else {
89                         LOG.warn("Could not find service reference object for plugin "
90                                         + plugin);
91                 }
92
93                 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
94                 if (nextNode != null) {
95                         if (LOG.isDebugEnabled()) {
96                                 LOG.debug("about to execute " + outValue + " branch");
97                         }
98                         return (nextNode);
99                 }
100
101                 nextNode = node.getOutcomeValue("Other");
102                 if (nextNode != null) {
103                         if (LOG.isDebugEnabled()) {
104                                 LOG.debug("about to execute Other branch");
105                         }
106                 } else {
107                         if (LOG.isDebugEnabled()) {
108                                 LOG.debug("no "+outValue+" or Other branch found");
109                         }
110                 }
111                 return (nextNode);
112         }
113
114
115 }