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