2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2022 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.gui.editors.apex.rest.handling;
25 import java.util.Map.Entry;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
27 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
28 import org.onap.policy.apex.model.modelapi.ApexApiResult;
29 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
30 import org.onap.policy.gui.editors.apex.rest.handling.bean.BeanEvent;
31 import org.onap.policy.gui.editors.apex.rest.handling.bean.BeanField;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
34 import org.springframework.stereotype.Service;
37 * This class handles commands on events in Apex models.
40 public class EventHandler implements RestCommandHandler {
41 // Get a reference to the logger
42 private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventHandler.class);
44 // Recurring string constants
45 private static final String OK = ": OK";
46 private static final String NOT_OK = ": Not OK";
52 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
53 final RestCommand command) {
54 return getUnsupportedCommandResultMessage(session, commandType, command);
61 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
62 final RestCommand command, final String jsonString) {
63 if (!RestCommandType.EVENT.equals(commandType)) {
64 return getUnsupportedCommandResultMessage(session, commandType, command);
69 return createEvent(session, jsonString);
71 return updateEvent(session, jsonString);
73 return getUnsupportedCommandResultMessage(session, commandType, command);
81 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
82 final RestCommand command, final String name, final String version) {
83 if (!RestCommandType.EVENT.equals(commandType)) {
84 return getUnsupportedCommandResultMessage(session, commandType, command);
89 return listEvents(session, name, version);
91 return deleteEvent(session, name, version);
93 return validateEvent(session, name, version);
95 return getUnsupportedCommandResultMessage(session, commandType, command);
100 * Creates an event with the information in the JSON string passed.
102 * @param session the Apex model editing session
103 * @param jsonString the JSON string to be parsed. See {@linkplain BeanEvent}
104 * @return an ApexAPIResult object. If successful then
105 * {@link ApexApiResult#isOk()} will return true. Any messages/errors
106 * can be retrieved using {@link ApexApiResult#getMessages()}
108 private ApexApiResult createEvent(final RestSession session, final String jsonString) {
109 LOGGER.entry(jsonString);
111 final var jsonbean = RestUtils.getJsonParameters(jsonString, BeanEvent.class);
115 ApexApiResult result = session.getApexModelEdited().createEvent(jsonbean.getName(), jsonbean.getVersion(),
116 jsonbean.getNameSpace(), jsonbean.getSource(), jsonbean.getTarget(), jsonbean.getUuid(),
117 jsonbean.getDescription());
120 result = createEventParameters(session, jsonbean);
123 session.finishSession(result.isOk());
125 LOGGER.exit("Event/Create" + (result.isOk() ? OK : NOT_OK));
130 * Create the parameters on an event.
132 * @param session the Apex editor session
133 * @param jsonbean the JSON bean holding the parameters
134 * @return result the result of the parameter creation operation
136 private ApexApiResult createEventParameters(final RestSession session, final BeanEvent jsonbean) {
137 var result = new ApexApiResult();
139 if (jsonbean.getParameters() == null || jsonbean.getParameters().isEmpty()) {
143 for (final Entry<String, BeanField> parameterEntry : jsonbean.getParameters().entrySet()) {
144 if (parameterEntry.getValue() == null) {
145 result.setResult(Result.FAILED);
146 result.addMessage("Null event parameter information for parameter \"" + parameterEntry.getKey()
147 + "\" in event " + jsonbean.getName() + ":" + jsonbean.getVersion()
148 + ". The event was created, but there was an error adding the event parameters."
149 + " The event has only been partially defined.");
153 final ApexApiResult createParResult = session.getApexModelEdited().createEventPar(jsonbean.getName(),
154 jsonbean.getVersion(), parameterEntry.getKey(), parameterEntry.getValue().getName(),
155 parameterEntry.getValue().getVersion(), parameterEntry.getValue().isOptional());
156 if (createParResult.isNok()) {
157 result.setResult(createParResult.getResult());
158 result.addMessage("Failed to add event parameter information for parameter \"" + parameterEntry.getKey()
159 + "\" in event " + jsonbean.getName() + ":" + jsonbean.getVersion()
160 + ". The event was created, but there was an error adding the event parameters."
161 + " The event has only been partially defined.");
169 * Update an event with the information in the JSON string passed.
171 * @param session the Apex model editing session
172 * @param jsonString the JSON string to be parsed. See {@linkplain BeanEvent}
173 * @return an ApexAPIResult object. If successful then
174 * {@link ApexApiResult#isOk()} will return true. Any messages/errors
175 * can be retrieved using {@link ApexApiResult#getMessages()}
177 private ApexApiResult updateEvent(final RestSession session, final String jsonString) {
178 LOGGER.entry(jsonString);
180 final var jsonbean = RestUtils.getJsonParameters(jsonString, BeanEvent.class);
182 if (blank2Null(jsonbean.getName()) == null || blank2Null(jsonbean.getVersion()) == null) {
183 LOGGER.exit("Event/Update" + NOT_OK);
184 return new ApexApiResult(Result.FAILED, "Null/Empty event name/version (\"" + jsonbean.getName() + ":"
185 + jsonbean.getVersion() + "\" passed to UpdateEvent");
190 ApexApiResult result = session.getApexModelEdited().deleteEvent(blank2Null(jsonbean.getName()),
191 blank2Null(jsonbean.getVersion()));
194 result = session.getApexModelEdited().createEvent(jsonbean.getName(), jsonbean.getVersion(),
195 jsonbean.getNameSpace(), jsonbean.getSource(), jsonbean.getTarget(), jsonbean.getUuid(),
196 jsonbean.getDescription());
198 if (result.isOk() && jsonbean.getParameters() != null) {
199 result = createEventParameters(session, jsonbean);
203 session.finishSession(result.isOk());
205 LOGGER.exit("Event/Update" + (result.isOk() ? OK : NOT_OK));
210 * List events with the given key names/versions. If successful the result(s)
211 * will be available in the result messages. The returned value(s) will be
212 * similar to {@link AxEvent}, with merged {@linkplain AxKeyInfo} for the root
215 * @param session the Apex model editing session
216 * @param name the name to search for. If null or empty, then all names will
218 * @param version the version to search for. If null then all versions will be
220 * @return an ApexAPIResult object. If successful then
221 * {@link ApexApiResult#isOk()} will return true. Any messages/errors
222 * can be retrieved using {@link ApexApiResult#getMessages()}
224 private ApexApiResult listEvents(final RestSession session, final String name, final String version) {
225 LOGGER.entry(name, version);
227 ApexApiResult result = session.getApexModel().listEvent(blank2Null(name), blank2Null(version));
229 LOGGER.exit("Event/Get" + (result != null && result.isOk() ? OK : NOT_OK));
234 * Delete events with the given key names/versions.
236 * @param session the Apex model editing session
237 * @param name the name to search for. If null or empty, then all names will
239 * @param version the version to search for. If null then all versions will be
241 * @return an ApexAPIResult object. If successful then
242 * {@link ApexApiResult#isOk()} will return true. Any messages/errors
243 * can be retrieved using {@link ApexApiResult#getMessages()}
245 private ApexApiResult deleteEvent(final RestSession session, final String name, final String version) {
246 LOGGER.entry(name, version);
250 ApexApiResult result = session.getApexModelEdited().deleteEvent(blank2Null(name), blank2Null(version));
252 session.finishSession(result.isOk());
254 LOGGER.exit("Event/Delete" + (result.isOk() ? OK : NOT_OK));
259 * Validate events with the given key names/versions. The result(s) will be
260 * available in the result messages.
262 * @param session the Apex model editing session
263 * @param name the name to search for. If null or empty, then all names will
265 * @param version the version to search for. If null then all versions will be
267 * @return an ApexAPIResult object. If successful then
268 * {@link ApexApiResult#isOk()} will return true. Any messages/errors
269 * can be retrieved using {@link ApexApiResult#getMessages()}
271 private ApexApiResult validateEvent(final RestSession session, final String name, final String version) {
272 LOGGER.entry(name, version);
274 ApexApiResult result = session.getApexModel().validateEvent(blank2Null(name), blank2Null(version));
276 LOGGER.exit("Validate/Event" + (result != null && result.isOk() ? OK : NOT_OK));