2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds.model;
26 import com.google.gson.annotations.Expose;
28 import java.util.ArrayList;
29 import java.util.List;
31 import javax.ws.rs.BadRequestException;
32 import javax.ws.rs.NotFoundException;
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;
41 * Represent a CLDS Model.
43 public class CldsModel {
45 private static final int UUID_LENGTH = 36;
47 * The constant STATUS_DESIGN.
49 public static final String STATUS_DESIGN = "DESIGN";
51 * The constant STATUS_DISTRIBUTED.
53 public static final String STATUS_DISTRIBUTED = "DISTRIBUTED";
55 * The constant STATUS_ACTIVE.
57 public static final String STATUS_ACTIVE = "ACTIVE";
59 * The constant STATUS_STOPPED.
61 public static final String STATUS_STOPPED = "STOPPED";
63 * The constant STATUS_DELETING.
65 public static final String STATUS_DELETING = "DELETING";
67 * The constant STATUS_ERROR.
69 public static final String STATUS_ERROR = "ERROR";
71 * The constant STATUS_UNKNOWN.
73 public static final String STATUS_UNKNOWN = "UNKNOWN";
78 private String templateId;
80 private String templateName;
84 private String controlNamePrefix;
86 private String controlNameUuid;
88 private String bpmnText;
90 private String propText;
92 private String imageText;
94 private String docText;
96 private String blueprintText;
98 private CldsEvent event;
100 private String status;
102 private List<String> permittedActionCd;
104 private List<CldsModelInstance> cldsModelInstanceList;
105 // This is a transient value used to return the failure message to UI
107 private String errorMessageForUi;
109 * The service type Id received from DCAE by querying it.
112 private String typeId;
114 private String typeName;
116 private String deploymentId;
118 private String deploymentStatusUrl;
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();
125 * Sets status handler.
127 * @param statHandler the stat handler
129 public static synchronized void setStatusHandler(StatusHandler statHandler) {
130 statusHandler = statHandler;
134 * Sets actions handler.
136 * @param cdHandler the cd handler
138 public static synchronized void setActionsHandler(ActionsHandler cdHandler) {
139 actionsHandler = cdHandler;
143 * Construct empty model.
146 event = new CldsEvent();
152 * @param cldsDao the clds dao
153 * @param name the name
154 * @param okIfNotFound the ok if not found
155 * @return the clds model
157 public static CldsModel retrieve(CldsDao cldsDao, String name, boolean okIfNotFound) {
159 CldsModel model = cldsDao.getModelTemplate(name);
160 if (model.getId() == null && !okIfNotFound) {
161 throw new NotFoundException();
163 model.determineStatus();
164 model.determinePermittedActionCd();
169 * Can dcae inventory call boolean.
171 * @return the boolean
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)) {
186 * @param cldsDao the clds dao
187 * @param userid the userid
188 * @return the clds model
190 public CldsModel save(CldsDao cldsDao, String userid) {
191 CldsModel cldsModel = cldsDao.setModel(this, userid);
193 determinePermittedActionCd();
198 * set the status in the model.
200 public void determineStatus() {
201 status = statusHandler.determineStatusOnLastEvent(event);
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
210 public void determinePermittedActionCd() {
211 permittedActionCd = actionsHandler.determinePermittedActionsOnLastEvent(event, propText);
215 * Validate requestedActionCd - determine permittedActionCd and then check if
216 * contained in permittedActionCd Throw IllegalArgumentException if requested
217 * actionCd is not permitted.
219 * @param requestedActionCd the requested action cd
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);
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.
236 * @param fullControlName the full control name
237 * @return the clds model
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);
245 CldsModel model = new CldsModel();
246 model.setControlNamePrefix(fullControlName.substring(0, fullControlName.length() - UUID_LENGTH));
247 model.setControlNameUuid(fullControlName.substring(fullControlName.length() - UUID_LENGTH));
254 * @return the controlName (controlNamePrefix + controlNameUuid)
256 public String getControlName() {
257 return controlNamePrefix + controlNameUuid;
261 * To insert modelInstance to the database.
263 * @param cldsDao the clds dao
264 * @param dcaeEvent the dcae event
265 * @param userid the userid
266 * @return the clds model
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);
279 cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
288 public String getName() {
295 * @param name the name to set
297 public void setName(String name) {
304 * @return the typeName
306 public String getTypeName() {
313 * @param typeName the type name
315 public void setTypeName(String typeName) {
316 this.typeName = typeName;
322 * @return the template id
324 public String getTemplateId() {
331 * @param templateId the template id
333 public void setTemplateId(String templateId) {
334 this.templateId = templateId;
338 * Gets control name prefix.
340 * @return the controlNamePrefix
342 public String getControlNamePrefix() {
343 return controlNamePrefix;
347 * Sets control name prefix.
349 * @param controlNamePrefix the controlNamePrefix to set
351 public void setControlNamePrefix(String controlNamePrefix) {
352 this.controlNamePrefix = controlNamePrefix;
356 * Gets control name uuid.
358 * @return the controlNameUuid
360 public String getControlNameUuid() {
361 return controlNameUuid;
365 * Sets control name uuid.
367 * @param controlNameUuid the controlNameUuid to set
369 public void setControlNameUuid(String controlNameUuid) {
370 this.controlNameUuid = controlNameUuid;
376 * @return the propText
378 public String getPropText() {
385 * @param propText the propText to set
387 public void setPropText(String propText) {
388 this.propText = propText;
396 public CldsEvent getEvent() {
405 public String getId() {
414 public void setId(String id) {
419 * Gets template name.
421 * @return the template name
423 public String getTemplateName() {
428 * Sets template name.
430 * @param templateName the template name
432 public void setTemplateName(String templateName) {
433 this.templateName = templateName;
439 * @param event the event to set
441 public void setEvent(CldsEvent event) {
450 public String getStatus() {
457 * @param status the status to set
459 public void setStatus(String status) {
460 this.status = status;
464 * Gets blueprint text.
466 * @return the blueprint text
468 public String getBlueprintText() {
469 return blueprintText;
473 * Sets blueprint text.
475 * @param blueprintText the blueprint text
477 public void setBlueprintText(String blueprintText) {
478 this.blueprintText = blueprintText;
484 * @return the bpmn text
486 public String getBpmnText() {
493 * @param bpmnText the bpmn text
495 public void setBpmnText(String bpmnText) {
496 this.bpmnText = bpmnText;
502 * @return the image text
504 public String getImageText() {
511 * @param imageText the image text
513 public void setImageText(String imageText) {
514 this.imageText = imageText;
520 * @return the doc text
522 public String getDocText() {
529 * @param docText the doc text
531 public void setDocText(String docText) {
532 this.docText = docText;
538 * @return the type id
540 public String getTypeId() {
547 * @param typeId the type id
549 public void setTypeId(String typeId) {
550 this.typeId = typeId;
554 * Gets clds model instance list.
556 * @return the clds model instance list
558 public List<CldsModelInstance> getCldsModelInstanceList() {
559 if (cldsModelInstanceList == null) {
560 cldsModelInstanceList = new ArrayList<>();
562 return cldsModelInstanceList;
566 * Gets deployment id.
568 * @return the deployment id
570 public String getDeploymentId() {
575 * Sets deployment id.
577 * @param deploymentId the deployment id
579 public void setDeploymentId(String deploymentId) {
580 this.deploymentId = deploymentId;
584 * Gets permitted action cd.
586 * @return the permitted action cd
588 public List<String> getPermittedActionCd() {
589 return permittedActionCd;
593 * Gets error message for ui.
595 * @return the error message for ui
597 public String getErrorMessageForUi() {
598 return errorMessageForUi;
602 * Sets error message for ui.
604 * @param errorMessageForUi the error message for ui
606 public void setErrorMessageForUi(String errorMessageForUi) {
607 this.errorMessageForUi = errorMessageForUi;
611 * Gets deployment status url.
613 * @return the deployment status url
615 public String getDeploymentStatusUrl() {
616 return deploymentStatusUrl;
620 * Sets deployment status url.
622 * @param deploymentStatusUrl the deployment status url
624 public void setDeploymentStatusUrl(String deploymentStatusUrl) {
625 this.deploymentStatusUrl = deploymentStatusUrl;