13f0bc0e65132e8f57256c937469c9f8b6428cdb
[policy/apex-pdp.git] / auth / cli-codegen / src / test / java / org / onap / policy / apex / auth / clicodegen / CliCodegenTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.auth.clicodegen;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.FileWriter;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Map.Entry;
37
38 import org.junit.Test;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
41 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
42 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
43 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
44 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
45 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
46 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
47 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
48 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
49 import org.onap.policy.apex.model.policymodel.concepts.AxState;
50 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
51 import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput;
52 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference;
53 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
54 import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic;
55 import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
56 import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic;
57 import org.stringtemplate.v4.ST;
58
59 /**
60  * Test CLI code generation.
61  */
62 public class CliCodegenTest {
63
64     private KeyInfoGetter kig;
65     private File outFile = null;
66
67     @Test
68     public void test() throws IOException {
69         final CodeGeneratorCliEditor codeGen = new CodeGeneratorCliEditor();
70
71         outFile = File.createTempFile("ApexTestGenerated", ".apex");
72
73         ApexModelReader<AxPolicyModel> modelReader = null;
74         try {
75             modelReader = new ApexModelReader<>(AxPolicyModel.class);
76         } catch (ApexModelException e1) {
77             fail("test should not throw an exception");
78         }
79         modelReader.setValidateFlag(false);
80         AxPolicyModel apexPolicyModel = null;
81         try {
82             apexPolicyModel = modelReader
83                             .read(new FileInputStream(new File("src/test/resources/models/TestPolicyModel.json")));
84         } catch (ApexModelException | FileNotFoundException e) {
85             fail("test should not throw an exception");
86         }
87
88         assertEquals(0, generateCli(codeGen, apexPolicyModel));
89     }
90
91     /**
92      * Generate the CLI from the model.
93      * 
94      * @param codeGen the code generator
95      * @param policyModel the policy model
96      * @throws IOException  on generation exceptions
97      */
98     private int generateCli(final CodeGeneratorCliEditor codeGen, final AxPolicyModel policyModel) throws IOException {
99         kig = new KeyInfoGetter(policyModel);
100
101         // Order is important. 0: model, 1: context schemas, 2: tasks, 3: events, 4: ContextAlbums, 5: Policies
102         // 0: model
103         final AxArtifactKey pmkey = policyModel.getKey();
104         codeGen.addModelParams(kig.getName(pmkey), kig.getVersion(pmkey), kig.getUuid(pmkey), kig.getDesc(pmkey));
105
106         // 1: Context Schemas
107         for (final AxContextSchema s : policyModel.getSchemas().getSchemasMap().values()) {
108             final AxArtifactKey key = s.getKey();
109
110             codeGen.addSchemaDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
111                             s.getSchemaFlavour(), s.getSchema());
112         }
113
114         // 2: tasks
115         for (final AxTask t : policyModel.getTasks().getTaskMap().values()) {
116             final AxArtifactKey key = t.getKey();
117             final List<ST> infields = getInfieldsForTask(codeGen, t);
118             final List<ST> outfields = getOutfieldsForTask(codeGen, t);
119             final ST logic = getLogicForTask(codeGen, t);
120             final List<ST> parameters = getParametersForTask(codeGen, t);
121             final List<ST> contextRefs = getCtxtRefsForTask(codeGen, t);
122
123             codeGen.addTaskDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
124                             infields, outfields, logic, parameters, contextRefs);
125         }
126
127         // 3: events
128         for (final AxEvent e : policyModel.getEvents().getEventMap().values()) {
129             final AxArtifactKey key = e.getKey();
130             final List<ST> fields = getParametersForEvent(codeGen, e);
131
132             codeGen.addEventDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
133                             e.getNameSpace(), e.getSource(), e.getTarget(), fields);
134         }
135
136         // 4: context albums
137         for (final AxContextAlbum a : policyModel.getAlbums().getAlbumsMap().values()) {
138             final AxArtifactKey key = a.getKey();
139
140             codeGen.addContextAlbumDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key),
141                             kig.getDesc(key), a.getScope(), a.isWritable(), kig.getName(a.getItemSchema()),
142                             kig.getVersion(a.getItemSchema()));
143         }
144
145         // 5: policies
146         for (final AxPolicy p : policyModel.getPolicies().getPolicyMap().values()) {
147             final AxArtifactKey key = p.getKey();
148             final List<ST> states = getStatesForPolicy(codeGen, p);
149             codeGen.addPolicyDefinition(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
150                             p.getTemplate(), p.getFirstState(), states);
151         }
152
153         final String out = codeGen.getModel().render();
154
155         FileWriter outFileWriter = new FileWriter(outFile);
156         outFileWriter.write(out);
157         outFileWriter.close();
158
159         return 0;
160     }
161
162     /**
163      * Gets the parameters for event.
164      *
165      * @param cg the code generator
166      * @param event the event
167      * @return the parameters for event
168      */
169     private List<ST> getParametersForEvent(final CodeGeneratorCliEditor cg, final AxEvent event) {
170         final Collection<AxField> fields = event.getFields();
171         final List<ST> ret = new ArrayList<>(fields.size());
172         for (final AxField f : fields) {
173             final AxReferenceKey fkey = f.getKey();
174
175             final ST val = cg.createEventFieldDefinition(kig.getPName(fkey), kig.getPVersion(fkey), kig.getLName(fkey),
176                             kig.getName(f.getSchema()), kig.getVersion(f.getSchema()), f.getOptional());
177
178             ret.add(val);
179         }
180         return ret;
181     }
182
183     /**
184      * Gets the context references for task.
185      *
186      * @param cg the code generator
187      * @param task the task
188      * @return the context references for task
189      */
190     private List<ST> getCtxtRefsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
191         final Collection<AxArtifactKey> ctxs = task.getContextAlbumReferences();
192         final List<ST> ret = new ArrayList<>(ctxs.size());
193         final AxArtifactKey tkey = task.getKey();
194         for (final AxArtifactKey ckey : ctxs) {
195
196             final ST val = cg.createTaskDefinitionContextRef(kig.getName(tkey), kig.getVersion(tkey), kig.getName(ckey),
197                             kig.getVersion(ckey));
198
199             ret.add(val);
200         }
201         return ret;
202     }
203
204     /**
205      * Gets the parameters for task.
206      *
207      * @param cg the code generator
208      * @param task the task
209      * @return the parameters for task
210      */
211     private List<ST> getParametersForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
212         final Collection<AxTaskParameter> pars = task.getTaskParameters().values();
213         final List<ST> ret = new ArrayList<>(pars.size());
214         for (final AxTaskParameter p : pars) {
215             final AxReferenceKey pkey = p.getKey();
216
217             final ST val = cg.createTaskDefinitionParameters(kig.getPName(pkey), kig.getPVersion(pkey),
218                             kig.getLName(pkey), p.getTaskParameterValue());
219
220             ret.add(val);
221         }
222         return ret;
223     }
224
225     /**
226      * Gets the logic for task.
227      *
228      * @param cg the code generator
229      * @param task the task
230      * @return the logic for task
231      */
232     private ST getLogicForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
233         final AxArtifactKey tkey = task.getKey();
234         final AxTaskLogic tl = task.getTaskLogic();
235
236         return cg.createTaskDefLogic(kig.getName(tkey), kig.getVersion(tkey), tl.getLogicFlavour(), tl.getLogic());
237     }
238
239     /**
240      * Gets the output fields for task.
241      *
242      * @param cg the code generator
243      * @param task the task
244      * @return the output fields for task
245      */
246     private List<ST> getOutfieldsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
247         final Collection<? extends AxField> fields = task.getOutputFields().values();
248         final List<ST> ret = new ArrayList<>(fields.size());
249         for (final AxField f : fields) {
250             final AxReferenceKey fkey = f.getKey();
251
252             final ST val = cg.createTaskDefinitionOutfields(kig.getPName(fkey), kig.getPVersion(fkey),
253                             kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema()));
254
255             ret.add(val);
256         }
257         return ret;
258     }
259
260     /**
261      * Gets the input fields for task.
262      *
263      * @param cg the code generator
264      * @param task the task
265      * @return the input fields for task
266      */
267     private List<ST> getInfieldsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
268         final Collection<? extends AxField> fields = task.getInputFields().values();
269         final List<ST> ret = new ArrayList<>(fields.size());
270         for (final AxField f : fields) {
271             final AxReferenceKey fkey = f.getKey();
272
273             final ST val = cg.createTaskDefinitionInfields(kig.getPName(fkey), kig.getPVersion(fkey),
274                             kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema()));
275
276             ret.add(val);
277         }
278         return ret;
279     }
280
281     /**
282      * Gets the states for policy.
283      *
284      * @param cg the code generator
285      * @param pol the policy
286      * @return the states for policy
287      */
288     private List<ST> getStatesForPolicy(final CodeGeneratorCliEditor cg, final AxPolicy pol) {
289         final Collection<AxState> states = pol.getStateMap().values();
290         final List<ST> ret = new ArrayList<>(states.size());
291         for (final AxState st : states) {
292             final AxReferenceKey skey = st.getKey();
293             final List<ST> outputs = getStateOutputsForState(cg, st);
294             final List<ST> finalizerLogics = getFinalizersForState(cg, st);
295             final List<ST> tasks = getTaskRefsForState(cg, st);
296             final List<ST> tsLogic = getTslForState(cg, st);
297             final List<ST> ctxRefs = getCtxtRefsForState(cg, st);
298
299             final ST val = cg.createPolicyStateDef(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
300                             kig.getName(st.getTrigger()), kig.getVersion(st.getTrigger()),
301                             kig.getName(st.getDefaultTask()), kig.getVersion(st.getDefaultTask()), outputs, tasks,
302                             tsLogic, finalizerLogics, ctxRefs);
303
304             ret.add(val);
305         }
306         return ret;
307     }
308
309     /**
310      * Gets the finalizers for state.
311      *
312      * @param cg the code generator
313      * @param st the state
314      * @return the finalizers for state
315      */
316     private List<ST> getFinalizersForState(final CodeGeneratorCliEditor cg, final AxState st) {
317         final Collection<AxStateFinalizerLogic> fins = st.getStateFinalizerLogicMap().values();
318         final List<ST> ret = new ArrayList<>(fins.size());
319         final AxReferenceKey skey = st.getKey();
320         for (final AxStateFinalizerLogic fin : fins) {
321             final AxReferenceKey finkey = fin.getKey();
322
323             final ST val = cg.createPolicyStateDefFinalizerLogic(kig.getPName(skey), kig.getPVersion(skey),
324                             kig.getLName(skey), kig.getLName(finkey), fin.getLogicFlavour(), fin.getLogic());
325
326             ret.add(val);
327         }
328         return ret;
329     }
330
331     /**
332      * Gets the context references for state.
333      *
334      * @param cg the code generator
335      * @param st the state
336      * @return the context references for state
337      */
338     private List<ST> getCtxtRefsForState(final CodeGeneratorCliEditor cg, final AxState st) {
339         final Collection<AxArtifactKey> ctxs = st.getContextAlbumReferences();
340         final List<ST> ret = new ArrayList<>(ctxs.size());
341         final AxReferenceKey skey = st.getKey();
342         for (final AxArtifactKey ctx : ctxs) {
343
344             final ST val = cg.createPolicyStateDefContextRef(kig.getPName(skey), kig.getPVersion(skey),
345                             kig.getLName(skey), kig.getName(ctx), kig.getVersion(ctx));
346
347             ret.add(val);
348         }
349         return ret;
350     }
351
352     /**
353      * Gets the Task Selection Logic for state.
354      *
355      * @param cg the code generator
356      * @param st the state
357      * @return the TSL for state (if any) in a list
358      */
359     private List<ST> getTslForState(final CodeGeneratorCliEditor cg, final AxState st) {
360         final AxReferenceKey skey = st.getKey();
361         if (st.checkSetTaskSelectionLogic()) {
362             final AxTaskSelectionLogic tsl = st.getTaskSelectionLogic();
363             final ST val = cg.createPolicyStateDefTaskSelLogic(kig.getPName(skey), kig.getPVersion(skey),
364                             kig.getLName(skey), tsl.getLogicFlavour(), tsl.getLogic());
365             return Collections.singletonList(val);
366         } else {
367             return Collections.emptyList();
368         }
369     }
370
371     /**
372      * Gets the task references for state.
373      *
374      * @param cg the code generator
375      * @param st the state
376      * @return the task references for state
377      */
378     private List<ST> getTaskRefsForState(final CodeGeneratorCliEditor cg, final AxState st) {
379         final Map<AxArtifactKey, AxStateTaskReference> taskrefs = st.getTaskReferences();
380         final List<ST> ret = new ArrayList<>(taskrefs.size());
381         final AxReferenceKey skey = st.getKey();
382         for (final Entry<AxArtifactKey, AxStateTaskReference> e : taskrefs.entrySet()) {
383             final AxArtifactKey tkey = e.getKey();
384             final AxStateTaskReference tr = e.getValue();
385             final AxReferenceKey trkey = tr.getKey();
386
387             final ST val = cg.createPolicyStateTask(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
388                             kig.getLName(trkey), kig.getName(tkey), kig.getVersion(tkey),
389                             tr.getStateTaskOutputType().name(), kig.getLName(tr.getOutput()));
390
391             ret.add(val);
392         }
393         return ret;
394     }
395
396     /**
397      * Gets the state outputs for state.
398      *
399      * @param cg the code generator
400      * @param st the state
401      * @return the state outputs for state
402      */
403     private List<ST> getStateOutputsForState(final CodeGeneratorCliEditor cg, final AxState st) {
404         final Collection<AxStateOutput> outs = st.getStateOutputs().values();
405         final List<ST> ret = new ArrayList<>(outs.size());
406         final AxReferenceKey skey = st.getKey();
407         for (final AxStateOutput out : outs) {
408             final AxReferenceKey outkey = out.getKey();
409
410             final ST val = cg.createPolicyStateOutput(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
411                             kig.getLName(outkey), kig.getName(out.getOutgingEvent()),
412                             kig.getVersion(out.getOutgingEvent()), kig.getLName(out.getNextState()));
413
414             ret.add(val);
415         }
416         return ret;
417     }
418
419 }