2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.client.editor.rest.handling;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import com.google.gson.JsonObject;
27 import java.util.ArrayList;
28 import java.util.List;
29 import org.onap.policy.apex.client.editor.rest.handling.bean.BeanModel;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
32 import org.onap.policy.apex.model.modelapi.ApexApiResult;
33 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
34 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
35 import org.slf4j.ext.XLogger;
36 import org.slf4j.ext.XLoggerFactory;
39 * This class handles commands on Apex models.
41 public class ModelHandler implements RestCommandHandler {
43 // Get a reference to the logger
44 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ModelHandler.class);
46 // Recurring string constants
47 private static final String OK = ": OK";
48 private static final String NOT_OK = ": Not OK";
49 private static final String KEY = "key";
50 private static final String NAME = "name";
51 private static final String VERSION = "version";
52 private static final String UUID = "uuid";
53 private static final String DESCRIPTION = "description";
54 private static final String POLICY_KEY = "policyKey";
55 private static final String APEX_KEY_INFO = "apexKeyInfo";
61 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
62 final RestCommand command) {
63 if (!RestCommandType.MODEL.equals(commandType)) {
64 return getUnsupportedCommandResultMessage(session, commandType, command);
69 return analyse(session);
71 return validate(session);
73 return getModelKey(session);
75 return listModel(session);
77 return downloadModel(session);
79 return deleteModel(session);
81 return getUnsupportedCommandResultMessage(session, commandType, command);
89 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
90 final RestCommand command, final String jsonString) {
91 if (!RestCommandType.MODEL.equals(commandType)) {
92 return getUnsupportedCommandResultMessage(session, commandType, command);
97 return loadFromString(session, jsonString);
99 return createModel(session, jsonString);
101 return updateModel(session, jsonString);
103 return getUnsupportedCommandResultMessage(session, commandType, command);
111 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
112 final RestCommand command, final String name, final String version) {
113 return getUnsupportedCommandResultMessage(session, commandType, command);
117 * Load the model from a JSON string for this session.
119 * @param session the Apex model editing session
120 * @param jsonString the JSON string to be parsed. The returned value(s) will be similar to {@link AxPolicyModel},
121 * with merged {@linkplain AxKeyInfo} for the root object.
122 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
123 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
125 private ApexApiResult loadFromString(final RestSession session, final String jsonString) {
126 LOGGER.entry(jsonString);
130 ApexApiResult result = session.getApexModelEdited().loadFromString(jsonString);
132 session.finishSession(result.isOk());
134 LOGGER.exit("Model/Load" + (result != null && result.isOk() ? OK : NOT_OK));
139 * Analyse the model and return analysis results. If successful the analysis results will be available in the
140 * messages in the result.
142 * @param session the Apex model editing session
143 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
144 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
146 private ApexApiResult analyse(final RestSession session) {
149 ApexApiResult result = session.getApexModel().analyse();
151 LOGGER.exit("Model/Analyse" + (result != null && result.isOk() ? OK : NOT_OK));
156 * Validate the model and return validation results. If successful the validation results will be available in the
157 * messages in the result.
159 * @param session the Apex model editing session
160 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
161 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
163 private ApexApiResult validate(final RestSession session) {
166 ApexApiResult result = session.getApexModel().validate();
168 LOGGER.exit("Model/Validate" + (result != null && result.isOk() ? OK : NOT_OK));
173 * Creates the new model model for this session.
175 * @param session the Apex model editing session
176 * @param jsonString the JSON string to be parsed containing the new model. See {@linkplain BeanModel}
177 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
178 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
180 private ApexApiResult createModel(final RestSession session, final String jsonString) {
181 LOGGER.entry(jsonString);
183 final BeanModel jsonbean = RestUtils.getJsonParameters(jsonString, BeanModel.class);
187 ApexApiResult result = session.getApexModelEdited().createModel(jsonbean.getName(), jsonbean.getVersion(),
188 jsonbean.getUuid(), jsonbean.getDescription());
190 session.finishSession(result.isOk());
192 LOGGER.exit("Model/Create" + (result != null && result.isOk() ? OK : NOT_OK));
197 * Update the model for this session.
199 * @param session the Apex model editing session
200 * @param jsonString the JSON string to be parsed containing the updated model. See {@linkplain BeanModel}
201 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
202 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
204 private ApexApiResult updateModel(final RestSession session, final String jsonString) {
205 LOGGER.entry(jsonString);
207 final BeanModel jsonbean = RestUtils.getJsonParameters(jsonString, BeanModel.class);
211 ApexApiResult result = session.getApexModelEdited().updateModel(jsonbean.getName(), jsonbean.getVersion(),
212 jsonbean.getUuid(), jsonbean.getDescription());
214 session.finishSession(result.isOk());
216 LOGGER.exit("Model/Update" + (result != null && result.isOk() ? OK : NOT_OK));
221 * Gets the key for the model for this session. If successful the model key will be available in the first message
222 * in the result. See {@linkplain AxKey}
224 * @param session the Apex model editing session
225 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
226 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
228 private ApexApiResult getModelKey(final RestSession session) {
231 ApexApiResult result = session.getApexModel().getModelKey();
233 LOGGER.exit("Model/GetKey" + (result != null && result.isOk() ? OK : NOT_OK));
238 * Retrieve the model for this session. If successful the model will be available in the first message in the
239 * result. The returned value will be similar to a {@link AxPolicyModel}, with merged {@linkplain AxKeyInfo} for the
242 * @param session the Apex model editing session
243 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
244 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
246 private ApexApiResult listModel(final RestSession session) {
249 ApexApiResult result = session.getApexModel().listModel();
251 result = addKeyInfo2Messages(session, result);
253 LOGGER.exit("Model/Get" + (result != null && result.isOk() ? OK : NOT_OK));
258 * Download the model for this session as a String.
260 * @param session the Apex model editing session
261 * @return the model represented as a JSON string. See {@linkplain AxPolicyModel}
263 private ApexApiResult downloadModel(final RestSession session) {
266 ApexApiResult result = session.getApexModel().listModel();
268 LOGGER.exit("Model/Download" + (result != null && result.isOk() ? OK : NOT_OK));
273 * Delete the model for this session.
275 * @param session the Apex model editing session
276 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
277 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
279 private ApexApiResult deleteModel(final RestSession session) {
284 ApexApiResult result = session.getApexModel().deleteModel();
286 session.finishSession(result.isOk());
288 LOGGER.exit("Model/Delete" + (result != null && result.isOk() ? OK : NOT_OK));
293 * The json strings representing the objects listed, stored in result.messages[], does not contain the
294 * AxKeyInformation for that object. This utility method retrieves the AxKeyInfo for each object and adds it to the
295 * json for the object.
297 * @param session the Apex model editing session
298 * @param incomingResult The list result, containing JSON representations of objects stored in its "messages" array
299 * @return The list result, containing JSON augmented representations of objects stored in its "messages" array
301 private ApexApiResult addKeyInfo2Messages(final RestSession session, final ApexApiResult incomingResult) {
302 final ApexApiResult result = new ApexApiResult(incomingResult.getResult());
303 result.setMessages(incomingResult.getMessages());
305 final List<String> messages = incomingResult.getMessages();
306 final List<String> augmentedMessages = new ArrayList<>(messages.size());
308 for (final String message : messages) {
309 augmentedMessages.add(addKeyInfo2Message(session, message));
311 result.setMessages(augmentedMessages);
313 if (messages.size() != augmentedMessages.size()) {
314 result.setResult(Result.OTHER_ERROR);
315 result.addMessage("Failed to add KeyInfo to all results. Results are not complete");
322 * Augment a message with key information.
324 * @param session the Apex model editing session
325 * @param message The message to augment
326 * @return the augmented message
328 private String addKeyInfo2Message(final RestSession session, final String message) {
329 final Gson gson = new GsonBuilder().serializeNulls().enableComplexMapKeySerialization().create();
331 JsonObject jsonObject = gson.fromJson(message, JsonObject.class);
332 if (jsonObject == null) {
336 String name = readFieldFromJsonObject(jsonObject, NAME, null);
337 String version = readFieldFromJsonObject(jsonObject, VERSION, null);
339 if (name == null && version == null) {
340 JsonObject newJsonObject = getSubJsonObject(jsonObject);
342 if (newJsonObject != null) {
343 jsonObject = newJsonObject;
344 name = readFieldFromJsonObject(jsonObject, NAME, name);
345 version = readFieldFromJsonObject(jsonObject, VERSION, version);
349 if (name == null || version == null || !setUuidAndDescription(session, jsonObject, name, version)) {
350 jsonObject.addProperty(UUID, (String) null);
351 jsonObject.addProperty(DESCRIPTION, (String) null);
354 return gson.toJson(jsonObject);
358 * Get an embedded JSON object for the given JSON object.
360 * @param jsonObject the input JSON object
361 * @return the embedded JSON object
363 private JsonObject getSubJsonObject(JsonObject jsonObject) {
364 if (jsonObject.entrySet() != null && !jsonObject.entrySet().isEmpty()) {
365 return (JsonObject) jsonObject.entrySet().iterator().next().getValue();
372 * Condition a field so its key information can be looked up.
374 * @param jsonObject the object to query
375 * @param fieldTag the tag of the field to condition
376 * @param fieldValue the value of the field to condition
377 * @return field read from the json
379 private String readFieldFromJsonObject(final JsonObject jsonObject, final String fieldTag, final String value) {
380 String lookedupValue = value;
382 if (jsonObject != null && jsonObject.get(KEY) != null && jsonObject.get(KEY).isJsonObject()
383 && jsonObject.getAsJsonObject(KEY).get(fieldTag) != null) {
384 lookedupValue = jsonObject.getAsJsonObject(KEY).get(fieldTag).getAsString();
385 } else if (jsonObject != null && jsonObject.get(POLICY_KEY) != null && jsonObject.get(POLICY_KEY).isJsonObject()
386 && jsonObject.getAsJsonObject(POLICY_KEY).get(fieldTag) != null) {
387 lookedupValue = jsonObject.getAsJsonObject(POLICY_KEY).get(fieldTag).getAsString();
389 return lookedupValue;
393 * Look up the UUID and description in the key information for a concept.
395 * @param session the Apex editor session
396 * @param jsonObject the JSON object to place the fields in
397 * @param name the concept name to look up
398 * @param version the concept version to look up
400 private boolean setUuidAndDescription(final RestSession session, JsonObject jsonObject, String name,
402 // Look up the key information for the name and version
403 JsonObject keyInfoJsonObject = lookupKeyInfo(session, name, version);
404 if (keyInfoJsonObject == null || keyInfoJsonObject.get(APEX_KEY_INFO) != null) {
408 if (keyInfoJsonObject.get(APEX_KEY_INFO).getAsJsonObject().get("UUID") != null) {
409 jsonObject.addProperty(UUID,
410 keyInfoJsonObject.get(APEX_KEY_INFO).getAsJsonObject().get("UUID").getAsString());
412 jsonObject.addProperty(UUID, (String) null);
415 if (keyInfoJsonObject.get(APEX_KEY_INFO).getAsJsonObject().get(DESCRIPTION) != null) {
416 jsonObject.addProperty(DESCRIPTION,
417 keyInfoJsonObject.get(APEX_KEY_INFO).getAsJsonObject().get(DESCRIPTION).getAsString());
419 jsonObject.addProperty(DESCRIPTION, (String) null);
426 * Look up the key information for the given concept name and value.
428 * @param session the Apex editor session
429 * @param name the concept name to look up
430 * @param version the concept version to look up
431 * @return a JSON version of the concept key information
433 private JsonObject lookupKeyInfo(final RestSession session, final String name, final String version) {
434 final ApexApiResult keyInfoResult = session.getApexModel().listKeyInformation(name, version);
435 final List<String> keyInfoMessages = keyInfoResult.getMessages();
437 if (keyInfoResult.isNok() || keyInfoMessages == null || keyInfoMessages.isEmpty()) {
441 final Gson gson = new GsonBuilder().serializeNulls().enableComplexMapKeySerialization().create();
442 final String keyInfoJson = keyInfoMessages.get(0);
443 return gson.fromJson(keyInfoJson, JsonObject.class);