1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
* Modifications Copyright (C) 2019-2022 Nordix Foundation.
* Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.policy.gui.editors.apex.rest.handling;
import java.util.Map.Entry;
import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
import org.onap.policy.apex.model.modelapi.ApexApiResult;
import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
import org.onap.policy.gui.editors.apex.rest.handling.bean.BeanEvent;
import org.onap.policy.gui.editors.apex.rest.handling.bean.BeanField;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;
import org.springframework.stereotype.Service;
/**
* This class handles commands on events in Apex models.
*/
@Service
public class EventHandler implements RestCommandHandler {
// Get a reference to the logger
private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventHandler.class);
// Recurring string constants
private static final String OK = ": OK";
private static final String NOT_OK = ": Not OK";
/**
* {@inheritDoc}.
*/
@Override
public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
final RestCommand command) {
return getUnsupportedCommandResultMessage(session, commandType, command);
}
/**
* {@inheritDoc}.
*/
@Override
public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
final RestCommand command, final String jsonString) {
if (!RestCommandType.EVENT.equals(commandType)) {
return getUnsupportedCommandResultMessage(session, commandType, command);
}
switch (command) {
case CREATE:
return createEvent(session, jsonString);
case UPDATE:
return updateEvent(session, jsonString);
default:
return getUnsupportedCommandResultMessage(session, commandType, command);
}
}
/**
* {@inheritDoc}.
*/
@Override
public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
final RestCommand command, final String name, final String version) {
if (!RestCommandType.EVENT.equals(commandType)) {
return getUnsupportedCommandResultMessage(session, commandType, command);
}
switch (command) {
case LIST:
return listEvents(session, name, version);
case DELETE:
return deleteEvent(session, name, version);
case VALIDATE:
return validateEvent(session, name, version);
default:
return getUnsupportedCommandResultMessage(session, commandType, command);
}
}
/**
* Creates an event with the information in the JSON string passed.
*
* @param session the Apex model editing session
* @param jsonString the JSON string to be parsed. See {@linkplain BeanEvent}
* @return an ApexAPIResult object. If successful then
* {@link ApexApiResult#isOk()} will return true. Any messages/errors
* can be retrieved using {@link ApexApiResult#getMessages()}
*/
private ApexApiResult createEvent(final RestSession session, final String jsonString) {
LOGGER.entry(jsonString);
final var jsonbean = RestUtils.getJsonParameters(jsonString, BeanEvent.class);
session.editModel();
ApexApiResult result = session.getApexModelEdited().createEvent(jsonbean.getName(), jsonbean.getVersion(),
jsonbean.getNameSpace(), jsonbean.getSource(), jsonbean.getTarget(), jsonbean.getUuid(),
jsonbean.getDescription());
if (result.isOk()) {
result = createEventParameters(session, jsonbean);
}
session.finishSession(result.isOk());
LOGGER.exit("Event/Create" + (result.isOk() ? OK : NOT_OK));
return result;
}
/**
* Create the parameters on an event.
*
* @param session the Apex editor session
* @param jsonbean the JSON bean holding the parameters
* @return result the result of the parameter creation operation
*/
private ApexApiResult createEventParameters(final RestSession session, final BeanEvent jsonbean) {
var result = new ApexApiResult();
if (jsonbean.getParameters() == null || jsonbean.getParameters().isEmpty()) {
return result;
}
for (final Entry<String, BeanField> parameterEntry : jsonbean.getParameters().entrySet()) {
if (parameterEntry.getValue() == null) {
result.setResult(Result.FAILED);
result.addMessage("Null event parameter information for parameter \"" + parameterEntry.getKey()
+ "\" in event " + jsonbean.getName() + ":" + jsonbean.getVersion()
+ ". The event was created, but there was an error adding the event parameters."
+ " The event has only been partially defined.");
continue;
}
final ApexApiResult createParResult = session.getApexModelEdited().createEventPar(jsonbean.getName(),
jsonbean.getVersion(), parameterEntry.getKey(), parameterEntry.getValue().getName(),
parameterEntry.getValue().getVersion(), parameterEntry.getValue().isOptional());
if (createParResult.isNok()) {
result.setResult(createParResult.getResult());
result.addMessage("Failed to add event parameter information for parameter \"" + parameterEntry.getKey()
+ "\" in event " + jsonbean.getName() + ":" + jsonbean.getVersion()
+ ". The event was created, but there was an error adding the event parameters."
+ " The event has only been partially defined.");
}
}
return result;
}
/**
* Update an event with the information in the JSON string passed.
*
* @param session the Apex model editing session
* @param jsonString the JSON string to be parsed. See {@linkplain BeanEvent}
* @return an ApexAPIResult object. If successful then
* {@link ApexApiResult#isOk()} will return true. Any messages/errors
* can be retrieved using {@link ApexApiResult#getMessages()}
*/
private ApexApiResult updateEvent(final RestSession session, final String jsonString) {
LOGGER.entry(jsonString);
final var jsonbean = RestUtils.getJsonParameters(jsonString, BeanEvent.class);
if (blank2Null(jsonbean.getName()) == null || blank2Null(jsonbean.getVersion()) == null) {
LOGGER.exit("Event/Update" + NOT_OK);
return new ApexApiResult(Result.FAILED, "Null/Empty event name/version (\"" + jsonbean.getName() + ":"
+ jsonbean.getVersion() + "\" passed to UpdateEvent");
}
session.editModel();
ApexApiResult result = session.getApexModelEdited().deleteEvent(blank2Null(jsonbean.getName()),
blank2Null(jsonbean.getVersion()));
if (result.isOk()) {
result = session.getApexModelEdited().createEvent(jsonbean.getName(), jsonbean.getVersion(),
jsonbean.getNameSpace(), jsonbean.getSource(), jsonbean.getTarget(), jsonbean.getUuid(),
jsonbean.getDescription());
if (result.isOk() && jsonbean.getParameters() != null) {
result = createEventParameters(session, jsonbean);
}
}
session.finishSession(result.isOk());
LOGGER.exit("Event/Update" + (result.isOk() ? OK : NOT_OK));
return result;
}
/**
* List events with the given key names/versions. If successful the result(s)
* will be available in the result messages. The returned value(s) will be
* similar to {@link AxEvent}, with merged {@linkplain AxKeyInfo} for the root
* object.
*
* @param session the Apex model editing session
* @param name the name to search for. If null or empty, then all names will
* be queried
* @param version the version to search for. If null then all versions will be
* searched for.
* @return an ApexAPIResult object. If successful then
* {@link ApexApiResult#isOk()} will return true. Any messages/errors
* can be retrieved using {@link ApexApiResult#getMessages()}
*/
private ApexApiResult listEvents(final RestSession session, final String name, final String version) {
LOGGER.entry(name, version);
ApexApiResult result = session.getApexModel().listEvent(blank2Null(name), blank2Null(version));
LOGGER.exit("Event/Get" + (result != null && result.isOk() ? OK : NOT_OK));
return result;
}
/**
* Delete events with the given key names/versions.
*
* @param session the Apex model editing session
* @param name the name to search for. If null or empty, then all names will
* be queried
* @param version the version to search for. If null then all versions will be
* searched for.
* @return an ApexAPIResult object. If successful then
* {@link ApexApiResult#isOk()} will return true. Any messages/errors
* can be retrieved using {@link ApexApiResult#getMessages()}
*/
private ApexApiResult deleteEvent(final RestSession session, final String name, final String version) {
LOGGER.entry(name, version);
session.editModel();
ApexApiResult result = session.getApexModelEdited().deleteEvent(blank2Null(name), blank2Null(version));
session.finishSession(result.isOk());
LOGGER.exit("Event/Delete" + (result.isOk() ? OK : NOT_OK));
return result;
}
/**
* Validate events with the given key names/versions. The result(s) will be
* available in the result messages.
*
* @param session the Apex model editing session
* @param name the name to search for. If null or empty, then all names will
* be queried
* @param version the version to search for. If null then all versions will be
* searched for.
* @return an ApexAPIResult object. If successful then
* {@link ApexApiResult#isOk()} will return true. Any messages/errors
* can be retrieved using {@link ApexApiResult#getMessages()}
*/
private ApexApiResult validateEvent(final RestSession session, final String name, final String version) {
LOGGER.entry(name, version);
ApexApiResult result = session.getApexModel().validateEvent(blank2Null(name), blank2Null(version));
LOGGER.exit("Validate/Event" + (result != null && result.isOk() ? OK : NOT_OK));
return result;
}
}
|