2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.sdnc.sli.provider;
24 import java.io.BufferedReader;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
28 import java.util.Enumeration;
29 import java.util.HashMap;
30 import java.util.LinkedList;
32 import java.util.Properties;
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;
65 import junit.framework.TestCase;
67 public class SvcLogicGraphExecutorTest extends TestCase {
68 private static final Logger LOG = LoggerFactory
69 .getLogger(SvcLogicGraph.class);
71 private static final Map<String, SvcLogicNodeExecutor> BUILTIN_NODES = new HashMap<String, SvcLogicNodeExecutor>() {
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());
95 public void testExecute() {
98 InputStream testStr = getClass().getResourceAsStream("/executor.tests");
99 BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
101 InputStream propStr = getClass().getResourceAsStream("/svclogic.properties");
103 SvcLogicStore store = SvcLogicStoreFactory.getSvcLogicStore(propStr);
105 assertNotNull(store);
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);
122 // Loop through executor tests
124 SvcLogicServiceImpl svc = new SvcLogicServiceImpl();
126 for (String nodeType : BUILTIN_NODES.keySet()) {
128 LOG.info("SLI - registering node executor for node type "+nodeType);
130 svc.registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));
133 String testCaseLine = null;
134 while ((testCaseLine = testsReader.readLine()) != null) {
136 String[] testCaseFields = testCaseLine.split(":");
137 String testCaseFile = testCaseFields[0];
138 String testCaseMethod = testCaseFields[1];
139 String testCaseParameters = null;
141 if (testCaseFields.length > 2) {
142 testCaseParameters = testCaseFields[2];
145 SvcLogicContext ctx = new SvcLogicContext();
146 if (testCaseParameters != null) {
147 String[] testCaseParameterSettings = testCaseParameters.split(",");
149 for (int i = 0 ; i < testCaseParameterSettings.length ; i++) {
150 String[] nameValue = testCaseParameterSettings[i].split("=");
151 if (nameValue != null) {
152 String name = nameValue[0];
154 if (nameValue.length > 1) {
155 value = nameValue[1];
158 ctx.setAttribute(name, value);
163 testCaseFile = testCaseFile.trim();
165 if (testCaseFile.length() > 0) {
166 if (!testCaseFile.startsWith("/")) {
167 testCaseFile = "/"+testCaseFile;
169 URL testCaseUrl = getClass().getResource(testCaseFile);
170 if (testCaseUrl == null) {
171 fail("Could not resolve test case file "+testCaseFile);
174 LinkedList<SvcLogicGraph> graphs = parser.parse(testCaseUrl.getPath());
177 assertNotNull(graphs);
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));
188 svc.execute(graph, ctx);
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));
205 } catch (Exception e) {
206 LOG.error("Caught exception executing directed graphs", e);
207 fail("Exception executing graphs");