Update groupId to org.onap.ccsdk.sli
[ccsdk/sli/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.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class GetResourceNodeExecutor extends SvcLogicNodeExecutor {
33
34         private static final Logger LOG = LoggerFactory
35                         .getLogger(GetResourceNodeExecutor.class);
36         
37         public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node,
38                         SvcLogicContext ctx) throws SvcLogicException {
39
40                 String plugin = SvcLogicExpressionResolver.evaluate(
41                                 node.getAttribute("plugin"), node, ctx);
42                 String resourceType = SvcLogicExpressionResolver.evaluate(
43                                 node.getAttribute("resource"), node, ctx);
44                 String key = SvcLogicExpressionResolver.evaluateAsKey(
45                                 node.getAttribute("key"), node, ctx);
46                 String pfx = SvcLogicExpressionResolver.evaluate(
47                                 node.getAttribute("pfx"), node, ctx);
48
49                 String localOnlyStr = SvcLogicExpressionResolver.evaluate(
50                                 node.getAttribute("local-only"), node, ctx);
51
52                 // Note: for get-resource, only refresh from A&AI if the DG explicitly set
53                 // local-only to false.  Otherwise, just read from local database.
54                 boolean localOnly = true;
55                 
56                 if ("false".equalsIgnoreCase(localOnlyStr)) {
57                         localOnly = false;
58                 } 
59
60                 SvcLogicExpression selectExpr = node.getAttribute("select");
61                 String select = null;
62
63                 if (selectExpr != null) {
64                         select = SvcLogicExpressionResolver.evaluateAsKey(selectExpr, node,
65                                         ctx);
66                 }
67                 
68                 SvcLogicExpression orderByExpr = node.getAttribute("order-by");
69                 String orderBy = null;
70
71                 if (orderByExpr != null) {
72                         orderBy = SvcLogicExpressionResolver.evaluateAsKey(orderByExpr, node,
73                                         ctx);
74                 }
75
76                 String outValue = "failure";
77
78                 if (LOG.isDebugEnabled()) {
79                         LOG.debug(node.getNodeType()
80                                         + " node encountered - looking for resource class "
81                                         + plugin);
82                 }
83
84
85                         SvcLogicResource resourcePlugin = getSvcLogicResource(plugin);
86
87                         if (resourcePlugin != null) {
88
89                                 try {
90                                         switch (resourcePlugin.query(resourceType, localOnly, select, key,
91                                                         pfx, orderBy, ctx)) {
92                                         case SUCCESS:
93                                                 outValue = "success";
94                                                 break;
95                                         case NOT_FOUND:
96                                                 outValue = "not-found";
97                                                 break;
98                                         case FAILURE:
99                                         default:
100                                                 outValue = "failure";
101                                         }
102                                 } catch (SvcLogicException e) {
103                                         LOG.error("Caught exception from resource plugin", e);
104                                         outValue = "failure";
105                                 }
106                         } else {
107                                 LOG.warn("Could not find SvcLogicResource object for plugin "
108                                                 + plugin);
109                         }
110
111
112                 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
113                 if (nextNode != null) {
114                         if (LOG.isDebugEnabled()) {
115                                 LOG.debug("about to execute " + outValue + " branch");
116                         }
117                         return (nextNode);
118                 }
119
120                 nextNode = node.getOutcomeValue("Other");
121                 if (nextNode != null) {
122                         if (LOG.isDebugEnabled()) {
123                                 LOG.debug("about to execute Other branch");
124                         }
125                 } else {
126                         if (LOG.isDebugEnabled()) {
127
128                                 LOG.debug("no "+outValue+" or Other branch found");
129                         }
130                 }
131                 return (nextNode);
132         }
133
134 }