71fdf3a8ff9330ccc133114e71b8dd190a76f742
[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                 BundleContext bctx = FrameworkUtil.getBundle(this.getClass())
88                                 .getBundleContext();
89
90                 ServiceReference sref = bctx.getServiceReference(plugin);
91
92                 if (sref != null) {
93                         SvcLogicResource resourcePlugin = (SvcLogicResource) bctx
94                                         .getService(sref);
95
96                         if (resourcePlugin != null) {
97
98                                 try {
99                                         switch (resourcePlugin.query(resourceType, localOnly, select, key,
100                                                         pfx, orderBy, ctx)) {
101                                         case SUCCESS:
102                                                 outValue = "success";
103                                                 break;
104                                         case NOT_FOUND:
105                                                 outValue = "not-found";
106                                                 break;
107                                         case FAILURE:
108                                         default:
109                                                 outValue = "failure";
110                                         }
111                                 } catch (SvcLogicException e) {
112                                         LOG.error("Caught exception from resource plugin", e);
113                                         outValue = "failure";
114                                 }
115                         } else {
116                                 LOG.warn("Could not find SvcLogicResource object for plugin "
117                                                 + plugin);
118                         }
119                 } else {
120                         LOG.warn("Cound not find service reference for plugin " + plugin);
121                 }
122
123                 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
124                 if (nextNode != null) {
125                         if (LOG.isDebugEnabled()) {
126                                 LOG.debug("about to execute " + outValue + " branch");
127                         }
128                         return (nextNode);
129                 }
130
131                 nextNode = node.getOutcomeValue("Other");
132                 if (nextNode != null) {
133                         if (LOG.isDebugEnabled()) {
134                                 LOG.debug("about to execute Other branch");
135                         }
136                 } else {
137                         if (LOG.isDebugEnabled()) {
138
139                                 LOG.debug("no "+outValue+" or Other branch found");
140                         }
141                 }
142                 return (nextNode);
143         }
144
145
146 }