Add flexibility
[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.fasterxml.jackson.databind.JsonNode;
28
29 import java.io.IOException;
30 import java.util.Arrays;
31 import java.util.List;
32
33 import org.onap.clamp.clds.model.CldsEvent;
34 import org.onap.clamp.clds.util.JacksonUtils;
35
36 /**
37  * Interface for actions that the user can do according to the last event.
38  *
39  *
40  */
41 public interface ActionsHandler {
42
43     enum ModelType {
44         SIMPLE_MODEL("simpleModel"), POLICY_MODEL("policyModel");
45         private final String type;
46
47         private ModelType(String type) {
48             this.type = type;
49         }
50
51         public final String getType() {
52             return this.type;
53         }
54     }
55
56     public EELFLogger getLogger();
57
58     /**
59      * This method determines a list of actions that the user can do according to
60      * the last event.
61      *
62      * @param event
63      *        The last event
64      * @param propText
65      *        The Json properties string
66      * @return A list of actions
67      */
68     default List<String> determinePermittedActionsOnLastEvent(CldsEvent event, String propText) {
69         List<String> permittedActions;
70         String actionCd = getCurrentActionCd(event);
71         switch (actionCd) {
72         case CldsEvent.ACTION_CREATE:
73             permittedActions = Arrays.asList(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_TEST, 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);
77             }
78             break;
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);
87             }
88             break;
89         case CldsEvent.ACTION_SUBMITDCAE:
90             permittedActions = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_DELETE);
91             break;
92         case CldsEvent.ACTION_SUBMITPOLICY:
93             permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
94             break;
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);
101             }
102             break;
103         case CldsEvent.ACTION_DEPLOY:
104             permittedActions = Arrays.asList(CldsEvent.ACTION_UNDEPLOY, CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
105             break;
106         case CldsEvent.ACTION_RESTART:
107         case CldsEvent.ACTION_UPDATE:
108             permittedActions = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP,
109                 CldsEvent.ACTION_UNDEPLOY);
110             if (isTypeModel(propText, ModelType.POLICY_MODEL)) {
111                 permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
112             }
113             break;
114         case CldsEvent.ACTION_STOP:
115             permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART,
116                 CldsEvent.ACTION_UNDEPLOY);
117             if (isTypeModel(propText, ModelType.POLICY_MODEL)) {
118                 permittedActions = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART,
119                     CldsEvent.ACTION_DELETE);
120             }
121             break;
122         default:
123             getLogger().warn("Invalid current actionCd: " + actionCd);
124             permittedActions = Arrays.asList();
125         }
126         return permittedActions;
127     }
128
129     /**
130      * This method returns the action of the event or a default one if not found.
131      *
132      * @param event
133      *        The last event
134      * @return The action
135      */
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();
141         }
142         return actionCd;
143     }
144
145     /**
146      * Check whether the text properties is of specified type ModelType.
147      *
148      * @param propText
149      *        The Clamp Json properties
150      * @param key
151      *        The model type
152      * @return True if matches the right type specified
153      */
154     default boolean isTypeModel(String propText, ModelType key) {
155         boolean result = false;
156         try {
157             if (propText != null) {
158                 JsonNode modelJson = JacksonUtils.getObjectMapperInstance().readTree(propText);
159                 JsonNode modelJsonOfType = modelJson.get(key.getType());
160                 if (modelJsonOfType != null && modelJsonOfType.asBoolean()) {
161                     result = true;
162                 }
163             }
164         } catch (IOException e) {
165             getLogger().error("Error while parsing propText json", e);
166         }
167         return result;
168     }
169 }