remove ODL/karaf from common
[ccsdk/sli/core.git] / sli / common / src / main / java / org / onap / ccsdk / sli / core / sli / SvcLogicContext.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
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.onap.ccsdk.sli.core.sli;
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Properties;
27 import java.util.Set;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Element;
32 import org.w3c.dom.Node;
33 import org.w3c.dom.NodeList;
34 import org.w3c.dom.Text;
35
36
37 public class SvcLogicContext {
38
39         private static final Logger LOG = LoggerFactory.getLogger(SvcLogicContext.class);
40
41         private HashMap<String, String> attributes;
42
43         private String status = "success";
44
45         public SvcLogicContext()
46         {
47                 this.attributes = new HashMap<> ();
48
49         }
50
51         public SvcLogicContext(Properties props)
52         {
53                 this.attributes = new HashMap<> ();
54
55                 if (props.containsKey(CommonConstants.SERVICE_LOGIC_STATUS))
56                 {
57                         this.status = props.getProperty(CommonConstants.SERVICE_LOGIC_STATUS);
58                 }
59
60                 for (Object nameObj : props.keySet())
61                 {
62                         String propName = (String) nameObj;
63                         attributes.put(propName, props.getProperty(propName));
64                 }
65         }
66
67         public String getAttribute(String name)
68         {
69                 if (attributes.containsKey(name))
70                 {
71                         return attributes.get(name);
72                 }
73                 else
74                 {
75                         return null;
76                 }
77         }
78
79         public void setAttribute(String name, String value)
80         {
81                 if (value == null) {
82                         if (attributes.containsKey(name)) {
83                                 attributes.remove(name);
84                         }
85                 } else {
86                         attributes.put(name, value);
87                 }
88         }
89
90         public Set<String> getAttributeKeySet()
91         {
92                 return attributes.keySet();
93         }
94
95         public String getStatus() {
96                 return status;
97         }
98
99         public void setStatus(String status) {
100                 this.status = status;
101         }
102
103         public Properties toProperties()
104         {
105                 Properties props = new Properties();
106
107                 if (status != null)
108                 {
109                         props.setProperty(CommonConstants.SERVICE_LOGIC_STATUS, status);
110                 }
111
112                 String attrName;
113                 String attrVal;
114                 for (Map.Entry<String, String> entry : attributes.entrySet())
115                 {
116                         attrName = entry.getKey();
117                         attrVal = entry.getValue();
118                         if (attrVal == null) {
119                                 LOG.warn("attribute {} value is null - setting to empty string", attrName);
120                                 props.setProperty(attrName, "");
121                         } else {
122                                 props.setProperty(attrName, attrVal);
123                         }
124                 }
125
126                 return props;
127         }
128
129         public void mergeDocument(String pfx, Document doc) {
130                 String prefix = "";
131
132                 if (pfx != null) {
133                         prefix = pfx;
134                 }
135
136                 Element root = doc.getDocumentElement();
137
138                 mergeElement(prefix, root, null);
139         }
140
141         public void mergeElement(String pfx, Element element, Map<String, Integer> nodeMap) {
142
143                 // In XML, cannot tell the difference between containers and lists.
144                 // So, have to treat each element as both (ugly but necessary).
145                 // We do this by passing a nodeMap to be used to count instance of each tag,
146                 // which will be used to set _length and to set index
147
148                 LOG.trace("mergeElement({},{},{})", pfx, element.getTagName(), nodeMap);
149
150                 String curTagName = element.getTagName();
151                 String prefix = curTagName;
152
153                 if (pfx != null) {
154                         prefix = pfx + "." + prefix;
155                 }
156
157                 int myIdx = 0;
158
159                 if (nodeMap != null) {
160                         if (nodeMap.containsKey(curTagName)) {
161                                 myIdx = nodeMap.get(curTagName);
162                         }
163
164                         nodeMap.put(curTagName, myIdx+1);
165                         this.setAttribute(prefix+"_length", Integer.toString(myIdx+1));
166                 }
167
168                 NodeList children = element.getChildNodes();
169
170                 int numChildren  = children.getLength();
171
172                 Map<String, Integer> childMap = new HashMap<>();
173                 Map<String, Integer> idxChildMap = new HashMap<>();
174
175                 for (int i = 0 ; i < numChildren ; i++) {
176                         Node curNode = children.item(i);
177
178                         if (curNode instanceof Text) {
179                                 Text curText = (Text) curNode;
180                                 String curTextValue = curText.getTextContent();
181                                 LOG.trace("Setting ctx variable {} = {}", prefix, curTextValue);
182                                 this.setAttribute(prefix, curText.getTextContent());
183
184
185                         } else if (curNode instanceof Element) {
186                                 mergeElement(prefix, (Element) curNode, childMap);
187                                 if (nodeMap != null) {
188
189                                         mergeElement(prefix+"["+myIdx+"]", (Element)curNode, idxChildMap);
190
191                                 }
192                         }
193                 }
194
195         }
196
197         public String resolve(String ctxVarName) {
198
199                 if (ctxVarName.indexOf('[') == -1) {
200                         // Ctx variable contains no arrays
201                         return getAttribute(ctxVarName);
202                 }
203
204                 // Resolve any array references
205                 StringBuilder sbuff = new StringBuilder();
206                 String[] ctxVarParts = ctxVarName.split("\\[");
207                 sbuff.append(ctxVarParts[0]);
208                 for (int i = 1; i < ctxVarParts.length; i++) {
209                         if (ctxVarParts[i].startsWith("$")) {
210                                 int endBracketLoc = ctxVarParts[i].indexOf(']');
211                                 if (endBracketLoc == -1) {
212                                         // Missing end bracket ... give up parsing
213                                         LOG.warn("Variable reference {} seems to be missing a ']'", ctxVarName);
214                                         return getAttribute(ctxVarName);
215                                 }
216
217                                 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
218                                 String remainder = ctxVarParts[i].substring(endBracketLoc);
219
220                                 sbuff.append("[");
221                                 sbuff.append(this.getAttribute(idxVarName));
222                                 sbuff.append(remainder);
223
224                         } else {
225                                 // Index is not a variable reference
226                                 sbuff.append("[");
227                                 sbuff.append(ctxVarParts[i]);
228                         }
229                 }
230
231                 return getAttribute(sbuff.toString());
232         }
233
234 }