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