Code refactoring
[clamp.git] / src / main / java / org / onap / clamp / clds / model / CldsModel.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.model;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.List;
32
33 import javax.ws.rs.BadRequestException;
34 import javax.ws.rs.NotFoundException;
35
36 import org.onap.clamp.clds.dao.CldsDao;
37
38 /**
39  * Represent a CLDS Model.
40  */
41 public class CldsModel {
42
43     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsModel.class);
44     private static final int UUID_LENGTH = 36;
45     private static final String STATUS_DESIGN = "DESIGN";
46     private static final String STATUS_DISTRIBUTED = "DISTRIBUTED";
47     private static final String STATUS_ACTIVE = "ACTIVE";
48     private static final String STATUS_STOPPED = "STOPPED";
49     private static final String STATUS_DELETING = "DELETING";
50     private static final String STATUS_ERROR = "ERROR";
51     private static final String STATUS_UNKNOWN = "UNKNOWN";
52     private String id;
53     private String templateId;
54     private String templateName;
55     private String name;
56     private String controlNamePrefix;
57     private String controlNameUuid;
58     private String bpmnText;
59     private String propText;
60     private String imageText;
61     private String docText;
62     private String blueprintText;
63     private CldsEvent event;
64     private String status;
65     private List<String> permittedActionCd;
66     private List<CldsModelInstance> cldsModelInstanceList;
67     private String typeId;
68     private String typeName;
69     private String deploymentId;
70
71     /**
72      * Construct empty model.
73      */
74     public CldsModel() {
75         event = new CldsEvent();
76     }
77
78     /**
79      * Retrieve from DB.
80      */
81     public static CldsModel retrieve(CldsDao cldsDao, String name, boolean okIfNotFound) {
82         // get from db
83         CldsModel model = cldsDao.getModelTemplate(name);
84         if (model.getId() == null && !okIfNotFound) {
85             throw new NotFoundException();
86         }
87         model.determineStatus();
88         model.determinePermittedActionCd();
89         return model;
90     }
91
92     public boolean canInventoryCall() {
93         boolean canCall = false;
94         /* Below checks the clds event is submit/resubmit */
95
96         if ((event.isActionCd(CldsEvent.ACTION_SUBMIT) || event.isActionCd(CldsEvent.ACTION_RESUBMIT))) {
97             canCall = true;
98         }
99         return canCall;
100     }
101
102     /**
103      * Save model to DB.
104      */
105     public void save(CldsDao cldsDao, String userid) {
106         cldsDao.setModel(this, userid);
107         determineStatus();
108         determinePermittedActionCd();
109     }
110
111     /**
112      * set the status in the model
113      */
114     private void determineStatus() {
115
116         status = STATUS_UNKNOWN;
117         if (event == null || event.getActionCd() == null) {
118             status = STATUS_DESIGN;
119         } else if (event.isActionStateCd(CldsEvent.ACTION_STATE_ERROR)) {
120             status = STATUS_ERROR;
121         } else if (event.isActionAndStateCd(CldsEvent.ACTION_CREATE, CldsEvent.ACTION_STATE_ANY)
122                 || event.isActionAndStateCd(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_STATE_ANY)
123                 || event.isActionAndStateCd(CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_STATE_ANY)
124                 || event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_RECEIVED)) {
125             status = STATUS_DESIGN;
126         } else if (event.isActionAndStateCd(CldsEvent.ACTION_DISTRIBUTE, CldsEvent.ACTION_STATE_RECEIVED)
127                 || event.isActionAndStateCd(CldsEvent.ACTION_UNDEPLOY, CldsEvent.ACTION_STATE_RECEIVED)) {
128             status = STATUS_DISTRIBUTED;
129         } else if (event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_SENT)) {
130             status = STATUS_DELETING;
131         } else if (event.isActionAndStateCd(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_STATE_RECEIVED)
132                 || event.isActionAndStateCd(CldsEvent.ACTION_RESTART, CldsEvent.ACTION_STATE_ANY)
133                 || event.isActionAndStateCd(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STATE_ANY)
134                 || event.isActionAndStateCd(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_STATE_ANY)) {
135             status = STATUS_ACTIVE;
136         } else if (event.isActionAndStateCd(CldsEvent.ACTION_STOP, CldsEvent.ACTION_STATE_ANY)) {
137             status = STATUS_STOPPED;
138         }
139
140     }
141
142     /**
143      * Get the actionCd from current event. If none, default value is
144      * CldsEvent.ACTION_CREATE
145      */
146     private String getCurrentActionCd() {
147         // current default actionCd is CREATE
148         String actionCd = CldsEvent.ACTION_CREATE;
149         if (event != null && event.getActionCd() != null) {
150             actionCd = event.getActionCd();
151         }
152         return actionCd;
153     }
154
155     /**
156      * Get the actionStateCd from current event. If none, default value is
157      * CldsEvent.ACTION_STATE_COMPLETED
158      */
159     private String getCurrentActionStateCd() {
160         // current default actionStateCd is CREATE
161         String actionStateCd = CldsEvent.ACTION_STATE_COMPLETED;
162         if (event != null && event.getActionStateCd() != null) {
163             actionStateCd = event.getActionStateCd();
164         }
165         return actionStateCd;
166     }
167
168     /**
169      * Determine permittedActionCd list using the actionCd from the current
170      * event. It's a states graph, given the next action that can be executed
171      * from the one that has been executed (described in the event object).
172      * ACTION_CREATE being the first one.
173      */
174     private void determinePermittedActionCd() {
175         String actionCd = getCurrentActionCd();
176         switch (actionCd) {
177             case CldsEvent.ACTION_CREATE:
178                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_TEST);
179                 break;
180             case CldsEvent.ACTION_SUBMIT:
181             case CldsEvent.ACTION_RESUBMIT:
182                 // for 1702 delete is not currently implemented (and resubmit
183                 // requires manually deleting artifact from sdc
184                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_RESUBMIT);
185                 break;
186             case CldsEvent.ACTION_DISTRIBUTE:
187                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_RESUBMIT);
188                 break;
189             case CldsEvent.ACTION_UNDEPLOY:
190                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY,
191                         CldsEvent.ACTION_RESUBMIT);
192                 break;
193             case CldsEvent.ACTION_DEPLOY:
194                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UNDEPLOY,
195                         CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
196                 break;
197             case CldsEvent.ACTION_RESTART:
198             case CldsEvent.ACTION_UPDATE:
199                 // for 1702 delete is not currently implemented
200                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UPDATE,
201                         CldsEvent.ACTION_STOP, CldsEvent.ACTION_UNDEPLOY);
202                 break;
203             case CldsEvent.ACTION_DELETE:
204                 if (getCurrentActionStateCd().equals(CldsEvent.ACTION_STATE_SENT)) {
205                     permittedActionCd = Arrays.asList();
206                 } else {
207                     permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT);
208                 }
209                 break;
210             case CldsEvent.ACTION_STOP:
211                 // for 1702 delete is not currently implemented
212                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART,
213                         CldsEvent.ACTION_UNDEPLOY);
214                 break;
215             default:
216                 logger.warn("Invalid current actionCd: " + actionCd);
217         }
218     }
219
220     /**
221      * Validate requestedActionCd - determine permittedActionCd and then check
222      * if contained in permittedActionCd Throw IllegalArgumentException if
223      * requested actionCd is not permitted.
224      */
225     public void validateAction(String requestedActionCd) {
226         determinePermittedActionCd();
227         if (!permittedActionCd.contains(requestedActionCd)) {
228             throw new IllegalArgumentException(
229                     "Invalid requestedActionCd: " + requestedActionCd + ".  Given current actionCd: "
230                             + getCurrentActionCd() + ", the permittedActionCd: " + permittedActionCd);
231         }
232     }
233
234     /**
235      * Extract the UUID portion of a given full control name (controlNamePrefix
236      * + controlNameUuid). No fields are populated other than controlNamePrefix
237      * and controlNameUuid. Throws BadRequestException if length of given
238      * control name is less than UUID_LENGTH.
239      */
240     public static CldsModel createUsingControlName(String fullControlName) {
241         if (fullControlName == null || fullControlName.length() < UUID_LENGTH) {
242             throw new BadRequestException(
243                     "closed loop id / control name length, " + (fullControlName != null ? fullControlName.length() : 0)
244                             + ", less than the minimum of: " + UUID_LENGTH);
245         }
246         CldsModel model = new CldsModel();
247         model.setControlNamePrefix(fullControlName.substring(0, fullControlName.length() - UUID_LENGTH));
248         model.setControlNameUuid(fullControlName.substring(fullControlName.length() - UUID_LENGTH));
249         return model;
250     }
251
252     /**
253      * @return the controlName (controlNamePrefix + controlNameUuid)
254      */
255     public String getControlName() {
256         return controlNamePrefix + controlNameUuid;
257     }
258
259     /**
260      * To insert modelInstance to the database
261      */
262     public static CldsModel insertModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent, String userid) {
263         String controlName = dcaeEvent.getControlName();
264         CldsModel cldsModel = createUsingControlName(controlName);
265         cldsModel = cldsDao.getModelByUuid(cldsModel.getControlNameUuid());
266         cldsModel.determineStatus();
267         if (dcaeEvent.getCldsActionCd().equals(CldsEvent.ACTION_UNDEPLOY) || (dcaeEvent.getCldsActionCd()
268                 .equals(CldsEvent.ACTION_DEPLOY)
269                 && (cldsModel.getStatus().equals(STATUS_DISTRIBUTED) || cldsModel.getStatus().equals(STATUS_DESIGN)))) {
270             CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userid, dcaeEvent.getCldsActionCd(),
271                     CldsEvent.ACTION_STATE_RECEIVED, null);
272         }
273         cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
274         return cldsModel;
275     }
276
277     /**
278      * @return the name
279      */
280     public String getName() {
281         return name;
282     }
283
284     /**
285      * @param name
286      *            the name to set
287      */
288     public void setName(String name) {
289         this.name = name;
290     }
291
292     /**
293      * @return the typeName
294      */
295     public String getTypeName() {
296         return typeName;
297     }
298
299     public void setTypeName(String typeName) {
300         this.typeName = typeName;
301     }
302
303     public String getTemplateId() {
304         return templateId;
305     }
306
307     public void setTemplateId(String templateId) {
308         this.templateId = templateId;
309     }
310
311     /**
312      * @return the controlNamePrefix
313      */
314     public String getControlNamePrefix() {
315         return controlNamePrefix;
316     }
317
318     /**
319      * @param controlNamePrefix
320      *            the controlNamePrefix to set
321      */
322     public void setControlNamePrefix(String controlNamePrefix) {
323         this.controlNamePrefix = controlNamePrefix;
324     }
325
326     /**
327      * @return the controlNameUuid
328      */
329     public String getControlNameUuid() {
330         return controlNameUuid;
331     }
332
333     /**
334      * @param controlNameUuid
335      *            the controlNameUuid to set
336      */
337     public void setControlNameUuid(String controlNameUuid) {
338         this.controlNameUuid = controlNameUuid;
339     }
340
341     /**
342      * @return the propText
343      */
344     public String getPropText() {
345         return propText;
346     }
347
348     /**
349      * @param propText
350      *            the propText to set
351      */
352     public void setPropText(String propText) {
353         this.propText = propText;
354     }
355
356     /**
357      * @return the event
358      */
359     public CldsEvent getEvent() {
360         return event;
361     }
362
363     public String getId() {
364         return id;
365     }
366
367     public void setId(String id) {
368         this.id = id;
369     }
370
371     public String getTemplateName() {
372         return templateName;
373     }
374
375     public void setTemplateName(String templateName) {
376         this.templateName = templateName;
377     }
378
379     /**
380      * @param event
381      *            the event to set
382      */
383     public void setEvent(CldsEvent event) {
384         this.event = event;
385     }
386
387     /**
388      * @return the status
389      */
390     public String getStatus() {
391         return status;
392     }
393
394     /**
395      * @param status
396      *            the status to set
397      */
398     public void setStatus(String status) {
399         this.status = status;
400     }
401
402     public String getBlueprintText() {
403         return blueprintText;
404     }
405
406     public void setBlueprintText(String blueprintText) {
407         this.blueprintText = blueprintText;
408     }
409
410     public String getBpmnText() {
411         return bpmnText;
412     }
413
414     public void setBpmnText(String bpmnText) {
415         this.bpmnText = bpmnText;
416     }
417
418     public String getImageText() {
419         return imageText;
420     }
421
422     public void setImageText(String imageText) {
423         this.imageText = imageText;
424     }
425
426     public String getDocText() {
427         return docText;
428     }
429
430     public void setDocText(String docText) {
431         this.docText = docText;
432     }
433
434     public String getTypeId() {
435         return typeId;
436     }
437
438     public void setTypeId(String typeId) {
439         this.typeId = typeId;
440     }
441
442     public List<CldsModelInstance> getCldsModelInstanceList() {
443         if (cldsModelInstanceList == null) {
444             cldsModelInstanceList = new ArrayList<>();
445         }
446         return cldsModelInstanceList;
447     }
448
449     public String getDeploymentId() {
450         return deploymentId;
451     }
452
453     public void setDeploymentId(String deploymentId) {
454         this.deploymentId = deploymentId;
455     }
456
457 }