2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd.
5 * Modifications Copyright (C) 2019 Nordix Foundation.
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.auth.clicodegen;
25 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.ALBUM_NAME;
26 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.ALBUM_VERSION;
27 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.CONTEXT_REFS;
28 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.CTX_REFS;
29 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.DECLARATION;
30 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.DEFAULT_TASK;
31 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.DEFAULT_TASK_VERSION;
32 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.DEFAULT_VALUE;
33 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.DEFINITIONS;
34 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.DESCRIPTION;
35 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.EVENT_NAME;
36 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.EVENT_VERSION;
37 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.FIELDS;
38 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.FIELD_NAME;
39 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.FIELD_SCHEMA;
40 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.FIELD_SCHEMA_VERSION;
41 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.FINALIZER_LOGICS;
42 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.FINALIZER_LOGIC_NAME;
43 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.FIRST_STATE;
44 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.FLAVOUR;
45 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.INFIELDS;
46 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.LOGIC;
47 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.LOGIC_FLAVOUR;
48 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.NAME;
49 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.NAME_SPACE;
50 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.NEXT_STATE;
51 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.OPTIONAL;
52 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.OUTFIELDS;
53 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.OUTPUTS;
54 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.OUTPUT_NAME;
55 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.OUTPUT_TYPE;
56 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.PARAMS;
57 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.PAR_NAME;
58 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.POLICY_NAME;
59 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.SCHEMA;
60 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.SCHEMA_NAME;
61 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.SCHEMA_VERSION;
62 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.SCOPE;
63 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.SOURCE;
64 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.STATES;
65 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.STATE_NAME;
66 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TARGET;
67 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TASKS;
68 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TASK_LOCAL_NAME;
69 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TASK_NAME;
70 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TASK_VERSION;
71 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TEMPLATE;
72 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TRIGGER_NAME;
73 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TRIGGER_VERSION;
74 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TS_LOGIC;
75 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.UUID;
76 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.VERSION;
77 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.WRITABLE;
79 import java.util.List;
80 import org.stringtemplate.v4.ST;
81 import org.stringtemplate.v4.STGroupFile;
84 * Code generator generating expressions for the APEX CLI Editor.
86 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
88 public class CodeGeneratorCliEditor {
90 // CHECKSTYLE:OFF: ParameterNumber
93 * The name of the STG file for the code generator.
95 public static final String STG_FILE = "org/onap/policy/apex/auth/clicodegen/cli-editor.stg";
98 * The String Template Group, taken from the context.
100 private final STGroupFile stg;
103 * The ST for the model, loaded from the STG.
105 private final ST model;
108 * A default name space, set from specification.
110 private String defaultNamespace;
113 * Creates a new code generator.
115 public CodeGeneratorCliEditor() {
116 stg = new STGroupFile(STG_FILE);
117 stg.registerRenderer(String.class, new CgStringRenderer(), true);
118 model = stg.getInstanceOf("policyModel");
122 * Adds model parameters to the template.
124 * @param name the name of the mode, must not be blank
125 * @param version a version, can be null
126 * @param uuid a UUID, can be null
127 * @param description a description, must not be blank
129 public void addModelParams(final String name, final String version, final String uuid, final String description) {
130 if (name == null || name.isEmpty()) {
131 throw new IllegalArgumentException("model name should not be blank");
133 if (description == null || description.isEmpty()) {
134 throw new IllegalArgumentException("model description should not be blank");
137 model.add(NAME, name);
138 model.add(VERSION, version);
139 model.add(UUID, uuid);
140 model.add(DESCRIPTION, description);
148 public ST getModel() {
153 * Sets the default name space.
155 * @param nameSpace new name space, ignored if blank
157 public void setDefaultNamespace(final String nameSpace) {
158 if (nameSpace != null && !nameSpace.isEmpty()) {
159 defaultNamespace = nameSpace;
164 * Adds a new schema declaration to the model.
166 * @param name the name of the schema
167 * @param version the version of the declaration
168 * @param uuid the UUID for the declaration
169 * @param description a description of the schema
170 * @param flavour the flavour of the schema declaration, e.g. Java or Avro
171 * @param schema the actual schema declaration, either a string or as <code>LS schema LE</code>
173 public void addSchemaDeclaration(final String name, final String version, final String uuid,
174 final String description, final String flavour, final String schema) {
175 final ST st = stg.getInstanceOf("schemaDecl");
177 st.add(VERSION, version);
179 st.add(DESCRIPTION, description);
180 st.add(FLAVOUR, flavour);
181 st.add(SCHEMA, schema);
182 model.add(DECLARATION, st);
186 * Adds a new context album declaration to the model.
188 * @param codeGenCliEditorBuilder The parameters for the context album
190 public void addContextAlbumDeclaration(CodeGenCliEditorBuilder codeGenCliEditorBuilder) {
191 final ST st = stg.getInstanceOf("ctxAlbumDecl");
192 st.add(NAME, codeGenCliEditorBuilder.getName());
193 st.add(VERSION, codeGenCliEditorBuilder.getVersion());
194 st.add(UUID, codeGenCliEditorBuilder.getUuid());
195 st.add(DESCRIPTION, codeGenCliEditorBuilder.getDescription());
196 st.add(SCOPE, codeGenCliEditorBuilder.getScope());
197 st.add(WRITABLE, codeGenCliEditorBuilder.isWritable());
198 st.add(SCHEMA_NAME, codeGenCliEditorBuilder.getSchemaName());
199 st.add(SCHEMA_VERSION, codeGenCliEditorBuilder.getSchemaVersion());
200 model.add(DECLARATION, st);
204 * Creates a new event field definition which belongs to an event.
206 * @param eventName the event name
207 * @param version the event version
208 * @param fieldName the name for the field in the event
209 * @param fieldSchema the schema of the field
210 * @param fieldSchemaVersion the version of the schema
211 * @param optional a flag for optional fields
212 * @return a CLI command for event field definition
214 public ST createEventFieldDefinition(final String eventName, final String version, final String fieldName,
215 final String fieldSchema, final String fieldSchemaVersion, final boolean optional) {
216 final ST st = stg.getInstanceOf("eventDefField");
217 st.add(EVENT_NAME, eventName);
218 st.add(VERSION, version);
219 st.add(FIELD_NAME, fieldName);
220 st.add(FIELD_SCHEMA, fieldSchema);
221 st.add(FIELD_SCHEMA_VERSION, fieldSchemaVersion);
222 st.add(OPTIONAL, optional);
227 * Creates a new task logic definition which belongs to a task.
229 * @param taskName the name of the task
230 * @param version the task version
231 * @param flavour the flavour, e.g. JAVA or JAVASCRIPT
232 * @param logic the actual logic (use either a string or a multi-line with <code>LS some code LE</code>
233 * @return a CLI command for task definition, logic
235 public ST createTaskDefLogic(final String taskName, final String version, final String flavour,
236 final String logic) {
237 final ST st = stg.getInstanceOf("taskDefLogic");
238 st.add(TASK_NAME, taskName);
239 st.add(VERSION, version);
240 st.add(FLAVOUR, flavour);
241 st.add(LOGIC, logic);
246 * Adds a new event declaration to the model.
248 * @param eventDeclarationBuilder param object for event declaration
250 public void addEventDeclaration(EventDeclarationBuilder eventDeclarationBuilder) {
251 final ST st = stg.getInstanceOf("eventDecl");
252 st.add(NAME, eventDeclarationBuilder.getName());
253 st.add(VERSION, eventDeclarationBuilder.getVersion());
254 st.add(UUID, eventDeclarationBuilder.getUuid());
255 st.add(DESCRIPTION, eventDeclarationBuilder.getDescription());
256 st.add(SOURCE, eventDeclarationBuilder.getSource());
257 st.add(TARGET, eventDeclarationBuilder.getTarget());
258 st.add(FIELDS, eventDeclarationBuilder.getFields());
260 if (eventDeclarationBuilder.getNameSpace() != null) {
261 st.add(NAME_SPACE, eventDeclarationBuilder.getNameSpace());
262 } else if (defaultNamespace != null) {
263 st.add(NAME_SPACE, defaultNamespace);
266 model.add(DECLARATION, st);
270 * Adds a new task declaration to the model.
272 * @param taskDeclarationBuilder builder for the task declaration parameters
274 public void addTaskDeclaration(TaskDeclarationBuilder taskDeclarationBuilder) {
275 final ST st = stg.getInstanceOf("taskDecl");
276 st.add(NAME, taskDeclarationBuilder.getName());
277 st.add(VERSION, taskDeclarationBuilder.getVersion());
278 st.add(UUID, taskDeclarationBuilder.getUuid());
279 st.add(DESCRIPTION, taskDeclarationBuilder.getDescription());
280 st.add(INFIELDS, taskDeclarationBuilder.getInfields());
281 st.add(OUTFIELDS, taskDeclarationBuilder.getOutfields());
282 st.add(LOGIC, taskDeclarationBuilder.getLogic());
283 st.add(PARAMS, taskDeclarationBuilder.getParameters());
284 st.add(CONTEXT_REFS, taskDeclarationBuilder.getContextRefs());
285 model.add(DECLARATION, st);
289 * Adds a new policy definition to the model.
291 * @param name the name of the policy
292 * @param version the version of the policy
293 * @param uuid a UUID for the definition
294 * @param description a description of the policy
295 * @param template the template type for this policy
296 * @param firstState the first state of the policy
297 * @param states all policy states
299 public void addPolicyDefinition(final String name, final String version, final String uuid,
300 final String description, final String template, final String firstState, final List<ST> states) {
301 final ST st = stg.getInstanceOf("policyDef");
303 st.add(VERSION, version);
305 st.add(DESCRIPTION, description);
306 st.add(TEMPLATE, template);
307 st.add(FIRST_STATE, firstState);
308 st.add(STATES, states);
309 model.add(DEFINITIONS, st);
313 * Creates a new task infield definition.
315 * @param taskName the name of the task
316 * @param version the version of the task
317 * @param fieldName the name of the infield
318 * @param fieldSchema the schema for the infield
319 * @param fieldSchemaVersion the version of the schema
320 * @return a CLI command for task infield definition
322 public ST createTaskDefinitionInfields(final String taskName, final String version, final String fieldName,
323 final String fieldSchema, final String fieldSchemaVersion) {
324 final ST st = stg.getInstanceOf("taskDefInputFields");
325 st.add(TASK_NAME, taskName);
326 st.add(VERSION, version);
327 st.add(FIELD_NAME, fieldName);
328 st.add(FIELD_SCHEMA, fieldSchema);
329 st.add(FIELD_SCHEMA_VERSION, fieldSchemaVersion);
334 * Creates a new task outfield definition.
336 * @param taskName the name of the task
337 * @param version the version of the task
338 * @param fieldName the name of the outfield
339 * @param fieldSchema the schema for the outfield
340 * @param fieldSchemaVersion the version of the schema
341 * @return a CLI command for task outfield definition
343 public ST createTaskDefinitionOutfields(final String taskName, final String version, final String fieldName,
344 final String fieldSchema, final String fieldSchemaVersion) {
345 final ST st = stg.getInstanceOf("taskDefOutputFields");
346 st.add(TASK_NAME, taskName);
347 st.add(VERSION, version);
348 st.add(FIELD_NAME, fieldName);
349 st.add(FIELD_SCHEMA, fieldSchema);
350 st.add(FIELD_SCHEMA_VERSION, fieldSchemaVersion);
355 * Creates a new task parameter definition belonging to a task.
357 * @param name the name of the task
358 * @param version the version of the task
359 * @param parName the parameter name
360 * @param defaultValue a default value for the parameter
361 * @return a CLI command for a task parameter definition
363 public ST createTaskDefinitionParameters(final String name, final String version, final String parName,
364 final String defaultValue) {
365 final ST st = stg.getInstanceOf("taskDefParameter");
367 st.add(VERSION, version);
368 st.add(PAR_NAME, parName);
369 st.add(DEFAULT_VALUE, defaultValue);
374 * Creates a new task definition context reference which belongs to a task.
376 * @param name the name of the task
377 * @param version the version of the task
378 * @param albumName the name of the context album
379 * @param albumVersion the version of the context album
380 * @return a CLI command for a task context reference definition
382 public ST createTaskDefinitionContextRef(final String name, final String version, final String albumName,
383 final String albumVersion) {
384 final ST st = stg.getInstanceOf("taskDefCtxRef");
386 st.add(VERSION, version);
387 st.add(ALBUM_NAME, albumName);
388 st.add(ALBUM_VERSION, albumVersion);
393 * Creates a new policy state task definition for a task which belongs to a state which belongs to a policy.
395 * @param policyStateTaskBuilder builder for the state task parameters
396 * @return a CLI command for a policy state task definition
398 public ST createPolicyStateTask(PolicyStateTaskBuilder policyStateTaskBuilder) {
399 final ST st = stg.getInstanceOf("policyStateTask");
400 st.add(POLICY_NAME, policyStateTaskBuilder.getPolicyName());
401 st.add(VERSION, policyStateTaskBuilder.getVersion());
402 st.add(STATE_NAME, policyStateTaskBuilder.getStateName());
403 st.add(TASK_LOCAL_NAME, policyStateTaskBuilder.getTaskLocalName());
404 st.add(TASK_NAME, policyStateTaskBuilder.getTaskName());
405 st.add(TASK_VERSION, policyStateTaskBuilder.getTaskVersion());
406 st.add(OUTPUT_TYPE, policyStateTaskBuilder.getOutputType());
407 st.add(OUTPUT_NAME, policyStateTaskBuilder.getOutputName());
412 * Creates a new policy state output definition for a state which belongs to a policy.
414 * @param policyName the name of the policy
415 * @param version the version of the policy
416 * @param stateName the name of the new state
417 * @param outputName the name of the new output
418 * @param eventName the event name for the output
419 * @param eventVersion the version of the event for the output
420 * @param nextState the next state if any
421 * @return a CLI command for a state output definition
423 public ST createPolicyStateOutput(final String policyName, final String version, final String stateName,
424 final String outputName, final String eventName, final String eventVersion,
425 final String nextState) {
426 final ST st = stg.getInstanceOf("policyStateOutput");
427 st.add(POLICY_NAME, policyName);
428 st.add(VERSION, version);
429 st.add(STATE_NAME, stateName);
430 st.add(OUTPUT_NAME, outputName);
431 st.add(EVENT_NAME, eventName);
432 st.add(EVENT_VERSION, eventVersion);
433 st.add(NEXT_STATE, nextState);
438 * Creates a new policy state definition for a state which belongs to a policy.
440 * @param policyStateDefBuilder builder for the state definition parameters
441 * @return a CLI command for a policy state definition
443 public ST createPolicyStateDef(PolicyStateDefBuilder policyStateDefBuilder) {
444 final ST st = stg.getInstanceOf("policyStateDef");
445 st.add(POLICY_NAME, policyStateDefBuilder.getPolicyName());
446 st.add(VERSION, policyStateDefBuilder.getVersion());
447 st.add(STATE_NAME, policyStateDefBuilder.getStateName());
448 st.add(TRIGGER_NAME, policyStateDefBuilder.getTriggerName());
449 st.add(TRIGGER_VERSION, policyStateDefBuilder.getTriggerVersion());
450 st.add(DEFAULT_TASK, policyStateDefBuilder.getDefaultTask());
451 st.add(DEFAULT_TASK_VERSION, policyStateDefBuilder.getDefaultTaskVersion());
452 st.add(OUTPUTS, policyStateDefBuilder.getOutputs());
453 st.add(TASKS, policyStateDefBuilder.getTasks());
454 st.add(TS_LOGIC, policyStateDefBuilder.getTsLogic());
455 st.add(FINALIZER_LOGICS, policyStateDefBuilder.getFinalizerLogics());
456 st.add(CTX_REFS, policyStateDefBuilder.getCtxRefs());
461 * Creates a new task selection logic definition for a state which belongs to a policy.
463 * @param name the name of the policy
464 * @param version the version of the policy
465 * @param stateName the name of the state
466 * @param logicFlavour the flavour, e.g. JAVA or JAVASCRIPT
467 * @param logic the actual logic (use either a string or a multi-line with <code>LS some code LE</code>
468 * @return a CLI command for task selection logic definition
470 public ST createPolicyStateDefTaskSelLogic(final String name, final String version, final String stateName,
471 final String logicFlavour, final String logic) {
472 final ST st = stg.getInstanceOf("policyStateTaskSelectionLogic");
474 st.add(VERSION, version);
475 st.add(STATE_NAME, stateName);
476 st.add(LOGIC_FLAVOUR, logicFlavour);
477 st.add(LOGIC, logic);
482 * Creates a new state finalizer definition for a state which belongs to a policy.
484 * @param name the name of the policy
485 * @param version the version of the policy
486 * @param stateName the name of the state
487 * @param finalizerLogicName name of the finalizer logic
488 * @param logicFlavour the flavour, e.g. JAVA or JAVASCRIPT
489 * @param logic the actual logic (use either a string or a multi-line with <code>LS some code LE</code>
490 * @return a CLI command for finalizer definition
492 public ST createPolicyStateDefFinalizerLogic(final String name, final String version, final String stateName,
493 final String finalizerLogicName, final String logicFlavour, final String logic) {
494 final ST st = stg.getInstanceOf("policyStateFinalizerLogic");
496 st.add(VERSION, version);
497 st.add(STATE_NAME, stateName);
498 st.add(FINALIZER_LOGIC_NAME, finalizerLogicName);
499 st.add(LOGIC_FLAVOUR, logicFlavour);
500 st.add(LOGIC, logic);
505 * Creates a new policy state context reference for a state which belongs to a policy.
507 * @param name the name of the policy
508 * @param version the version of the policy
509 * @param stateName the name of the state
510 * @param albumName the name of the album
511 * @param albumVersion the version of the album
512 * @return a CLI command for state context reference
514 public ST createPolicyStateDefContextRef(final String name, final String version, final String stateName,
515 final String albumName, final String albumVersion) {
516 final ST st = stg.getInstanceOf("policyStateContextRef");
518 st.add(VERSION, version);
519 st.add(STATE_NAME, stateName);
520 st.add(ALBUM_NAME, albumName);
521 st.add(ALBUM_VERSION, albumVersion);