2e8b35e124e178c65fb2cfdbed1dc694d38d015c
[ccsdk/sli/core.git] / sli / provider / src / test / java / org / openecomp / sdnc / sli / provider / SvcLogicGraphExecutorTest.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 java.io.BufferedReader;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.net.URL;
28 import java.util.Enumeration;
29 import java.util.HashMap;
30 import java.util.LinkedList;
31 import java.util.Map;
32 import java.util.Properties;
33
34 import org.openecomp.sdnc.sli.MetricLogger;
35 import org.openecomp.sdnc.sli.SvcLogicContext;
36 import org.openecomp.sdnc.sli.SvcLogicGraph;
37 import org.openecomp.sdnc.sli.SvcLogicNode;
38 import org.openecomp.sdnc.sli.SvcLogicParser;
39 import org.openecomp.sdnc.sli.SvcLogicStore;
40 import org.openecomp.sdnc.sli.SvcLogicStoreFactory;
41 import org.openecomp.sdnc.sli.provider.BlockNodeExecutor;
42 import org.openecomp.sdnc.sli.provider.CallNodeExecutor;
43 import org.openecomp.sdnc.sli.provider.ConfigureNodeExecutor;
44 import org.openecomp.sdnc.sli.provider.DeleteNodeExecutor;
45 import org.openecomp.sdnc.sli.provider.ExecuteNodeExecutor;
46 import org.openecomp.sdnc.sli.provider.ExistsNodeExecutor;
47 import org.openecomp.sdnc.sli.provider.ForNodeExecutor;
48 import org.openecomp.sdnc.sli.provider.GetResourceNodeExecutor;
49 import org.openecomp.sdnc.sli.provider.IsAvailableNodeExecutor;
50 import org.openecomp.sdnc.sli.provider.NotifyNodeExecutor;
51 import org.openecomp.sdnc.sli.provider.RecordNodeExecutor;
52 import org.openecomp.sdnc.sli.provider.ReleaseNodeExecutor;
53 import org.openecomp.sdnc.sli.provider.ReserveNodeExecutor;
54 import org.openecomp.sdnc.sli.provider.ReturnNodeExecutor;
55 import org.openecomp.sdnc.sli.provider.SaveNodeExecutor;
56 import org.openecomp.sdnc.sli.provider.SetNodeExecutor;
57 import org.openecomp.sdnc.sli.provider.SvcLogicNodeExecutor;
58 import org.openecomp.sdnc.sli.provider.SvcLogicServiceImpl;
59 import org.openecomp.sdnc.sli.provider.SwitchNodeExecutor;
60 import org.openecomp.sdnc.sli.provider.UpdateNodeExecutor;
61 import org.osgi.framework.ServiceRegistration;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64
65 import junit.framework.TestCase;
66
67 public class SvcLogicGraphExecutorTest extends TestCase {
68         private static final Logger LOG = LoggerFactory
69                         .getLogger(SvcLogicGraph.class);
70         
71         private static final Map<String, SvcLogicNodeExecutor> BUILTIN_NODES = new HashMap<String, SvcLogicNodeExecutor>() {
72                 {
73                         put("block", new BlockNodeExecutor());
74                         put("call", new CallNodeExecutor());
75                         put("configure", new ConfigureNodeExecutor());
76                         put("delete", new DeleteNodeExecutor());
77                         put("execute", new ExecuteNodeExecutor());
78                         put("exists", new ExistsNodeExecutor());
79                         put("for", new ForNodeExecutor());
80                         put("get-resource", new GetResourceNodeExecutor());
81                         put("is-available", new IsAvailableNodeExecutor());
82                         put("notify", new NotifyNodeExecutor());
83                         put("record", new RecordNodeExecutor());
84                         put("release", new ReleaseNodeExecutor());
85                         put("reserve", new ReserveNodeExecutor());
86                         put("return", new ReturnNodeExecutor());
87                         put("save", new SaveNodeExecutor());
88                         put("set", new SetNodeExecutor());
89                         put("switch", new SwitchNodeExecutor());
90                         put("update", new UpdateNodeExecutor());
91
92                 }
93         };
94         
95         public void testExecute() {
96                 
97                 try {
98                         InputStream testStr = getClass().getResourceAsStream("/executor.tests");
99                         BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
100                         
101                         InputStream propStr = getClass().getResourceAsStream("/svclogic.properties");
102                         
103                         SvcLogicStore store = SvcLogicStoreFactory.getSvcLogicStore(propStr);
104                         
105                         assertNotNull(store);
106                         
107                         store.registerNodeType("switch");
108                         store.registerNodeType("block");
109                         store.registerNodeType("get-resource");
110                         store.registerNodeType("reserve");
111                         store.registerNodeType("is-available");
112                         store.registerNodeType("exists");
113                         store.registerNodeType("configure");
114                         store.registerNodeType("return");
115                         store.registerNodeType("record");
116                         store.registerNodeType("allocate");
117                         store.registerNodeType("release");
118                         store.registerNodeType("for");
119                         store.registerNodeType("set");
120                         SvcLogicParser parser = new SvcLogicParser(store);
121                         
122                         // Loop through executor tests
123
124                         SvcLogicServiceImpl svc = new SvcLogicServiceImpl();
125                         
126                         for (String nodeType : BUILTIN_NODES.keySet()) {
127
128                                 LOG.info("SLI - registering node executor for node type "+nodeType);
129                                 
130                                 svc.registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));
131
132                         }
133                         String testCaseLine = null;
134                         while ((testCaseLine = testsReader.readLine()) != null) {
135                                 
136                                 String[] testCaseFields = testCaseLine.split(":");
137                                 String testCaseFile = testCaseFields[0];
138                                 String testCaseMethod = testCaseFields[1];
139                                 String testCaseParameters = null;
140                                 
141                                 if (testCaseFields.length > 2) {
142                                         testCaseParameters = testCaseFields[2];
143                                 }
144
145                                 SvcLogicContext ctx = new SvcLogicContext();
146                                 if (testCaseParameters != null) {
147                                         String[] testCaseParameterSettings = testCaseParameters.split(",");
148                                         
149                                         for (int i = 0 ; i < testCaseParameterSettings.length ; i++) {
150                                                 String[] nameValue = testCaseParameterSettings[i].split("=");
151                                                 if (nameValue != null) {
152                                                         String name = nameValue[0];
153                                                         String value = "";
154                                                         if (nameValue.length > 1) {
155                                                                 value = nameValue[1];
156                                                         }
157                                                         
158                                                         ctx.setAttribute(name,  value);
159                                                 }
160                                         }
161                                 }
162                                 
163                                 testCaseFile = testCaseFile.trim();
164                                 
165                                 if (testCaseFile.length() > 0) {
166                                         if (!testCaseFile.startsWith("/")) {
167                                                 testCaseFile = "/"+testCaseFile;
168                                         }
169                                         URL testCaseUrl = getClass().getResource(testCaseFile);
170                                         if (testCaseUrl == null) {
171                                                 fail("Could not resolve test case file "+testCaseFile);
172                                         }
173                                         
174                                         LinkedList<SvcLogicGraph> graphs = parser.parse(testCaseUrl.getPath());
175                                         
176
177                                         assertNotNull(graphs);
178                                         
179                                         for (SvcLogicGraph graph: graphs) {
180                                                 if (graph.getRpc().equals(testCaseMethod)) {
181                                                         Properties props = ctx.toProperties();
182                                                         LOG.info("SvcLogicContext before executing "+testCaseMethod+":");
183                                                         for (Enumeration e1 = props.propertyNames(); e1.hasMoreElements() ; ) {
184                                                                 String propName = (String) e1.nextElement();
185                                                                 LOG.info(propName+" = "+props.getProperty(propName));
186                                                         }
187                                                         
188                                                         svc.execute(graph, ctx);
189                                                         
190                                                         props = ctx.toProperties();
191                                                         LOG.info("SvcLogicContext after executing "+testCaseMethod+":");
192                                                         for (Enumeration e2 = props.propertyNames(); e2.hasMoreElements() ; ) {
193                                                                 String propName = (String) e2.nextElement();
194                                                                 LOG.info(propName+" = "+props.getProperty(propName));
195                                                         }
196                                                 }
197                                         }
198
199                                 }
200                                 
201                                 
202                         }
203                         
204                         
205                 } catch (Exception e) {
206                         LOG.error("Caught exception executing directed graphs", e);
207                         fail("Exception executing graphs");
208                 }
209         }
210
211         
212 }