2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.modelapi.impl;
25 import java.util.Map.Entry;
26 import java.util.Properties;
28 import java.util.TreeSet;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
33 import org.onap.policy.apex.model.basicmodel.handling.ApexModelStringWriter;
34 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
35 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
36 import org.onap.policy.apex.model.modelapi.ApexApiResult;
37 import org.onap.policy.apex.model.modelapi.ApexModel;
38 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
39 import org.onap.policy.apex.model.policymodel.concepts.AxState;
40 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
41 import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput;
42 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskOutputType;
43 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference;
44 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
45 import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic;
46 import org.onap.policy.common.utils.validation.Assertions;
49 * This class acts as a facade for operations towards a policy model for policy operations.
51 * @author Liam Fallon (liam.fallon@ericsson.com)
53 public class PolicyFacade {
54 private static final String STATE_NAME_MAY_NOT_BE_NULL = "stateName may not be null";
55 private static final String DOES_NOT_EXIST_ON_STATE = " does not exist on state ";
56 private static final String STATE_FINALIZER_LOGIC = "state finalizer logic ";
57 private static final String DO_ES_NOT_EXIST = " do(es) not exist";
58 private static final String CONCEPT_S = "concept(s) ";
59 private static final String DOES_NOT_EXIST = " does not exist";
60 private static final String CONCEPT = "concept ";
61 private static final String ALREADY_EXISTS = " already exists";
63 // Apex model we're working towards
64 private final ApexModel apexModel;
66 // Properties to use for the model
67 private final Properties apexProperties;
69 // Facade classes for working towards the real Apex model
70 private final KeyInformationFacade keyInformationFacade;
72 // JSON output on list/delete if set
73 private final boolean jsonMode;
76 * Constructor that creates a policy facade for the Apex Model API.
78 * @param apexModel the apex model
79 * @param apexProperties Properties for the model
80 * @param jsonMode set to true to return JSON strings in list and delete operations, otherwise
83 public PolicyFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
84 this.apexModel = apexModel;
85 this.apexProperties = apexProperties;
86 this.jsonMode = jsonMode;
88 keyInformationFacade = new KeyInformationFacade(apexModel, apexProperties, jsonMode);
94 * @param name name of the policy
95 * @param version version of the policy, set to null to use the default version
96 * @param template template used to create the policy, set to null to use the default template
97 * @param firstState the first state of the policy
98 * @param uuid policy UUID, set to null to generate a UUID
99 * @param description policy description, set to null to generate a description
100 * @return result of the operation
102 public ApexApiResult createPolicy(final String name, final String version, final String template,
103 final String firstState, final String uuid, final String description) {
105 final AxArtifactKey key = new AxArtifactKey();
107 if (version != null) {
108 key.setVersion(version);
110 key.setVersion(apexProperties.getProperty("DEFAULT_CONCEPT_VERSION"));
113 String templateString = template;
114 if (templateString == null) {
115 templateString = apexProperties.getProperty("DEFAULT_POLICY_TEMPLATE");
118 if (apexModel.getPolicyModel().getPolicies().getPolicyMap().containsKey(key)) {
119 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, CONCEPT + key.getId() + ALREADY_EXISTS);
122 final AxPolicy policy = new AxPolicy(key);
123 policy.setTemplate(templateString);
124 policy.setFirstState(firstState);
126 apexModel.getPolicyModel().getPolicies().getPolicyMap().put(key, policy);
128 if (apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().containsKey(key)) {
129 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
131 return keyInformationFacade.createKeyInformation(name, version, uuid, description);
133 } catch (final Exception e) {
134 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
141 * @param name name of the policy
142 * @param version version of the policy, set to null to use the latest version
143 * @param template template used to create the policy, set to null to not update
144 * @param firstState the first state of the policy
145 * @param uuid policy UUID, set to null to not update
146 * @param description policy description, set to null to not update
147 * @return result of the operation
149 public ApexApiResult updatePolicy(final String name, final String version, final String template,
150 final String firstState, final String uuid, final String description) {
152 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
153 if (policy == null) {
154 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
155 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
158 if (template != null) {
159 policy.setTemplate(template);
161 if (firstState != null) {
162 policy.setFirstState(firstState);
165 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
166 } catch (final Exception e) {
167 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
174 * @param name name of the policy, set to null to list all
175 * @param version starting version of the policy, set to null to list all versions
176 * @return result of the operation
178 public ApexApiResult listPolicy(final String name, final String version) {
180 final Set<AxPolicy> policySet = apexModel.getPolicyModel().getPolicies().getAll(name, version);
181 if (name != null && policySet.isEmpty()) {
182 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
183 CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
186 final ApexApiResult result = new ApexApiResult();
187 for (final AxPolicy policy : policySet) {
189 new ApexModelStringWriter<AxPolicy>(false).writeString(policy, AxPolicy.class, jsonMode));
192 } catch (final Exception e) {
193 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
200 * @param name name of the policy
201 * @param version version of the policy, set to null to use the latest version
202 * @return result of the operation
204 public ApexApiResult deletePolicy(final String name, final String version) {
206 if (version != null) {
207 final AxArtifactKey key = new AxArtifactKey(name, version);
208 final AxPolicy removedPolicy = apexModel.getPolicyModel().getPolicies().getPolicyMap().remove(key);
209 if (removedPolicy != null) {
210 return new ApexApiResult(ApexApiResult.Result.SUCCESS, new ApexModelStringWriter<AxPolicy>(false)
211 .writeString(removedPolicy, AxPolicy.class, jsonMode));
213 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
214 CONCEPT + key.getId() + DOES_NOT_EXIST);
218 final Set<AxPolicy> policySet = apexModel.getPolicyModel().getPolicies().getAll(name, version);
219 if (policySet.isEmpty()) {
220 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
221 CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
224 final ApexApiResult result = new ApexApiResult();
225 for (final AxPolicy policy : policySet) {
227 new ApexModelStringWriter<AxPolicy>(false).writeString(policy, AxPolicy.class, jsonMode));
228 apexModel.getPolicyModel().getPolicies().getPolicyMap().remove(policy.getKey());
229 keyInformationFacade.deleteKeyInformation(name, version);
232 } catch (final Exception e) {
233 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
240 * @param name name of the policy, set to null to list all
241 * @param version starting version of the policy, set to null to list all versions
242 * @return result of the operation
244 public ApexApiResult validatePolicy(final String name, final String version) {
246 final Set<AxPolicy> policySet = apexModel.getPolicyModel().getPolicies().getAll(name, version);
247 if (policySet.isEmpty()) {
248 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
249 CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
252 final ApexApiResult result = new ApexApiResult();
253 for (final AxPolicy policy : policySet) {
254 final AxValidationResult validationResult = policy.validate(new AxValidationResult());
255 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(policy.getKey(),
256 AxArtifactKey.class, jsonMode));
257 result.addMessage(validationResult.toString());
260 } catch (final Exception e) {
261 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
266 * Create a policy state.
268 * @param name name of the policy
269 * @param version version of the policy, set to null to use the latest version
270 * @param stateName of the state
271 * @param triggerName name of the trigger event for this state
272 * @param triggerVersion version of the trigger event for this state, set to null to use the
274 * @param defaultTaskName the default task name
275 * @param defaltTaskVersion the default task version, set to null to use the latest version
276 * @return result of the operation
278 public ApexApiResult createPolicyState(final String name, final String version, final String stateName,
279 final String triggerName, final String triggerVersion, final String defaultTaskName,
280 final String defaltTaskVersion) {
282 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
284 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
285 if (policy == null) {
286 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
287 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
290 final AxReferenceKey refKey = new AxReferenceKey(policy.getKey(), stateName);
292 if (policy.getStateMap().containsKey(refKey.getLocalName())) {
293 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
294 CONCEPT + refKey.getId() + ALREADY_EXISTS);
297 final AxEvent triggerEvent = apexModel.getPolicyModel().getEvents().get(triggerName, triggerVersion);
298 if (triggerEvent == null) {
299 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
300 CONCEPT + triggerName + ':' + triggerVersion + DOES_NOT_EXIST);
303 final AxTask defaultTask = apexModel.getPolicyModel().getTasks().get(defaultTaskName, defaltTaskVersion);
304 if (defaultTask == null) {
305 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
306 CONCEPT + defaultTaskName + ':' + defaltTaskVersion + DOES_NOT_EXIST);
309 final AxState state = new AxState(refKey);
310 state.setTrigger(triggerEvent.getKey());
311 state.setDefaultTask(defaultTask.getKey());
313 policy.getStateMap().put(state.getKey().getLocalName(), state);
314 return new ApexApiResult();
315 } catch (final Exception e) {
316 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
321 * Update a policy state.
323 * @param name name of the policy
324 * @param version version of the policy, set to null to use the latest version
325 * @param stateName of the state
326 * @param triggerName name of the trigger event for this state, set to null to not update
327 * @param triggerVersion version of the trigger event for this state, set to use latest version
329 * @param defaultTaskName the default task name, set to null to not update
330 * @param defaltTaskVersion the default task version, set to use latest version of default task
331 * @return result of the operation
333 public ApexApiResult updatePolicyState(final String name, final String version, final String stateName,
334 final String triggerName, final String triggerVersion, final String defaultTaskName,
335 final String defaltTaskVersion) {
337 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
339 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
340 if (policy == null) {
341 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
342 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
345 final AxState state = policy.getStateMap().get(stateName);
347 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
348 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
351 if (triggerName != null) {
352 final AxEvent triggerEvent = apexModel.getPolicyModel().getEvents().get(triggerName, triggerVersion);
353 if (triggerEvent == null) {
354 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
355 CONCEPT + triggerName + ':' + triggerVersion + DOES_NOT_EXIST);
357 state.setTrigger(triggerEvent.getKey());
360 if (defaultTaskName != null) {
361 final AxTask defaultTask =
362 apexModel.getPolicyModel().getTasks().get(defaultTaskName, defaltTaskVersion);
363 if (defaultTask == null) {
364 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
365 CONCEPT + defaultTaskName + ':' + defaltTaskVersion + DOES_NOT_EXIST);
367 state.setDefaultTask(defaultTask.getKey());
370 return new ApexApiResult();
371 } catch (final Exception e) {
372 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
377 * List policy states.
379 * @param name name of the policy
380 * @param version version of the policy, set to null to use the latest version
381 * @param stateName of the state, set to null to list all states of the policy
382 * @return result of the operation
384 public ApexApiResult listPolicyState(final String name, final String version, final String stateName) {
386 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
387 if (policy == null) {
388 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
389 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
392 if (stateName != null) {
393 final AxState state = policy.getStateMap().get(stateName);
395 return new ApexApiResult(ApexApiResult.Result.SUCCESS,
396 new ApexModelStringWriter<AxState>(false).writeString(state, AxState.class, jsonMode));
398 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
399 CONCEPT + name + ':' + version + ':' + state + DOES_NOT_EXIST);
402 if (policy.getStateMap().size() == 0) {
403 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
404 "no states defined on policy " + policy.getKey().getId());
406 final ApexApiResult result = new ApexApiResult();
407 for (final AxState state : policy.getStateMap().values()) {
409 new ApexModelStringWriter<AxState>(false).writeString(state, AxState.class, jsonMode));
413 } catch (final Exception e) {
414 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
419 * Delete a policy state.
421 * @param name name of the policy
422 * @param version version of the policy, set to null to use the latest version
423 * @param stateName of the state, set to null to delete all states
424 * @return result of the operation
426 public ApexApiResult deletePolicyState(final String name, final String version, final String stateName) {
428 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
429 if (policy == null) {
430 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
431 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
434 final ApexApiResult result = new ApexApiResult();
435 if (stateName != null) {
436 if (policy.getStateMap().containsKey(stateName)) {
437 result.addMessage(new ApexModelStringWriter<AxState>(false)
438 .writeString(policy.getStateMap().get(stateName), AxState.class, jsonMode));
439 policy.getStateMap().remove(stateName);
442 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
443 CONCEPT + name + ':' + version + ':' + stateName + DOES_NOT_EXIST);
446 if (policy.getStateMap().size() == 0) {
447 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
448 "no states defined on policy " + policy.getKey().getId());
450 for (final AxState state : policy.getStateMap().values()) {
452 new ApexModelStringWriter<AxState>(false).writeString(state, AxState.class, jsonMode));
454 policy.getStateMap().clear();
457 } catch (final Exception e) {
458 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
463 * Create task selection logic for a state.
465 * @param name name of the policy
466 * @param version version of the policy, set to null to use the latest version
467 * @param stateName of the state
468 * @param logicFlavour the task selection logic flavour for the state, set to null to use the
469 * default task logic flavour
470 * @param logic the source code for the logic of the state
471 * @return result of the operation
473 public ApexApiResult createPolicyStateTaskSelectionLogic(final String name, final String version,
474 final String stateName, final String logicFlavour, final String logic) {
476 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
478 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
479 if (policy == null) {
480 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
481 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
484 final AxState state = policy.getStateMap().get(stateName);
486 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
487 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
490 // There is only one logic item associated with a state so we use a hard coded logic
492 final AxReferenceKey refKey = new AxReferenceKey(state.getKey(), "TaskSelectionLogic");
494 if (!state.getTaskSelectionLogic().getKey().getLocalName().equals(AxKey.NULL_KEY_NAME)) {
495 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
496 CONCEPT + refKey.getId() + ALREADY_EXISTS);
499 state.setTaskSelectionLogic(new AxTaskSelectionLogic(refKey, logicFlavour, logic));
500 return new ApexApiResult();
501 } catch (final Exception e) {
502 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
507 * Update task selection logic for a state.
509 * @param name name of the policy
510 * @param version version of the policy, set to null to use the latest version
511 * @param stateName of the state
512 * @param logicFlavour the task selection logic flavour for the state, set to null to not update
513 * @param logic the source code for the logic of the state, set to null to not update
514 * @return result of the operation
516 public ApexApiResult updatePolicyStateTaskSelectionLogic(final String name, final String version,
517 final String stateName, final String logicFlavour, final String logic) {
519 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
521 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
522 if (policy == null) {
523 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
524 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
527 final AxState state = policy.getStateMap().get(stateName);
529 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
530 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
533 if (state.getTaskSelectionLogic().getKey().getLocalName().equals(AxKey.NULL_KEY_NAME)) {
534 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
535 CONCEPT + state.getTaskSelectionLogic().getKey().getId() + DOES_NOT_EXIST);
538 final AxTaskSelectionLogic taskSelectionLogic = state.getTaskSelectionLogic();
539 if (logicFlavour != null) {
540 taskSelectionLogic.setLogicFlavour(logicFlavour);
543 taskSelectionLogic.setLogic(logic);
546 return new ApexApiResult();
547 } catch (final Exception e) {
548 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
553 * List task selection logic for a state.
555 * @param name name of the policy
556 * @param version version of the policy, set to null to use the latest version
557 * @param stateName of the state
558 * @return result of the operation
560 public ApexApiResult listPolicyStateTaskSelectionLogic(final String name, final String version,
561 final String stateName) {
563 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
565 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
566 if (policy == null) {
567 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
568 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
571 final AxState state = policy.getStateMap().get(stateName);
573 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
574 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
577 return new ApexApiResult(ApexApiResult.Result.SUCCESS,
578 new ApexModelStringWriter<AxTaskSelectionLogic>(false).writeString(state.getTaskSelectionLogic(),
579 AxTaskSelectionLogic.class, jsonMode));
580 } catch (final Exception e) {
581 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
586 * Delete task selection logic for a state.
588 * @param name name of the policy
589 * @param version version of the policy, set to null to use the latest version
590 * @param stateName of the state
591 * @return result of the operation
593 public ApexApiResult deletePolicyStateTaskSelectionLogic(final String name, final String version,
594 final String stateName) {
596 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
598 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
599 if (policy == null) {
600 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
601 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
604 final AxState state = policy.getStateMap().get(stateName);
606 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
607 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
610 if (state.getTaskSelectionLogic().getKey().getLocalName().equals(AxKey.NULL_KEY_NAME)) {
611 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
612 CONCEPT + state.getTaskSelectionLogic().getKey().getId() + DOES_NOT_EXIST);
615 final ApexApiResult result = new ApexApiResult();
616 result.addMessage(new ApexModelStringWriter<AxTaskSelectionLogic>(false)
617 .writeString(state.getTaskSelectionLogic(), AxTaskSelectionLogic.class, jsonMode));
618 state.setTaskSelectionLogic(new AxTaskSelectionLogic());
620 } catch (final Exception e) {
621 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
626 * Create a policy state output.
628 * @param name name of the policy
629 * @param version version of the policy, set to null to use the latest version
630 * @param stateName of the state
631 * @param outputName of the state output
632 * @param eventName name of the output event for this state output
633 * @param eventVersion version of the output event for this state output, set to null to use the
635 * @param nextState for this state to transition to, set to null if this is the last state that
636 * the policy transitions to on this branch
637 * @return result of the operation
639 public ApexApiResult createPolicyStateOutput(final String name, final String version, final String stateName,
640 final String outputName, final String eventName, final String eventVersion, final String nextState) {
642 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
643 Assertions.argumentNotNull(outputName, "outputName may not be null");
645 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
646 if (policy == null) {
647 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
648 "Policy concept " + name + ':' + version + DOES_NOT_EXIST);
651 final AxState state = policy.getStateMap().get(stateName);
653 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
654 "State concept " + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
657 final AxReferenceKey refKey = new AxReferenceKey(state.getKey(), outputName);
658 if (state.getStateOutputs().containsKey(refKey.getLocalName())) {
659 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
660 "Output concept " + refKey.getId() + ALREADY_EXISTS);
663 final AxEvent event = apexModel.getPolicyModel().getEvents().get(eventName, eventVersion);
665 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
666 "Event concept " + eventName + ':' + eventVersion + DOES_NOT_EXIST);
669 AxReferenceKey nextStateKey = AxReferenceKey.getNullKey();
670 if (nextState != null && !(AxReferenceKey.getNullKey().getLocalName().equals(nextState))) {
671 if (state.getKey().getLocalName().equals(nextState)) {
672 return new ApexApiResult(ApexApiResult.Result.FAILED,
673 "next state " + nextState + " of a state cannot be the state itself");
675 nextStateKey = new AxReferenceKey(state.getKey().getParentArtifactKey(), nextState);
677 if (!policy.getStateMap().containsKey(nextState)) {
678 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
679 "Next state concept " + nextStateKey.getId() + DOES_NOT_EXIST);
683 state.getStateOutputs().put(refKey.getLocalName(), new AxStateOutput(refKey, event.getKey(), nextStateKey));
684 return new ApexApiResult();
685 } catch (final Exception e) {
686 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
691 * List policy state outputs.
693 * @param name name of the policy
694 * @param version version of the policy, set to null to use the latest version
695 * @param stateName of the state
696 * @param outputName of the state output, set to null to list all outputs of the state
697 * @return result of the operation
699 public ApexApiResult listPolicyStateOutput(final String name, final String version, final String stateName,
700 final String outputName) {
702 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
704 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
705 if (policy == null) {
706 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
707 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
710 final AxState state = policy.getStateMap().get(stateName);
712 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
713 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
716 if (outputName != null) {
717 final AxStateOutput stateOutput = state.getStateOutputs().get(outputName);
718 if (stateOutput != null) {
719 return new ApexApiResult(ApexApiResult.Result.SUCCESS,
720 new ApexModelStringWriter<AxStateOutput>(false).writeString(stateOutput,
721 AxStateOutput.class, jsonMode));
723 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
724 CONCEPT + state.getKey().getId() + ':' + outputName + DOES_NOT_EXIST);
727 if (state.getStateOutputs().size() == 0) {
728 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
729 "no state output concepts exist for state " + state.getKey().getId());
732 final ApexApiResult result = new ApexApiResult();
734 for (final AxStateOutput stateOutput : state.getStateOutputs().values()) {
735 result.addMessage(new ApexModelStringWriter<AxStateOutput>(false).writeString(stateOutput,
736 AxStateOutput.class, jsonMode));
740 } catch (final Exception e) {
741 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
746 * Delete a policy state output.
748 * @param name name of the policy
749 * @param version version of the policy, set to null to use the latest version
750 * @param stateName of the state
751 * @param outputName of the state output, set to null to delete all state outputs
752 * @return result of the operation
754 public ApexApiResult deletePolicyStateOutput(final String name, final String version, final String stateName,
755 final String outputName) {
757 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
759 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
760 if (policy == null) {
761 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
762 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
765 final AxState state = policy.getStateMap().get(stateName);
767 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
768 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
771 if (outputName != null) {
772 final AxStateOutput stateOutput = state.getStateOutputs().get(outputName);
773 if (stateOutput != null) {
774 final ApexApiResult result = new ApexApiResult(ApexApiResult.Result.SUCCESS,
775 new ApexModelStringWriter<AxStateOutput>(false).writeString(stateOutput,
776 AxStateOutput.class, jsonMode));
777 state.getStateOutputs().remove(outputName);
780 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
781 CONCEPT + state.getKey().getId() + ':' + outputName + DOES_NOT_EXIST);
784 if (state.getStateOutputs().size() == 0) {
785 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
786 "no state output concepts exist for state " + state.getKey().getId());
789 final ApexApiResult result = new ApexApiResult();
791 for (final Entry<String, AxStateOutput> stateOutputEntry : state.getStateOutputs().entrySet()) {
792 result.addMessage(new ApexModelStringWriter<AxStateOutput>(false)
793 .writeString(stateOutputEntry.getValue(), AxStateOutput.class, jsonMode));
795 state.getStateOutputs().clear();
798 } catch (final Exception e) {
799 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
804 * Create policy finalizer logic for a state.
806 * @param name name of the policy
807 * @param version version of the policy, set to null to use the latest version
808 * @param stateName of the state
809 * @param finalizerLogicName name of the state finalizer logic
810 * @param logicFlavour the policy finalizer logic flavour for the state, set to null to use the
811 * default task logic flavour
812 * @param logic the source code for the logic of the state
813 * @return result of the operation
815 public ApexApiResult createPolicyStateFinalizerLogic(final String name, final String version,
816 final String stateName, final String finalizerLogicName, final String logicFlavour, final String logic) {
818 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
819 Assertions.argumentNotNull(finalizerLogicName, "finalizerlogicName may not be null");
821 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
822 if (policy == null) {
823 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
824 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
827 final AxState state = policy.getStateMap().get(stateName);
829 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
830 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
833 final AxReferenceKey refKey = new AxReferenceKey(state.getKey(), finalizerLogicName);
835 if (state.getStateFinalizerLogicMap().containsKey(refKey.getLocalName())) {
836 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
837 CONCEPT + refKey.getId() + ALREADY_EXISTS);
840 state.getStateFinalizerLogicMap().put(finalizerLogicName,
841 new AxStateFinalizerLogic(refKey, logicFlavour, logic));
842 return new ApexApiResult();
843 } catch (final Exception e) {
844 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
849 * Update policy finalizer logic for a state.
851 * @param name name of the policy
852 * @param version version of the policy, set to null to use the latest version
853 * @param stateName of the state
854 * @param finalizerLogicName name of the state finalizer logic
855 * @param logicFlavour the policy finalizer logic flavour for the state, set to null to not
857 * @param logic the source code for the logic of the state, set to null to not update
858 * @return result of the operation
860 public ApexApiResult updatePolicyStateFinalizerLogic(final String name, final String version,
861 final String stateName, final String finalizerLogicName, final String logicFlavour, final String logic) {
863 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
864 Assertions.argumentNotNull(finalizerLogicName, "finalizerLogicName may not be null");
866 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
867 if (policy == null) {
868 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
869 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
872 final AxState state = policy.getStateMap().get(stateName);
874 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
875 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
878 final AxReferenceKey refKey = new AxReferenceKey(state.getKey(), finalizerLogicName);
879 final AxStateFinalizerLogic stateFinalizerLogic =
880 state.getStateFinalizerLogicMap().get(refKey.getKey().getLocalName());
881 if (stateFinalizerLogic == null) {
882 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
883 STATE_FINALIZER_LOGIC + refKey.getId() + DOES_NOT_EXIST);
886 if (logicFlavour != null) {
887 stateFinalizerLogic.setLogicFlavour(logicFlavour);
890 stateFinalizerLogic.setLogic(logic);
893 return new ApexApiResult();
894 } catch (final Exception e) {
895 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
900 * List policy finalizer logic for a state.
902 * @param name name of the policy
903 * @param version version of the policy, set to null to use the latest version
904 * @param stateName of the state
905 * @param finalizerLogicName name of the state finalizer logic
906 * @return result of the operation
908 public ApexApiResult listPolicyStateFinalizerLogic(final String name, final String version, final String stateName,
909 final String finalizerLogicName) {
911 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
913 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
914 if (policy == null) {
915 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
916 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
919 final AxState state = policy.getStateMap().get(stateName);
921 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
922 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
925 if (finalizerLogicName != null) {
926 final AxReferenceKey refKey = new AxReferenceKey(state.getKey(), finalizerLogicName);
927 final AxStateFinalizerLogic stateFinalizerLogic =
928 state.getStateFinalizerLogicMap().get(refKey.getKey().getLocalName());
929 if (stateFinalizerLogic == null) {
930 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
931 STATE_FINALIZER_LOGIC + refKey.getId() + DOES_NOT_EXIST);
934 return new ApexApiResult(ApexApiResult.Result.SUCCESS,
935 new ApexModelStringWriter<AxStateFinalizerLogic>(false).writeString(stateFinalizerLogic,
936 AxStateFinalizerLogic.class, jsonMode));
938 if (state.getStateFinalizerLogicMap().size() == 0) {
939 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
940 "no state finalizer logic defined on state " + state.getKey().getId());
942 final ApexApiResult result = new ApexApiResult();
943 for (final AxStateFinalizerLogic stateFinalizerLogic : state.getStateFinalizerLogicMap().values()) {
944 result.addMessage(new ApexModelStringWriter<AxStateFinalizerLogic>(false)
945 .writeString(stateFinalizerLogic, AxStateFinalizerLogic.class, jsonMode));
949 } catch (final Exception e) {
950 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
955 * Delete policy finalizer logic for a state.
957 * @param name name of the policy
958 * @param version version of the policy, set to null to use the latest version
959 * @param stateName of the state
960 * @param finalizerLogicName name of the state finalizer logic
961 * @return result of the operation
963 public ApexApiResult deletePolicyStateFinalizerLogic(final String name, final String version,
964 final String stateName, final String finalizerLogicName) {
966 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
968 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
969 if (policy == null) {
970 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
971 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
974 final AxState state = policy.getStateMap().get(stateName);
976 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
977 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
980 if (finalizerLogicName != null) {
981 final AxReferenceKey refKey = new AxReferenceKey(state.getKey(), finalizerLogicName);
982 final AxStateFinalizerLogic stateFinalizerLogic =
983 state.getStateFinalizerLogicMap().get(refKey.getKey().getLocalName());
984 if (stateFinalizerLogic == null) {
985 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
986 STATE_FINALIZER_LOGIC + refKey.getId() + DOES_NOT_EXIST);
989 final ApexApiResult result = new ApexApiResult();
990 result.addMessage(new ApexModelStringWriter<AxStateFinalizerLogic>(false)
991 .writeString(stateFinalizerLogic, AxStateFinalizerLogic.class, jsonMode));
992 state.getStateFinalizerLogicMap().remove(refKey.getLocalName());
995 if (state.getStateFinalizerLogicMap().size() == 0) {
996 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
997 "no state finalizer logic defined on state " + state.getKey().getId());
1000 final ApexApiResult result = new ApexApiResult();
1001 for (final AxStateFinalizerLogic stateFinalizerLogic : state.getStateFinalizerLogicMap().values()) {
1002 result.addMessage(new ApexModelStringWriter<AxStateFinalizerLogic>(false)
1003 .writeString(stateFinalizerLogic, AxStateFinalizerLogic.class, jsonMode));
1005 state.getStateFinalizerLogicMap().clear();
1008 } catch (final Exception e) {
1009 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
1014 * Create a policy state task reference.
1016 * @param builder builder for the state task reference
1017 * @return result of the operation
1019 public ApexApiResult createPolicyStateTaskRef(CreatePolicyStateTaskRefBuilder builder) {
1021 Assertions.argumentNotNull(builder.getStateName(), STATE_NAME_MAY_NOT_BE_NULL);
1022 Assertions.argumentNotNull(builder.getOutputName(), "outputName may not be null");
1024 final AxPolicy policy =
1025 apexModel.getPolicyModel().getPolicies().get(builder.getName(), builder.getVersion());
1026 if (policy == null) {
1027 return new ApexApiResult(
1028 ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1029 CONCEPT + builder.getName() + ':' + builder.getVersion() + DOES_NOT_EXIST);
1032 final AxState state = policy.getStateMap().get(builder.getStateName());
1033 if (state == null) {
1034 return new ApexApiResult(
1035 ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1036 CONCEPT + policy.getKey().getId() + ':' + builder.getStateName() + DOES_NOT_EXIST);
1043 .get(builder.getTaskName(), builder.getTaskVersion());
1045 return new ApexApiResult(
1046 ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1047 CONCEPT + builder.getTaskName() + ':' + builder.getTaskVersion() + DOES_NOT_EXIST);
1050 if (state.getTaskReferences().containsKey(task.getKey())) {
1051 return new ApexApiResult(
1052 ApexApiResult.Result.CONCEPT_EXISTS,
1054 + task.getKey().getId()
1055 + " already has reference with output "
1056 + state.getTaskReferences().get(task.getKey()));
1059 AxReferenceKey refKey;
1060 if (builder.getTaskLocalName() == null) {
1061 refKey = new AxReferenceKey(state.getKey(), state.getKey().getParentKeyName());
1063 refKey = new AxReferenceKey(state.getKey(), builder.getTaskLocalName());
1066 // The reference to the output we're using here
1067 final AxReferenceKey outputRefKey =
1068 new AxReferenceKey(state.getKey(), builder.getOutputName());
1070 final AxStateTaskOutputType stateTaskOutputType =
1071 AxStateTaskOutputType.valueOf(builder.getOutputType());
1072 if (stateTaskOutputType.equals(AxStateTaskOutputType.DIRECT)) {
1073 if (!state.getStateOutputs().containsKey(outputRefKey.getLocalName())) {
1074 return new ApexApiResult(
1075 ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1076 "state output concept " + outputRefKey.getId() + DOES_NOT_EXIST);
1078 } else if (stateTaskOutputType.equals(AxStateTaskOutputType.LOGIC)) {
1079 if (!state.getStateFinalizerLogicMap().containsKey(outputRefKey.getLocalName())) {
1080 return new ApexApiResult(
1081 ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1082 "state finalizer logic concept " + outputRefKey.getId() + DOES_NOT_EXIST);
1085 return new ApexApiResult(
1086 ApexApiResult.Result.FAILED, "output type " + builder.getOutputType() + " invalid");
1090 .getTaskReferences()
1091 .put(task.getKey(), new AxStateTaskReference(refKey, stateTaskOutputType, outputRefKey));
1092 return new ApexApiResult();
1093 } catch (final Exception e) {
1094 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
1099 * List policy state task references.
1101 * @param name name of the policy
1102 * @param version version of the policy, set to null to use the latest version
1103 * @param stateName of the state
1104 * @param taskName name of the task, set to null to list all task references
1105 * @param taskVersion version of the task, set to null to use the latest version
1106 * @return result of the operation
1108 public ApexApiResult listPolicyStateTaskRef(final String name, final String version, final String stateName,
1109 final String taskName, final String taskVersion) {
1111 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
1113 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
1114 if (policy == null) {
1115 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1116 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
1119 final AxState state = policy.getStateMap().get(stateName);
1120 if (state == null) {
1121 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1122 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
1125 final ApexApiResult result = new ApexApiResult();
1126 boolean found = false;
1127 final Map<AxArtifactKey, AxStateTaskReference> taskReferences = state.getTaskReferences();
1128 for (final Entry<AxArtifactKey, AxStateTaskReference> taskReferenceEntry : taskReferences.entrySet()) {
1129 final AxArtifactKey key = taskReferenceEntry.getKey();
1130 final AxStateTaskReference value = taskReferenceEntry.getValue();
1131 if ((taskName != null && !key.getName().equals(taskName))
1132 || (taskVersion != null && !key.getVersion().equals(taskVersion))) {
1137 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(key, AxArtifactKey.class,
1139 result.addMessage(new ApexModelStringWriter<AxStateTaskReference>(false).writeString(value,
1140 AxStateTaskReference.class, jsonMode));
1145 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1146 "no task references found for state " + state.getKey().getId());
1148 } catch (final Exception e) {
1149 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
1154 * Delete a policy state task reference.
1156 * @param name name of the policy
1157 * @param version version of the policy, set to null to use the latest version
1158 * @param stateName of the state
1159 * @param taskName name of the task, set to null to delete all task references
1160 * @param taskVersion version of the task, set to null to use the latest version
1161 * @return result of the operation
1163 public ApexApiResult deletePolicyStateTaskRef(final String name, final String version, final String stateName,
1164 final String taskName, final String taskVersion) {
1166 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
1168 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
1169 if (policy == null) {
1170 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1171 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
1174 final AxState state = policy.getStateMap().get(stateName);
1175 if (state == null) {
1176 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1177 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
1180 final Set<AxArtifactKey> deleteSet = new TreeSet<>();
1182 for (final AxArtifactKey taskReferenceKey : state.getTaskReferences().keySet()) {
1183 if ((taskName != null && !taskReferenceKey.getName().equals(taskName))
1184 || (taskVersion != null && !taskReferenceKey.getVersion().equals(taskVersion))) {
1187 deleteSet.add(taskReferenceKey);
1189 if (deleteSet.isEmpty()) {
1190 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1191 CONCEPT + taskName + ':' + taskVersion + DOES_NOT_EXIST_ON_STATE + state.getKey().getId());
1194 final ApexApiResult result = new ApexApiResult();
1195 for (final AxArtifactKey keyToDelete : deleteSet) {
1196 state.getTaskReferences().remove(keyToDelete);
1197 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(keyToDelete,
1198 AxArtifactKey.class, jsonMode));
1201 } catch (final Exception e) {
1202 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
1207 * Create a policy state context album reference.
1209 * @param name name of the policy
1210 * @param version version of the policy, set to null to use the latest version
1211 * @param stateName of the state
1212 * @param contextAlbumName name of the context album for the context album reference
1213 * @param contextAlbumVersion version of the context album for the context album reference, set
1214 * to null to use the latest version
1215 * @return result of the operation
1217 public ApexApiResult createPolicyStateContextRef(final String name, final String version, final String stateName,
1218 final String contextAlbumName, final String contextAlbumVersion) {
1220 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
1222 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
1223 if (policy == null) {
1224 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1225 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
1228 final AxState state = policy.getStateMap().get(stateName);
1229 if (state == null) {
1230 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1231 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
1234 final AxContextAlbum contextAlbum =
1235 apexModel.getPolicyModel().getAlbums().get(contextAlbumName, contextAlbumVersion);
1236 if (contextAlbum == null) {
1237 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1238 CONCEPT + contextAlbumName + ':' + contextAlbumVersion + DOES_NOT_EXIST);
1241 if (state.getContextAlbumReferences().contains(contextAlbum.getKey())) {
1242 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, "concept album reference for concept "
1243 + contextAlbum.getKey().getId() + " already exists in state");
1246 state.getContextAlbumReferences().add(contextAlbum.getKey());
1247 return new ApexApiResult();
1248 } catch (final Exception e) {
1249 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
1254 * List policy state context album references.
1256 * @param name name of the policy
1257 * @param version version of the policy, set to null to use the latest version
1258 * @param stateName of the state
1259 * @param contextAlbumName name of the context album for the context album reference, set to
1260 * null to list all task context album references
1261 * @param contextAlbumVersion version of the context album for the context album reference, set
1262 * to null to use the latest version
1263 * @return result of the operation
1265 public ApexApiResult listPolicyStateContextRef(final String name, final String version, final String stateName,
1266 final String contextAlbumName, final String contextAlbumVersion) {
1268 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
1270 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
1271 if (policy == null) {
1272 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1273 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
1276 final AxState state = policy.getStateMap().get(stateName);
1277 if (state == null) {
1278 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1279 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
1282 final ApexApiResult result = new ApexApiResult();
1283 boolean found = false;
1284 for (final AxArtifactKey albumKey : state.getContextAlbumReferences()) {
1285 if ((contextAlbumName != null && !albumKey.getName().equals(contextAlbumName))
1286 || (contextAlbumVersion != null && !albumKey.getVersion().equals(contextAlbumVersion))) {
1289 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(albumKey,
1290 AxArtifactKey.class, jsonMode));
1294 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, CONCEPT + contextAlbumName + ':'
1295 + contextAlbumVersion + DOES_NOT_EXIST_ON_STATE + state.getKey().getId());
1298 } catch (final Exception e) {
1299 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
1304 * Delete a policy state context album reference.
1306 * @param name name of the policy
1307 * @param version version of the policy, set to null to use the default version
1308 * @param stateName of the state
1309 * @param contextAlbumName name of the context album for the context album reference, set to
1310 * null to delete all task context album references
1311 * @param contextAlbumVersion version of the context album for the context album reference, set
1312 * to null to use the latest version
1313 * @return result of the operation
1315 public ApexApiResult deletePolicyStateContextRef(final String name, final String version, final String stateName,
1316 final String contextAlbumName, final String contextAlbumVersion) {
1318 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
1320 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
1321 if (policy == null) {
1322 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1323 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
1326 final AxState state = policy.getStateMap().get(stateName);
1327 if (state == null) {
1328 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1329 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
1332 final Set<AxArtifactKey> deleteSet = new TreeSet<>();
1334 for (final AxArtifactKey albumKey : state.getContextAlbumReferences()) {
1336 if ((contextAlbumName != null && !albumKey.getName().equals(contextAlbumName))
1337 || (contextAlbumVersion != null && !albumKey.getVersion().equals(contextAlbumVersion))) {
1341 deleteSet.add(albumKey);
1343 if (deleteSet.isEmpty()) {
1344 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, CONCEPT + contextAlbumName + ':'
1345 + contextAlbumVersion + DOES_NOT_EXIST_ON_STATE + state.getKey().getId());
1348 final ApexApiResult result = new ApexApiResult();
1349 for (final AxArtifactKey keyToDelete : deleteSet) {
1350 state.getContextAlbumReferences().remove(keyToDelete);
1351 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(keyToDelete,
1352 AxArtifactKey.class, jsonMode));
1355 } catch (final Exception e) {
1356 return new ApexApiResult(ApexApiResult.Result.FAILED, e);