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