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