Refactor dblib
[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 ONAP
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.sli.core.sli;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Properties;
26 import java.util.Set;
27
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
33 import org.w3c.dom.Node;
34 import org.w3c.dom.NodeList;
35 import org.w3c.dom.Text;
36
37
38 public class SvcLogicContext {
39
40         private static final Logger LOG = LoggerFactory
41                         .getLogger(SvcLogicContext.class);
42         
43         private HashMap<String, String> attributes;
44         
45         private DOMDataBroker domDataBroker;
46         
47         private String status = "success";
48         
49         public SvcLogicContext()
50         {
51                 this.attributes = new HashMap<String,String> ();
52                 
53         }
54         
55         public SvcLogicContext(Properties props)
56         {
57                 this.attributes = new HashMap<String, String> ();
58                 
59                 if (props.containsKey("SvcLogic.status"))
60                 {
61                         this.status = props.getProperty("SvcLogic.status");
62                 }
63                 
64                 for (Object nameObj : props.keySet())
65                 {
66                         String propName = (String) nameObj;
67                         attributes.put(propName, props.getProperty(propName));
68                 }
69         }
70         
71         
72         
73         public DOMDataBroker getDomDataBroker() {
74                 return domDataBroker;
75         }
76
77         public void setDomDataBroker(DOMDataBroker domDataBroker) {
78                 this.domDataBroker = domDataBroker;
79         }
80
81         public String getAttribute(String name)
82         {
83                 if (attributes.containsKey(name))
84                 {
85                         return(attributes.get(name));
86                 }
87                 else
88                 {
89                         return(null);
90                 }
91         }
92         
93         public void setAttribute(String name, String value)
94         {
95                 if (value == null) {
96                         if (attributes.containsKey(name)) {
97                                 attributes.remove(name);
98                         }
99                 } else {
100                         attributes.put(name, value);
101                 }
102         }
103         
104         public Set<String> getAttributeKeySet()
105         {
106                 return(attributes.keySet());
107         }
108
109         public String getStatus() {
110                 return status;
111         }
112
113         public void setStatus(String status) {
114                 this.status = status;
115         }
116         
117         public Properties toProperties()
118         {
119                 Properties props = new Properties();
120                 
121                 if (status != null)
122                 {
123                         props.setProperty("SvcLogic.status", status);
124                 }
125                 
126                 for (String attrName : attributes.keySet())
127                 {
128                         String attrVal = attributes.get(attrName);
129                         if (attrVal == null) {
130                                 LOG.warn("attribute " + attrName
131                                                 + "null - setting to empty string");
132                                 props.setProperty(attrName, "");
133                         } else {
134                                 props.setProperty(attrName, attributes.get(attrName));
135                         }
136                 }
137                 
138                 return(props);
139         }
140         
141         public void mergeDocument(String pfx, Document doc) {
142                 String prefix = "";
143                 
144                 if (pfx != null) {
145                         prefix = pfx;
146                 }
147                 
148                 Element root = doc.getDocumentElement();
149                 
150                 mergeElement(prefix, root, null);
151         }
152         
153         public void mergeElement(String pfx, Element element, Map<String, Integer> nodeMap) {
154                 
155                 // In XML, cannot tell the difference between containers and lists.
156                 // So, have to treat each element as both (ugly but necessary).
157                 // We do this by passing a nodeMap to be used to count instance of each tag, 
158                 // which will be used to set _length and to set index 
159                 
160                 LOG.trace("mergeElement("+pfx+","+element.getTagName()+","+nodeMap+")");
161
162                 String curTagName = element.getTagName();
163                 String prefix = curTagName;
164                 
165                 if (pfx != null) {
166                         prefix = pfx + "." + prefix;
167                 }
168                 
169                 int myIdx = 0;
170                 
171                 if (nodeMap != null) {
172                         if (nodeMap.containsKey(curTagName)) {
173                                 myIdx = nodeMap.get(curTagName).intValue();
174                         }
175
176                         nodeMap.put(curTagName, new Integer(myIdx+1));
177                         this.setAttribute(prefix+"_length", ""+(myIdx+1));
178                 }
179                 
180                 NodeList children = element.getChildNodes();
181                 
182                 int numChildren  = children.getLength();
183                 
184                 Map<String, Integer> childMap = new HashMap<String, Integer>();
185                 Map<String, Integer> idxChildMap = new HashMap<String, Integer>();
186                 
187                 for (int i = 0 ; i < numChildren ; i++) {
188                         Node curNode = children.item(i);
189                         
190                         if (curNode instanceof Text) {
191                                 Text curText = (Text) curNode;
192                                 String curTextValue = curText.getTextContent();
193                                 LOG.trace("Setting ctx variable "+prefix+" = "+curTextValue);
194                                 this.setAttribute(prefix, curText.getTextContent());
195                                 
196
197                         } else if (curNode instanceof Element) {
198                                 mergeElement(prefix, (Element) curNode, childMap);
199                                 if (nodeMap != null) {
200
201                                         mergeElement(prefix+"["+myIdx+"]", (Element)curNode, idxChildMap);
202
203                                 }
204                         }
205                 }
206                 
207         }
208         
209         public String resolve(String ctxVarName) {
210
211                 if (ctxVarName.indexOf('[') == -1) {
212                         // Ctx variable contains no arrays
213                         return (this.getAttribute(ctxVarName));
214                 }
215
216                 // Resolve any array references
217                 StringBuffer sbuff = new StringBuffer();
218                 String[] ctxVarParts = ctxVarName.split("\\[");
219                 sbuff.append(ctxVarParts[0]);
220                 for (int i = 1; i < ctxVarParts.length; i++) {
221                         if (ctxVarParts[i].startsWith("$")) {
222                                 int endBracketLoc = ctxVarParts[i].indexOf("]");
223                                 if (endBracketLoc == -1) {
224                                         // Missing end bracket ... give up parsing
225                                         LOG.warn("Variable reference " + ctxVarName
226                                                         + " seems to be missing a ']'");
227                                         return (this.getAttribute(ctxVarName));
228                                 }
229
230                                 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
231                                 String remainder = ctxVarParts[i].substring(endBracketLoc);
232
233                                 sbuff.append("[");
234                                 sbuff.append(this.getAttribute(idxVarName));
235                                 sbuff.append(remainder);
236
237                         } else {
238                                 // Index is not a variable reference
239                                 sbuff.append("[");
240                                 sbuff.append(ctxVarParts[i]);
241                         }
242                 }
243
244                 return (this.getAttribute(sbuff.toString()));
245         }
246
247 }