[CLAMP-1] Initial ONAP CLAMP seed code commit
[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 org.onap.clamp.clds.dao.CldsDao;
27 import org.jboss.resteasy.spi.BadRequestException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import javax.ws.rs.NotFoundException;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
35
36 /**
37  * Represent a CLDS Model.
38  */
39 public class CldsModel {
40     private static final Logger logger = LoggerFactory.getLogger(CldsModel.class);
41
42     private static final int UUID_LENGTH = 36;
43
44     public static final String STATUS_DESIGN = "DESIGN";
45     public static final String STATUS_DISTRIBUTED = "DISTRIBUTED";
46     public static final String STATUS_ACTIVE = "ACTIVE";
47     public static final String STATUS_STOPPED = "STOPPED";
48     public static final String STATUS_DELETING = "DELETING";
49     public static final String STATUS_ERROR = "ERROR"; // manual intervention required
50     public static final String STATUS_UNKNOWN = "UNKNOWN";
51
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 bpmnId;
59     private String bpmnUserid;
60     private String bpmnText;
61     private String propId;
62     private String propUserid;
63     private String propText;
64     private String imageId;
65     private String imageUserid;
66     private String imageText;
67     private String docId;
68     private String docUserid;
69     private String docText;
70     private String blueprintId;
71     private String blueprintUserid;
72     private String blueprintText;
73     private CldsEvent event;
74     private String status;
75     private List<String> permittedActionCd;
76     private List<CldsModelInstance> cldsModelInstanceList;
77
78     /**
79      * Construct empty model.
80      */
81     public CldsModel() {
82         event = new CldsEvent();
83     }
84
85     /**
86      * Retrieve from DB.
87      *
88      * @param cldsDao
89      * @param name
90      * @return
91      */
92     public static CldsModel retrieve(CldsDao cldsDao, String name, boolean okIfNotFound) {
93         // get from db
94         CldsModel model = cldsDao.getModelTemplate(name);
95         if (model.getId() == null && !okIfNotFound) {
96             throw new NotFoundException();
97         }
98         model.determineStatus();
99         model.determinePermittedActionCd();
100         return model;
101     }
102
103     /**
104      * Save model to DB.
105      *
106      * @param cldsDao
107      * @param userid
108      */
109     public void save(CldsDao cldsDao, String userid) {
110         cldsDao.setModel(this, userid);
111         determineStatus();
112         determinePermittedActionCd();
113     }
114
115     /**
116      * Insert a new event for the new action.
117      * Throw IllegalArgumentException if requested actionCd is not permitted.
118      *
119      * @param cldsDao
120      * @param userid
121      * @param actionCd
122      * @param actionStateCd
123      */
124     public void insEvent(CldsDao cldsDao, String userid, String actionCd, String actionStateCd) {
125         validateAction(actionCd);
126         event = CldsEvent.insEvent(cldsDao, this, actionCd, actionStateCd, null);
127         determineStatus();
128         determinePermittedActionCd();
129     }
130
131     /**
132      * Update event with processInstanceId
133      *
134      * @param cldsDao
135      * @param processInstanceId
136      */
137     public void updEvent(CldsDao cldsDao, String processInstanceId) {
138         cldsDao.updEvent(event.getId(), processInstanceId);
139     }
140
141     /**
142      * set the status in the model
143      */
144     private void determineStatus() {
145
146         status = STATUS_UNKNOWN;
147         if (event == null || event.getActionCd() == null) {
148             status = STATUS_DESIGN;
149         } else if (event.isActionStateCd(CldsEvent.ACTION_STATE_ERROR)) {
150             status = STATUS_ERROR;
151         } else if (event.isActionAndStateCd(CldsEvent.ACTION_CREATE, CldsEvent.ACTION_STATE_ANY) ||
152                 event.isActionAndStateCd(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_STATE_ANY) ||
153                 event.isActionAndStateCd(CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_STATE_ANY) ||
154                 event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_RECEIVED)) {
155             status = STATUS_DESIGN;
156         } else if (event.isActionAndStateCd(CldsEvent.ACTION_DISTRIBUTE, CldsEvent.ACTION_STATE_RECEIVED) ||
157                 event.isActionAndStateCd(CldsEvent.ACTION_UNDEPLOY, CldsEvent.ACTION_STATE_RECEIVED)) {
158             status = STATUS_DISTRIBUTED;
159         } else if (event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_SENT)) {
160             status = STATUS_DELETING;
161         } else if (event.isActionAndStateCd(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_STATE_RECEIVED) ||
162                 event.isActionAndStateCd(CldsEvent.ACTION_RESTART, CldsEvent.ACTION_STATE_ANY) ||
163                 event.isActionAndStateCd(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STATE_ANY)) {
164             status = STATUS_ACTIVE;
165         } else if (event.isActionAndStateCd(CldsEvent.ACTION_STOP, CldsEvent.ACTION_STATE_ANY)) {
166             status = STATUS_STOPPED;
167         }
168
169     }
170
171     /**
172      * Get the actionCd from current event.  If none, default value is CldsEvent.ACTION_CREATE
173      *
174      * @return
175      */
176     private String getCurrentActionCd() {
177         // current default actionCd is CREATE
178         String actionCd = CldsEvent.ACTION_CREATE;
179         if (event != null && event.getActionCd() != null) {
180             actionCd = event.getActionCd();
181         }
182         return actionCd;
183     }
184
185     /**
186      * Get the actionStateCd from current event.  If none, default value is CldsEvent.ACTION_STATE_COMPLETED
187      *
188      * @return
189      */
190     private String getCurrentActionStateCd() {
191         // current default actionStateCd is CREATE
192         String actionStateCd = CldsEvent.ACTION_STATE_COMPLETED;
193         if (event != null && event.getActionStateCd() != null) {
194             actionStateCd = event.getActionStateCd();
195         }
196         return actionStateCd;
197     }
198
199     /**
200      * Determine permittedActionCd list using the actionCd from the current event.
201      */
202     private void determinePermittedActionCd() {
203         String actionCd = getCurrentActionCd();
204         switch (actionCd) {
205             case CldsEvent.ACTION_CREATE:
206                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT);
207                 break;
208             case CldsEvent.ACTION_SUBMIT:
209             case CldsEvent.ACTION_RESUBMIT:
210                 // for 1702 delete is not currently implemented (and resubmit requires manually deleting artifact from asdc
211                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_RESUBMIT);
212                 break;
213             case CldsEvent.ACTION_DISTRIBUTE:
214             case CldsEvent.ACTION_UNDEPLOY:
215                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE);
216                 break;
217             case CldsEvent.ACTION_DEPLOY:
218             case CldsEvent.ACTION_RESTART:
219             case CldsEvent.ACTION_UPDATE:
220                 // for 1702 delete is not currently implemented
221                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
222                 break;
223             case CldsEvent.ACTION_DELETE:
224                 if (getCurrentActionStateCd().equals(CldsEvent.ACTION_STATE_SENT)) {
225                     permittedActionCd = Arrays.asList();
226                 } else {
227                     permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT);
228                 }
229                 break;
230             case CldsEvent.ACTION_STOP:
231                 // for 1702 delete is not currently implemented
232                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART);
233                 break;
234             default:
235                 logger.warn("Invalid current actionCd: " + actionCd);
236         }
237     }
238
239     /**
240      * Validate requestedActionCd - determine permittedActionCd and then check if contained in permittedActionCd
241      * Throw IllegalArgumentException if requested actionCd is not permitted.
242      *
243      * @param requestedActionCd
244      */
245     public void validateAction(String requestedActionCd) {
246         determinePermittedActionCd();
247         if (!permittedActionCd.contains(requestedActionCd)) {
248             throw new IllegalArgumentException("Invalid requestedActionCd: " + requestedActionCd + ".  Given current actionCd: " + getCurrentActionCd() + ", the permittedActionCd: " + permittedActionCd);
249         }
250     }
251
252     /**
253      * Extract the UUID portion of a given full control name (controlNamePrefix + controlNameUuid).
254      * No fields are populated other than controlNamePrefix and controlNameUuid.
255      * Throws BadRequestException if length of given control name is less than UUID_LENGTH.
256      *
257      * @param fullControlName
258      * @return
259      */
260     public static CldsModel createUsingControlName(String fullControlName) {
261
262         int len = 0;
263
264         if (fullControlName != null) {
265             len = fullControlName.length();
266         }
267         if (len < UUID_LENGTH) {
268             throw new BadRequestException("closed loop id / control name length, " + len + ", less than the minimum of: " + UUID_LENGTH);
269         }
270         CldsModel model = new CldsModel();
271         model.setControlNamePrefix(fullControlName.substring(0, len - UUID_LENGTH));
272         model.setControlNameUuid(fullControlName.substring(len - UUID_LENGTH));
273         return model;
274     }
275
276     /**
277      * @return the controlName (controlNamePrefix + controlNameUuid)
278      */
279     public String getControlName() {
280         return controlNamePrefix + controlNameUuid;
281     }
282
283     /**
284      * To insert modelInstance to the database
285      *
286      * @param cldsDao
287      * @param dcaeEvent
288      */
289     public static CldsModel insertModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent, String userid) {
290         String controlName = dcaeEvent.getControlName();
291         CldsModel cldsModel = createUsingControlName(controlName);
292         cldsModel = cldsDao.getModelByUuid(cldsModel.getControlNameUuid());
293         cldsModel.determineStatus();
294         if (dcaeEvent.getCldsActionCd().equals(CldsEvent.ACTION_UNDEPLOY) ||
295                 (dcaeEvent.getCldsActionCd().equals(CldsEvent.ACTION_DEPLOY) && (cldsModel.getStatus().equals(STATUS_DISTRIBUTED) || cldsModel.getStatus().equals(STATUS_DESIGN)))) {
296             CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userid, dcaeEvent.getCldsActionCd(), CldsEvent.ACTION_STATE_RECEIVED, null);
297         }
298         cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
299         return cldsModel;
300     }
301
302     /**
303      * To remove modelInstance from the database
304      * This method is defunct - DCAE Proxy will not undeploy individual instances.  It will send an empty list of
305      * deployed instances to indicate all have been removed.  Or it will send an updated list to indicate those that
306      * are still deployed with any not on the list considered undeployed.
307      *
308      * @param cldsDao
309      * @param dcaeEvent
310      */
311     @SuppressWarnings("unused")
312     private static CldsModel removeModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent) {
313         String controlName = dcaeEvent.getControlName();
314         //cldsModel = cldsDao.delModelInstance(cldsModel.getControlNameUuid(), dcaeEvent.getInstances() );
315         return createUsingControlName(controlName);
316     }
317
318     /**
319      * @return the name
320      */
321     public String getName() {
322         return name;
323     }
324
325     /**
326      * @param name the name to set
327      */
328     public void setName(String name) {
329         this.name = name;
330     }
331
332     public String getTemplateId() {
333         return templateId;
334     }
335
336     public void setTemplateId(String templateId) {
337         this.templateId = templateId;
338     }
339
340     /**
341      * @return the controlNamePrefix
342      */
343     public String getControlNamePrefix() {
344         return controlNamePrefix;
345     }
346
347     /**
348      * @param controlNamePrefix the controlNamePrefix to set
349      */
350     public void setControlNamePrefix(String controlNamePrefix) {
351         this.controlNamePrefix = controlNamePrefix;
352     }
353
354     /**
355      * @return the controlNameUuid
356      */
357     public String getControlNameUuid() {
358         return controlNameUuid;
359     }
360
361     /**
362      * @param controlNameUuid the controlNameUuid to set
363      */
364     public void setControlNameUuid(String controlNameUuid) {
365         this.controlNameUuid = controlNameUuid;
366     }
367
368
369     /**
370      * @return the propUserid
371      */
372     public String getPropUserid() {
373         return propUserid;
374     }
375
376     /**
377      * @param propUserid the propUserid to set
378      */
379     public void setPropUserid(String propUserid) {
380         this.propUserid = propUserid;
381     }
382
383     /**
384      * @return the propText
385      */
386     public String getPropText() {
387         return propText;
388     }
389
390     /**
391      * @param propText the propText to set
392      */
393     public void setPropText(String propText) {
394         this.propText = propText;
395     }
396
397     /**
398      * @return the event
399      */
400     public CldsEvent getEvent() {
401         return event;
402     }
403
404     public String getId() {
405         return id;
406     }
407
408     public void setId(String id) {
409         this.id = id;
410     }
411
412     public String getTemplateName() {
413         return templateName;
414     }
415
416     public void setTemplateName(String templateName) {
417         this.templateName = templateName;
418     }
419
420     public String getPropId() {
421         return propId;
422     }
423
424     public void setPropId(String propId) {
425         this.propId = propId;
426     }
427
428     /**
429      * @param event the event to set
430      */
431     public void setEvent(CldsEvent event) {
432         this.event = event;
433     }
434
435     /**
436      * @return the status
437      */
438     public String getStatus() {
439         return status;
440     }
441
442     /**
443      * @param status the status to set
444      */
445     public void setStatus(String status) {
446         this.status = status;
447     }
448
449     /**
450      * @return the permittedActionCd
451      */
452     public List<String> getPermittedActionCd() {
453         return permittedActionCd;
454     }
455
456     /**
457      * @param permittedActionCd the permittedActionCd to set
458      */
459     public void setPermittedActionCd(List<String> permittedActionCd) {
460         this.permittedActionCd = permittedActionCd;
461     }
462
463     public String getBlueprintId() {
464         return blueprintId;
465     }
466
467     public void setBlueprintId(String blueprintId) {
468         this.blueprintId = blueprintId;
469     }
470
471     public String getBlueprintUserid() {
472         return blueprintUserid;
473     }
474
475     public void setBlueprintUserid(String blueprintUserid) {
476         this.blueprintUserid = blueprintUserid;
477     }
478
479     public String getBlueprintText() {
480         return blueprintText;
481     }
482
483     public void setBlueprintText(String blueprintText) {
484         this.blueprintText = blueprintText;
485     }
486
487     public String getBpmnId() {
488         return bpmnId;
489     }
490
491     public void setBpmnId(String bpmnId) {
492         this.bpmnId = bpmnId;
493     }
494
495     public String getBpmnUserid() {
496         return bpmnUserid;
497     }
498
499     public void setBpmnUserid(String bpmnUserid) {
500         this.bpmnUserid = bpmnUserid;
501     }
502
503     public String getBpmnText() {
504         return bpmnText;
505     }
506
507     public void setBpmnText(String bpmnText) {
508         this.bpmnText = bpmnText;
509     }
510
511     public String getImageId() {
512         return imageId;
513     }
514
515     public void setImageId(String imageId) {
516         this.imageId = imageId;
517     }
518
519     public String getImageUserid() {
520         return imageUserid;
521     }
522
523     public void setImageUserid(String imageUserid) {
524         this.imageUserid = imageUserid;
525     }
526
527     public String getImageText() {
528         return imageText;
529     }
530
531     public void setImageText(String imageText) {
532         this.imageText = imageText;
533     }
534
535     public String getDocId() {
536         return docId;
537     }
538
539     public void setDocId(String docId) {
540         this.docId = docId;
541     }
542
543     public String getDocUserid() {
544         return docUserid;
545     }
546
547     public void setDocUserid(String docUserid) {
548         this.docUserid = docUserid;
549     }
550
551     public String getDocText() {
552         return docText;
553     }
554
555     public void setDocText(String docText) {
556         this.docText = docText;
557     }
558
559     public List<CldsModelInstance> getCldsModelInstanceList() {
560         if (cldsModelInstanceList == null) {
561             cldsModelInstanceList = new ArrayList<>();
562         }
563         return cldsModelInstanceList;
564     }
565
566     public void setCldsModelInstanceList(List<CldsModelInstance> cldsModelInstanceList) {
567         this.cldsModelInstanceList = cldsModelInstanceList;
568     }
569
570 }