c260db2a8dbefd536436b593cb49d94ca9ca53bc
[sdnc/core.git] / sli / provider / src / main / java / org / openecomp / sdnc / sli / provider / GetResourceNodeExecutor.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.SvcLogicExpression;
27 import org.openecomp.sdnc.sli.SvcLogicNode;
28 import org.openecomp.sdnc.sli.SvcLogicResource;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.FrameworkUtil;
31 import org.osgi.framework.ServiceReference;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class GetResourceNodeExecutor extends SvcLogicNodeExecutor {
36
37         private static final Logger LOG = LoggerFactory
38                         .getLogger(GetResourceNodeExecutor.class);
39         
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 key = SvcLogicExpressionResolver.evaluateAsKey(
48                                 node.getAttribute("key"), node, ctx);
49                 String pfx = SvcLogicExpressionResolver.evaluate(
50                                 node.getAttribute("pfx"), node, ctx);
51
52                 String localOnlyStr = SvcLogicExpressionResolver.evaluate(
53                                 node.getAttribute("local-only"), node, ctx);
54
55                 // Note: for get-resource, only refresh from A&AI if the DG explicitly set
56                 // local-only to false.  Otherwise, just read from local database.
57                 boolean localOnly = true;
58                 
59                 if ("false".equalsIgnoreCase(localOnlyStr)) {
60                         localOnly = false;
61                 } 
62
63                 SvcLogicExpression selectExpr = node.getAttribute("select");
64                 String select = null;
65
66                 if (selectExpr != null) {
67                         select = SvcLogicExpressionResolver.evaluateAsKey(selectExpr, node,
68                                         ctx);
69                 }
70                 
71                 SvcLogicExpression orderByExpr = node.getAttribute("order-by");
72                 String orderBy = null;
73
74                 if (orderByExpr != null) {
75                         orderBy = SvcLogicExpressionResolver.evaluateAsKey(orderByExpr, node,
76                                         ctx);
77                 }
78
79                 String outValue = "failure";
80
81                 if (LOG.isDebugEnabled()) {
82                         LOG.debug(node.getNodeType()
83                                         + " node encountered - looking for resource class "
84                                         + plugin);
85                 }
86
87
88                         SvcLogicResource resourcePlugin = getSvcLogicResource(plugin);
89
90                         if (resourcePlugin != null) {
91
92                                 try {
93                                         switch (resourcePlugin.query(resourceType, localOnly, select, key,
94                                                         pfx, orderBy, ctx)) {
95                                         case SUCCESS:
96                                                 outValue = "success";
97                                                 break;
98                                         case NOT_FOUND:
99                                                 outValue = "not-found";
100                                                 break;
101                                         case FAILURE:
102                                         default:
103                                                 outValue = "failure";
104                                         }
105                                 } catch (SvcLogicException e) {
106                                         LOG.error("Caught exception from resource plugin", e);
107                                         outValue = "failure";
108                                 }
109                         } else {
110                                 LOG.warn("Could not find SvcLogicResource object for plugin "
111                                                 + plugin);
112                         }
113
114
115                 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
116                 if (nextNode != null) {
117                         if (LOG.isDebugEnabled()) {
118                                 LOG.debug("about to execute " + outValue + " branch");
119                         }
120                         return (nextNode);
121                 }
122
123                 nextNode = node.getOutcomeValue("Other");
124                 if (nextNode != null) {
125                         if (LOG.isDebugEnabled()) {
126                                 LOG.debug("about to execute Other branch");
127                         }
128                 } else {
129                         if (LOG.isDebugEnabled()) {
130
131                                 LOG.debug("no "+outValue+" or Other branch found");
132                         }
133                 }
134                 return (nextNode);
135         }
136         
137     protected SvcLogicResource getSvcLogicResource(String plugin) {
138         BundleContext bctx = FrameworkUtil.getBundle(this.getClass())
139                 .getBundleContext();
140
141         ServiceReference sref = bctx.getServiceReference(plugin);
142         if (sref != null) {
143             SvcLogicResource resourcePlugin = (SvcLogicResource) bctx
144                     .getService(sref);
145             return resourcePlugin;
146         }
147         else {
148             LOG.warn("Could not find service reference object for plugin " + plugin);
149             return null;
150         }
151     }
152
153
154 }