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