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;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
34 import org.onap.policy.apex.model.basicmodel.handling.ApexModelStringWriter;
35 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
36 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
37 import org.onap.policy.apex.model.modelapi.ApexApiResult;
38 import org.onap.policy.apex.model.modelapi.ApexModel;
39 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
40 import org.onap.policy.apex.model.policymodel.concepts.AxState;
41 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
42 import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput;
43 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskOutputType;
44 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference;
45 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
46 import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic;
47 import org.onap.policy.common.utils.validation.Assertions;
50 * This class acts as a facade for operations towards a policy model for policy operations.
52 * @author Liam Fallon (liam.fallon@ericsson.com)
54 public class PolicyFacade {
55 private static final String STATE_NAME_MAY_NOT_BE_NULL = "stateName may not be null";
56 private static final String DOES_NOT_EXIST_ON_STATE = " does not exist on state ";
57 private static final String STATE_FINALIZER_LOGIC = "state finalizer logic ";
58 private static final String DO_ES_NOT_EXIST = " do(es) not exist";
59 private static final String CONCEPT_S = "concept(s) ";
60 private static final String DOES_NOT_EXIST = " does not exist";
61 private static final String CONCEPT = "concept ";
62 private static final String ALREADY_EXISTS = " already exists";
64 // Apex model we're working towards
65 private final ApexModel apexModel;
67 // Properties to use for the model
68 private final Properties apexProperties;
70 // Facade classes for working towards the real Apex model
71 private final KeyInformationFacade keyInformationFacade;
73 // JSON output on list/delete if set
74 private final boolean jsonMode;
77 * Constructor that creates a policy facade for the Apex Model API.
79 * @param apexModel the apex model
80 * @param apexProperties Properties for the model
81 * @param jsonMode set to true to return JSON strings in list and delete operations, otherwise
84 public PolicyFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
85 this.apexModel = apexModel;
86 this.apexProperties = apexProperties;
87 this.jsonMode = jsonMode;
89 keyInformationFacade = new KeyInformationFacade(apexModel, apexProperties, jsonMode);
95 * @param name name of the policy
96 * @param version version of the policy, set to null to use the default version
97 * @param template template used to create the policy, set to null to use the default template
98 * @param firstState the first state of the policy
99 * @param uuid policy UUID, set to null to generate a UUID
100 * @param description policy description, set to null to generate a description
101 * @return result of the operation
103 public ApexApiResult createPolicy(final String name, final String version, final String template,
104 final String firstState, final String uuid, final String description) {
106 final AxArtifactKey key = new AxArtifactKey();
108 if (version != null) {
109 key.setVersion(version);
111 key.setVersion(apexProperties.getProperty("DEFAULT_CONCEPT_VERSION"));
114 String templateString = template;
115 if (templateString == null) {
116 templateString = apexProperties.getProperty("DEFAULT_POLICY_TEMPLATE");
119 if (apexModel.getPolicyModel().getPolicies().getPolicyMap().containsKey(key)) {
120 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, CONCEPT + key.getId() + ALREADY_EXISTS);
123 final AxPolicy policy = new AxPolicy(key);
124 policy.setTemplate(templateString);
125 policy.setFirstState(firstState);
127 apexModel.getPolicyModel().getPolicies().getPolicyMap().put(key, policy);
129 if (apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().containsKey(key)) {
130 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
132 return keyInformationFacade.createKeyInformation(name, version, uuid, description);
134 } catch (final Exception e) {
135 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
142 * @param name name of the policy
143 * @param version version of the policy, set to null to use the latest version
144 * @param template template used to create the policy, set to null to not update
145 * @param firstState the first state of the policy
146 * @param uuid policy UUID, set to null to not update
147 * @param description policy description, set to null to not update
148 * @return result of the operation
150 public ApexApiResult updatePolicy(final String name, final String version, final String template,
151 final String firstState, final String uuid, final String description) {
153 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
154 if (policy == null) {
155 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
156 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
159 if (template != null) {
160 policy.setTemplate(template);
162 if (firstState != null) {
163 policy.setFirstState(firstState);
166 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
167 } catch (final Exception e) {
168 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
175 * @param name name of the policy, set to null to list all
176 * @param version starting version of the policy, set to null to list all versions
177 * @return result of the operation
179 public ApexApiResult listPolicy(final String name, final String version) {
181 final Set<AxPolicy> policySet = apexModel.getPolicyModel().getPolicies().getAll(name, version);
182 if (name != null && policySet.isEmpty()) {
183 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
184 CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
187 final ApexApiResult result = new ApexApiResult();
188 for (final AxPolicy policy : policySet) {
190 new ApexModelStringWriter<AxPolicy>(false).writeString(policy, AxPolicy.class, jsonMode));
193 } catch (final Exception e) {
194 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
201 * @param name name of the policy
202 * @param version version of the policy, set to null to use the latest version
203 * @return result of the operation
205 public ApexApiResult deletePolicy(final String name, final String version) {
207 if (version != null) {
208 final AxArtifactKey key = new AxArtifactKey(name, version);
209 final AxPolicy removedPolicy = apexModel.getPolicyModel().getPolicies().getPolicyMap().remove(key);
210 if (removedPolicy != null) {
211 return new ApexApiResult(ApexApiResult.Result.SUCCESS, new ApexModelStringWriter<AxPolicy>(false)
212 .writeString(removedPolicy, AxPolicy.class, jsonMode));
214 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
215 CONCEPT + key.getId() + DOES_NOT_EXIST);
219 final Set<AxPolicy> policySet = apexModel.getPolicyModel().getPolicies().getAll(name, version);
220 if (policySet.isEmpty()) {
221 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
222 CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
225 final ApexApiResult result = new ApexApiResult();
226 for (final AxPolicy policy : policySet) {
228 new ApexModelStringWriter<AxPolicy>(false).writeString(policy, AxPolicy.class, jsonMode));
229 apexModel.getPolicyModel().getPolicies().getPolicyMap().remove(policy.getKey());
230 keyInformationFacade.deleteKeyInformation(name, version);
233 } catch (final Exception e) {
234 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
241 * @param name name of the policy, set to null to list all
242 * @param version starting version of the policy, set to null to list all versions
243 * @return result of the operation
245 public ApexApiResult validatePolicy(final String name, final String version) {
247 final Set<AxPolicy> policySet = apexModel.getPolicyModel().getPolicies().getAll(name, version);
248 if (policySet.isEmpty()) {
249 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
250 CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
253 final ApexApiResult result = new ApexApiResult();
254 for (final AxPolicy policy : policySet) {
255 final AxValidationResult validationResult = policy.validate(new AxValidationResult());
256 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(policy.getKey(),
257 AxArtifactKey.class, jsonMode));
258 result.addMessage(validationResult.toString());
261 } catch (final Exception e) {
262 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
267 * Create a policy state.
269 * @param name name of the policy
270 * @param version version of the policy, set to null to use the latest version
271 * @param stateName of the state
272 * @param triggerName name of the trigger event for this state
273 * @param triggerVersion version of the trigger event for this state, set to null to use the
275 * @param defaultTaskName the default task name
276 * @param defaltTaskVersion the default task version, set to null to use the latest version
277 * @return result of the operation
279 public ApexApiResult createPolicyState(final String name, final String version, final String stateName,
280 final String triggerName, final String triggerVersion, final String defaultTaskName,
281 final String defaltTaskVersion) {
283 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
285 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
286 if (policy == null) {
287 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
288 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
291 final AxReferenceKey refKey = new AxReferenceKey(policy.getKey(), stateName);
293 if (policy.getStateMap().containsKey(refKey.getLocalName())) {
294 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
295 CONCEPT + refKey.getId() + ALREADY_EXISTS);
298 final AxEvent triggerEvent = apexModel.getPolicyModel().getEvents().get(triggerName, triggerVersion);
299 if (triggerEvent == null) {
300 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
301 CONCEPT + triggerName + ':' + triggerVersion + DOES_NOT_EXIST);
304 final AxTask defaultTask = apexModel.getPolicyModel().getTasks().get(defaultTaskName, defaltTaskVersion);
305 if (defaultTask == null) {
306 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
307 CONCEPT + defaultTaskName + ':' + defaltTaskVersion + DOES_NOT_EXIST);
310 final AxState state = new AxState(refKey);
311 state.setTrigger(triggerEvent.getKey());
312 state.setDefaultTask(defaultTask.getKey());
314 policy.getStateMap().put(state.getKey().getLocalName(), state);
315 return new ApexApiResult();
316 } catch (final Exception e) {
317 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
322 * Update a policy state.
324 * @param name name of the policy
325 * @param version version of the policy, set to null to use the latest version
326 * @param stateName of the state
327 * @param triggerName name of the trigger event for this state, set to null to not update
328 * @param triggerVersion version of the trigger event for this state, set to use latest version
330 * @param defaultTaskName the default task name, set to null to not update
331 * @param defaltTaskVersion the default task version, set to use latest version of default task
332 * @return result of the operation
334 public ApexApiResult updatePolicyState(final String name, final String version, final String stateName,
335 final String triggerName, final String triggerVersion, final String defaultTaskName,
336 final String defaltTaskVersion) {
338 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
340 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
341 if (policy == null) {
342 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
343 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
346 final AxState state = policy.getStateMap().get(stateName);
348 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
349 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
352 if (triggerName != null) {
353 final AxEvent triggerEvent = apexModel.getPolicyModel().getEvents().get(triggerName, triggerVersion);
354 if (triggerEvent == null) {
355 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
356 CONCEPT + triggerName + ':' + triggerVersion + DOES_NOT_EXIST);
358 state.setTrigger(triggerEvent.getKey());
361 if (defaultTaskName != null) {
362 final AxTask defaultTask =
363 apexModel.getPolicyModel().getTasks().get(defaultTaskName, defaltTaskVersion);
364 if (defaultTask == null) {
365 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
366 CONCEPT + defaultTaskName + ':' + defaltTaskVersion + DOES_NOT_EXIST);
368 state.setDefaultTask(defaultTask.getKey());
371 return new ApexApiResult();
372 } catch (final Exception e) {
373 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
378 * List policy states.
380 * @param name name of the policy
381 * @param version version of the policy, set to null to use the latest version
382 * @param stateName of the state, set to null to list all states of the policy
383 * @return result of the operation
385 public ApexApiResult listPolicyState(final String name, final String version, final String stateName) {
387 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
388 if (policy == null) {
389 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
390 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
393 if (stateName != null) {
394 final AxState state = policy.getStateMap().get(stateName);
396 return new ApexApiResult(ApexApiResult.Result.SUCCESS,
397 new ApexModelStringWriter<AxState>(false).writeString(state, AxState.class, jsonMode));
399 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
400 CONCEPT + name + ':' + version + ':' + state + DOES_NOT_EXIST);
403 if (policy.getStateMap().size() == 0) {
404 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
405 "no states defined on policy " + policy.getKey().getId());
407 final ApexApiResult result = new ApexApiResult();
408 for (final AxState state : policy.getStateMap().values()) {
410 new ApexModelStringWriter<AxState>(false).writeString(state, AxState.class, jsonMode));
414 } catch (final Exception e) {
415 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
420 * Delete a policy state.
422 * @param name name of the policy
423 * @param version version of the policy, set to null to use the latest version
424 * @param stateName of the state, set to null to delete all states
425 * @return result of the operation
427 public ApexApiResult deletePolicyState(final String name, final String version, final String stateName) {
429 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
430 if (policy == null) {
431 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
432 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
435 final ApexApiResult result = new ApexApiResult();
436 if (stateName != null) {
437 if (policy.getStateMap().containsKey(stateName)) {
438 result.addMessage(new ApexModelStringWriter<AxState>(false)
439 .writeString(policy.getStateMap().get(stateName), AxState.class, jsonMode));
440 policy.getStateMap().remove(stateName);
443 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
444 CONCEPT + name + ':' + version + ':' + stateName + DOES_NOT_EXIST);
447 if (policy.getStateMap().size() == 0) {
448 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
449 "no states defined on policy " + policy.getKey().getId());
451 for (final AxState state : policy.getStateMap().values()) {
453 new ApexModelStringWriter<AxState>(false).writeString(state, AxState.class, jsonMode));
455 policy.getStateMap().clear();
458 } catch (final Exception e) {
459 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
464 * Create task selection logic for a state.
466 * @param name name of the policy
467 * @param version version of the policy, set to null to use the latest version
468 * @param stateName of the state
469 * @param logicFlavour the task selection logic flavour for the state, set to null to use the
470 * default task logic flavour
471 * @param logic the source code for the logic of the state
472 * @return result of the operation
474 public ApexApiResult createPolicyStateTaskSelectionLogic(final String name, final String version,
475 final String stateName, final String logicFlavour, final String logic) {
477 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
479 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
480 if (policy == null) {
481 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
482 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
485 final AxState state = policy.getStateMap().get(stateName);
487 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
488 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
491 // There is only one logic item associated with a state so we use a hard coded logic
493 final AxReferenceKey refKey = new AxReferenceKey(state.getKey(), "TaskSelectionLogic");
495 if (!state.getTaskSelectionLogic().getKey().getLocalName().equals(AxKey.NULL_KEY_NAME)) {
496 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
497 CONCEPT + refKey.getId() + ALREADY_EXISTS);
500 state.setTaskSelectionLogic(new AxTaskSelectionLogic(refKey, logicFlavour, logic));
501 return new ApexApiResult();
502 } catch (final Exception e) {
503 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
508 * Update task selection logic for a state.
510 * @param name name of the policy
511 * @param version version of the policy, set to null to use the latest version
512 * @param stateName of the state
513 * @param logicFlavour the task selection logic flavour for the state, set to null to not update
514 * @param logic the source code for the logic of the state, set to null to not update
515 * @return result of the operation
517 public ApexApiResult updatePolicyStateTaskSelectionLogic(final String name, final String version,
518 final String stateName, final String logicFlavour, final String logic) {
520 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
522 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
523 if (policy == null) {
524 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
525 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
528 final AxState state = policy.getStateMap().get(stateName);
530 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
531 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
534 if (state.getTaskSelectionLogic().getKey().getLocalName().equals(AxKey.NULL_KEY_NAME)) {
535 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
536 CONCEPT + state.getTaskSelectionLogic().getKey().getId() + DOES_NOT_EXIST);
539 final AxTaskSelectionLogic taskSelectionLogic = state.getTaskSelectionLogic();
540 if (logicFlavour != null) {
541 taskSelectionLogic.setLogicFlavour(logicFlavour);
544 taskSelectionLogic.setLogic(logic);
547 return new ApexApiResult();
548 } catch (final Exception e) {
549 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
554 * List task selection logic for a state.
556 * @param name name of the policy
557 * @param version version of the policy, set to null to use the latest version
558 * @param stateName of the state
559 * @return result of the operation
561 public ApexApiResult listPolicyStateTaskSelectionLogic(final String name, final String version,
562 final String stateName) {
564 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
566 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
567 if (policy == null) {
568 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
569 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
572 final AxState state = policy.getStateMap().get(stateName);
574 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
575 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
578 return new ApexApiResult(ApexApiResult.Result.SUCCESS,
579 new ApexModelStringWriter<AxTaskSelectionLogic>(false).writeString(state.getTaskSelectionLogic(),
580 AxTaskSelectionLogic.class, jsonMode));
581 } catch (final Exception e) {
582 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
587 * Delete task selection logic for a state.
589 * @param name name of the policy
590 * @param version version of the policy, set to null to use the latest version
591 * @param stateName of the state
592 * @return result of the operation
594 public ApexApiResult deletePolicyStateTaskSelectionLogic(final String name, final String version,
595 final String stateName) {
597 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
599 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
600 if (policy == null) {
601 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
602 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
605 final AxState state = policy.getStateMap().get(stateName);
607 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
608 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
611 if (state.getTaskSelectionLogic().getKey().getLocalName().equals(AxKey.NULL_KEY_NAME)) {
612 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
613 CONCEPT + state.getTaskSelectionLogic().getKey().getId() + DOES_NOT_EXIST);
616 final ApexApiResult result = new ApexApiResult();
617 result.addMessage(new ApexModelStringWriter<AxTaskSelectionLogic>(false)
618 .writeString(state.getTaskSelectionLogic(), AxTaskSelectionLogic.class, jsonMode));
619 state.setTaskSelectionLogic(new AxTaskSelectionLogic());
621 } catch (final Exception e) {
622 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
627 * Create a policy state output.
629 * @param name name of the policy
630 * @param version version of the policy, set to null to use the latest version
631 * @param stateName of the state
632 * @param outputName of the state output
633 * @param eventName name of the output event for this state output
634 * @param eventVersion version of the output event for this state output, set to null to use the
636 * @param nextState for this state to transition to, set to null if this is the last state that
637 * the policy transitions to on this branch
638 * @return result of the operation
640 public ApexApiResult createPolicyStateOutput(final String name, final String version, final String stateName,
641 final String outputName, final String eventName, final String eventVersion, final String nextState) {
643 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
644 Assertions.argumentNotNull(outputName, "outputName may not be null");
646 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
647 if (policy == null) {
648 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
649 "Policy concept " + name + ':' + version + DOES_NOT_EXIST);
652 final AxState state = policy.getStateMap().get(stateName);
654 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
655 "State concept " + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
658 final AxReferenceKey refKey = new AxReferenceKey(state.getKey(), outputName);
659 if (state.getStateOutputs().containsKey(refKey.getLocalName())) {
660 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
661 "Output concept " + refKey.getId() + ALREADY_EXISTS);
664 final AxEvent event = apexModel.getPolicyModel().getEvents().get(eventName, eventVersion);
666 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
667 "Event concept " + eventName + ':' + eventVersion + DOES_NOT_EXIST);
670 AxReferenceKey nextStateKey = AxReferenceKey.getNullKey();
671 if (nextState != null && !(AxReferenceKey.getNullKey().getLocalName().equals(nextState))) {
672 if (state.getKey().getLocalName().equals(nextState)) {
673 return new ApexApiResult(ApexApiResult.Result.FAILED,
674 "next state " + nextState + " of a state cannot be the state itself");
676 nextStateKey = new AxReferenceKey(state.getKey().getParentArtifactKey(), nextState);
678 if (!policy.getStateMap().containsKey(nextState)) {
679 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
680 "Next state concept " + nextStateKey.getId() + DOES_NOT_EXIST);
684 state.getStateOutputs().put(refKey.getLocalName(), new AxStateOutput(refKey, event.getKey(), nextStateKey));
685 return new ApexApiResult();
686 } catch (final Exception e) {
687 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
692 * List policy state outputs.
694 * @param name name of the policy
695 * @param version version of the policy, set to null to use the latest version
696 * @param stateName of the state
697 * @param outputName of the state output, set to null to list all outputs of the state
698 * @return result of the operation
700 public ApexApiResult listPolicyStateOutput(final String name, final String version, final String stateName,
701 final String outputName) {
703 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
705 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
706 if (policy == null) {
707 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
708 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
711 final AxState state = policy.getStateMap().get(stateName);
713 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
714 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
717 if (outputName != null) {
718 final AxStateOutput stateOutput = state.getStateOutputs().get(outputName);
719 if (stateOutput != null) {
720 return new ApexApiResult(ApexApiResult.Result.SUCCESS,
721 new ApexModelStringWriter<AxStateOutput>(false).writeString(stateOutput,
722 AxStateOutput.class, jsonMode));
724 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
725 CONCEPT + state.getKey().getId() + ':' + outputName + DOES_NOT_EXIST);
728 if (state.getStateOutputs().size() == 0) {
729 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
730 "no state output concepts exist for state " + state.getKey().getId());
733 final ApexApiResult result = new ApexApiResult();
735 for (final AxStateOutput stateOutput : state.getStateOutputs().values()) {
736 result.addMessage(new ApexModelStringWriter<AxStateOutput>(false).writeString(stateOutput,
737 AxStateOutput.class, jsonMode));
741 } catch (final Exception e) {
742 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
747 * Delete a policy state output.
749 * @param name name of the policy
750 * @param version version of the policy, set to null to use the latest version
751 * @param stateName of the state
752 * @param outputName of the state output, set to null to delete all state outputs
753 * @return result of the operation
755 public ApexApiResult deletePolicyStateOutput(final String name, final String version, final String stateName,
756 final String outputName) {
758 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
760 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
761 if (policy == null) {
762 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
763 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
766 final AxState state = policy.getStateMap().get(stateName);
768 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
769 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
772 if (outputName != null) {
773 final AxStateOutput stateOutput = state.getStateOutputs().get(outputName);
774 if (stateOutput != null) {
775 final ApexApiResult result = new ApexApiResult(ApexApiResult.Result.SUCCESS,
776 new ApexModelStringWriter<AxStateOutput>(false).writeString(stateOutput,
777 AxStateOutput.class, jsonMode));
778 state.getStateOutputs().remove(outputName);
781 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
782 CONCEPT + state.getKey().getId() + ':' + outputName + DOES_NOT_EXIST);
785 if (state.getStateOutputs().size() == 0) {
786 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
787 "no state output concepts exist for state " + state.getKey().getId());
790 final ApexApiResult result = new ApexApiResult();
792 for (final Entry<String, AxStateOutput> stateOutputEntry : state.getStateOutputs().entrySet()) {
793 result.addMessage(new ApexModelStringWriter<AxStateOutput>(false)
794 .writeString(stateOutputEntry.getValue(), AxStateOutput.class, jsonMode));
796 state.getStateOutputs().clear();
799 } catch (final Exception e) {
800 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
805 * Create policy finalizer logic for a state.
807 * @param name name of the policy
808 * @param version version of the policy, set to null to use the latest version
809 * @param stateName of the state
810 * @param finalizerLogicName name of the state finalizer logic
811 * @param logicFlavour the policy finalizer logic flavour for the state, set to null to use the
812 * default task logic flavour
813 * @param logic the source code for the logic of the state
814 * @return result of the operation
816 public ApexApiResult createPolicyStateFinalizerLogic(final String name, final String version,
817 final String stateName, final String finalizerLogicName, final String logicFlavour, final String logic) {
819 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
820 Assertions.argumentNotNull(finalizerLogicName, "finalizerlogicName may not be null");
822 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
823 if (policy == null) {
824 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
825 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
828 final AxState state = policy.getStateMap().get(stateName);
830 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
831 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
834 final AxReferenceKey refKey = new AxReferenceKey(state.getKey(), finalizerLogicName);
836 if (state.getStateFinalizerLogicMap().containsKey(refKey.getLocalName())) {
837 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
838 CONCEPT + refKey.getId() + ALREADY_EXISTS);
841 state.getStateFinalizerLogicMap().put(finalizerLogicName,
842 new AxStateFinalizerLogic(refKey, logicFlavour, logic));
843 return new ApexApiResult();
844 } catch (final Exception e) {
845 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
850 * Update policy finalizer logic for a state.
852 * @param name name of the policy
853 * @param version version of the policy, set to null to use the latest version
854 * @param stateName of the state
855 * @param finalizerLogicName name of the state finalizer logic
856 * @param logicFlavour the policy finalizer logic flavour for the state, set to null to not
858 * @param logic the source code for the logic of the state, set to null to not update
859 * @return result of the operation
861 public ApexApiResult updatePolicyStateFinalizerLogic(final String name, final String version,
862 final String stateName, final String finalizerLogicName, final String logicFlavour, final String logic) {
864 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
865 Assertions.argumentNotNull(finalizerLogicName, "finalizerLogicName may not be null");
867 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
868 if (policy == null) {
869 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
870 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
873 final AxState state = policy.getStateMap().get(stateName);
875 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
876 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
879 final AxReferenceKey refKey = new AxReferenceKey(state.getKey(), finalizerLogicName);
880 final AxStateFinalizerLogic stateFinalizerLogic =
881 state.getStateFinalizerLogicMap().get(refKey.getKey().getLocalName());
882 if (stateFinalizerLogic == null) {
883 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
884 STATE_FINALIZER_LOGIC + refKey.getId() + DOES_NOT_EXIST);
887 if (logicFlavour != null) {
888 stateFinalizerLogic.setLogicFlavour(logicFlavour);
891 stateFinalizerLogic.setLogic(logic);
894 return new ApexApiResult();
895 } catch (final Exception e) {
896 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
901 * List policy finalizer logic for a state.
903 * @param name name of the policy
904 * @param version version of the policy, set to null to use the latest version
905 * @param stateName of the state
906 * @param finalizerLogicName name of the state finalizer logic
907 * @return result of the operation
909 public ApexApiResult listPolicyStateFinalizerLogic(final String name, final String version, final String stateName,
910 final String finalizerLogicName) {
912 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
914 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
915 if (policy == null) {
916 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
917 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
920 final AxState state = policy.getStateMap().get(stateName);
922 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
923 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
926 if (finalizerLogicName != null) {
927 final AxReferenceKey refKey = new AxReferenceKey(state.getKey(), finalizerLogicName);
928 final AxStateFinalizerLogic stateFinalizerLogic =
929 state.getStateFinalizerLogicMap().get(refKey.getKey().getLocalName());
930 if (stateFinalizerLogic == null) {
931 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
932 STATE_FINALIZER_LOGIC + refKey.getId() + DOES_NOT_EXIST);
935 return new ApexApiResult(ApexApiResult.Result.SUCCESS,
936 new ApexModelStringWriter<AxStateFinalizerLogic>(false).writeString(stateFinalizerLogic,
937 AxStateFinalizerLogic.class, jsonMode));
939 if (state.getStateFinalizerLogicMap().size() == 0) {
940 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
941 "no state finalizer logic defined on state " + state.getKey().getId());
943 final ApexApiResult result = new ApexApiResult();
944 for (final AxStateFinalizerLogic stateFinalizerLogic : state.getStateFinalizerLogicMap().values()) {
945 result.addMessage(new ApexModelStringWriter<AxStateFinalizerLogic>(false)
946 .writeString(stateFinalizerLogic, AxStateFinalizerLogic.class, jsonMode));
950 } catch (final Exception e) {
951 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
956 * Delete policy finalizer logic for a state.
958 * @param name name of the policy
959 * @param version version of the policy, set to null to use the latest version
960 * @param stateName of the state
961 * @param finalizerLogicName name of the state finalizer logic
962 * @return result of the operation
964 public ApexApiResult deletePolicyStateFinalizerLogic(final String name, final String version,
965 final String stateName, final String finalizerLogicName) {
967 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
969 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
970 if (policy == null) {
971 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
972 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
975 final AxState state = policy.getStateMap().get(stateName);
977 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
978 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
981 if (finalizerLogicName != null) {
982 final AxReferenceKey refKey = new AxReferenceKey(state.getKey(), finalizerLogicName);
983 final AxStateFinalizerLogic stateFinalizerLogic =
984 state.getStateFinalizerLogicMap().get(refKey.getKey().getLocalName());
985 if (stateFinalizerLogic == null) {
986 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
987 STATE_FINALIZER_LOGIC + refKey.getId() + DOES_NOT_EXIST);
990 final ApexApiResult result = new ApexApiResult();
991 result.addMessage(new ApexModelStringWriter<AxStateFinalizerLogic>(false)
992 .writeString(stateFinalizerLogic, AxStateFinalizerLogic.class, jsonMode));
993 state.getStateFinalizerLogicMap().remove(refKey.getLocalName());
996 if (state.getStateFinalizerLogicMap().size() == 0) {
997 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
998 "no state finalizer logic defined on state " + state.getKey().getId());
1001 final ApexApiResult result = new ApexApiResult();
1002 for (final AxStateFinalizerLogic stateFinalizerLogic : state.getStateFinalizerLogicMap().values()) {
1003 result.addMessage(new ApexModelStringWriter<AxStateFinalizerLogic>(false)
1004 .writeString(stateFinalizerLogic, AxStateFinalizerLogic.class, jsonMode));
1006 state.getStateFinalizerLogicMap().clear();
1009 } catch (final Exception e) {
1010 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
1015 * Create a policy state task reference.
1017 * @param builder builder for the state task reference
1018 * @return result of the operation
1020 public ApexApiResult createPolicyStateTaskRef(CreatePolicyStateTaskRefBuilder builder) {
1022 Assertions.argumentNotNull(builder.getStateName(), STATE_NAME_MAY_NOT_BE_NULL);
1023 Assertions.argumentNotNull(builder.getOutputName(), "outputName may not be null");
1025 final AxPolicy policy =
1026 apexModel.getPolicyModel().getPolicies().get(builder.getName(), builder.getVersion());
1027 if (policy == null) {
1028 return new ApexApiResult(
1029 ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1030 CONCEPT + builder.getName() + ':' + builder.getVersion() + DOES_NOT_EXIST);
1033 final AxState state = policy.getStateMap().get(builder.getStateName());
1034 if (state == null) {
1035 return new ApexApiResult(
1036 ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1037 CONCEPT + policy.getKey().getId() + ':' + builder.getStateName() + DOES_NOT_EXIST);
1044 .get(builder.getTaskName(), builder.getTaskVersion());
1046 return new ApexApiResult(
1047 ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1048 CONCEPT + builder.getTaskName() + ':' + builder.getTaskVersion() + DOES_NOT_EXIST);
1051 if (state.getTaskReferences().containsKey(task.getKey())) {
1052 return new ApexApiResult(
1053 ApexApiResult.Result.CONCEPT_EXISTS,
1055 + task.getKey().getId()
1056 + " already has reference with output "
1057 + state.getTaskReferences().get(task.getKey()));
1060 AxReferenceKey refKey;
1061 if (builder.getTaskLocalName() == null) {
1062 refKey = new AxReferenceKey(state.getKey(), state.getKey().getParentKeyName());
1064 refKey = new AxReferenceKey(state.getKey(), builder.getTaskLocalName());
1067 // The reference to the output we're using here
1068 final AxReferenceKey outputRefKey =
1069 new AxReferenceKey(state.getKey(), builder.getOutputName());
1071 final AxStateTaskOutputType stateTaskOutputType =
1072 AxStateTaskOutputType.valueOf(builder.getOutputType());
1073 if (stateTaskOutputType.equals(AxStateTaskOutputType.DIRECT)) {
1074 if (!state.getStateOutputs().containsKey(outputRefKey.getLocalName())) {
1075 return new ApexApiResult(
1076 ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1077 "state output concept " + outputRefKey.getId() + DOES_NOT_EXIST);
1079 } else if (stateTaskOutputType.equals(AxStateTaskOutputType.LOGIC)) {
1080 if (!state.getStateFinalizerLogicMap().containsKey(outputRefKey.getLocalName())) {
1081 return new ApexApiResult(
1082 ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1083 "state finalizer logic concept " + outputRefKey.getId() + DOES_NOT_EXIST);
1086 return new ApexApiResult(
1087 ApexApiResult.Result.FAILED, "output type " + builder.getOutputType() + " invalid");
1091 .getTaskReferences()
1092 .put(task.getKey(), new AxStateTaskReference(refKey, stateTaskOutputType, outputRefKey));
1093 return new ApexApiResult();
1094 } catch (final Exception e) {
1095 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
1100 * List policy state task references.
1102 * @param name name of the policy
1103 * @param version version of the policy, set to null to use the latest version
1104 * @param stateName of the state
1105 * @param taskName name of the task, set to null to list all task references
1106 * @param taskVersion version of the task, set to null to use the latest version
1107 * @return result of the operation
1109 public ApexApiResult listPolicyStateTaskRef(final String name, final String version, final String stateName,
1110 final String taskName, final String taskVersion) {
1112 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
1114 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
1115 if (policy == null) {
1116 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1117 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
1120 final AxState state = policy.getStateMap().get(stateName);
1121 if (state == null) {
1122 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1123 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
1126 final ApexApiResult result = new ApexApiResult();
1127 boolean found = false;
1128 final Map<AxArtifactKey, AxStateTaskReference> taskReferences = state.getTaskReferences();
1129 for (final Entry<AxArtifactKey, AxStateTaskReference> taskReferenceEntry : taskReferences.entrySet()) {
1130 final AxArtifactKey key = taskReferenceEntry.getKey();
1131 final AxStateTaskReference value = taskReferenceEntry.getValue();
1132 if ((taskName != null && !key.getName().equals(taskName))
1133 || (taskVersion != null && !key.getVersion().equals(taskVersion))) {
1138 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(key, AxArtifactKey.class,
1140 result.addMessage(new ApexModelStringWriter<AxStateTaskReference>(false).writeString(value,
1141 AxStateTaskReference.class, jsonMode));
1146 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1147 "no task references found for state " + state.getKey().getId());
1149 } catch (final Exception e) {
1150 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
1155 * Delete a policy state task reference.
1157 * @param name name of the policy
1158 * @param version version of the policy, set to null to use the latest version
1159 * @param stateName of the state
1160 * @param taskName name of the task, set to null to delete all task references
1161 * @param taskVersion version of the task, set to null to use the latest version
1162 * @return result of the operation
1164 public ApexApiResult deletePolicyStateTaskRef(final String name, final String version, final String stateName,
1165 final String taskName, final String taskVersion) {
1167 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
1169 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
1170 if (policy == null) {
1171 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1172 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
1175 final AxState state = policy.getStateMap().get(stateName);
1176 if (state == null) {
1177 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1178 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
1181 final Set<AxArtifactKey> deleteSet = new TreeSet<>();
1183 for (final AxArtifactKey taskReferenceKey : state.getTaskReferences().keySet()) {
1184 if ((taskName != null && !taskReferenceKey.getName().equals(taskName))
1185 || (taskVersion != null && !taskReferenceKey.getVersion().equals(taskVersion))) {
1188 deleteSet.add(taskReferenceKey);
1190 if (deleteSet.isEmpty()) {
1191 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1192 CONCEPT + taskName + ':' + taskVersion + DOES_NOT_EXIST_ON_STATE + state.getKey().getId());
1195 final ApexApiResult result = new ApexApiResult();
1196 for (final AxArtifactKey keyToDelete : deleteSet) {
1197 state.getTaskReferences().remove(keyToDelete);
1198 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(keyToDelete,
1199 AxArtifactKey.class, jsonMode));
1202 } catch (final Exception e) {
1203 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
1208 * Create a policy state context album reference.
1210 * @param name name of the policy
1211 * @param version version of the policy, set to null to use the latest version
1212 * @param stateName of the state
1213 * @param contextAlbumName name of the context album for the context album reference
1214 * @param contextAlbumVersion version of the context album for the context album reference, set
1215 * to null to use the latest version
1216 * @return result of the operation
1218 public ApexApiResult createPolicyStateContextRef(final String name, final String version, final String stateName,
1219 final String contextAlbumName, final String contextAlbumVersion) {
1221 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
1223 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
1224 if (policy == null) {
1225 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1226 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
1229 final AxState state = policy.getStateMap().get(stateName);
1230 if (state == null) {
1231 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1232 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
1235 final AxContextAlbum contextAlbum =
1236 apexModel.getPolicyModel().getAlbums().get(contextAlbumName, contextAlbumVersion);
1237 if (contextAlbum == null) {
1238 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1239 CONCEPT + contextAlbumName + ':' + contextAlbumVersion + DOES_NOT_EXIST);
1242 if (state.getContextAlbumReferences().contains(contextAlbum.getKey())) {
1243 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, "concept album reference for concept "
1244 + contextAlbum.getKey().getId() + " already exists in state");
1247 state.getContextAlbumReferences().add(contextAlbum.getKey());
1248 return new ApexApiResult();
1249 } catch (final Exception e) {
1250 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
1255 * List policy state context album references.
1257 * @param name name of the policy
1258 * @param version version of the policy, set to null to use the latest version
1259 * @param stateName of the state
1260 * @param contextAlbumName name of the context album for the context album reference, set to
1261 * null to list all task context album references
1262 * @param contextAlbumVersion version of the context album for the context album reference, set
1263 * to null to use the latest version
1264 * @return result of the operation
1266 public ApexApiResult listPolicyStateContextRef(final String name, final String version, final String stateName,
1267 final String contextAlbumName, final String contextAlbumVersion) {
1269 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
1271 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
1272 if (policy == null) {
1273 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1274 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
1277 final AxState state = policy.getStateMap().get(stateName);
1278 if (state == null) {
1279 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1280 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
1283 final ApexApiResult result = new ApexApiResult();
1284 boolean found = false;
1285 for (final AxArtifactKey albumKey : state.getContextAlbumReferences()) {
1286 if ((contextAlbumName != null && !albumKey.getName().equals(contextAlbumName))
1287 || (contextAlbumVersion != null && !albumKey.getVersion().equals(contextAlbumVersion))) {
1290 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(albumKey,
1291 AxArtifactKey.class, jsonMode));
1295 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, CONCEPT + contextAlbumName + ':'
1296 + contextAlbumVersion + DOES_NOT_EXIST_ON_STATE + state.getKey().getId());
1299 } catch (final Exception e) {
1300 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
1305 * Delete a policy state context album reference.
1307 * @param name name of the policy
1308 * @param version version of the policy, set to null to use the default version
1309 * @param stateName of the state
1310 * @param contextAlbumName name of the context album for the context album reference, set to
1311 * null to delete all task context album references
1312 * @param contextAlbumVersion version of the context album for the context album reference, set
1313 * to null to use the latest version
1314 * @return result of the operation
1316 public ApexApiResult deletePolicyStateContextRef(final String name, final String version, final String stateName,
1317 final String contextAlbumName, final String contextAlbumVersion) {
1319 Assertions.argumentNotNull(stateName, STATE_NAME_MAY_NOT_BE_NULL);
1321 final AxPolicy policy = apexModel.getPolicyModel().getPolicies().get(name, version);
1322 if (policy == null) {
1323 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1324 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
1327 final AxState state = policy.getStateMap().get(stateName);
1328 if (state == null) {
1329 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
1330 CONCEPT + policy.getKey().getId() + ':' + stateName + DOES_NOT_EXIST);
1333 final Set<AxArtifactKey> deleteSet = new TreeSet<>();
1335 for (final AxArtifactKey albumKey : state.getContextAlbumReferences()) {
1337 if ((contextAlbumName != null && !albumKey.getName().equals(contextAlbumName))
1338 || (contextAlbumVersion != null && !albumKey.getVersion().equals(contextAlbumVersion))) {
1342 deleteSet.add(albumKey);
1344 if (deleteSet.isEmpty()) {
1345 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, CONCEPT + contextAlbumName + ':'
1346 + contextAlbumVersion + DOES_NOT_EXIST_ON_STATE + state.getKey().getId());
1349 final ApexApiResult result = new ApexApiResult();
1350 for (final AxArtifactKey keyToDelete : deleteSet) {
1351 state.getContextAlbumReferences().remove(keyToDelete);
1352 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(keyToDelete,
1353 AxArtifactKey.class, jsonMode));
1356 } catch (final Exception e) {
1357 return new ApexApiResult(ApexApiResult.Result.FAILED, e);