Removal of dead code
[clamp.git] / src / main / java / org / onap / clamp / clds / model / actions / ActionsHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.model.actions;
25
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;
34
35 /**
36  * Interface for actions that the user can do according to the last event.
37  *
38  *
39  */
40 public interface ActionsHandler {
41
42     enum ModelType {
43         SIMPLE_MODEL("simpleModel"), POLICY_MODEL("policyModel");
44         private final String type;
45
46         ModelType(String type) {
47             this.type = type;
48         }
49
50         public final String getType() {
51             return this.type;
52         }
53     }
54
55     EELFLogger getLogger();
56
57     /**
58      * This method determines a list of actions that the user can do according to
59      * the last event.
60      *
61      * @param event
62      *        The last event
63      * @param propText
64      *        The Json properties string
65      * @return A list of actions
66      */
67     default List<String> determinePermittedActionsOnLastEvent(CldsEvent event, String propText) {
68         List<String> permittedActions;
69         String actionCd = getCurrentActionCd(event);
70         switch (actionCd) {
71         case CldsEvent.ACTION_CREATE:
72             permittedActions = Arrays.asList(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_TEST, CldsEvent.ACTION_DELETE);
73             if (isTypeModel(propText, ModelType.SIMPLE_MODEL)) {
74                 permittedActions = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_SUBMITPOLICY,
75                     CldsEvent.ACTION_TEST, CldsEvent.ACTION_DELETE);
76             }
77             break;
78         case CldsEvent.ACTION_SUBMIT:
79         case CldsEvent.ACTION_RESUBMIT:
80         case CldsEvent.ACTION_DISTRIBUTE:
81             permittedActions = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_RESUBMIT,
82                 CldsEvent.ACTION_DELETE);
83             if (isTypeModel(propText, ModelType.SIMPLE_MODEL)) {
84                 permittedActions = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_SUBMITDCAE,
85                     CldsEvent.ACTION_DELETE);
86             }
87             break;
88         case CldsEvent.ACTION_SUBMITDCAE:
89             permittedActions = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_DELETE);
90             break;
91         case CldsEvent.ACTION_SUBMITPOLICY:
92             permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
93             break;
94         case CldsEvent.ACTION_UNDEPLOY:
95             permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY,
96                 CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_DELETE);
97             if (isTypeModel(propText, ModelType.SIMPLE_MODEL)) {
98                 permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY,
99                     CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_DELETE);
100             }
101             break;
102         case CldsEvent.ACTION_DEPLOY:
103             permittedActions = Arrays.asList(CldsEvent.ACTION_UNDEPLOY, CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
104             break;
105         case CldsEvent.ACTION_RESTART:
106         case CldsEvent.ACTION_UPDATE:
107             permittedActions = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP,
108                 CldsEvent.ACTION_UNDEPLOY);
109             if (isTypeModel(propText, ModelType.POLICY_MODEL)) {
110                 permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
111             }
112             break;
113         case CldsEvent.ACTION_STOP:
114             permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART,
115                 CldsEvent.ACTION_UNDEPLOY);
116             if (isTypeModel(propText, ModelType.POLICY_MODEL)) {
117                 permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART,
118                     CldsEvent.ACTION_DELETE);
119             }
120             break;
121         default:
122             getLogger().warn("Invalid current actionCd: " + actionCd);
123             permittedActions = Arrays.asList();
124         }
125         return permittedActions;
126     }
127
128     /**
129      * This method returns the action of the event or a default one if not found.
130      *
131      * @param event
132      *        The last event
133      * @return The action
134      */
135     default String getCurrentActionCd(CldsEvent event) {
136         // current default actionCd is CREATE
137         String actionCd = CldsEvent.ACTION_CREATE;
138         if (event != null && event.getActionCd() != null) {
139             actionCd = event.getActionCd();
140         }
141         return actionCd;
142     }
143
144     /**
145      * Check whether the text properties is of specified type ModelType.
146      *
147      * @param propText
148      *        The Clamp Json properties
149      * @param key
150      *        The model type
151      * @return True if matches the right type specified
152      */
153     default boolean isTypeModel(String propText, ModelType key) {
154         boolean result = false;
155         try {
156             if (propText != null) {
157                 JsonObject modelJson = JsonUtils.GSON.fromJson(propText, JsonObject.class);
158                 JsonElement modelJsonOfType = modelJson.get(key.getType());
159                 if (modelJsonOfType != null
160                     && modelJsonOfType.isJsonPrimitive()
161                     && modelJsonOfType.getAsJsonPrimitive().getAsBoolean()) {
162                     result = true;
163                 }
164             }
165         } catch (JsonParseException e) {
166             getLogger().error("Error while parsing propText json", e);
167         }
168         return result;
169     }
170 }