4d2ca89a436a6215e47805925f7d88776c6ade5e
[clamp.git] / src / main / java / org / onap / clamp / clds / model / CldsModel.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2019 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  *
22  */
23
24 package org.onap.clamp.clds.model;
25
26 import com.google.gson.annotations.Expose;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import javax.ws.rs.BadRequestException;
32 import javax.ws.rs.NotFoundException;
33
34 import org.onap.clamp.clds.dao.CldsDao;
35 import org.onap.clamp.clds.model.actions.ActionsHandler;
36 import org.onap.clamp.clds.model.actions.ActionsHandlerImpl;
37 import org.onap.clamp.clds.model.status.StatusHandler;
38 import org.onap.clamp.clds.model.status.StatusHandlerImpl;
39
40 /**
41  * Represent a CLDS Model.
42  */
43 public class CldsModel {
44
45     private static final int UUID_LENGTH = 36;
46     /**
47      * The constant STATUS_DESIGN.
48      */
49     public static final String STATUS_DESIGN = "DESIGN";
50     /**
51      * The constant STATUS_DISTRIBUTED.
52      */
53     public static final String STATUS_DISTRIBUTED = "DISTRIBUTED";
54     /**
55      * The constant STATUS_ACTIVE.
56      */
57     public static final String STATUS_ACTIVE = "ACTIVE";
58     /**
59      * The constant STATUS_STOPPED.
60      */
61     public static final String STATUS_STOPPED = "STOPPED";
62     /**
63      * The constant STATUS_DELETING.
64      */
65     public static final String STATUS_DELETING = "DELETING";
66     /**
67      * The constant STATUS_ERROR.
68      */
69     public static final String STATUS_ERROR = "ERROR";
70     /**
71      * The constant STATUS_UNKNOWN.
72      */
73     public static final String STATUS_UNKNOWN = "UNKNOWN";
74
75     @Expose
76     private String id;
77     @Expose
78     private String templateId;
79     @Expose
80     private String templateName;
81     @Expose
82     private String name;
83     @Expose
84     private String controlNamePrefix;
85     @Expose
86     private String controlNameUuid;
87     @Expose
88     private String bpmnText;
89     @Expose
90     private String propText;
91     @Expose
92     private String imageText;
93     @Expose
94     private String docText;
95     @Expose
96     private String blueprintText;
97     @Expose
98     private CldsEvent event;
99     @Expose
100     private String status;
101     @Expose
102     private List<String> permittedActionCd;
103     @Expose
104     private List<CldsModelInstance> cldsModelInstanceList;
105     // This is a transient value used to return the failure message to UI
106     @Expose
107     private String errorMessageForUi;
108     /**
109      * The service type Id received from DCAE by querying it.
110      */
111     @Expose
112     private String typeId;
113     @Expose
114     private String typeName;
115     @Expose
116     private String deploymentId;
117     @Expose
118     private String deploymentStatusUrl;
119
120     // Set default handlers but this can be changed if needed.
121     private static StatusHandler statusHandler = new StatusHandlerImpl();
122     private static ActionsHandler actionsHandler = new ActionsHandlerImpl();
123
124     /**
125      * Sets status handler.
126      *
127      * @param statHandler the stat handler
128      */
129     public static synchronized void setStatusHandler(StatusHandler statHandler) {
130         statusHandler = statHandler;
131     }
132
133     /**
134      * Sets actions handler.
135      *
136      * @param cdHandler the cd handler
137      */
138     public static synchronized void setActionsHandler(ActionsHandler cdHandler) {
139         actionsHandler = cdHandler;
140     }
141
142     /**
143      * Construct empty model.
144      */
145     public CldsModel() {
146         event = new CldsEvent();
147     }
148
149     /**
150      * Retrieve from DB.
151      *
152      * @param cldsDao      the clds dao
153      * @param name         the name
154      * @param okIfNotFound the ok if not found
155      * @return the clds model
156      */
157     public static CldsModel retrieve(CldsDao cldsDao, String name, boolean okIfNotFound) {
158         // get from db
159         CldsModel model = cldsDao.getModelTemplate(name);
160         if (model.getId() == null && !okIfNotFound) {
161             throw new NotFoundException();
162         }
163         model.determineStatus();
164         model.determinePermittedActionCd();
165         return model;
166     }
167
168     /**
169      * Can dcae inventory call boolean.
170      *
171      * @return the boolean
172      */
173     public boolean canDcaeInventoryCall() {
174         boolean canCall = false;
175         /* Below checks the clds event is submit/resubmit/distribute */
176         if (event.isActionCd(CldsEvent.ACTION_SUBMIT) || event.isActionCd(CldsEvent.ACTION_RESUBMIT)
177                 || event.isActionCd(CldsEvent.ACTION_DISTRIBUTE) || event.isActionCd(CldsEvent.ACTION_SUBMITDCAE)) {
178             canCall = true;
179         }
180         return canCall;
181     }
182
183     /**
184      * Save model to DB.
185      *
186      * @param cldsDao the clds dao
187      * @param userid  the userid
188      * @return the clds model
189      */
190     public CldsModel save(CldsDao cldsDao, String userid) {
191         CldsModel cldsModel = cldsDao.setModel(this, userid);
192         determineStatus();
193         determinePermittedActionCd();
194         return cldsModel;
195     }
196
197     /**
198      * set the status in the model.
199      */
200     public void determineStatus() {
201         status = statusHandler.determineStatusOnLastEvent(event);
202     }
203
204     /**
205      * Determine permittedActionCd list using the actionCd from the current event.
206      * It's a states graph, given the next action that can be executed from the one
207      * that has been executed (described in the event object). ACTION_CREATE being
208      * the first one.
209      */
210     public void determinePermittedActionCd() {
211         permittedActionCd = actionsHandler.determinePermittedActionsOnLastEvent(event, propText);
212     }
213
214     /**
215      * Validate requestedActionCd - determine permittedActionCd and then check if
216      * contained in permittedActionCd Throw IllegalArgumentException if requested
217      * actionCd is not permitted.
218      *
219      * @param requestedActionCd the requested action cd
220      */
221     public void validateAction(String requestedActionCd) {
222         determinePermittedActionCd();
223         if (!permittedActionCd.contains(requestedActionCd)) {
224             throw new IllegalArgumentException("Invalid requestedActionCd: " + requestedActionCd
225                     + ".  Given current actionCd: " + actionsHandler.getCurrentActionCd(event)
226                     + ", the permittedActionCd: " + permittedActionCd);
227         }
228     }
229
230     /**
231      * Extract the UUID portion of a given full control name (controlNamePrefix +
232      * controlNameUuid). No fields are populated other than controlNamePrefix and
233      * controlNameUuid. Throws BadRequestException if length of given control name
234      * is less than UUID_LENGTH.
235      *
236      * @param fullControlName the full control name
237      * @return the clds model
238      */
239     public static CldsModel createUsingControlName(String fullControlName) {
240         if (fullControlName == null || fullControlName.length() < UUID_LENGTH) {
241             throw new BadRequestException(
242                     "closed loop id / control name length, " + (fullControlName != null ? fullControlName.length() : 0)
243                             + ", less than the minimum of: " + UUID_LENGTH);
244         }
245         CldsModel model = new CldsModel();
246         model.setControlNamePrefix(fullControlName.substring(0, fullControlName.length() - UUID_LENGTH));
247         model.setControlNameUuid(fullControlName.substring(fullControlName.length() - UUID_LENGTH));
248         return model;
249     }
250
251     /**
252      * Gets control name.
253      *
254      * @return the controlName (controlNamePrefix + controlNameUuid)
255      */
256     public String getControlName() {
257         return controlNamePrefix + controlNameUuid;
258     }
259
260     /**
261      * To insert modelInstance to the database.
262      *
263      * @param cldsDao   the clds dao
264      * @param dcaeEvent the dcae event
265      * @param userid    the userid
266      * @return the clds model
267      */
268     public static CldsModel insertModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent, String userid) {
269         String controlName = dcaeEvent.getControlName();
270         CldsModel cldsModel = createUsingControlName(controlName);
271         cldsModel = cldsDao.getModelByUuid(cldsModel.getControlNameUuid());
272         cldsModel.determineStatus();
273         if (dcaeEvent.getCldsActionCd().equals(CldsEvent.ACTION_UNDEPLOY) || (dcaeEvent.getCldsActionCd()
274                 .equals(CldsEvent.ACTION_DEPLOY)
275                 && (cldsModel.getStatus().equals(STATUS_DISTRIBUTED) || cldsModel.getStatus().equals(STATUS_DESIGN)))) {
276             CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userid, dcaeEvent.getCldsActionCd(),
277                     CldsEvent.ACTION_STATE_RECEIVED, null);
278         }
279         cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
280         return cldsModel;
281     }
282
283     /**
284      * Gets name.
285      *
286      * @return the name
287      */
288     public String getName() {
289         return name;
290     }
291
292     /**
293      * Sets name.
294      *
295      * @param name the name to set
296      */
297     public void setName(String name) {
298         this.name = name;
299     }
300
301     /**
302      * Gets type name.
303      *
304      * @return the typeName
305      */
306     public String getTypeName() {
307         return typeName;
308     }
309
310     /**
311      * Sets type name.
312      *
313      * @param typeName the type name
314      */
315     public void setTypeName(String typeName) {
316         this.typeName = typeName;
317     }
318
319     /**
320      * Gets template id.
321      *
322      * @return the template id
323      */
324     public String getTemplateId() {
325         return templateId;
326     }
327
328     /**
329      * Sets template id.
330      *
331      * @param templateId the template id
332      */
333     public void setTemplateId(String templateId) {
334         this.templateId = templateId;
335     }
336
337     /**
338      * Gets control name prefix.
339      *
340      * @return the controlNamePrefix
341      */
342     public String getControlNamePrefix() {
343         return controlNamePrefix;
344     }
345
346     /**
347      * Sets control name prefix.
348      *
349      * @param controlNamePrefix the controlNamePrefix to set
350      */
351     public void setControlNamePrefix(String controlNamePrefix) {
352         this.controlNamePrefix = controlNamePrefix;
353     }
354
355     /**
356      * Gets control name uuid.
357      *
358      * @return the controlNameUuid
359      */
360     public String getControlNameUuid() {
361         return controlNameUuid;
362     }
363
364     /**
365      * Sets control name uuid.
366      *
367      * @param controlNameUuid the controlNameUuid to set
368      */
369     public void setControlNameUuid(String controlNameUuid) {
370         this.controlNameUuid = controlNameUuid;
371     }
372
373     /**
374      * Gets prop text.
375      *
376      * @return the propText
377      */
378     public String getPropText() {
379         return propText;
380     }
381
382     /**
383      * Sets prop text.
384      *
385      * @param propText the propText to set
386      */
387     public void setPropText(String propText) {
388         this.propText = propText;
389     }
390
391     /**
392      * Gets event.
393      *
394      * @return the event
395      */
396     public CldsEvent getEvent() {
397         return event;
398     }
399
400     /**
401      * Gets id.
402      *
403      * @return the id
404      */
405     public String getId() {
406         return id;
407     }
408
409     /**
410      * Sets id.
411      *
412      * @param id the id
413      */
414     public void setId(String id) {
415         this.id = id;
416     }
417
418     /**
419      * Gets template name.
420      *
421      * @return the template name
422      */
423     public String getTemplateName() {
424         return templateName;
425     }
426
427     /**
428      * Sets template name.
429      *
430      * @param templateName the template name
431      */
432     public void setTemplateName(String templateName) {
433         this.templateName = templateName;
434     }
435
436     /**
437      * Sets event.
438      *
439      * @param event the event to set
440      */
441     public void setEvent(CldsEvent event) {
442         this.event = event;
443     }
444
445     /**
446      * Gets status.
447      *
448      * @return the status
449      */
450     public String getStatus() {
451         return status;
452     }
453
454     /**
455      * Sets status.
456      *
457      * @param status the status to set
458      */
459     public void setStatus(String status) {
460         this.status = status;
461     }
462
463     /**
464      * Gets blueprint text.
465      *
466      * @return the blueprint text
467      */
468     public String getBlueprintText() {
469         return blueprintText;
470     }
471
472     /**
473      * Sets blueprint text.
474      *
475      * @param blueprintText the blueprint text
476      */
477     public void setBlueprintText(String blueprintText) {
478         this.blueprintText = blueprintText;
479     }
480
481     /**
482      * Gets bpmn text.
483      *
484      * @return the bpmn text
485      */
486     public String getBpmnText() {
487         return bpmnText;
488     }
489
490     /**
491      * Sets bpmn text.
492      *
493      * @param bpmnText the bpmn text
494      */
495     public void setBpmnText(String bpmnText) {
496         this.bpmnText = bpmnText;
497     }
498
499     /**
500      * Gets image text.
501      *
502      * @return the image text
503      */
504     public String getImageText() {
505         return imageText;
506     }
507
508     /**
509      * Sets image text.
510      *
511      * @param imageText the image text
512      */
513     public void setImageText(String imageText) {
514         this.imageText = imageText;
515     }
516
517     /**
518      * Gets doc text.
519      *
520      * @return the doc text
521      */
522     public String getDocText() {
523         return docText;
524     }
525
526     /**
527      * Sets doc text.
528      *
529      * @param docText the doc text
530      */
531     public void setDocText(String docText) {
532         this.docText = docText;
533     }
534
535     /**
536      * Gets type id.
537      *
538      * @return the type id
539      */
540     public String getTypeId() {
541         return typeId;
542     }
543
544     /**
545      * Sets type id.
546      *
547      * @param typeId the type id
548      */
549     public void setTypeId(String typeId) {
550         this.typeId = typeId;
551     }
552
553     /**
554      * Gets clds model instance list.
555      *
556      * @return the clds model instance list
557      */
558     public List<CldsModelInstance> getCldsModelInstanceList() {
559         if (cldsModelInstanceList == null) {
560             cldsModelInstanceList = new ArrayList<>();
561         }
562         return cldsModelInstanceList;
563     }
564
565     /**
566      * Gets deployment id.
567      *
568      * @return the deployment id
569      */
570     public String getDeploymentId() {
571         return deploymentId;
572     }
573
574     /**
575      * Sets deployment id.
576      *
577      * @param deploymentId the deployment id
578      */
579     public void setDeploymentId(String deploymentId) {
580         this.deploymentId = deploymentId;
581     }
582
583     /**
584      * Gets permitted action cd.
585      *
586      * @return the permitted action cd
587      */
588     public List<String> getPermittedActionCd() {
589         return permittedActionCd;
590     }
591
592     /**
593      * Gets error message for ui.
594      *
595      * @return the error message for ui
596      */
597     public String getErrorMessageForUi() {
598         return errorMessageForUi;
599     }
600
601     /**
602      * Sets error message for ui.
603      *
604      * @param errorMessageForUi the error message for ui
605      */
606     public void setErrorMessageForUi(String errorMessageForUi) {
607         this.errorMessageForUi = errorMessageForUi;
608     }
609
610     /**
611      * Gets deployment status url.
612      *
613      * @return the deployment status url
614      */
615     public String getDeploymentStatusUrl() {
616         return deploymentStatusUrl;
617     }
618
619     /**
620      * Sets deployment status url.
621      *
622      * @param deploymentStatusUrl the deployment status url
623      */
624     public void setDeploymentStatusUrl(String deploymentStatusUrl) {
625         this.deploymentStatusUrl = deploymentStatusUrl;
626     }
627 }