aboutsummaryrefslogtreecommitdiffstats
path: root/sli/common/src/main/java/org/openecomp/sdnc/sli/SvcLogicContext.java
blob: 79082af298b80092d9f07d7f7d680a369d955290 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : SDN-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 						reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdnc.sli;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;


public class SvcLogicContext {

	private static final Logger LOG = LoggerFactory
			.getLogger(SvcLogicContext.class);
	
	private HashMap<String, String> attributes;
	
	private DOMDataBroker domDataBroker;
	
	private String status = "success";
	
	public SvcLogicContext()
	{
		this.attributes = new HashMap<String,String> ();
		
	}
	
	public SvcLogicContext(Properties props)
	{
		this.attributes = new HashMap<String, String> ();
		
		if (props.containsKey("SvcLogic.status"))
		{
			this.status = props.getProperty("SvcLogic.status");
		}
		
		for (Object nameObj : props.keySet())
		{
			String propName = (String) nameObj;
			attributes.put(propName, props.getProperty(propName));
		}
	}
	
	
	
	public DOMDataBroker getDomDataBroker() {
		return domDataBroker;
	}

	public void setDomDataBroker(DOMDataBroker domDataBroker) {
		this.domDataBroker = domDataBroker;
	}

	public String getAttribute(String name)
	{
		if (attributes.containsKey(name))
		{
			return(attributes.get(name));
		}
		else
		{
			return(null);
		}
	}
	
	public void setAttribute(String name, String value)
	{
		if (value == null) {
			if (attributes.containsKey(name)) {
				attributes.remove(name);
			}
		} else {
			attributes.put(name, value);
		}
	}
	
	public Set<String> getAttributeKeySet()
	{
		return(attributes.keySet());
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	
	public Properties toProperties()
	{
		Properties props = new Properties();
		
		if (status != null)
		{
			props.setProperty("SvcLogic.status", status);
		}
		
		for (String attrName : attributes.keySet())
		{
			String attrVal = attributes.get(attrName);
			if (attrVal == null) {
				LOG.warn("attribute " + attrName
						+ "null - setting to empty string");
				props.setProperty(attrName, "");
			} else {
				props.setProperty(attrName, attributes.get(attrName));
			}
		}
		
		return(props);
	}
	
	public void mergeDocument(String pfx, Document doc) {
		String prefix = "";
		
		if (pfx != null) {
			prefix = pfx;
		}
		
		Element root = doc.getDocumentElement();
		
		mergeElement(prefix, root, null);
	}
	
	public void mergeElement(String pfx, Element element, Map<String, Integer> nodeMap) {
		
		// In XML, cannot tell the difference between containers and lists.
		// So, have to treat each element as both (ugly but necessary).
		// We do this by passing a nodeMap to be used to count instance of each tag, 
		// which will be used to set _length and to set index 
		
		LOG.trace("mergeElement("+pfx+","+element.getTagName()+","+nodeMap+")");

		String curTagName = element.getTagName();
		String prefix = curTagName;
		
		if (pfx != null) {
			prefix = pfx + "." + prefix;
		}
		
		int myIdx = 0;
		
		if (nodeMap != null) {
			if (nodeMap.containsKey(curTagName)) {
				myIdx = nodeMap.get(curTagName).intValue();
			}

			nodeMap.put(curTagName, new Integer(myIdx+1));
			this.setAttribute(prefix+"_length", ""+(myIdx+1));
		}
		
		NodeList children = element.getChildNodes();
		
		int numChildren  = children.getLength();
		
		Map<String, Integer> childMap = new HashMap<String, Integer>();
		Map<String, Integer> idxChildMap = new HashMap<String, Integer>();
		
		for (int i = 0 ; i < numChildren ; i++) {
			Node curNode = children.item(i);
			
			if (curNode instanceof Text) {
				Text curText = (Text) curNode;
				String curTextValue = curText.getTextContent();
				LOG.trace("Setting ctx variable "+prefix+" = "+curTextValue);
				this.setAttribute(prefix, curText.getTextContent());
				

			} else if (curNode instanceof Element) {
				mergeElement(prefix, (Element) curNode, childMap);
				if (nodeMap != null) {

					mergeElement(prefix+"["+myIdx+"]", (Element)curNode, idxChildMap);

				}
			}
		}
		
	}
	
	public String resolve(String ctxVarName) {

		if (ctxVarName.indexOf('[') == -1) {
			// Ctx variable contains no arrays
			return (this.getAttribute(ctxVarName));
		}

		// Resolve any array references
		StringBuffer sbuff = new StringBuffer();
		String[] ctxVarParts = ctxVarName.split("\\[");
		sbuff.append(ctxVarParts[0]);
		for (int i = 1; i < ctxVarParts.length; i++) {
			if (ctxVarParts[i].startsWith("$")) {
				int endBracketLoc = ctxVarParts[i].indexOf("]");
				if (endBracketLoc == -1) {
					// Missing end bracket ... give up parsing
					LOG.warn("Variable reference " + ctxVarName
							+ " seems to be missing a ']'");
					return (this.getAttribute(ctxVarName));
				}

				String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
				String remainder = ctxVarParts[i].substring(endBracketLoc);

				sbuff.append("[");
				sbuff.append(this.getAttribute(idxVarName));
				sbuff.append(remainder);

			} else {
				// Index is not a variable reference
				sbuff.append("[");
				sbuff.append(ctxVarParts[i]);
			}
		}

		return (this.getAttribute(sbuff.toString()));
	}

}