2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.client.editor.rest;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonObject;
27 import java.util.ArrayList;
28 import java.util.List;
30 import java.util.Map.Entry;
31 import java.util.TreeMap;
33 import javax.ws.rs.Consumes;
34 import javax.ws.rs.DELETE;
35 import javax.ws.rs.GET;
36 import javax.ws.rs.POST;
37 import javax.ws.rs.PUT;
38 import javax.ws.rs.Path;
39 import javax.ws.rs.PathParam;
40 import javax.ws.rs.Produces;
41 import javax.ws.rs.QueryParam;
42 import javax.ws.rs.core.MediaType;
44 import org.onap.policy.apex.client.editor.rest.bean.BeanContextAlbum;
45 import org.onap.policy.apex.client.editor.rest.bean.BeanContextSchema;
46 import org.onap.policy.apex.client.editor.rest.bean.BeanEvent;
47 import org.onap.policy.apex.client.editor.rest.bean.BeanField;
48 import org.onap.policy.apex.client.editor.rest.bean.BeanKeyRef;
49 import org.onap.policy.apex.client.editor.rest.bean.BeanLogic;
50 import org.onap.policy.apex.client.editor.rest.bean.BeanModel;
51 import org.onap.policy.apex.client.editor.rest.bean.BeanPolicy;
52 import org.onap.policy.apex.client.editor.rest.bean.BeanState;
53 import org.onap.policy.apex.client.editor.rest.bean.BeanStateOutput;
54 import org.onap.policy.apex.client.editor.rest.bean.BeanStateTaskRef;
55 import org.onap.policy.apex.client.editor.rest.bean.BeanTask;
56 import org.onap.policy.apex.client.editor.rest.bean.BeanTaskParameter;
57 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
58 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
59 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
60 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
61 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
62 import org.onap.policy.apex.model.modelapi.ApexApiResult;
63 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
64 import org.onap.policy.apex.model.modelapi.ApexModel;
65 import org.onap.policy.apex.model.modelapi.ApexModelFactory;
66 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
67 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
68 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
69 import org.slf4j.ext.XLogger;
70 import org.slf4j.ext.XLoggerFactory;
73 * The class represents the root resource exposed at the base URL<br> The url to access this resource would be in the
74 * form {@code <baseURL>/rest/<session>/....} <br> For example: a PUT request to the following URL
75 * {@code http://localhost:8080/apex/rest/109/ContextSchema/Update}, with a JSON string payload containing the new
76 * {@code Schema} in the body, can be explained as: <ul> <li>The server or servlet is running at the base URL
77 * {@code http://localhost:8080/apex} <li>This resource {@code ApexRestEditorResource} is used because the path
78 * {@code rest/109} matches the {@code Path} filter specification for this Resource ({@code @Path("rest/{session}")}),
79 * where the {@code int} path parameter {@code session} is assigned the {@code int} value {@code 109} <li>The path
80 * {@code ContextSchema/Update} redirects this call to the method {@link #updateContextSchema(String)}, which should be
81 * a {@link javax.ws.rs.PUT}, with a single String in the body/payload which gets mapped to the single String parameter
82 * for the method. <li>So, in summary, the REST request updates a {@code ContextSchema} as specified in the payload for
83 * {@code session} number {@code 109} </ul>
85 * <b>Note:</b> An allocated {@code Session} identifier must be included in (almost) all requests. Models for different
86 * {@code Session} identifiers are completely isolated from one another.
88 * <b>Note:</b> To create a new {@code Session}, and have a new session ID allocated use {@link javax.ws.rs.GET} request
89 * to {@code <baseURL>/rest/-1/Session/Create} (for example: {@code http://localhost:8080/apex/rest/-1/Session/Create} )
92 @Path("editor/{session}")
94 { MediaType.APPLICATION_JSON })
96 { MediaType.APPLICATION_JSON })
98 public class ApexEditorRestResource {
99 // Get a reference to the logger
100 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEditorRestResource.class);
102 // Recurring string constants
103 private static final String IN_TASK = "\" in task ";
104 private static final String POLICY_STATE_CREATED_OTHER_ERROR =
105 "\". The policy and state were created, but there was an error adding the";
106 private static final String IN_STATE = "\" in state \"";
107 private static final String POLICY_CREATED_STATE_ERROR =
108 "\". The policy was created, but there was an error adding the state.";
109 private static final String FOR_POLICY = "\" for policy \"";
110 private static final String NOT_OK = ": Not OK";
111 private static final String TASK_PARTIALLY_DEFINED = " The task has only been partially defined.";
112 private static final String POLICY_PARTIALLY_DEFINED = " The policy has only been partially defined.";
113 private static final String POLICY_WAS_CREATED = "\". The policy was created, ";
114 private static final String VERSION = "version";
115 private static final String POLICY_KEY = "policyKey";
116 private static final String DESCRIPTION = "description";
117 private static final String APEX_KEY_INFO = "apexKeyInfo";
119 // The next session will have this number, stating at 0
120 private static int nextSession = 0;
122 // All REST editor sessions being handled by the server
123 private static final Map<Integer, ApexModel> SESSIONMODELMAP = new TreeMap<>();
125 // The ID of this session. This gets injected from the URL.
126 @PathParam("session")
127 private int sessionId = -1;
129 // The Apex model for the session
130 private ApexModel sessionApexModel = null;
133 * This method sets the Apex model for the current editor session. Don't forget to call {@link #commitChanges()}
134 * when finished! This makes requests atomic.
136 * @return the result of finding the session Apex model and setting it
138 private ApexApiResult initialiseSessionForChanges() {
140 return new ApexApiResult(Result.FAILED, "Session ID \"" + sessionId + "\" is negative");
143 if (!SESSIONMODELMAP.containsKey(sessionId)) {
144 return new ApexApiResult(Result.FAILED, "A session with session ID \"" + sessionId + "\" does not exist");
147 if (sessionApexModel == null) {
148 sessionApexModel = SESSIONMODELMAP.get(sessionId).clone();
150 return new ApexApiResult();
154 * This method sets the Apex model for the current editor session. Don't make any changes to the model.
156 * @return the result of finding the session Apex model and setting it
158 private ApexApiResult initialiseSessionForReadOnly() {
160 return new ApexApiResult(Result.FAILED, "Session ID \"" + sessionId + "\" is negative");
163 if (!SESSIONMODELMAP.containsKey(sessionId)) {
164 return new ApexApiResult(Result.FAILED, "A session with session ID \"" + sessionId + "\" does not exist");
167 if (sessionApexModel == null) {
168 sessionApexModel = SESSIONMODELMAP.get(sessionId);
170 return new ApexApiResult();
174 * This method commits changes to the Apex model for the current editor session. This should only be called once, at
175 * the end of a successful change to the model for this session
177 * @return the result of committing the session Apex model
179 private ApexApiResult commitChanges() {
181 if (sessionApexModel == null) {
182 return new ApexApiResult(Result.FAILED, "Cannot commit a changes for Session ID \"" + sessionId
183 + "\", because it has not been initialised / started");
186 SESSIONMODELMAP.put(sessionId, sessionApexModel);
188 return new ApexApiResult();
192 * Creates a new session. Always call this method with sessionID -1, whereby a new sessionID will be allocated. If
193 * successful the new sessionID will be available in the first message in the result.
195 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
196 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}. This includes the session id
200 @Path("Session/Create")
201 public ApexApiResult createSession() {
202 ApexApiResult ret = null;
205 if (sessionId != -1) {
206 ret = new ApexApiResult(Result.FAILED, "Session ID must be set to -1 to create sessions: " + sessionId);
210 final int newSessionId = nextSession;
212 if (SESSIONMODELMAP.containsKey(newSessionId)) {
213 ret = new ApexApiResult(Result.FAILED, "Session already exists for session: " + newSessionId);
217 SESSIONMODELMAP.put(newSessionId, new ApexModelFactory().createApexModel(null, true));
218 incrementNextSession();
220 ret = new ApexApiResult(Result.SUCCESS, Integer.toString(newSessionId));
222 } catch (final Exception e) {
226 String result = "Session/Create" + (ret != null && ret.isOk() ? ": OK" : NOT_OK);
232 * Load the model from a JSON string for this session.
234 * @param jsonString the JSON string to be parsed. The returned value(s) will be similar to {@link AxPolicyModel},
235 * with merged {@linkplain AxKeyInfo} for the root object.
236 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
237 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
241 public ApexApiResult loadFromString(final String jsonString) {
242 ApexApiResult ret = null;
243 LOGGER.entry(jsonString);
245 ret = initialiseSessionForChanges();
250 ret = sessionApexModel.loadFromString(jsonString);
255 } catch (final Exception e) {
259 String result = "Model/Load" + (ret != null && ret.isOk() ? ": OK" : NOT_OK);
265 * Analyse the model and return analysis results. If successful the analysis results will be available in the
266 * messages in the result.
268 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
269 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
272 @Path("Model/Analyse")
273 public ApexApiResult analyse() {
274 ApexApiResult ret = null;
277 ret = initialiseSessionForReadOnly();
282 ret = sessionApexModel.analyse();
284 } catch (final Exception e) {
288 LOGGER.exit("Model/Analyse" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
293 * Validate the model and return validation results. If successful the validation results will be available in the
294 * messages in the result.
296 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
297 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
300 @Path("Model/Validate")
301 public ApexApiResult validate() {
302 ApexApiResult ret = null;
305 ret = initialiseSessionForReadOnly();
310 ret = sessionApexModel.validate();
312 ret = addKeyInfo2Messages(ret);
314 } catch (final Exception e) {
318 LOGGER.exit("Model/Validate" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
323 * Creates the new model model for this session.
325 * @param jsonString the JSON string to be parsed containing the new model. See {@linkplain BeanModel}
326 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
327 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
330 @Path("Model/Create")
331 public ApexApiResult createModel(final String jsonString) {
332 ApexApiResult ret = null;
333 LOGGER.entry(jsonString);
335 ret = initialiseSessionForChanges();
340 final BeanModel jsonbean = RestUtils.getJsonParameters(jsonString, BeanModel.class);
341 ret = sessionApexModel.createModel(jsonbean.getName(), jsonbean.getVersion(), jsonbean.getUuid(),
342 jsonbean.getDescription());
345 ret = addKeyInfo2Messages(ret);
351 } catch (final Exception e) {
355 LOGGER.exit("Model/Create" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
360 * Update the model for this session.
362 * @param jsonString the JSON string to be parsed containing the updated model. See {@linkplain BeanModel}
363 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
364 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
367 @Path("Model/Update")
368 public ApexApiResult updateModel(final String jsonString) {
369 ApexApiResult ret = null;
370 LOGGER.entry(jsonString);
372 ret = initialiseSessionForChanges();
377 final BeanModel jsonbean = RestUtils.getJsonParameters(jsonString, BeanModel.class);
378 ret = sessionApexModel.updateModel(jsonbean.getName(), jsonbean.getVersion(), jsonbean.getUuid(),
379 jsonbean.getDescription());
384 } catch (final Exception e) {
388 LOGGER.exit("Model/Update" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
393 * Gets the key for the model for this session. If successful the model key will be available in the first message
394 * in the result. See {@linkplain AxKey}
396 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
397 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
400 @Path("Model/GetKey")
401 public ApexApiResult getModelKey() {
402 ApexApiResult ret = null;
405 ret = initialiseSessionForReadOnly();
410 ret = sessionApexModel.getModelKey();
412 } catch (final Exception e) {
416 LOGGER.exit("Model/GetKey" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
421 * Retrieve the model for this session. If successful the model will be available in the first message in the
422 * result. The returned value will be similar to a {@link AxPolicyModel}, with merged {@linkplain AxKeyInfo} for the
425 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
426 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
430 public ApexApiResult listModel() {
431 ApexApiResult ret = null;
434 ret = initialiseSessionForReadOnly();
439 ret = sessionApexModel.listModel();
444 ret = addKeyInfo2Messages(ret);
446 } catch (final Exception e) {
450 LOGGER.exit("Model/Get" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
455 * Download the model for this session as a String.
457 * @return the model represented as a JSON string. See {@linkplain AxPolicyModel}
460 @Path("Model/Download")
461 public String downloadModel() {
462 ApexApiResult ret = null;
466 ret = initialiseSessionForReadOnly();
468 throw new IllegalStateException("Cannot download file: " + ret.getMessage());
471 ret = sessionApexModel.listModel();
473 throw new IllegalStateException("Cannot download file: " + ret.getMessage());
476 return ret.getMessage();
477 } catch (final Exception e) {
481 String result = "Model/Download" + (ret != null && ret.isOk() ? ": OK" : NOT_OK);
487 * Delete the model for this session.
489 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
490 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
493 @Path("Model/Delete")
494 public ApexApiResult deleteModel() {
495 ApexApiResult ret = null;
498 ret = initialiseSessionForChanges();
503 ret = sessionApexModel.deleteModel();
508 } catch (final Exception e) {
512 LOGGER.exit("Model/Delete" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
517 * List key information with the given key names/versions. If successful the result(s) will be available in the
518 * result messages. See {@linkplain AxKeyInfo}
520 * @param name the name to search for. If null or empty, then all names will be queried
521 * @param version the version to search for. If null then all versions will be searched for.
522 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
523 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
526 @Path("KeyInformation/Get")
527 public ApexApiResult listKeyInformation(@QueryParam("name") final String name,
528 @QueryParam(VERSION) final String version) {
529 ApexApiResult ret = null;
531 String version1 = version;
532 LOGGER.entry(name1, version1);
534 ret = initialiseSessionForReadOnly();
539 if (name1 == null || name1.equals("")) {
542 if (version1 == null || version1.equals("")) {
546 ret = sessionApexModel.listKeyInformation(name1, version1);
548 } catch (final Exception e) {
552 LOGGER.exit("KeyInformation/Get" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
557 * Creates a context schema with the information in the JSON string passed.
559 * @param jsonString the JSON string to be parsed. See {@linkplain BeanContextSchema}
560 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
561 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
564 @Path("ContextSchema/Create")
565 public ApexApiResult createContextSchema(final String jsonString) {
566 ApexApiResult ret = null;
567 LOGGER.entry(jsonString);
569 ret = initialiseSessionForChanges();
574 final BeanContextSchema jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextSchema.class);
575 ret = sessionApexModel.createContextSchema(jsonbean.getName(), jsonbean.getVersion(),
576 jsonbean.getSchemaFlavour(), jsonbean.getSchemaDefinition(), jsonbean.getUuid(),
577 jsonbean.getDescription());
582 } catch (final Exception e) {
586 LOGGER.exit("ContextSchema/Create" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
591 * Update a context schema with the information in the JSON string passed.
593 * @param jsonString the JSON string to be parsed. See {@linkplain BeanContextSchema}
594 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
595 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
598 @Path("ContextSchema/Update")
599 public ApexApiResult updateContextSchema(final String jsonString) {
600 ApexApiResult ret = null;
601 LOGGER.entry(jsonString);
603 ret = initialiseSessionForChanges();
608 final BeanContextSchema jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextSchema.class);
610 ret = sessionApexModel.updateContextSchema(jsonbean.getName(), jsonbean.getVersion(),
611 jsonbean.getSchemaFlavour(), jsonbean.getSchemaDefinition(), jsonbean.getUuid(),
612 jsonbean.getDescription());
617 } catch (final Exception e) {
621 LOGGER.exit("ContextSchema/Update" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
626 * List context schemas with the given key names/versions. If successful the result(s) will be available in the
627 * result messages. The returned value(s) will be similar to {@link AxContextSchema}, with merged
628 * {@linkplain AxKeyInfo} for the root object.
630 * @param name the name to search for. If null or empty, then all names will be queried
631 * @param version the version to search for. If null then all versions will be searched for.
632 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
633 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
636 @Path("ContextSchema/Get")
637 public ApexApiResult listContextSchemas(@QueryParam("name") final String name,
638 @QueryParam(VERSION) final String version) {
639 ApexApiResult ret = null;
641 String version1 = version;
642 LOGGER.entry(name1, version1);
644 ret = initialiseSessionForReadOnly();
649 if (name1 == null || name1.equals("")) {
652 if (version1 == null || version1.equals("")) {
656 ret = sessionApexModel.listContextSchemas(name1, version1);
661 ret = addKeyInfo2Messages(ret);
663 } catch (final Exception e) {
667 LOGGER.exit("ContextSchema/Get" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
672 * Delete context schemas with the given key names/versions.
674 * @param name the name to search for. If null or empty, then all names will be queried
675 * @param version the version to search for. If null then all versions will be searched for.
676 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
677 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
680 @Path("ContextSchema/Delete")
681 public ApexApiResult deleteContextSchema(@QueryParam("name") final String name,
682 @QueryParam(VERSION) final String version) {
683 ApexApiResult ret = null;
685 String version1 = version;
686 LOGGER.entry(name1, version1);
688 ret = initialiseSessionForChanges();
693 if (name1 == null || name1.equals("")) {
696 if (version1 == null || version1.equals("")) {
700 ret = sessionApexModel.deleteContextSchema(name1, version1);
705 } catch (final Exception e) {
709 LOGGER.exit("ContextSchema/Delete" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
714 * Validate context schemas with the given key names/versions. The result(s) will be available in the result
717 * @param name the name to search for. If null or empty, then all names will be queried
718 * @param version the version to search for. If null then all versions will be searched for.
719 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
720 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
723 @Path("Validate/ContextSchema")
724 public ApexApiResult validateContextSchemas(@QueryParam("name") final String name,
725 @QueryParam(VERSION) final String version) {
726 ApexApiResult ret = null;
728 String version1 = version;
729 LOGGER.entry(name1, version1);
731 if (name1 == null || name1.equals("")) {
734 if (version1 == null || version1.equals("")) {
738 ret = initialiseSessionForReadOnly();
743 ret = sessionApexModel.validateContextSchemas(name1, version1);
748 ret = addKeyInfo2Messages(ret);
750 } catch (final Exception e) {
754 LOGGER.exit("Validate/ContextSchema" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
759 * Creates a context album with the information in the JSON string passed.
761 * @param jsonString the JSON string to be parsed. See {@linkplain BeanContextAlbum}
762 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
763 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
766 @Path("ContextAlbum/Create")
767 public ApexApiResult createContextAlbum(final String jsonString) {
768 ApexApiResult ret = null;
769 LOGGER.entry(jsonString);
771 ret = initialiseSessionForChanges();
776 final BeanContextAlbum jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class);
778 ret = sessionApexModel.createContextAlbum(jsonbean.getName(), jsonbean.getVersion(), jsonbean.getScope(),
779 Boolean.toString(jsonbean.getWriteable()), jsonbean.getItemSchema().getName(),
780 jsonbean.getItemSchema().getVersion(), jsonbean.getUuid(), jsonbean.getDescription());
785 } catch (final Exception e) {
789 LOGGER.exit("ContextAlbum/Create" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
794 * Update a context album with the information in the JSON string passed.
796 * @param jsonString the JSON string to be parsed. See {@linkplain BeanContextAlbum}
797 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
798 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
801 @Path("ContextAlbum/Update")
802 public ApexApiResult updateContextAlbum(final String jsonString) {
803 ApexApiResult ret = null;
804 LOGGER.entry(jsonString);
806 ret = initialiseSessionForChanges();
811 final BeanContextAlbum jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class);
813 ret = sessionApexModel.updateContextAlbum(jsonbean.getName(), jsonbean.getVersion(), jsonbean.getScope(),
814 Boolean.toString(jsonbean.getWriteable()), jsonbean.getItemSchema().getName(),
815 jsonbean.getItemSchema().getVersion(), jsonbean.getUuid(), jsonbean.getDescription());
820 } catch (final Exception e) {
824 LOGGER.exit("ContextAlbum/Update" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
829 * List context albums with the given key names/versions. If successful the result(s) will be available in the
830 * result messages. The returned value(s) will be similar to {@link AxContextAlbum}, with merged
831 * {@linkplain AxKeyInfo} for the root object.
833 * @param name the name to search for. If null or empty, then all names will be queried
834 * @param version the version to search for. If null then all versions will be searched for.
835 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
836 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
839 @Path("ContextAlbum/Get")
840 public ApexApiResult listContextAlbums(@QueryParam("name") final String name,
841 @QueryParam(VERSION) final String version) {
842 ApexApiResult ret = null;
844 String version1 = version;
845 LOGGER.entry(name1, version1);
847 ret = initialiseSessionForReadOnly();
852 if (name1 == null || name1.equals("")) {
855 if (version1 == null || version1.equals("")) {
859 ret = sessionApexModel.listContextAlbum(name1, version1);
864 ret = addKeyInfo2Messages(ret);
866 } catch (final Exception e) {
870 LOGGER.exit("ContextAlbum/Get" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
875 * Delete context albums with the given key names/versions.
877 * @param name the name to search for. If null or empty, then all names will be queried
878 * @param version the version to search for. If null then all versions will be searched for.
879 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
880 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
883 @Path("ContextAlbum/Delete")
884 public ApexApiResult deleteContextAlbum(@QueryParam("name") final String name,
885 @QueryParam(VERSION) final String version) {
886 ApexApiResult ret = null;
888 String version1 = version;
889 LOGGER.entry(name1, version1);
891 ret = initialiseSessionForChanges();
896 if (name1 == null || name1.equals("")) {
899 if (version1 == null || version1.equals("")) {
903 ret = sessionApexModel.deleteContextAlbum(name1, version1);
908 } catch (final Exception e) {
912 LOGGER.exit("ContextAlbum/Delete" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
917 * Validate context albums with the given key names/versions. The result(s) will be available in the result
920 * @param name the name to search for. If null or empty, then all names will be queried
921 * @param version the version to search for. If null then all versions will be searched for.
922 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
923 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
926 @Path("Validate/ContextAlbum")
927 public ApexApiResult validateContextAlbums(@QueryParam("name") final String name,
928 @QueryParam(VERSION) final String version) {
929 ApexApiResult ret = null;
931 String version1 = version;
932 LOGGER.entry(name1, version1);
934 if (name1 == null || name1.equals("")) {
937 if (version1 == null || version1.equals("")) {
941 ret = initialiseSessionForReadOnly();
946 ret = sessionApexModel.listContextAlbum(name1, version1);
951 ret = addKeyInfo2Messages(ret);
953 } catch (final Exception e) {
957 LOGGER.exit("Validate/ContextAlbum" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
962 * Creates an event with the information in the JSON string passed.
964 * @param jsonString the JSON string to be parsed. See {@linkplain BeanEvent}
965 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
966 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
969 @Path("Event/Create")
970 public ApexApiResult createEvent(final String jsonString) {
971 ApexApiResult ret = null;
972 LOGGER.entry(jsonString);
974 ret = initialiseSessionForChanges();
979 final BeanEvent jsonbean = RestUtils.getJsonParameters(jsonString, BeanEvent.class);
981 ret = sessionApexModel.createEvent(jsonbean.getName(), jsonbean.getVersion(), jsonbean.getNameSpace(),
982 jsonbean.getSource(), jsonbean.getTarget(), jsonbean.getUuid(), jsonbean.getDescription());
986 if (jsonbean.getParameters() != null) {
987 for (final Entry<String, BeanField> p : jsonbean.getParameters().entrySet()) {
988 if (p.getValue() == null) {
989 ret = new ApexApiResult(Result.FAILED, "Null event parameter information for parameter \""
990 + p.getKey() + "\" in event " + jsonbean.getName() + ":" + jsonbean.getVersion()
991 + ". The event was created, but there was an error adding the event parameters."
992 + " The event has only been partially defined.");
995 final ApexApiResult rettmp = sessionApexModel.createEventPar(jsonbean.getName(),
996 jsonbean.getVersion(), p.getKey(), p.getValue().getName(),
997 p.getValue().getVersion(), p.getValue().getOptional());
998 if (rettmp.isNok()) {
999 rettmp.addMessage("Failed to add event parameter information for parameter \"" + p.getKey()
1000 + "\" in event " + jsonbean.getName() + ":" + jsonbean.getVersion()
1001 + ". The event was created, but there was an error adding the event parameters."
1002 + " The event has only been partially defined.");
1012 } catch (final Exception e) {
1016 LOGGER.exit("Event/Create" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
1021 * Update an event with the information in the JSON string passed.
1023 * @param jsonString the JSON string to be parsed. See {@linkplain BeanEvent}
1024 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
1025 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
1028 @Path("Event/Update")
1029 public ApexApiResult updateEvent(final String jsonString) {
1030 ApexApiResult ret = null;
1031 LOGGER.entry(jsonString);
1033 ret = initialiseSessionForChanges();
1038 final BeanEvent jsonbean = RestUtils.getJsonParameters(jsonString, BeanEvent.class);
1040 if (jsonbean.getName() == null || jsonbean.getName().equals("") || jsonbean.getVersion() == null
1041 || jsonbean.getVersion().equals("")) {
1042 ret = new ApexApiResult(Result.FAILED, "Null/Empty event name/version (\"" + jsonbean.getName() + ":"
1043 + jsonbean.getVersion() + "\" passed to UpdateEvent");
1047 ret = sessionApexModel.deleteEvent(jsonbean.getName(), jsonbean.getVersion());
1052 ret = createEvent(jsonString);
1057 } catch (final Exception e) {
1061 LOGGER.exit("Event/Update" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
1066 * List events with the given key names/versions. If successful the result(s) will be available in the result
1067 * messages. The returned value(s) will be similar to {@link AxEvent}, with merged {@linkplain AxKeyInfo} for the
1070 * @param name the name to search for. If null or empty, then all names will be queried
1071 * @param version the version to search for. If null then all versions will be searched for.
1072 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
1073 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
1077 public ApexApiResult listEvent(@QueryParam("name") final String name, @QueryParam(VERSION) final String version) {
1078 ApexApiResult ret = null;
1079 String name1 = name;
1080 String version1 = version;
1081 LOGGER.entry(name1, version1);
1083 ret = initialiseSessionForReadOnly();
1088 if (name1 == null || name1.equals("")) {
1091 if (version1 == null || version1.equals("")) {
1095 ret = sessionApexModel.listEvent(name1, version1);
1100 ret = addKeyInfo2Messages(ret);
1102 } catch (final Exception e) {
1106 LOGGER.exit("Event/Get" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
1111 * Delete events with the given key names/versions.
1113 * @param name the name to search for. If null or empty, then all names will be queried
1114 * @param version the version to search for. If null then all versions will be searched for.
1115 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
1116 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
1119 @Path("Event/Delete")
1120 public ApexApiResult deleteEvent(@QueryParam("name") final String name,
1121 @QueryParam(VERSION) final String version) {
1122 ApexApiResult ret = null;
1123 String name1 = name;
1124 String version1 = version;
1125 LOGGER.entry(name1, version1);
1127 if (name1 == null || name1.equals("")) {
1130 if (version1 == null || version1.equals("")) {
1134 ret = initialiseSessionForChanges();
1139 ret = sessionApexModel.deleteEvent(name1, version1);
1144 } catch (final Exception e) {
1148 LOGGER.exit("Event/Delete" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
1153 * Validate events with the given key names/versions. The result(s) will be available in the result messages.
1155 * @param name the name to search for. If null or empty, then all names will be queried
1156 * @param version the version to search for. If null then all versions will be searched for.
1157 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
1158 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
1161 @Path("Validate/Event")
1162 public ApexApiResult validateEvent(@QueryParam("name") final String name,
1163 @QueryParam(VERSION) final String version) {
1164 ApexApiResult ret = null;
1165 String name1 = name;
1166 String version1 = version;
1167 LOGGER.entry(name1, version1);
1169 ret = initialiseSessionForReadOnly();
1174 if (name1 == null || name1.equals("")) {
1177 if (version1 == null || version1.equals("")) {
1181 ret = sessionApexModel.listEvent(name1, version1);
1186 ret = addKeyInfo2Messages(ret);
1188 } catch (final Exception e) {
1192 LOGGER.exit("Validate/Event" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
1197 * Creates a task with the information in the JSON string passed.
1199 * @param jsonString the JSON string to be parsed. See {@linkplain BeanTask}
1200 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
1201 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
1204 @Path("Task/Create")
1205 public ApexApiResult createTask(final String jsonString) {
1206 ApexApiResult ret = null;
1207 ApexApiResult tempres = null;
1208 LOGGER.entry(jsonString);
1210 ret = initialiseSessionForChanges();
1215 final BeanTask jsonbean = RestUtils.getJsonParameters(jsonString, BeanTask.class);
1217 ret = sessionApexModel.createTask(jsonbean.getName(), jsonbean.getVersion(), jsonbean.getUuid(),
1218 jsonbean.getDescription());
1222 if (jsonbean.getInputFields() != null) {
1223 for (final Entry<String, BeanField> fin : jsonbean.getInputFields().entrySet()) {
1224 if (fin.getValue() == null) {
1225 ret = new ApexApiResult(Result.FAILED, "Null task input field information for field \""
1226 + fin.getKey() + IN_TASK + jsonbean.getName() + ":" + jsonbean.getVersion()
1227 + ". The task was created, but there was an error adding the input fields."
1228 + TASK_PARTIALLY_DEFINED);
1231 if (fin.getKey() == null || !fin.getKey().equals(fin.getValue().getLocalName())) {
1232 ret = new ApexApiResult(Result.FAILED, "Invalid task input field information for field \""
1233 + fin.getKey() + IN_TASK + jsonbean.getName() + ":" + jsonbean.getVersion()
1234 + ". The localName of the field (\"" + fin.getValue().getLocalName()
1235 + "\") is not the same as the field name. "
1236 + "The task was created, but there was an error adding the input fields."
1237 + TASK_PARTIALLY_DEFINED);
1240 tempres = sessionApexModel.createTaskInputField(jsonbean.getName(), jsonbean.getVersion(),
1241 fin.getKey(), fin.getValue().getName(), fin.getValue().getVersion(),
1242 fin.getValue().getOptional());
1243 if (tempres.isNok()) {
1244 tempres.addMessage("Failed to add task input field information for field \"" + fin.getKey()
1245 + IN_TASK + jsonbean.getName() + ":" + jsonbean.getVersion()
1246 + ". The task was created, but there was an error adding the input fields."
1247 + TASK_PARTIALLY_DEFINED);
1253 if (jsonbean.getOutputFields() != null) {
1254 for (final Entry<String, BeanField> fout : jsonbean.getOutputFields().entrySet()) {
1255 if (fout.getValue() == null) {
1256 ret = new ApexApiResult(Result.FAILED, "Null task output field information for field \""
1257 + fout.getKey() + IN_TASK + jsonbean.getName() + ":" + jsonbean.getVersion()
1258 + ". The task was created, but there was an error adding the output fields."
1259 + TASK_PARTIALLY_DEFINED);
1262 if (fout.getKey() == null || !fout.getKey().equals(fout.getValue().getLocalName())) {
1263 ret = new ApexApiResult(Result.FAILED, "Invalid task output field information for field \""
1264 + fout.getKey() + IN_TASK + jsonbean.getName() + ":" + jsonbean.getVersion()
1265 + ". The localName of the field (\"" + fout.getValue().getLocalName()
1266 + "\") is not the same as the field name. "
1267 + "The task was created, but there was an error adding the output fields."
1268 + TASK_PARTIALLY_DEFINED);
1271 tempres = sessionApexModel.createTaskOutputField(jsonbean.getName(), jsonbean.getVersion(),
1272 fout.getKey(), fout.getValue().getName(), fout.getValue().getVersion(),
1273 fout.getValue().getOptional());
1274 if (tempres.isNok()) {
1275 tempres.addMessage("Failed to add task output field information for field \"" + fout.getKey()
1276 + IN_TASK + jsonbean.getName() + ":" + jsonbean.getVersion()
1277 + ". The task was created, but there was an error adding the output fields."
1278 + TASK_PARTIALLY_DEFINED);
1284 if (jsonbean.getTaskLogic() != null) {
1285 final BeanLogic logic = jsonbean.getTaskLogic();
1286 tempres = sessionApexModel.createTaskLogic(jsonbean.getName(), jsonbean.getVersion(),
1287 logic.getLogicFlavour(), logic.getLogic());
1288 if (tempres.isNok()) {
1289 tempres.addMessage("Failed to add task logic in task " + jsonbean.getName() + ":"
1290 + jsonbean.getVersion()
1291 + ". The task was created, but there was an error adding the logic."
1292 + TASK_PARTIALLY_DEFINED);
1297 if (jsonbean.getParameters() != null) {
1298 for (final Entry<String, BeanTaskParameter> param : jsonbean.getParameters().entrySet()) {
1299 if (param.getKey() == null || param.getValue() == null
1300 || !param.getKey().equals(param.getValue().getParameterName())) {
1301 ret = new ApexApiResult(Result.FAILED,
1302 "Null or invalid task parameter information for parameter \"" + param.getKey()
1303 + IN_TASK + jsonbean.getName() + ":" + jsonbean.getVersion()
1304 + ". The task was created, "
1305 + "but there was an error adding the parameters."
1306 + TASK_PARTIALLY_DEFINED);
1309 tempres = sessionApexModel.createTaskParameter(jsonbean.getName(), jsonbean.getVersion(),
1310 param.getValue().getParameterName(), param.getValue().getDefaultValue());
1311 if (tempres.isNok()) {
1312 tempres.addMessage("Failed to add task parameter \"" + param.getKey() + IN_TASK
1313 + jsonbean.getName() + ":" + jsonbean.getVersion()
1314 + ". The task was created, but there was an error adding the parameters."
1315 + TASK_PARTIALLY_DEFINED);
1321 if (jsonbean.getContexts() != null) {
1322 for (final BeanKeyRef contextalbum : jsonbean.getContexts()) {
1323 if (contextalbum.getName() == null || contextalbum.getVersion() == null) {
1324 ret = new ApexApiResult(Result.FAILED,
1325 "Null or invalid context album reference information in task "
1326 + jsonbean.getName() + ":" + jsonbean.getVersion()
1327 + ". The task was created, but there was an error adding the"
1328 + " context album reference. "
1329 + "The task has only been partially defined.");
1332 tempres = sessionApexModel.createTaskContextRef(jsonbean.getName(), jsonbean.getVersion(),
1333 contextalbum.getName(), contextalbum.getVersion());
1334 if (tempres.isNok()) {
1335 ret = new ApexApiResult(Result.FAILED,
1336 "Failed to add context album reference information in task "
1337 + jsonbean.getName() + ":" + jsonbean.getVersion()
1338 + ". The task was created, but there was an error adding the"
1339 + " context album reference. "
1340 + "The task has only been partially defined.");
1349 } catch (final Exception e) {
1353 LOGGER.exit("Task/Create" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
1358 * Update a task with the information in the JSON string passed.
1360 * @param jsonString the JSON string to be parsed. See {@linkplain BeanTask}
1361 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
1362 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
1365 @Path("Task/Update")
1366 public ApexApiResult updateTask(final String jsonString) {
1367 ApexApiResult ret = null;
1368 LOGGER.entry(jsonString);
1370 ret = initialiseSessionForChanges();
1375 final BeanTask jsonbean = RestUtils.getJsonParameters(jsonString, BeanTask.class);
1377 if (jsonbean.getName() == null || jsonbean.getName().equals("") || jsonbean.getVersion() == null
1378 || jsonbean.getVersion().equals("")) {
1379 ret = new ApexApiResult(Result.FAILED, "Null/Empty task name/version (\"" + jsonbean.getName() + ":"
1380 + jsonbean.getVersion() + "\" passed to UpdateTask");
1384 ret = sessionApexModel.deleteTask(jsonbean.getName(), jsonbean.getVersion());
1389 ret = createTask(jsonString);
1394 } catch (final Exception e) {
1398 LOGGER.exit("Task/Update" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
1403 * List tasks with the given key names/versions. If successful the result(s) will be available in the result
1404 * messages. The returned value(s) will be similar to {@link AxTask}, with merged {@linkplain AxKeyInfo} for the
1407 * @param name the name to search for. If null or empty, then all names will be queried
1408 * @param version the version to search for. If null then all versions will be searched for.
1409 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
1410 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
1414 public ApexApiResult listTask(@QueryParam("name") final String name, @QueryParam(VERSION) final String version) {
1415 ApexApiResult ret = null;
1416 String name1 = name;
1417 String version1 = version;
1418 LOGGER.entry(name1, version1);
1420 ret = initialiseSessionForReadOnly();
1425 if (name1 == null || name1.equals("")) {
1428 if (version1 == null || version1.equals("")) {
1432 ret = sessionApexModel.listTask(name1, version1);
1437 ret = addKeyInfo2Messages(ret);
1439 } catch (final Exception e) {
1443 LOGGER.exit("Task/Get" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
1448 * Delete tasks with the given key names/versions.
1450 * @param name the name to search for. If null or empty, then all names will be queried
1451 * @param version the version to search for. If null then all versions will be searched for.
1452 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
1453 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
1456 @Path("Task/Delete")
1457 public ApexApiResult deleteTask(@QueryParam("name") final String name,
1458 @QueryParam(VERSION) final String version) {
1459 ApexApiResult ret = null;
1460 String name1 = name;
1461 String version1 = version;
1462 LOGGER.entry(name1, version1);
1464 ret = initialiseSessionForChanges();
1469 if (name1 == null || name1.equals("")) {
1472 if (version1 == null || version1.equals("")) {
1476 // all input/output fields, parameters, logic, context references is "owned"/contained
1478 // deleting the task removes all of these
1479 ret = sessionApexModel.deleteTask(name1, version1);
1484 } catch (final Exception e) {
1488 LOGGER.exit("Task/Delete" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
1493 * Validate tasks with the given key names/versions. The result(s) will be available in the result messages.
1495 * @param name the name to search for. If null or empty, then all names will be queried
1496 * @param version the version to search for. If null then all versions will be searched for.
1497 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
1498 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
1501 @Path("Validate/Task")
1502 public ApexApiResult validateTask(@QueryParam("name") final String name,
1503 @QueryParam(VERSION) final String version) {
1504 ApexApiResult ret = null;
1505 String name1 = name;
1506 String version1 = version;
1507 LOGGER.entry(name1, version1);
1509 ret = initialiseSessionForReadOnly();
1514 if (name1 == null || name1.equals("")) {
1517 if (version1 == null || version1.equals("")) {
1521 ret = sessionApexModel.listTask(name1, version1);
1526 ret = addKeyInfo2Messages(ret);
1528 } catch (final Exception e) {
1532 LOGGER.exit("Validate/Task" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
1536 // CHECKSTYLE:OFF: MethodLength
1538 * Creates a policy with the information in the JSON string passed.
1540 * @param jsonString the JSON string to be parsed See {@linkplain BeanPolicy}
1541 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
1542 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
1545 @Path("Policy/Create")
1546 public ApexApiResult createPolicy(final String jsonString) {
1548 ApexApiResult ret = null;
1549 ApexApiResult tempres = null;
1550 LOGGER.entry(jsonString);
1552 ret = initialiseSessionForChanges();
1557 final BeanPolicy jsonbean = RestUtils.getJsonParameters(jsonString, BeanPolicy.class);
1558 final String policyname = jsonbean.getName();
1559 final String policyversion = jsonbean.getVersion();
1561 ret = sessionApexModel.createPolicy(policyname, policyversion, jsonbean.getTemplate(),
1562 jsonbean.getFirstState(), jsonbean.getUuid(), jsonbean.getDescription());
1567 if (jsonbean.getStates() == null || jsonbean.getStates().isEmpty()) {
1568 ret = new ApexApiResult(Result.FAILED,
1569 "Null or empty state map; no states defined for policy \"" + policyname + ":"
1571 + "\". The policy was created, but there was an error adding states."
1572 + POLICY_PARTIALLY_DEFINED);
1576 final Map<String, BeanState> statemap = jsonbean.getStates();
1577 // need to create all the states first, before populating them
1578 for (final Map.Entry<String, BeanState> e : statemap.entrySet()) {
1579 final String statename = e.getKey();
1580 final BeanState state = e.getValue();
1581 if (state == null) {
1582 ret = new ApexApiResult(Result.FAILED,
1583 "Null or invalid state information for state \"" + statename + FOR_POLICY
1584 + policyname + ":" + policyversion + POLICY_CREATED_STATE_ERROR
1585 + POLICY_PARTIALLY_DEFINED);
1588 if (state.getTrigger() == null) {
1589 ret = new ApexApiResult(Result.FAILED,
1590 "Null or invalid state trigger for state \"" + statename + FOR_POLICY + policyname
1591 + ":" + policyversion + POLICY_CREATED_STATE_ERROR
1592 + POLICY_PARTIALLY_DEFINED);
1595 if (state.getDefaultTask() == null) {
1596 ret = new ApexApiResult(Result.FAILED,
1597 "Null or invalid default task for state \"" + statename + FOR_POLICY + policyname
1598 + ":" + policyversion + POLICY_CREATED_STATE_ERROR
1599 + POLICY_PARTIALLY_DEFINED);
1602 tempres = sessionApexModel.createPolicyState(policyname, policyversion, statename,
1603 state.getTrigger().getName(), state.getTrigger().getVersion(),
1604 state.getDefaultTask().getName(), state.getDefaultTask().getVersion());
1605 if (tempres.isNok()) {
1611 for (final Map.Entry<String, BeanState> e : statemap.entrySet()) {
1612 final String statename = e.getKey();
1613 final BeanState state = e.getValue();
1615 final BeanLogic tsl = state.getTaskSelectionLogic();
1617 tempres = sessionApexModel.createPolicyStateTaskSelectionLogic(policyname, policyversion, statename,
1618 tsl.getLogicFlavour(), tsl.getLogic());
1619 if (tempres.isNok()) {
1620 tempres.addMessage("Failed to add task selection logic for state \"" + statename + "\" for"
1621 + " policy \"" + policyname + ":" + policyversion
1622 + POLICY_WAS_CREATED
1623 + "but there was an error adding the task selection logic "
1624 + "for the state. The policy has only been partially defined.");
1630 final BeanKeyRef[] contexts = state.getContexts();
1631 if (contexts != null) {
1632 for (final BeanKeyRef c : contexts) {
1634 ret = new ApexApiResult(Result.FAILED, "Null or invalid context reference \"" + c + "\" for"
1635 + " state \"" + statename + FOR_POLICY + policyname + ":" + policyversion
1636 + "\". The policy was created, but there was an error adding the context "
1637 + "reference for the state. The policy has only been partially defined.");
1640 tempres = sessionApexModel.createPolicyStateContextRef(policyname, policyversion, statename,
1641 c.getName(), c.getVersion());
1642 if (tempres.isNok()) {
1643 tempres.addMessage("Failed to add context reference \"" + c + "\" for state \"" + statename
1644 + FOR_POLICY + policyname + ":" + policyversion
1645 + POLICY_WAS_CREATED
1646 + "but there was an error adding the context reference "
1647 + "for the state. The policy has only been partially defined.");
1654 final Map<String, BeanLogic> finalizers = state.getFinalizers();
1655 if (finalizers != null) {
1656 for (final Map.Entry<String, BeanLogic> f : finalizers.entrySet()) {
1657 final String finalizername = f.getKey();
1658 final BeanLogic finalizer = f.getValue();
1659 if (finalizername == null || finalizer == null) {
1660 ret = new ApexApiResult(Result.FAILED,
1661 "Null or invalid finalizer information for finalizer " + "named \""
1662 + finalizername + IN_STATE + statename + FOR_POLICY
1663 + policyname + ":" + policyversion
1664 + POLICY_STATE_CREATED_OTHER_ERROR
1665 + " finalizer. The policy has only "
1666 + "been partially defined.");
1669 tempres = sessionApexModel.createPolicyStateFinalizerLogic(policyname, policyversion, statename,
1670 finalizername, finalizer.getLogicFlavour(), finalizer.getLogic());
1671 if (tempres.isNok()) {
1672 tempres.addMessage("Failed to add finalizer information for finalizer named \""
1673 + finalizername + "\" in" + " state \"" + statename + FOR_POLICY
1674 + policyname + ":" + policyversion + POLICY_STATE_CREATED_OTHER_ERROR
1675 + " finalizer. The policy has only been partially defined.");
1681 final Map<String, BeanStateOutput> outputs = state.getStateOutputs();
1682 if (outputs == null || outputs.isEmpty()) {
1683 ret = new ApexApiResult(Result.FAILED, "No state outputs have been defined in state \"" + statename
1684 + FOR_POLICY + policyname + ":" + policyversion
1685 + "\". The policy and state were created, but there was an error adding state"
1686 + " outputs. The policy has only been partially defined.");
1689 for (final Map.Entry<String, BeanStateOutput> o : outputs.entrySet()) {
1690 final String outputname = o.getKey();
1691 final BeanStateOutput output = o.getValue();
1692 if (outputname == null || output == null || output.getEvent() == null) {
1693 ret = new ApexApiResult(Result.FAILED,
1694 "Null or invalid output information for output named \"" + outputname + IN_STATE
1695 + statename + FOR_POLICY + policyname + ":" + policyversion
1696 + POLICY_STATE_CREATED_OTHER_ERROR
1697 + " output. The policy has only been partially defined.");
1700 tempres = sessionApexModel.createPolicyStateOutput(policyname, policyversion, statename, outputname,
1701 output.getEvent().getName(), output.getEvent().getVersion(), output.getNextState());
1702 if (tempres.isNok()) {
1703 tempres.addMessage("Failed to add output information for output named \"" + outputname
1704 + IN_STATE + statename + FOR_POLICY + policyname + ":" + policyversion
1705 + "\". The policy and state were created, "
1706 + "but there was an error adding the output." + POLICY_PARTIALLY_DEFINED);
1712 final Map<String, BeanStateTaskRef> taskmap = state.getTasks();
1713 if (taskmap == null || taskmap.isEmpty()) {
1714 ret = new ApexApiResult(Result.FAILED, "No tasks have been defined in state \"" + statename
1715 + FOR_POLICY + policyname + ":" + policyversion
1716 + "\". The policy and state were created, but there was an error adding tasks."
1717 + POLICY_PARTIALLY_DEFINED);
1720 for (final Map.Entry<String, BeanStateTaskRef> t : taskmap.entrySet()) {
1721 final String tasklocalname = t.getKey();
1722 final BeanStateTaskRef taskref = t.getValue();
1723 if (tasklocalname == null || taskref == null || taskref.getTask() == null) {
1724 ret = new ApexApiResult(Result.FAILED, "Null or invalid task information for task named \""
1725 + tasklocalname + IN_STATE + statename + "\" for for policy \"" + policyname
1726 + ":" + policyversion
1727 + "\". The policy and state were created, but there was an error adding the "
1728 + "task. The policy has only been partially defined.");
1731 tempres = sessionApexModel.createPolicyStateTaskRef(policyname, policyversion, statename,
1732 tasklocalname, taskref.getTask().getName(), taskref.getTask().getVersion(),
1733 taskref.getOutputType(), taskref.getOutputName());
1734 if (tempres.isNok()) {
1735 tempres.addMessage("Failed to add task reference \"" + t + "\" for state \"" + statename
1736 + FOR_POLICY + policyname + ":" + policyversion + POLICY_WAS_CREATED
1737 + "but there was an error adding the task reference for"
1738 + " the state. The policy has only been partially defined.");
1749 } catch (final Exception e) {
1753 LOGGER.exit("Policy/Create" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
1756 // CHECKSTYLE:ON: MethodLength
1759 * Update a policy with the information in the JSON string passed.
1761 * @param firstStatePeriodic indicates if periodic event should be created and added to model
1762 * @param jsonString the JSON string to be parsed. See {@linkplain BeanPolicy}
1763 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
1764 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
1767 @Path("Policy/Update")
1768 public ApexApiResult updatePolicy(@QueryParam("firstStatePeriodic") final boolean firstStatePeriodic,
1769 final String jsonString) {
1770 ApexApiResult ret = null;
1771 LOGGER.entry(jsonString);
1773 ret = initialiseSessionForChanges();
1778 final BeanPolicy jsonbean = RestUtils.getJsonParameters(jsonString, BeanPolicy.class);
1780 if (jsonbean.getName() == null || jsonbean.getName().equals("") || jsonbean.getVersion() == null
1781 || jsonbean.getVersion().equals("")) {
1782 ret = new ApexApiResult(Result.FAILED, "Null/Empty Policy name/version (\"" + jsonbean.getName() + ":"
1783 + jsonbean.getVersion() + "\" passed to UpdatePolicy");
1787 ret = sessionApexModel.deletePolicy(jsonbean.getName(), jsonbean.getVersion());
1791 if (firstStatePeriodic) {
1792 final ApexApiResult existingPeriodicEvent = sessionApexModel.listEvent("PeriodicEvent", null);
1793 if (existingPeriodicEvent.isNok()) {
1794 final String periodicEventString = "{\"name\":\"PeriodicEvent\",\"version\":\"0.0.1\","
1795 + "\"uuid\":\"44236da1-3d47-4988-8033-b6fee9d6a0f4\","
1796 + "\"description\":\"Generated description for concept referred to by key "
1797 + "'PeriodicEvent:0.0.1'\",\"source\":\"System\",\"target\":\"Apex\","
1798 + "\"nameSpace\":\"org.onap.policy.apex.domains.aadm.events\",\"parameters\":{}}";
1799 ret = createEvent(periodicEventString);
1805 ret = createPolicy(jsonString);
1810 } catch (final Exception e) {
1814 LOGGER.exit("Policy/Update" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
1819 * List policies with the given key names/versions. If successful the result(s) will be available in the result
1820 * messages. The returned value(s) will be similar to {@link AxPolicy}, with merged {@linkplain AxKey Info} for the
1823 * @param name the name to search for. If null or empty, then all names will be queried
1824 * @param version the version to search for. If null then all versions will be searched for.
1825 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
1826 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
1830 public ApexApiResult listPolicy(@QueryParam("name") final String name,
1831 @QueryParam(VERSION) final String version) {
1832 ApexApiResult ret = null;
1833 String name1 = name;
1834 String version1 = version;
1835 LOGGER.entry(name1, version1);
1837 ret = initialiseSessionForReadOnly();
1842 if (name1 == null || name1.equals("")) {
1845 if (version1 == null || version1.equals("")) {
1849 ret = sessionApexModel.listPolicy(name1, version1);
1855 ret = addKeyInfo2Messages(ret);
1857 } catch (final Exception e) {
1861 LOGGER.exit("Policy/Get" + (ret != null && ret.isOk() ? ": OK" : NOT_OK));
1866 * Delete policies with the given key names/versions.
1868 * @param name the name to search for. If null or empty, then all names will be queried
1869 * @param version the version to search for. If null then all versions will be searched for.
1870 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
1871 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
1874 @Path("Policy/Delete")
1875 public ApexApiResult deletePolicy(@QueryParam("name") final String name,
1876 @QueryParam(VERSION) final String version) {
1877 ApexApiResult ret = null;
1878 String name1 = name;
1879 String version1 = version;
1880 LOGGER.entry(name1, version1);
1882 ret = initialiseSessionForChanges();
1887 if (name1 == null || name1.equals("")) {
1890 if (version1 == null || version1.equals("")) {
1894 // all input/output fields, parameters, logic, context references is "owned"/contained
1896 // deleting the task removes all of these
1897 ret = sessionApexModel.deletePolicy(name1, version1);
1902 } catch (final Exception e) {
1906 String result = "Policy/Delete" + (ret != null && ret.isOk() ? ": OK" : NOT_OK);
1907 LOGGER.exit(result);
1912 * The json strings representing the objects listed, stored in result.messages[], does not contain the
1913 * AxKeyInformation for that object. This utility method retrieves the AxKeyInfo for each object and adds it to the
1914 * json for the object.
1916 * @param result The list result, containing json representations of objects stored in its "messages" array
1917 * @return The list result, containing json augmented representations of objects stored in its "messages" array
1919 private ApexApiResult addKeyInfo2Messages(final ApexApiResult result) {
1920 if (result.isNok()) {
1924 final ApexApiResult ret = new ApexApiResult(result.getResult());
1925 ret.setMessages(result.getMessages());
1927 final List<String> messages = result.getMessages();
1928 final List<String> augmessages = new ArrayList<>(messages.size());
1929 final GsonBuilder gb = new GsonBuilder();
1930 gb.serializeNulls().enableComplexMapKeySerialization();
1931 final Gson gson = gb.create();
1932 for (final String message : messages) {
1934 final JsonObject jsonObject = gson.fromJson(message, JsonObject.class);
1935 JsonObject objecttochange = jsonObject;
1937 if (jsonObject != null && jsonObject.get("key") != null && jsonObject.get("key").isJsonObject()
1938 && jsonObject.getAsJsonObject("key").get("name") != null) {
1939 name = jsonObject.getAsJsonObject("key").get("name").getAsString();
1940 } else if (jsonObject != null && jsonObject.get(POLICY_KEY) != null
1941 && jsonObject.get(POLICY_KEY).isJsonObject()
1942 && jsonObject.getAsJsonObject(POLICY_KEY).get("name") != null) {
1943 name = jsonObject.getAsJsonObject(POLICY_KEY).get("name").getAsString();
1945 String version = null;
1946 if (jsonObject != null && jsonObject.get("key") != null && jsonObject.get("key").isJsonObject()
1947 && jsonObject.getAsJsonObject("key").get(VERSION) != null) {
1948 version = jsonObject.getAsJsonObject("key").get(VERSION).getAsString();
1949 } else if (jsonObject != null && jsonObject.get(POLICY_KEY) != null
1950 && jsonObject.get(POLICY_KEY).isJsonObject()
1951 && jsonObject.getAsJsonObject(POLICY_KEY).get(VERSION) != null) {
1952 version = jsonObject.getAsJsonObject(POLICY_KEY).get(VERSION).getAsString();
1955 if (name == null && version == null && jsonObject.entrySet() != null
1956 && !jsonObject.entrySet().isEmpty()) {
1957 objecttochange = (JsonObject) jsonObject.entrySet().iterator().next().getValue();
1958 if (objecttochange != null && objecttochange.get("key") != null
1959 && objecttochange.get("key").isJsonObject()
1960 && objecttochange.getAsJsonObject("key").get("name") != null) {
1961 name = objecttochange.getAsJsonObject("key").get("name").getAsString();
1962 } else if (objecttochange != null && objecttochange.get(POLICY_KEY) != null
1963 && objecttochange.get(POLICY_KEY).isJsonObject()
1964 && objecttochange.getAsJsonObject(POLICY_KEY).get("name") != null) {
1965 name = objecttochange.getAsJsonObject(POLICY_KEY).get("name").getAsString();
1967 if (objecttochange != null && objecttochange.get("key") != null
1968 && objecttochange.get("key").isJsonObject()
1969 && objecttochange.getAsJsonObject("key").get(VERSION) != null) {
1970 version = objecttochange.getAsJsonObject("key").get(VERSION).getAsString();
1971 } else if (objecttochange != null && objecttochange.get(POLICY_KEY) != null
1972 && objecttochange.get(POLICY_KEY).isJsonObject()
1973 && objecttochange.getAsJsonObject(POLICY_KEY).get(VERSION) != null) {
1974 version = objecttochange.getAsJsonObject(POLICY_KEY).get(VERSION).getAsString();
1981 if (name != null && version != null) {
1982 final ApexApiResult keyInfoResult = sessionApexModel.listKeyInformation(name, version);
1983 final List<String> keyInfoMessages = keyInfoResult.getMessages();
1984 if (keyInfoResult.isOk() && keyInfoMessages != null && !keyInfoMessages.isEmpty()) {
1985 final String keyInfoJson = keyInfoMessages.get(0);
1986 final JsonObject keyInfoJsonObject = gson.fromJson(keyInfoJson, JsonObject.class);
1987 if (keyInfoJsonObject != null && keyInfoJsonObject.get(APEX_KEY_INFO) != null
1988 && keyInfoJsonObject.get(APEX_KEY_INFO).getAsJsonObject().get("UUID") != null) {
1989 uuid = keyInfoJsonObject.get(APEX_KEY_INFO).getAsJsonObject().get("UUID").getAsString();
1991 if (keyInfoJsonObject != null && keyInfoJsonObject.get(APEX_KEY_INFO) != null
1992 && keyInfoJsonObject.get(APEX_KEY_INFO).getAsJsonObject()
1993 .get(DESCRIPTION) != null) {
1994 desc = keyInfoJsonObject.get(APEX_KEY_INFO).getAsJsonObject().get(DESCRIPTION)
1999 objecttochange.addProperty("uuid", uuid);
2000 objecttochange.addProperty(DESCRIPTION, desc);
2001 augmessages.add(gson.toJson(jsonObject));
2002 } catch (final Exception e) {
2003 LOGGER.debug("error adding key information", e);
2004 augmessages.add(message);
2007 ret.setMessages(augmessages);
2009 if (messages.size() != augmessages.size()) {
2010 ret.setResult(Result.OTHER_ERROR);
2011 ret.addMessage("Failed to add KeyInfo to all results. Results are not complete");
2018 * This method is used only for testing and is used to cause an exception on calls from unit test to test exception
2021 protected static int createCorruptSession() {
2022 final ApexEditorRestResource apexEditorRestResource = new ApexEditorRestResource();
2023 final ApexApiResult result = apexEditorRestResource.createSession();
2024 final int corruptSessionId = Integer.parseInt(result.getMessages().get(0));
2026 SESSIONMODELMAP.put(corruptSessionId, null);
2028 return corruptSessionId;
2032 * Increment the session number.
2034 private static void incrementNextSession() {