ad439cdb41bf6011fe6d3bed4894b329fff92cf0
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
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.onap.ccsdk.sli.core.sli.provider;
23
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
26
27 import java.io.BufferedReader;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.net.URL;
31 import java.util.Enumeration;
32 import java.util.HashMap;
33 import java.util.LinkedList;
34 import java.util.Map;
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;
70
71 public class ITCaseSvcLogicGraphExecutor {
72
73     private static final Logger LOG = LoggerFactory.getLogger(SvcLogicGraph.class);
74     private static final Map<String, AbstractSvcLogicNodeExecutor> BUILTIN_NODES = new HashMap<String, AbstractSvcLogicNodeExecutor>() {
75         {
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());
96
97         }
98     };
99
100     private static SvcLogicClassResolver svcLogicClassResolver;
101
102     @BeforeClass
103     public static void setUpBeforeClass() throws Exception {
104
105         LOG.info("before class");
106
107         InputStream propStr = ITCaseSvcLogicGraphExecutor.class.getResourceAsStream("/svclogic.properties");
108
109         Properties svcprops = new Properties();
110         svcprops.load(propStr);
111
112         SvcLogicStore store = SvcLogicStoreFactory.getSvcLogicStore(svcprops);
113
114         assertNotNull(store);
115
116         SvcLogicParser parser = new SvcLogicParser();
117
118         SvcLogicPropertiesProvider resourceProvider = new SvcLogicPropertiesProviderImpl();
119         svcLogicClassResolver = new SvcLogicClassResolver();
120         SvcLogicServiceImpl svc = new SvcLogicServiceImpl(resourceProvider, svcLogicClassResolver);
121
122         for (String nodeType : BUILTIN_NODES.keySet()) {
123             svc.registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));
124         }
125
126
127
128
129     }
130
131     @AfterClass
132     public static void tearDownAfterClass() throws Exception {
133         LOG.info("after class");
134     }
135
136     @Before
137     public void setUp() throws Exception {
138         LOG.info("before");
139     }
140
141     @After
142     public void tearDown() throws Exception {
143         LOG.info("after");
144     }
145
146     @Test
147     public void testExecute() {
148
149         try {
150             InputStream testStr = getClass().getResourceAsStream("/executor.tests");
151             BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
152
153             InputStream propStr = getClass().getResourceAsStream("/svclogic.properties");
154
155             Properties svcprops = new Properties();
156             svcprops.load(propStr);
157
158
159
160
161             SvcLogicParser parser = new SvcLogicParser();
162
163             // Loop through executor tests
164             SvcLogicPropertiesProvider resourceProvider = new SvcLogicPropertiesProvider() {
165                 @Override
166                 public Properties getProperties() {
167                     return svcprops;
168                 }
169             };
170             SvcLogicServiceImpl svc = new SvcLogicServiceImpl(resourceProvider, svcLogicClassResolver);
171             SvcLogicStore store = svc.getStore();
172             assertNotNull(store);
173             for (String nodeType : BUILTIN_NODES.keySet()) {
174
175
176                 svc.registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));
177
178             }
179             String testCaseLine = null;
180             while ((testCaseLine = testsReader.readLine()) != null) {
181
182                 String[] testCaseFields = testCaseLine.split(":");
183                 String testCaseFile = testCaseFields[0];
184                 String testCaseMethod = testCaseFields[1];
185                 String testCaseParameters = null;
186
187                 if (testCaseFields.length > 2) {
188                     testCaseParameters = testCaseFields[2];
189                 }
190
191                 SvcLogicContext ctx = new SvcLogicContext();
192                 if (testCaseParameters != null) {
193                     String[] testCaseParameterSettings = testCaseParameters.split(",");
194
195                     for (int i = 0; i < testCaseParameterSettings.length; i++) {
196                         String[] nameValue = testCaseParameterSettings[i].split("=");
197                         if (nameValue != null) {
198                             String name = nameValue[0];
199                             String value = "";
200                             if (nameValue.length > 1) {
201                                 value = nameValue[1];
202                             }
203
204                             ctx.setAttribute(name, value);
205                         }
206                     }
207                 }
208
209                 testCaseFile = testCaseFile.trim();
210
211                 if (testCaseFile.length() > 0) {
212                     if (!testCaseFile.startsWith("/")) {
213                         testCaseFile = "/" + testCaseFile;
214                     }
215                     URL testCaseUrl = getClass().getResource(testCaseFile);
216                     if (testCaseUrl == null) {
217                         fail("Could not resolve test case file " + testCaseFile);
218                     }
219
220
221                     LinkedList<SvcLogicGraph> graphs = parser.parse(testCaseUrl.getPath());
222
223                     assertNotNull(graphs);
224
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);
236
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));
244                             }
245
246                             svc.execute(graph, ctx);
247
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));
253                             }
254                         }
255                     }
256
257                 }
258             }
259
260         } catch (Exception e) {
261             LOG.error("Caught exception executing directed graphs", e);
262             fail("Exception executing graphs");
263         }
264     }
265 }