diff options
6 files changed, 323 insertions, 142 deletions
diff --git a/src/main/java/org/onap/clamp/clds/model/CldsEvent.java b/src/main/java/org/onap/clamp/clds/model/CldsEvent.java index 90dc996cc..b993c6376 100644 --- a/src/main/java/org/onap/clamp/clds/model/CldsEvent.java +++ b/src/main/java/org/onap/clamp/clds/model/CldsEvent.java @@ -31,6 +31,7 @@ import org.onap.clamp.clds.dao.CldsDao; public class CldsEvent { public static final String ACTION_TEST = "TEST"; public static final String ACTION_CREATE = "CREATE"; + public static final String ACTION_MODIFY = "MODIFY"; public static final String ACTION_SUBMIT = "SUBMIT"; // an update before model is active public static final String ACTION_RESUBMIT = "RESUBMIT"; diff --git a/src/main/java/org/onap/clamp/clds/model/CldsModel.java b/src/main/java/org/onap/clamp/clds/model/CldsModel.java index 14fe44390..2c178eb8b 100644 --- a/src/main/java/org/onap/clamp/clds/model/CldsModel.java +++ b/src/main/java/org/onap/clamp/clds/model/CldsModel.java @@ -25,18 +25,18 @@ package org.onap.clamp.clds.model; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.databind.JsonNode; -import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import javax.ws.rs.BadRequestException; import javax.ws.rs.NotFoundException; import org.onap.clamp.clds.dao.CldsDao; -import org.onap.clamp.clds.util.JacksonUtils; +import org.onap.clamp.clds.model.actions.ActionsHandler; +import org.onap.clamp.clds.model.actions.ActionsHandlerImpl; +import org.onap.clamp.clds.model.status.StatusHandler; +import org.onap.clamp.clds.model.status.StatusHandlerImpl; /** * Represent a CLDS Model. @@ -77,6 +77,18 @@ public class CldsModel { private String deploymentId; private String deploymentStatusUrl; + // Set default handlers but this can be changed if needed. + private static StatusHandler statusHandler = new StatusHandlerImpl(); + private static ActionsHandler actionsHandler = new ActionsHandlerImpl(); + + public static synchronized void setStatusHandler(StatusHandler statHandler) { + statusHandler = statHandler; + } + + public static synchronized void setActionsHandler(ActionsHandler cdHandler) { + actionsHandler = cdHandler; + } + /** * Construct empty model. */ @@ -122,54 +134,7 @@ public class CldsModel { * set the status in the model */ public void determineStatus() { - status = STATUS_UNKNOWN; - if (event == null || event.getActionCd() == null) { - status = STATUS_DESIGN; - } else if (event.isActionStateCd(CldsEvent.ACTION_STATE_ERROR)) { - status = STATUS_ERROR; - } else if (event.isActionAndStateCd(CldsEvent.ACTION_CREATE, CldsEvent.ACTION_STATE_ANY)) { - status = STATUS_DESIGN; - } else if (event.isActionAndStateCd(CldsEvent.ACTION_DISTRIBUTE, CldsEvent.ACTION_STATE_RECEIVED) - || event.isActionAndStateCd(CldsEvent.ACTION_UNDEPLOY, CldsEvent.ACTION_STATE_COMPLETED) - || event.isActionAndStateCd(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_STATE_COMPLETED) - || event.isActionAndStateCd(CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_STATE_COMPLETED)) { - status = STATUS_DISTRIBUTED; - } else if (event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_SENT)) { - status = STATUS_DELETING; - } else if (event.isActionAndStateCd(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_STATE_COMPLETED) - || event.isActionAndStateCd(CldsEvent.ACTION_RESTART, CldsEvent.ACTION_STATE_COMPLETED) - || event.isActionAndStateCd(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STATE_COMPLETED) - || event.isActionAndStateCd(CldsEvent.ACTION_SUBMITPOLICY, CldsEvent.ACTION_STATE_ANY)) { - status = STATUS_ACTIVE; - } else if (event.isActionAndStateCd(CldsEvent.ACTION_STOP, CldsEvent.ACTION_STATE_COMPLETED)) { - status = STATUS_STOPPED; - } - } - - /** - * Get the actionCd from current event. If none, default value is - * CldsEvent.ACTION_CREATE - */ - private String getCurrentActionCd() { - // current default actionCd is CREATE - String actionCd = CldsEvent.ACTION_CREATE; - if (event != null && event.getActionCd() != null) { - actionCd = event.getActionCd(); - } - return actionCd; - } - - /** - * Get the actionStateCd from current event. If none, default value is - * CldsEvent.ACTION_STATE_COMPLETED - */ - private String getCurrentActionStateCd() { - // current default actionStateCd is CREATE - String actionStateCd = CldsEvent.ACTION_STATE_COMPLETED; - if (event != null && event.getActionStateCd() != null) { - actionStateCd = event.getActionStateCd(); - } - return actionStateCd; + status = statusHandler.determineStatusOnLastEvent(event); } /** @@ -179,94 +144,7 @@ public class CldsModel { * the first one. */ public void determinePermittedActionCd() { - String actionCd = getCurrentActionCd(); - switch (actionCd) { - case CldsEvent.ACTION_CREATE: - permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_TEST, CldsEvent.ACTION_DELETE); - if (isSimplifiedModel()) { - permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_SUBMITPOLICY, - CldsEvent.ACTION_TEST, CldsEvent.ACTION_DELETE); - } - break; - case CldsEvent.ACTION_SUBMIT: - case CldsEvent.ACTION_RESUBMIT: - case CldsEvent.ACTION_DISTRIBUTE: - permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_RESUBMIT, - CldsEvent.ACTION_DELETE); - if (isSimplifiedModel()) { - permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_SUBMITDCAE, - CldsEvent.ACTION_DELETE); - } - break; - case CldsEvent.ACTION_SUBMITDCAE: - permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_DELETE); - break; - case CldsEvent.ACTION_SUBMITPOLICY: - permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP); - break; - case CldsEvent.ACTION_UNDEPLOY: - permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY, - CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_DELETE); - if (isSimplifiedModel()) { - permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY, - CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_DELETE); - } - break; - case CldsEvent.ACTION_DEPLOY: - permittedActionCd = Arrays.asList(CldsEvent.ACTION_UNDEPLOY, CldsEvent.ACTION_UPDATE, - CldsEvent.ACTION_STOP); - break; - case CldsEvent.ACTION_RESTART: - case CldsEvent.ACTION_UPDATE: - permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP, - CldsEvent.ACTION_UNDEPLOY); - if (isPolicyOnly()) { - permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP); - } - break; - case CldsEvent.ACTION_STOP: - permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART, - CldsEvent.ACTION_UNDEPLOY); - if (isPolicyOnly()) { - permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART, - CldsEvent.ACTION_DELETE); - } - break; - default: - logger.warn("Invalid current actionCd: " + actionCd); - } - } - - private boolean isSimplifiedModel() { - boolean result = false; - try { - if (propText != null) { - JsonNode modelJson = JacksonUtils.getObjectMapperInstance().readTree(propText); - JsonNode simpleModelJson = modelJson.get("simpleModel"); - if (simpleModelJson != null && simpleModelJson.asBoolean()) { - result = true; - } - } - } catch (IOException e) { - logger.error("Error while parsing propText json", e); - } - return result; - } - - private boolean isPolicyOnly() { - boolean result = false; - try { - if (propText != null) { - JsonNode modelJson = JacksonUtils.getObjectMapperInstance().readTree(propText); - JsonNode policyOnlyJson = modelJson.get("policyOnly"); - if (policyOnlyJson != null && policyOnlyJson.asBoolean()) { - result = true; - } - } - } catch (IOException e) { - logger.error("Error while parsing propText json", e); - } - return result; + permittedActionCd = actionsHandler.determinePermittedActionsOnLastEvent(event, propText); } /** @@ -278,8 +156,8 @@ public class CldsModel { determinePermittedActionCd(); if (!permittedActionCd.contains(requestedActionCd)) { throw new IllegalArgumentException( - "Invalid requestedActionCd: " + requestedActionCd + ". Given current actionCd: " + getCurrentActionCd() - + ", the permittedActionCd: " + permittedActionCd); + "Invalid requestedActionCd: " + requestedActionCd + ". Given current actionCd: " + + actionsHandler.getCurrentActionCd(event) + ", the permittedActionCd: " + permittedActionCd); } } diff --git a/src/main/java/org/onap/clamp/clds/model/actions/ActionsHandler.java b/src/main/java/org/onap/clamp/clds/model/actions/ActionsHandler.java new file mode 100644 index 000000000..1c54dae01 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/model/actions/ActionsHandler.java @@ -0,0 +1,169 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2018 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.clds.model.actions; + +import com.att.eelf.configuration.EELFLogger; +import com.fasterxml.jackson.databind.JsonNode; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +import org.onap.clamp.clds.model.CldsEvent; +import org.onap.clamp.clds.util.JacksonUtils; + +/** + * Interface for actions that the user can do according to the last event. + * + * + */ +public interface ActionsHandler { + + enum ModelType { + SIMPLE_MODEL("simpleModel"), POLICY_MODEL("policyModel"); + private final String type; + + private ModelType(String type) { + this.type = type; + } + + public final String getType() { + return this.type; + } + } + + public EELFLogger getLogger(); + + /** + * This method determines a list of actions that the user can do according to + * the last event. + * + * @param event + * The last event + * @param propText + * The Json properties string + * @return A list of actions + */ + default List<String> determinePermittedActionsOnLastEvent(CldsEvent event, String propText) { + List<String> permittedActions; + String actionCd = getCurrentActionCd(event); + switch (actionCd) { + case CldsEvent.ACTION_CREATE: + permittedActions = Arrays.asList(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_TEST, CldsEvent.ACTION_DELETE); + if (isTypeModel(propText, ModelType.SIMPLE_MODEL)) { + permittedActions = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_SUBMITPOLICY, + CldsEvent.ACTION_TEST, CldsEvent.ACTION_DELETE); + } + break; + case CldsEvent.ACTION_SUBMIT: + case CldsEvent.ACTION_RESUBMIT: + case CldsEvent.ACTION_DISTRIBUTE: + permittedActions = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_RESUBMIT, + CldsEvent.ACTION_DELETE); + if (isTypeModel(propText, ModelType.SIMPLE_MODEL)) { + permittedActions = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_SUBMITDCAE, + CldsEvent.ACTION_DELETE); + } + break; + case CldsEvent.ACTION_SUBMITDCAE: + permittedActions = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_DELETE); + break; + case CldsEvent.ACTION_SUBMITPOLICY: + permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP); + break; + case CldsEvent.ACTION_UNDEPLOY: + permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY, + CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_DELETE); + if (isTypeModel(propText, ModelType.SIMPLE_MODEL)) { + permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY, + CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_DELETE); + } + break; + case CldsEvent.ACTION_DEPLOY: + permittedActions = Arrays.asList(CldsEvent.ACTION_UNDEPLOY, CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP); + break; + case CldsEvent.ACTION_RESTART: + case CldsEvent.ACTION_UPDATE: + permittedActions = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP, + CldsEvent.ACTION_UNDEPLOY); + if (isTypeModel(propText, ModelType.POLICY_MODEL)) { + permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP); + } + break; + case CldsEvent.ACTION_STOP: + permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART, + CldsEvent.ACTION_UNDEPLOY); + if (isTypeModel(propText, ModelType.POLICY_MODEL)) { + permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART, + CldsEvent.ACTION_DELETE); + } + break; + default: + getLogger().warn("Invalid current actionCd: " + actionCd); + permittedActions = Arrays.asList(); + } + return permittedActions; + } + + /** + * This method returns the action of the event or a default one if not found. + * + * @param event + * The last event + * @return The action + */ + default String getCurrentActionCd(CldsEvent event) { + // current default actionCd is CREATE + String actionCd = CldsEvent.ACTION_CREATE; + if (event != null && event.getActionCd() != null) { + actionCd = event.getActionCd(); + } + return actionCd; + } + + /** + * Check whether the text properties is of specified type ModelType. + * + * @param propText + * The Clamp Json properties + * @param key + * The model type + * @return True if matches the right type specified + */ + default boolean isTypeModel(String propText, ModelType key) { + boolean result = false; + try { + if (propText != null) { + JsonNode modelJson = JacksonUtils.getObjectMapperInstance().readTree(propText); + JsonNode modelJsonOfType = modelJson.get(key.getType()); + if (modelJsonOfType != null && modelJsonOfType.asBoolean()) { + result = true; + } + } + } catch (IOException e) { + getLogger().error("Error while parsing propText json", e); + } + return result; + } +} diff --git a/src/main/java/org/onap/clamp/clds/model/actions/ActionsHandlerImpl.java b/src/main/java/org/onap/clamp/clds/model/actions/ActionsHandlerImpl.java new file mode 100644 index 000000000..8d7d24089 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/model/actions/ActionsHandlerImpl.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2018 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.clds.model.actions; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +public class ActionsHandlerImpl implements ActionsHandler { + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(ActionsHandlerImpl.class); + + @Override + public EELFLogger getLogger() { + return logger; + } + +} diff --git a/src/main/java/org/onap/clamp/clds/model/status/StatusHandler.java b/src/main/java/org/onap/clamp/clds/model/status/StatusHandler.java new file mode 100644 index 000000000..300c8b133 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/model/status/StatusHandler.java @@ -0,0 +1,68 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2018 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.clds.model.status; + +import org.onap.clamp.clds.model.CldsEvent; +import org.onap.clamp.clds.model.CldsModel; + +/** + * This interface describe the handler of the status in CldsModel, it's normally + * based on the last event stored in db. + * + */ +public interface StatusHandler { + + /** + * This method determines the status of the closed loop based on the last event. + * + * @param event + * The last event + * @return The status + */ + default String determineStatusOnLastEvent(CldsEvent event) { + String status = CldsModel.STATUS_UNKNOWN; + if (event == null || event.getActionCd() == null) { + status = CldsModel.STATUS_DESIGN; + } else if (event.isActionStateCd(CldsEvent.ACTION_STATE_ERROR)) { + status = CldsModel.STATUS_ERROR; + } else if (event.isActionAndStateCd(CldsEvent.ACTION_CREATE, CldsEvent.ACTION_STATE_ANY)) { + status = CldsModel.STATUS_DESIGN; + } else if (event.isActionAndStateCd(CldsEvent.ACTION_DISTRIBUTE, CldsEvent.ACTION_STATE_RECEIVED) + || event.isActionAndStateCd(CldsEvent.ACTION_UNDEPLOY, CldsEvent.ACTION_STATE_COMPLETED) + || event.isActionAndStateCd(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_STATE_COMPLETED) + || event.isActionAndStateCd(CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_STATE_COMPLETED)) { + status = CldsModel.STATUS_DISTRIBUTED; + } else if (event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_SENT)) { + status = CldsModel.STATUS_DELETING; + } else if (event.isActionAndStateCd(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_STATE_COMPLETED) + || event.isActionAndStateCd(CldsEvent.ACTION_RESTART, CldsEvent.ACTION_STATE_COMPLETED) + || event.isActionAndStateCd(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STATE_COMPLETED) + || event.isActionAndStateCd(CldsEvent.ACTION_SUBMITPOLICY, CldsEvent.ACTION_STATE_ANY)) { + status = CldsModel.STATUS_ACTIVE; + } else if (event.isActionAndStateCd(CldsEvent.ACTION_STOP, CldsEvent.ACTION_STATE_COMPLETED)) { + status = CldsModel.STATUS_STOPPED; + } + return status; + } +} diff --git a/src/main/java/org/onap/clamp/clds/model/status/StatusHandlerImpl.java b/src/main/java/org/onap/clamp/clds/model/status/StatusHandlerImpl.java new file mode 100644 index 000000000..b539e9050 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/model/status/StatusHandlerImpl.java @@ -0,0 +1,27 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2018 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.clds.model.status; + +public class StatusHandlerImpl implements StatusHandler { +} |