2 * ============LICENSE_START=======================================================
3 * Copyright (C) 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.handling;
23 import java.util.Map.Entry;
25 import org.onap.policy.apex.client.editor.rest.handling.bean.BeanEvent;
26 import org.onap.policy.apex.client.editor.rest.handling.bean.BeanField;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
28 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
29 import org.onap.policy.apex.model.modelapi.ApexApiResult;
30 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
35 * This class handles commands on events in Apex models.
37 public class EventHandler implements RestCommandHandler {
38 // Get a reference to the logger
39 private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventHandler.class);
41 // Recurring string constants
42 private static final String OK = ": OK";
43 private static final String NOT_OK = ": Not OK";
49 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
50 final RestCommand command) {
51 return getUnsupportedCommandResultMessage(session, commandType, command);
58 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
59 final RestCommand command, final String jsonString) {
60 if (!RestCommandType.EVENT.equals(commandType)) {
61 return getUnsupportedCommandResultMessage(session, commandType, command);
66 return createEvent(session, jsonString);
68 return updateEvent(session, jsonString);
70 return getUnsupportedCommandResultMessage(session, commandType, command);
78 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
79 final RestCommand command, final String name, final String version) {
80 if (!RestCommandType.EVENT.equals(commandType)) {
81 return getUnsupportedCommandResultMessage(session, commandType, command);
86 return listEvents(session, name, version);
88 return deleteEvent(session, name, version);
90 return validateEvent(session, name, version);
92 return getUnsupportedCommandResultMessage(session, commandType, command);
97 * Creates an event with the information in the JSON string passed.
99 * @param session the Apex model editing session
100 * @param jsonString the JSON string to be parsed. See {@linkplain BeanEvent}
101 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
102 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
104 private ApexApiResult createEvent(final RestSession session, final String jsonString) {
105 LOGGER.entry(jsonString);
107 final BeanEvent jsonbean = RestUtils.getJsonParameters(jsonString, BeanEvent.class);
111 ApexApiResult result = session.getApexModelEdited().createEvent(jsonbean.getName(), jsonbean.getVersion(),
112 jsonbean.getNameSpace(), jsonbean.getSource(), jsonbean.getTarget(), jsonbean.getUuid(),
113 jsonbean.getDescription());
116 result = createEventParameters(session, jsonbean);
119 session.finishSession(result.isOk());
121 LOGGER.exit("Event/Create" + (result != null && result.isOk() ? OK : NOT_OK));
126 * Create the parameters on an event.
128 * @param session the Apex editor session
129 * @param jsonbean the JSON bean holding the parameters
130 * @param result the result of the parameter creation operation
133 private ApexApiResult createEventParameters(final RestSession session, final BeanEvent jsonbean) {
134 ApexApiResult result = new ApexApiResult();
136 if (jsonbean.getParameters() == null || jsonbean.getParameters().isEmpty()) {
140 for (final Entry<String, BeanField> parameterEntry : jsonbean.getParameters().entrySet()) {
141 if (parameterEntry.getValue() == null) {
142 result.setResult(Result.FAILED);
143 result.addMessage("Null event parameter information for parameter \"" + parameterEntry.getKey()
144 + "\" in event " + jsonbean.getName() + ":" + jsonbean.getVersion()
145 + ". The event was created, but there was an error adding the event parameters."
146 + " The event has only been partially defined.");
150 final ApexApiResult createParResult = session.getApexModelEdited().createEventPar(jsonbean.getName(),
151 jsonbean.getVersion(), parameterEntry.getKey(), parameterEntry.getValue().getName(),
152 parameterEntry.getValue().getVersion(), parameterEntry.getValue().getOptional());
153 if (createParResult.isNok()) {
154 result.setResult(createParResult.getResult());
155 result.addMessage("Failed to add event parameter information for parameter \"" + parameterEntry.getKey()
156 + "\" in event " + jsonbean.getName() + ":" + jsonbean.getVersion()
157 + ". The event was created, but there was an error adding the event parameters."
158 + " The event has only been partially defined.");
166 * Update an event with the information in the JSON string passed.
168 * @param session the Apex model editing session
169 * @param jsonString the JSON string to be parsed. See {@linkplain BeanEvent}
170 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
171 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
173 private ApexApiResult updateEvent(final RestSession session, final String jsonString) {
174 LOGGER.entry(jsonString);
176 final BeanEvent jsonbean = RestUtils.getJsonParameters(jsonString, BeanEvent.class);
178 if (blank2Null(jsonbean.getName()) == null || blank2Null(jsonbean.getVersion()) == null) {
179 LOGGER.exit("Event/Update" + NOT_OK);
180 return new ApexApiResult(Result.FAILED, "Null/Empty event name/version (\"" + jsonbean.getName() + ":"
181 + jsonbean.getVersion() + "\" passed to UpdateEvent");
186 ApexApiResult result = session.getApexModelEdited().deleteEvent(blank2Null(jsonbean.getName()),
187 blank2Null(jsonbean.getVersion()));
190 result = session.getApexModelEdited().createEvent(jsonbean.getName(), jsonbean.getVersion(),
191 jsonbean.getNameSpace(), jsonbean.getSource(), jsonbean.getTarget(), jsonbean.getUuid(),
192 jsonbean.getDescription());
194 if (result.isOk() && jsonbean.getParameters() != null) {
195 result = createEventParameters(session, jsonbean);
199 session.finishSession(result.isOk());
201 LOGGER.exit("Event/Update" + (result != null && result.isOk() ? OK : NOT_OK));
206 * List events with the given key names/versions. If successful the result(s) will be available in the result
207 * messages. The returned value(s) will be similar to {@link AxEvent}, with merged {@linkplain AxKeyInfo} for the
210 * @param session the Apex model editing session
211 * @param name the name to search for. If null or empty, then all names will be queried
212 * @param version the version to search for. If null then all versions will be searched for.
213 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
214 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
216 private ApexApiResult listEvents(final RestSession session, final String name, final String version) {
217 LOGGER.entry(name, version);
219 ApexApiResult result = session.getApexModel().listEvent(blank2Null(name), blank2Null(version));
221 LOGGER.exit("Event/Get" + (result != null && result.isOk() ? OK : NOT_OK));
226 * Delete events with the given key names/versions.
228 * @param session the Apex model editing session
229 * @param name the name to search for. If null or empty, then all names will be queried
230 * @param version the version to search for. If null then all versions will be searched for.
231 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
232 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
234 private ApexApiResult deleteEvent(final RestSession session, final String name, final String version) {
235 LOGGER.entry(name, version);
239 ApexApiResult result = session.getApexModelEdited().deleteEvent(blank2Null(name), blank2Null(version));
241 session.finishSession(result.isOk());
243 LOGGER.exit("Event/Delete" + (result != null && result.isOk() ? OK : NOT_OK));
248 * Validate events with the given key names/versions. The result(s) will be available in the result messages.
250 * @param session the Apex model editing session
251 * @param name the name to search for. If null or empty, then all names will be queried
252 * @param version the version to search for. If null then all versions will be searched for.
253 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
254 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
256 private ApexApiResult validateEvent(final RestSession session, final String name, final String version) {
257 LOGGER.entry(name, version);
259 ApexApiResult result = session.getApexModel().validateEvent(blank2Null(name), blank2Null(version));
261 LOGGER.exit("Validate/Event" + (result != null && result.isOk() ? OK : NOT_OK));