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.onap.ccsdk.sli.core.sli.provider;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
27 import java.io.BufferedReader;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
31 import java.util.Enumeration;
32 import java.util.HashMap;
33 import java.util.LinkedList;
35 import java.util.Properties;
36 import org.junit.After;
37 import org.junit.AfterClass;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicGraph;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicParser;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;
46 import org.onap.ccsdk.sli.core.sli.provider.base.AbstractSvcLogicNodeExecutor;
47 import org.onap.ccsdk.sli.core.sli.provider.base.BlockNodeExecutor;
48 import org.onap.ccsdk.sli.core.sli.provider.base.BreakNodeExecutor;
49 import org.onap.ccsdk.sli.core.sli.provider.base.CallNodeExecutor;
50 import org.onap.ccsdk.sli.core.sli.provider.base.ConfigureNodeExecutor;
51 import org.onap.ccsdk.sli.core.sli.provider.base.DeleteNodeExecutor;
52 import org.onap.ccsdk.sli.core.sli.provider.base.ExecuteNodeExecutor;
53 import org.onap.ccsdk.sli.core.sli.provider.base.ExistsNodeExecutor;
54 import org.onap.ccsdk.sli.core.sli.provider.base.ForNodeExecutor;
55 import org.onap.ccsdk.sli.core.sli.provider.base.GetResourceNodeExecutor;
56 import org.onap.ccsdk.sli.core.sli.provider.base.IsAvailableNodeExecutor;
57 import org.onap.ccsdk.sli.core.sli.provider.base.NotifyNodeExecutor;
58 import org.onap.ccsdk.sli.core.sli.provider.base.RecordNodeExecutor;
59 import org.onap.ccsdk.sli.core.sli.provider.base.ReleaseNodeExecutor;
60 import org.onap.ccsdk.sli.core.sli.provider.base.ReserveNodeExecutor;
61 import org.onap.ccsdk.sli.core.sli.provider.base.ReturnNodeExecutor;
62 import org.onap.ccsdk.sli.core.sli.provider.base.SaveNodeExecutor;
63 import org.onap.ccsdk.sli.core.sli.provider.base.SetNodeExecutor;
64 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicPropertiesProvider;
65 import org.onap.ccsdk.sli.core.sli.provider.base.SwitchNodeExecutor;
66 import org.onap.ccsdk.sli.core.sli.provider.base.UpdateNodeExecutor;
67 import org.onap.ccsdk.sli.core.sli.provider.base.WhileNodeExecutor;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
71 public class ITCaseSvcLogicGraphExecutor {
73 private static final Logger LOG = LoggerFactory.getLogger(SvcLogicGraph.class);
74 private static final Map<String, AbstractSvcLogicNodeExecutor> BUILTIN_NODES = new HashMap<String, AbstractSvcLogicNodeExecutor>() {
76 put("block", new BlockNodeExecutor());
77 put("break", new BreakNodeExecutor());
78 put("call", new CallNodeExecutor());
79 put("configure", new ConfigureNodeExecutor());
80 put("delete", new DeleteNodeExecutor());
81 put("execute", new ExecuteNodeExecutor());
82 put("exists", new ExistsNodeExecutor());
83 put("for", new ForNodeExecutor());
84 put("get-resource", new GetResourceNodeExecutor());
85 put("is-available", new IsAvailableNodeExecutor());
86 put("notify", new NotifyNodeExecutor());
87 put("record", new RecordNodeExecutor());
88 put("release", new ReleaseNodeExecutor());
89 put("reserve", new ReserveNodeExecutor());
90 put("return", new ReturnNodeExecutor());
91 put("save", new SaveNodeExecutor());
92 put("set", new SetNodeExecutor());
93 put("switch", new SwitchNodeExecutor());
94 put("update", new UpdateNodeExecutor());
95 put("while", new WhileNodeExecutor());
100 private static SvcLogicClassResolver svcLogicClassResolver;
103 public static void setUpBeforeClass() throws Exception {
105 LOG.info("before class");
107 InputStream propStr = ITCaseSvcLogicGraphExecutor.class.getResourceAsStream("/svclogic.properties");
109 Properties svcprops = new Properties();
110 svcprops.load(propStr);
112 SvcLogicStore store = SvcLogicStoreFactory.getSvcLogicStore(svcprops);
114 assertNotNull(store);
116 SvcLogicParser parser = new SvcLogicParser();
118 SvcLogicPropertiesProvider resourceProvider = new SvcLogicPropertiesProviderImpl();
119 svcLogicClassResolver = new SvcLogicClassResolver();
120 SvcLogicServiceImpl svc = new SvcLogicServiceImpl(resourceProvider, svcLogicClassResolver);
122 for (String nodeType : BUILTIN_NODES.keySet()) {
123 svc.registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));
132 public static void tearDownAfterClass() throws Exception {
133 LOG.info("after class");
137 public void setUp() throws Exception {
142 public void tearDown() throws Exception {
147 public void testExecute() {
150 InputStream testStr = getClass().getResourceAsStream("/executor.tests");
151 BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
153 InputStream propStr = getClass().getResourceAsStream("/svclogic.properties");
155 Properties svcprops = new Properties();
156 svcprops.load(propStr);
161 SvcLogicParser parser = new SvcLogicParser();
163 // Loop through executor tests
164 SvcLogicPropertiesProvider resourceProvider = new SvcLogicPropertiesProvider() {
166 public Properties getProperties() {
170 SvcLogicServiceImpl svc = new SvcLogicServiceImpl(resourceProvider, svcLogicClassResolver);
171 SvcLogicStore store = svc.getStore();
172 assertNotNull(store);
173 for (String nodeType : BUILTIN_NODES.keySet()) {
176 svc.registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));
179 String testCaseLine = null;
180 while ((testCaseLine = testsReader.readLine()) != null) {
182 String[] testCaseFields = testCaseLine.split(":");
183 String testCaseFile = testCaseFields[0];
184 String testCaseMethod = testCaseFields[1];
185 String testCaseParameters = null;
187 if (testCaseFields.length > 2) {
188 testCaseParameters = testCaseFields[2];
191 SvcLogicContext ctx = new SvcLogicContext();
192 if (testCaseParameters != null) {
193 String[] testCaseParameterSettings = testCaseParameters.split(",");
195 for (int i = 0; i < testCaseParameterSettings.length; i++) {
196 String[] nameValue = testCaseParameterSettings[i].split("=");
197 if (nameValue != null) {
198 String name = nameValue[0];
200 if (nameValue.length > 1) {
201 value = nameValue[1];
204 ctx.setAttribute(name, value);
209 testCaseFile = testCaseFile.trim();
211 if (testCaseFile.length() > 0) {
212 if (!testCaseFile.startsWith("/")) {
213 testCaseFile = "/" + testCaseFile;
215 URL testCaseUrl = getClass().getResource(testCaseFile);
216 if (testCaseUrl == null) {
217 fail("Could not resolve test case file " + testCaseFile);
221 LinkedList<SvcLogicGraph> graphs = parser.parse(testCaseUrl.getPath());
223 assertNotNull(graphs);
225 // Load grqphs into db to support call node
226 SvcLogicParser.load(testCaseUrl.getPath(), store);
227 SvcLogicParser.activate("neutron", "canCreateNetwork", "1.0.0", "sync", store);
228 SvcLogicParser.activate("neutron", "switchTester", "1.0.0", "sync", store);
229 SvcLogicParser.activate("neutron", "forRecordTester", "1.0.0", "sync", store);
230 SvcLogicParser.activate("neutron", "whileNodeTester", "1.0.0", "sync", store);
231 SvcLogicParser.activate("neutron", "resourceTester", "1.0.0", "sync", store);
232 SvcLogicParser.activate("neutron", "configureTester", "1.0.0", "sync", store);
233 SvcLogicParser.activate("neutron", "javaPluginTester", "1.0.0", "sync", store);
234 SvcLogicParser.activate("neutron", "allNodesTester", "1.0.0", "sync", store);
235 SvcLogicParser.activate("neutron", "networkCreated", "1.0.0", "sync", store);
237 for (SvcLogicGraph graph : graphs) {
238 if (graph.getRpc().equals(testCaseMethod)) {
239 Properties props = ctx.toProperties();
240 LOG.info("SvcLogicContext before executing {}:", testCaseMethod);
241 for (Enumeration e1 = props.propertyNames(); e1.hasMoreElements(); ) {
242 String propName = (String) e1.nextElement();
243 LOG.info(propName + " = " + props.getProperty(propName));
246 svc.execute(graph, ctx);
248 props = ctx.toProperties();
249 LOG.info("SvcLogicContext after executing {}:", testCaseMethod);
250 for (Enumeration e2 = props.propertyNames(); e2.hasMoreElements(); ) {
251 String propName = (String) e2.nextElement();
252 LOG.info(propName + " = " + props.getProperty(propName));
260 } catch (Exception e) {
261 LOG.error("Caught exception executing directed graphs", e);
262 fail("Exception executing graphs");