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