Code improvement
[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 java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
29
30 import javax.ws.rs.NotFoundException;
31
32 import org.jboss.resteasy.spi.BadRequestException;
33 import org.onap.clamp.clds.dao.CldsDao;
34
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
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, CldsEvent.ACTION_RESUBMIT);
247                 break;
248             case CldsEvent.ACTION_DEPLOY:
249                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UNDEPLOY, CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
250                 break;
251             case CldsEvent.ACTION_RESTART:
252             case CldsEvent.ACTION_UPDATE:
253                 // for 1702 delete is not currently implemented
254                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP, CldsEvent.ACTION_UNDEPLOY);
255                 break;
256             case CldsEvent.ACTION_DELETE:
257                 if (getCurrentActionStateCd().equals(CldsEvent.ACTION_STATE_SENT)) {
258                     permittedActionCd = Arrays.asList();
259                 } else {
260                     permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT);
261                 }
262                 break;
263             case CldsEvent.ACTION_STOP:
264                 // for 1702 delete is not currently implemented
265                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART,
266                         CldsEvent.ACTION_UNDEPLOY);
267                 break;
268             default:
269                 logger.warn("Invalid current actionCd: " + actionCd);
270         }
271     }
272
273     /**
274      * Validate requestedActionCd - determine permittedActionCd and then check
275      * if contained in permittedActionCd Throw IllegalArgumentException if
276      * requested actionCd is not permitted.
277      *
278      * @param requestedActionCd
279      */
280     public void validateAction(String requestedActionCd) {
281         determinePermittedActionCd();
282         if (!permittedActionCd.contains(requestedActionCd)) {
283             throw new IllegalArgumentException(
284                     "Invalid requestedActionCd: " + requestedActionCd + ".  Given current actionCd: "
285                             + getCurrentActionCd() + ", the permittedActionCd: " + permittedActionCd);
286         }
287     }
288
289     /**
290      * Extract the UUID portion of a given full control name (controlNamePrefix
291      * + controlNameUuid). No fields are populated other than controlNamePrefix
292      * and controlNameUuid. Throws BadRequestException if length of given
293      * control name is less than UUID_LENGTH.
294      *
295      * @param fullControlName
296      * @return
297      */
298     public static CldsModel createUsingControlName(String fullControlName) {
299
300         int len = 0;
301
302         if (fullControlName != null) {
303             len = fullControlName.length();
304         }
305         if (len < UUID_LENGTH) {
306             throw new BadRequestException(
307                     "closed loop id / control name length, " + len + ", less than the minimum of: " + UUID_LENGTH);
308         }
309         CldsModel model = new CldsModel();
310         model.setControlNamePrefix(fullControlName.substring(0, len - UUID_LENGTH));
311         model.setControlNameUuid(fullControlName.substring(len - UUID_LENGTH));
312         return model;
313     }
314
315     /**
316      * @return the controlName (controlNamePrefix + controlNameUuid)
317      */
318     public String getControlName() {
319         return controlNamePrefix + controlNameUuid;
320     }
321
322     /**
323      * To insert modelInstance to the database
324      *
325      * @param cldsDao
326      * @param dcaeEvent
327      */
328     public static CldsModel insertModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent, String userid) {
329         String controlName = dcaeEvent.getControlName();
330         CldsModel cldsModel = createUsingControlName(controlName);
331         cldsModel = cldsDao.getModelByUuid(cldsModel.getControlNameUuid());
332         cldsModel.determineStatus();
333         if (dcaeEvent.getCldsActionCd().equals(CldsEvent.ACTION_UNDEPLOY) || (dcaeEvent.getCldsActionCd()
334                 .equals(CldsEvent.ACTION_DEPLOY)
335                 && (cldsModel.getStatus().equals(STATUS_DISTRIBUTED) || cldsModel.getStatus().equals(STATUS_DESIGN)))) {
336             CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userid, dcaeEvent.getCldsActionCd(),
337                     CldsEvent.ACTION_STATE_RECEIVED, null);
338         }
339         cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
340         return cldsModel;
341     }
342
343     /**
344      * To remove modelInstance from the database This method is defunct - DCAE
345      * Proxy will not undeploy individual instances. It will send an empty list
346      * of deployed instances to indicate all have been removed. Or it will send
347      * an updated list to indicate those that are still deployed with any not on
348      * the list considered undeployed.
349      *
350      * @param cldsDao
351      * @param dcaeEvent
352      */
353     @SuppressWarnings("unused")
354     private static CldsModel removeModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent) {
355         String controlName = dcaeEvent.getControlName();
356         // cldsModel = cldsDao.delModelInstance(cldsModel.getControlNameUuid(),
357         // dcaeEvent.getInstances() );
358         return createUsingControlName(controlName);
359     }
360
361     /**
362      * @return the name
363      */
364     public String getName() {
365         return name;
366     }
367
368     /**
369      * @param name
370      *            the name to set
371      */
372     public void setName(String name) {
373         this.name = name;
374     }
375
376     /**
377      * @return the typeName
378      */
379     public String getTypeName() {
380         return typeName;
381     }
382
383     /**
384      * @param name
385      *            the typeName to set
386      */
387     public void setTypeName(String typeName) {
388         this.typeName = typeName;
389     }
390
391     public String getTemplateId() {
392         return templateId;
393     }
394
395     public void setTemplateId(String templateId) {
396         this.templateId = templateId;
397     }
398
399     /**
400      * @return the controlNamePrefix
401      */
402     public String getControlNamePrefix() {
403         return controlNamePrefix;
404     }
405
406     /**
407      * @param controlNamePrefix
408      *            the controlNamePrefix to set
409      */
410     public void setControlNamePrefix(String controlNamePrefix) {
411         this.controlNamePrefix = controlNamePrefix;
412     }
413
414     /**
415      * @return the controlNameUuid
416      */
417     public String getControlNameUuid() {
418         return controlNameUuid;
419     }
420
421     /**
422      * @param controlNameUuid
423      *            the controlNameUuid to set
424      */
425     public void setControlNameUuid(String controlNameUuid) {
426         this.controlNameUuid = controlNameUuid;
427     }
428
429     /**
430      * @return the propUserid
431      */
432     public String getPropUserid() {
433         return propUserid;
434     }
435
436     /**
437      * @param propUserid
438      *            the propUserid to set
439      */
440     public void setPropUserid(String propUserid) {
441         this.propUserid = propUserid;
442     }
443
444     /**
445      * @return the propText
446      */
447     public String getPropText() {
448         return propText;
449     }
450
451     /**
452      * @param propText
453      *            the propText to set
454      */
455     public void setPropText(String propText) {
456         this.propText = propText;
457     }
458
459     /**
460      * @return the event
461      */
462     public CldsEvent getEvent() {
463         return event;
464     }
465
466     public String getId() {
467         return id;
468     }
469
470     public void setId(String id) {
471         this.id = id;
472     }
473
474     public String getTemplateName() {
475         return templateName;
476     }
477
478     public void setTemplateName(String templateName) {
479         this.templateName = templateName;
480     }
481
482     public String getPropId() {
483         return propId;
484     }
485
486     public void setPropId(String propId) {
487         this.propId = propId;
488     }
489
490     /**
491      * @param event
492      *            the event to set
493      */
494     public void setEvent(CldsEvent event) {
495         this.event = event;
496     }
497
498     /**
499      * @return the status
500      */
501     public String getStatus() {
502         return status;
503     }
504
505     /**
506      * @param status
507      *            the status to set
508      */
509     public void setStatus(String status) {
510         this.status = status;
511     }
512
513     /**
514      * @return the permittedActionCd
515      */
516     public List<String> getPermittedActionCd() {
517         return permittedActionCd;
518     }
519
520     /**
521      * @param permittedActionCd
522      *            the permittedActionCd to set
523      */
524     public void setPermittedActionCd(List<String> permittedActionCd) {
525         this.permittedActionCd = permittedActionCd;
526     }
527
528     public String getBlueprintId() {
529         return blueprintId;
530     }
531
532     public void setBlueprintId(String blueprintId) {
533         this.blueprintId = blueprintId;
534     }
535
536     public String getBlueprintUserid() {
537         return blueprintUserid;
538     }
539
540     public void setBlueprintUserid(String blueprintUserid) {
541         this.blueprintUserid = blueprintUserid;
542     }
543
544     public String getBlueprintText() {
545         return blueprintText;
546     }
547
548     public void setBlueprintText(String blueprintText) {
549         this.blueprintText = blueprintText;
550     }
551
552     public String getBpmnId() {
553         return bpmnId;
554     }
555
556     public void setBpmnId(String bpmnId) {
557         this.bpmnId = bpmnId;
558     }
559
560     public String getBpmnUserid() {
561         return bpmnUserid;
562     }
563
564     public void setBpmnUserid(String bpmnUserid) {
565         this.bpmnUserid = bpmnUserid;
566     }
567
568     public String getBpmnText() {
569         return bpmnText;
570     }
571
572     public void setBpmnText(String bpmnText) {
573         this.bpmnText = bpmnText;
574     }
575
576     public String getImageId() {
577         return imageId;
578     }
579
580     public void setImageId(String imageId) {
581         this.imageId = imageId;
582     }
583
584     public String getImageUserid() {
585         return imageUserid;
586     }
587
588     public void setImageUserid(String imageUserid) {
589         this.imageUserid = imageUserid;
590     }
591
592     public String getImageText() {
593         return imageText;
594     }
595
596     public void setImageText(String imageText) {
597         this.imageText = imageText;
598     }
599
600     public String getDocId() {
601         return docId;
602     }
603
604     public void setDocId(String docId) {
605         this.docId = docId;
606     }
607
608     public String getDocUserid() {
609         return docUserid;
610     }
611
612     public void setDocUserid(String docUserid) {
613         this.docUserid = docUserid;
614     }
615
616     public String getDocText() {
617         return docText;
618     }
619
620     public void setDocText(String docText) {
621         this.docText = docText;
622     }
623
624     public String getTypeId() {
625         return typeId;
626     }
627
628     public void setTypeId(String typeId) {
629         this.typeId = typeId;
630     }
631
632     public List<CldsModelInstance> getCldsModelInstanceList() {
633         if (cldsModelInstanceList == null) {
634             cldsModelInstanceList = new ArrayList<>();
635         }
636         return cldsModelInstanceList;
637     }
638
639     public void setCldsModelInstanceList(List<CldsModelInstance> cldsModelInstanceList) {
640         this.cldsModelInstanceList = cldsModelInstanceList;
641     }
642
643     public void setDispatcherResponse(String dispatcherResponse) {
644         this.dispatcherResponse = dispatcherResponse;
645
646     }
647
648     public String getDispatcherResponse() {
649         return this.dispatcherResponse;
650     }
651
652     public String getDeploymentId() {
653         return deploymentId;
654     }
655
656     public void setDeploymentId(String deploymentId) {
657         this.deploymentId = deploymentId;
658     }
659
660     public boolean isUserAuthorizedToUpdate() {
661         return userAuthorizedToUpdate;
662     }
663
664     public void setUserAuthorizedToUpdate(boolean userAuthorizedToUpdate) {
665         this.userAuthorizedToUpdate = userAuthorizedToUpdate;
666     }
667 }