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.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import java.util.ArrayList;
30 import java.util.List;
32 import javax.ws.rs.BadRequestException;
33 import javax.ws.rs.NotFoundException;
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;
42 * Represent a CLDS Model.
44 public class CldsModel {
46 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsModel.class);
47 private static final int UUID_LENGTH = 36;
49 * The constant STATUS_DESIGN.
51 public static final String STATUS_DESIGN = "DESIGN";
53 * The constant STATUS_DISTRIBUTED.
55 public static final String STATUS_DISTRIBUTED = "DISTRIBUTED";
57 * The constant STATUS_ACTIVE.
59 public static final String STATUS_ACTIVE = "ACTIVE";
61 * The constant STATUS_STOPPED.
63 public static final String STATUS_STOPPED = "STOPPED";
65 * The constant STATUS_DELETING.
67 public static final String STATUS_DELETING = "DELETING";
69 * The constant STATUS_ERROR.
71 public static final String STATUS_ERROR = "ERROR";
73 * The constant STATUS_UNKNOWN.
75 public static final String STATUS_UNKNOWN = "UNKNOWN";
77 private String templateId;
78 private String templateName;
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;
94 * The service type Id received from DCAE by querying it.
96 private String typeId;
97 private String typeName;
98 private String deploymentId;
99 private String deploymentStatusUrl;
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();
106 * Sets status handler.
108 * @param statHandler the stat handler
110 public static synchronized void setStatusHandler(StatusHandler statHandler) {
111 statusHandler = statHandler;
115 * Sets actions handler.
117 * @param cdHandler the cd handler
119 public static synchronized void setActionsHandler(ActionsHandler cdHandler) {
120 actionsHandler = cdHandler;
124 * Construct empty model.
127 event = new CldsEvent();
133 * @param cldsDao the clds dao
134 * @param name the name
135 * @param okIfNotFound the ok if not found
136 * @return the clds model
138 public static CldsModel retrieve(CldsDao cldsDao, String name, boolean okIfNotFound) {
140 CldsModel model = cldsDao.getModelTemplate(name);
141 if (model.getId() == null && !okIfNotFound) {
142 throw new NotFoundException();
144 model.determineStatus();
145 model.determinePermittedActionCd();
150 * Can dcae inventory call boolean.
152 * @return the boolean
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)) {
167 * @param cldsDao the clds dao
168 * @param userid the userid
169 * @return the clds model
171 public CldsModel save(CldsDao cldsDao, String userid) {
172 CldsModel cldsModel = cldsDao.setModel(this, userid);
174 determinePermittedActionCd();
179 * set the status in the model.
181 public void determineStatus() {
182 status = statusHandler.determineStatusOnLastEvent(event);
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
191 public void determinePermittedActionCd() {
192 permittedActionCd = actionsHandler.determinePermittedActionsOnLastEvent(event, propText);
196 * Validate requestedActionCd - determine permittedActionCd and then check if
197 * contained in permittedActionCd Throw IllegalArgumentException if requested
198 * actionCd is not permitted.
200 * @param requestedActionCd the requested action cd
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);
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.
217 * @param fullControlName the full control name
218 * @return the clds model
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);
226 CldsModel model = new CldsModel();
227 model.setControlNamePrefix(fullControlName.substring(0, fullControlName.length() - UUID_LENGTH));
228 model.setControlNameUuid(fullControlName.substring(fullControlName.length() - UUID_LENGTH));
235 * @return the controlName (controlNamePrefix + controlNameUuid)
237 public String getControlName() {
238 return controlNamePrefix + controlNameUuid;
242 * To insert modelInstance to the database.
244 * @param cldsDao the clds dao
245 * @param dcaeEvent the dcae event
246 * @param userid the userid
247 * @return the clds model
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);
260 cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
269 public String getName() {
276 * @param name the name to set
278 public void setName(String name) {
285 * @return the typeName
287 public String getTypeName() {
294 * @param typeName the type name
296 public void setTypeName(String typeName) {
297 this.typeName = typeName;
303 * @return the template id
305 public String getTemplateId() {
312 * @param templateId the template id
314 public void setTemplateId(String templateId) {
315 this.templateId = templateId;
319 * Gets control name prefix.
321 * @return the controlNamePrefix
323 public String getControlNamePrefix() {
324 return controlNamePrefix;
328 * Sets control name prefix.
330 * @param controlNamePrefix the controlNamePrefix to set
332 public void setControlNamePrefix(String controlNamePrefix) {
333 this.controlNamePrefix = controlNamePrefix;
337 * Gets control name uuid.
339 * @return the controlNameUuid
341 public String getControlNameUuid() {
342 return controlNameUuid;
346 * Sets control name uuid.
348 * @param controlNameUuid the controlNameUuid to set
350 public void setControlNameUuid(String controlNameUuid) {
351 this.controlNameUuid = controlNameUuid;
357 * @return the propText
359 public String getPropText() {
366 * @param propText the propText to set
368 public void setPropText(String propText) {
369 this.propText = propText;
377 public CldsEvent getEvent() {
386 public String getId() {
395 public void setId(String id) {
400 * Gets template name.
402 * @return the template name
404 public String getTemplateName() {
409 * Sets template name.
411 * @param templateName the template name
413 public void setTemplateName(String templateName) {
414 this.templateName = templateName;
420 * @param event the event to set
422 public void setEvent(CldsEvent event) {
431 public String getStatus() {
438 * @param status the status to set
440 public void setStatus(String status) {
441 this.status = status;
445 * Gets blueprint text.
447 * @return the blueprint text
449 public String getBlueprintText() {
450 return blueprintText;
454 * Sets blueprint text.
456 * @param blueprintText the blueprint text
458 public void setBlueprintText(String blueprintText) {
459 this.blueprintText = blueprintText;
465 * @return the bpmn text
467 public String getBpmnText() {
474 * @param bpmnText the bpmn text
476 public void setBpmnText(String bpmnText) {
477 this.bpmnText = bpmnText;
483 * @return the image text
485 public String getImageText() {
492 * @param imageText the image text
494 public void setImageText(String imageText) {
495 this.imageText = imageText;
501 * @return the doc text
503 public String getDocText() {
510 * @param docText the doc text
512 public void setDocText(String docText) {
513 this.docText = docText;
519 * @return the type id
521 public String getTypeId() {
528 * @param typeId the type id
530 public void setTypeId(String typeId) {
531 this.typeId = typeId;
535 * Gets clds model instance list.
537 * @return the clds model instance list
539 public List<CldsModelInstance> getCldsModelInstanceList() {
540 if (cldsModelInstanceList == null) {
541 cldsModelInstanceList = new ArrayList<>();
543 return cldsModelInstanceList;
547 * Gets deployment id.
549 * @return the deployment id
551 public String getDeploymentId() {
556 * Sets deployment id.
558 * @param deploymentId the deployment id
560 public void setDeploymentId(String deploymentId) {
561 this.deploymentId = deploymentId;
565 * Gets permitted action cd.
567 * @return the permitted action cd
569 public List<String> getPermittedActionCd() {
570 return permittedActionCd;
574 * Gets error message for ui.
576 * @return the error message for ui
578 public String getErrorMessageForUi() {
579 return errorMessageForUi;
583 * Sets error message for ui.
585 * @param errorMessageForUi the error message for ui
587 public void setErrorMessageForUi(String errorMessageForUi) {
588 this.errorMessageForUi = errorMessageForUi;
592 * Gets deployment status url.
594 * @return the deployment status url
596 public String getDeploymentStatusUrl() {
597 return deploymentStatusUrl;
601 * Sets deployment status url.
603 * @param deploymentStatusUrl the deployment status url
605 public void setDeploymentStatusUrl(String deploymentStatusUrl) {
606 this.deploymentStatusUrl = deploymentStatusUrl;