375c63154cac85a4c6eeb43e261560161bb0bb31
[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
57                 SvcLogicResource resourcePlugin = getSvcLogicResource(plugin);
58                         if (resourcePlugin != null) {
59
60                                 try {
61
62                                         switch (resourcePlugin.delete(resourceType, key, ctx)) {
63                                         case SUCCESS:
64                                                 outValue = "success";
65                                                 break;
66                                         case NOT_FOUND:
67                                                 outValue = "not-found";
68                                                 break;
69                                         case FAILURE:
70                                         default:
71                                                 outValue = "failure";
72                                         }
73                                 } catch (SvcLogicException e) {
74                                         LOG.error("Caught exception from resource plugin", e);
75                                         outValue = "failure";
76                                 }
77                         } else {
78                                 LOG.warn("Could not find SvcLogicResource object for plugin "
79                                                 + plugin);
80                         }
81
82
83                 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
84                 if (nextNode != null) {
85                         if (LOG.isDebugEnabled()) {
86                                 LOG.debug("about to execute " + outValue + " branch");
87                         }
88                         return (nextNode);
89                 }
90
91                 nextNode = node.getOutcomeValue("Other");
92                 if (nextNode != null) {
93                         if (LOG.isDebugEnabled()) {
94                                 LOG.debug("about to execute Other branch");
95                         }
96                 } else {
97                         if (LOG.isDebugEnabled()) {
98                                 LOG.debug("no "+outValue+" or Other branch found");
99                         }
100                 }
101                 return (nextNode);
102         }
103         
104     protected SvcLogicResource getSvcLogicResource(String plugin) {
105         BundleContext bctx = FrameworkUtil.getBundle(this.getClass())
106                 .getBundleContext();
107
108         ServiceReference sref = bctx.getServiceReference(plugin);
109         if (sref != null) {
110             SvcLogicResource resourcePlugin = (SvcLogicResource) bctx
111                     .getService(sref);
112             return resourcePlugin;
113         }
114         else {
115             LOG.warn("Could not find service reference object for plugin " + plugin);
116             return null;
117         }
118     }
119
120
121 }