2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.ccsdk.sli.core.sli.provider;
23 import java.io.BufferedReader;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
27 import java.util.Enumeration;
28 import java.util.HashMap;
29 import java.util.LinkedList;
31 import java.util.Properties;
33 import org.onap.ccsdk.sli.core.sli.MetricLogger;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicGraph;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicNode;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicParser;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;
40 import org.onap.ccsdk.sli.core.sli.provider.BlockNodeExecutor;
41 import org.onap.ccsdk.sli.core.sli.provider.CallNodeExecutor;
42 import org.onap.ccsdk.sli.core.sli.provider.ConfigureNodeExecutor;
43 import org.onap.ccsdk.sli.core.sli.provider.DeleteNodeExecutor;
44 import org.onap.ccsdk.sli.core.sli.provider.ExecuteNodeExecutor;
45 import org.onap.ccsdk.sli.core.sli.provider.ExistsNodeExecutor;
46 import org.onap.ccsdk.sli.core.sli.provider.ForNodeExecutor;
47 import org.onap.ccsdk.sli.core.sli.provider.GetResourceNodeExecutor;
48 import org.onap.ccsdk.sli.core.sli.provider.IsAvailableNodeExecutor;
49 import org.onap.ccsdk.sli.core.sli.provider.NotifyNodeExecutor;
50 import org.onap.ccsdk.sli.core.sli.provider.RecordNodeExecutor;
51 import org.onap.ccsdk.sli.core.sli.provider.ReleaseNodeExecutor;
52 import org.onap.ccsdk.sli.core.sli.provider.ReserveNodeExecutor;
53 import org.onap.ccsdk.sli.core.sli.provider.ReturnNodeExecutor;
54 import org.onap.ccsdk.sli.core.sli.provider.SaveNodeExecutor;
55 import org.onap.ccsdk.sli.core.sli.provider.SetNodeExecutor;
56 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicNodeExecutor;
57 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicServiceImpl;
58 import org.onap.ccsdk.sli.core.sli.provider.SwitchNodeExecutor;
59 import org.onap.ccsdk.sli.core.sli.provider.UpdateNodeExecutor;
60 import org.osgi.framework.ServiceRegistration;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
64 import junit.framework.TestCase;
66 public class SvcLogicGraphExecutorTest extends TestCase {
67 private static final Logger LOG = LoggerFactory
68 .getLogger(SvcLogicGraph.class);
70 private static final Map<String, SvcLogicNodeExecutor> BUILTIN_NODES = new HashMap<String, SvcLogicNodeExecutor>() {
72 put("block", new BlockNodeExecutor());
73 put("call", new CallNodeExecutor());
74 put("configure", new ConfigureNodeExecutor());
75 put("delete", new DeleteNodeExecutor());
76 put("execute", new ExecuteNodeExecutor());
77 put("exists", new ExistsNodeExecutor());
78 put("for", new ForNodeExecutor());
79 put("get-resource", new GetResourceNodeExecutor());
80 put("is-available", new IsAvailableNodeExecutor());
81 put("notify", new NotifyNodeExecutor());
82 put("record", new RecordNodeExecutor());
83 put("release", new ReleaseNodeExecutor());
84 put("reserve", new ReserveNodeExecutor());
85 put("return", new ReturnNodeExecutor());
86 put("save", new SaveNodeExecutor());
87 put("set", new SetNodeExecutor());
88 put("switch", new SwitchNodeExecutor());
89 put("update", new UpdateNodeExecutor());
94 public void testExecute() {
97 InputStream testStr = getClass().getResourceAsStream("/executor.tests");
98 BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
100 InputStream propStr = getClass().getResourceAsStream("/svclogic.properties");
102 SvcLogicStore store = SvcLogicStoreFactory.getSvcLogicStore(propStr);
104 assertNotNull(store);
106 store.registerNodeType("switch");
107 store.registerNodeType("block");
108 store.registerNodeType("get-resource");
109 store.registerNodeType("reserve");
110 store.registerNodeType("is-available");
111 store.registerNodeType("exists");
112 store.registerNodeType("configure");
113 store.registerNodeType("return");
114 store.registerNodeType("record");
115 store.registerNodeType("allocate");
116 store.registerNodeType("release");
117 store.registerNodeType("for");
118 store.registerNodeType("set");
119 SvcLogicParser parser = new SvcLogicParser(store);
121 // Loop through executor tests
123 SvcLogicServiceImpl svc = new SvcLogicServiceImpl();
125 for (String nodeType : BUILTIN_NODES.keySet()) {
127 LOG.info("SLI - registering node executor for node type "+nodeType);
129 svc.registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));
132 String testCaseLine = null;
133 while ((testCaseLine = testsReader.readLine()) != null) {
135 String[] testCaseFields = testCaseLine.split(":");
136 String testCaseFile = testCaseFields[0];
137 String testCaseMethod = testCaseFields[1];
138 String testCaseParameters = null;
140 if (testCaseFields.length > 2) {
141 testCaseParameters = testCaseFields[2];
144 SvcLogicContext ctx = new SvcLogicContext();
145 if (testCaseParameters != null) {
146 String[] testCaseParameterSettings = testCaseParameters.split(",");
148 for (int i = 0 ; i < testCaseParameterSettings.length ; i++) {
149 String[] nameValue = testCaseParameterSettings[i].split("=");
150 if (nameValue != null) {
151 String name = nameValue[0];
153 if (nameValue.length > 1) {
154 value = nameValue[1];
157 ctx.setAttribute(name, value);
162 testCaseFile = testCaseFile.trim();
164 if (testCaseFile.length() > 0) {
165 if (!testCaseFile.startsWith("/")) {
166 testCaseFile = "/"+testCaseFile;
168 URL testCaseUrl = getClass().getResource(testCaseFile);
169 if (testCaseUrl == null) {
170 fail("Could not resolve test case file "+testCaseFile);
173 LinkedList<SvcLogicGraph> graphs = parser.parse(testCaseUrl.getPath());
176 assertNotNull(graphs);
178 for (SvcLogicGraph graph: graphs) {
179 if (graph.getRpc().equals(testCaseMethod)) {
180 Properties props = ctx.toProperties();
181 LOG.info("SvcLogicContext before executing "+testCaseMethod+":");
182 for (Enumeration e1 = props.propertyNames(); e1.hasMoreElements() ; ) {
183 String propName = (String) e1.nextElement();
184 LOG.info(propName+" = "+props.getProperty(propName));
187 svc.execute(graph, ctx);
189 props = ctx.toProperties();
190 LOG.info("SvcLogicContext after executing "+testCaseMethod+":");
191 for (Enumeration e2 = props.propertyNames(); e2.hasMoreElements() ; ) {
192 String propName = (String) e2.nextElement();
193 LOG.info(propName+" = "+props.getProperty(propName));
204 } catch (Exception e) {
205 LOG.error("Caught exception executing directed graphs", e);
206 fail("Exception executing graphs");