2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.clds.model.actions;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
29 import com.google.gson.JsonParseException;
30 import java.util.Arrays;
31 import java.util.List;
32 import org.onap.clamp.clds.model.CldsEvent;
33 import org.onap.clamp.clds.util.JsonUtils;
36 * Interface for actions that the user can do according to the last event.
40 public interface ActionsHandler {
43 SIMPLE_MODEL("simpleModel"), POLICY_MODEL("policyModel");
44 private final String type;
46 ModelType(String type) {
50 public final String getType() {
55 EELFLogger getLogger();
58 * This method determines a list of actions that the user can do according to
64 * The Json properties string
65 * @return A list of actions
67 default List<String> determinePermittedActionsOnLastEvent(CldsEvent event, String propText) {
68 List<String> permittedActions;
69 String actionCd = getCurrentActionCd(event);
71 case CldsEvent.ACTION_CREATE:
72 permittedActions = Arrays.asList(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_TEST,
73 CldsEvent.ACTION_DELETE);
74 if (isTypeModel(propText, ModelType.SIMPLE_MODEL)) {
75 permittedActions = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_SUBMITPOLICY,
76 CldsEvent.ACTION_TEST, CldsEvent.ACTION_DELETE);
79 case CldsEvent.ACTION_SUBMIT:
80 case CldsEvent.ACTION_RESUBMIT:
81 case CldsEvent.ACTION_DISTRIBUTE:
82 permittedActions = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_RESUBMIT,
83 CldsEvent.ACTION_DELETE);
84 if (isTypeModel(propText, ModelType.SIMPLE_MODEL)) {
85 permittedActions = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_SUBMITDCAE,
86 CldsEvent.ACTION_DELETE);
89 case CldsEvent.ACTION_SUBMITDCAE:
90 permittedActions = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_DELETE);
92 case CldsEvent.ACTION_SUBMITPOLICY:
93 permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
95 case CldsEvent.ACTION_UNDEPLOY:
96 permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY,
97 CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_DELETE);
98 if (isTypeModel(propText, ModelType.SIMPLE_MODEL)) {
99 permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY,
100 CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_DELETE);
103 case CldsEvent.ACTION_DEPLOY:
104 permittedActions = Arrays.asList(CldsEvent.ACTION_UNDEPLOY, CldsEvent.ACTION_UPDATE,
105 CldsEvent.ACTION_STOP);
107 case CldsEvent.ACTION_RESTART:
108 case CldsEvent.ACTION_UPDATE:
109 permittedActions = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UPDATE,
110 CldsEvent.ACTION_STOP, CldsEvent.ACTION_UNDEPLOY);
111 if (isTypeModel(propText, ModelType.POLICY_MODEL)) {
112 permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
115 case CldsEvent.ACTION_STOP:
116 permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART,
117 CldsEvent.ACTION_UNDEPLOY);
118 if (isTypeModel(propText, ModelType.POLICY_MODEL)) {
119 permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART,
120 CldsEvent.ACTION_DELETE);
124 getLogger().warn("Invalid current actionCd: " + actionCd);
125 permittedActions = Arrays.asList();
127 return permittedActions;
131 * This returns the action of the event or a default one if not found.
133 * @param event The last event
136 default String getCurrentActionCd(CldsEvent event) {
137 // current default actionCd is CREATE
138 String actionCd = CldsEvent.ACTION_CREATE;
139 if (event != null && event.getActionCd() != null) {
140 actionCd = event.getActionCd();
146 * Check whether the text properties is of specified type ModelType.
149 * The Clamp Json properties
152 * @return True if matches the right type specified
154 default boolean isTypeModel(String propText, ModelType key) {
155 boolean result = false;
157 if (propText != null) {
158 JsonObject modelJson = JsonUtils.GSON.fromJson(propText, JsonObject.class);
159 JsonElement modelJsonOfType = modelJson.get(key.getType());
160 if (modelJsonOfType != null
161 && modelJsonOfType.isJsonPrimitive()
162 && modelJsonOfType.getAsJsonPrimitive().getAsBoolean()) {
166 } catch (JsonParseException e) {
167 getLogger().error("Error while parsing propText json", e);