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;
40 import com.google.gson.annotations.Expose;
43 * Represent a CLDS Model.
45 public class CldsModel {
47 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsModel.class);
48 private static final int UUID_LENGTH = 36;
50 * The constant STATUS_DESIGN.
52 public static final String STATUS_DESIGN = "DESIGN";
54 * The constant STATUS_DISTRIBUTED.
56 public static final String STATUS_DISTRIBUTED = "DISTRIBUTED";
58 * The constant STATUS_ACTIVE.
60 public static final String STATUS_ACTIVE = "ACTIVE";
62 * The constant STATUS_STOPPED.
64 public static final String STATUS_STOPPED = "STOPPED";
66 * The constant STATUS_DELETING.
68 public static final String STATUS_DELETING = "DELETING";
70 * The constant STATUS_ERROR.
72 public static final String STATUS_ERROR = "ERROR";
74 * The constant STATUS_UNKNOWN.
76 public static final String STATUS_UNKNOWN = "UNKNOWN";
81 private String templateId;
83 private String templateName;
87 private String controlNamePrefix;
89 private String controlNameUuid;
91 private String bpmnText;
93 private String propText;
95 private String imageText;
97 private String docText;
99 private String blueprintText;
101 private CldsEvent event;
103 private String status;
105 private List<String> permittedActionCd;
107 private List<CldsModelInstance> cldsModelInstanceList;
108 // This is a transient value used to return the failure message to UI
110 private String errorMessageForUi;
112 * The service type Id received from DCAE by querying it.
115 private String typeId;
117 private String typeName;
119 private String deploymentId;
121 private String deploymentStatusUrl;
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();
128 * Sets status handler.
130 * @param statHandler the stat handler
132 public static synchronized void setStatusHandler(StatusHandler statHandler) {
133 statusHandler = statHandler;
137 * Sets actions handler.
139 * @param cdHandler the cd handler
141 public static synchronized void setActionsHandler(ActionsHandler cdHandler) {
142 actionsHandler = cdHandler;
146 * Construct empty model.
149 event = new CldsEvent();
155 * @param cldsDao the clds dao
156 * @param name the name
157 * @param okIfNotFound the ok if not found
158 * @return the clds model
160 public static CldsModel retrieve(CldsDao cldsDao, String name, boolean okIfNotFound) {
162 CldsModel model = cldsDao.getModelTemplate(name);
163 if (model.getId() == null && !okIfNotFound) {
164 throw new NotFoundException();
166 model.determineStatus();
167 model.determinePermittedActionCd();
172 * Can dcae inventory call boolean.
174 * @return the boolean
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)) {
189 * @param cldsDao the clds dao
190 * @param userid the userid
191 * @return the clds model
193 public CldsModel save(CldsDao cldsDao, String userid) {
194 CldsModel cldsModel = cldsDao.setModel(this, userid);
196 determinePermittedActionCd();
201 * set the status in the model.
203 public void determineStatus() {
204 status = statusHandler.determineStatusOnLastEvent(event);
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
213 public void determinePermittedActionCd() {
214 permittedActionCd = actionsHandler.determinePermittedActionsOnLastEvent(event, propText);
218 * Validate requestedActionCd - determine permittedActionCd and then check if
219 * contained in permittedActionCd Throw IllegalArgumentException if requested
220 * actionCd is not permitted.
222 * @param requestedActionCd the requested action cd
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);
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.
239 * @param fullControlName the full control name
240 * @return the clds model
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);
248 CldsModel model = new CldsModel();
249 model.setControlNamePrefix(fullControlName.substring(0, fullControlName.length() - UUID_LENGTH));
250 model.setControlNameUuid(fullControlName.substring(fullControlName.length() - UUID_LENGTH));
257 * @return the controlName (controlNamePrefix + controlNameUuid)
259 public String getControlName() {
260 return controlNamePrefix + controlNameUuid;
264 * To insert modelInstance to the database.
266 * @param cldsDao the clds dao
267 * @param dcaeEvent the dcae event
268 * @param userid the userid
269 * @return the clds model
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);
282 cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
291 public String getName() {
298 * @param name the name to set
300 public void setName(String name) {
307 * @return the typeName
309 public String getTypeName() {
316 * @param typeName the type name
318 public void setTypeName(String typeName) {
319 this.typeName = typeName;
325 * @return the template id
327 public String getTemplateId() {
334 * @param templateId the template id
336 public void setTemplateId(String templateId) {
337 this.templateId = templateId;
341 * Gets control name prefix.
343 * @return the controlNamePrefix
345 public String getControlNamePrefix() {
346 return controlNamePrefix;
350 * Sets control name prefix.
352 * @param controlNamePrefix the controlNamePrefix to set
354 public void setControlNamePrefix(String controlNamePrefix) {
355 this.controlNamePrefix = controlNamePrefix;
359 * Gets control name uuid.
361 * @return the controlNameUuid
363 public String getControlNameUuid() {
364 return controlNameUuid;
368 * Sets control name uuid.
370 * @param controlNameUuid the controlNameUuid to set
372 public void setControlNameUuid(String controlNameUuid) {
373 this.controlNameUuid = controlNameUuid;
379 * @return the propText
381 public String getPropText() {
388 * @param propText the propText to set
390 public void setPropText(String propText) {
391 this.propText = propText;
399 public CldsEvent getEvent() {
408 public String getId() {
417 public void setId(String id) {
422 * Gets template name.
424 * @return the template name
426 public String getTemplateName() {
431 * Sets template name.
433 * @param templateName the template name
435 public void setTemplateName(String templateName) {
436 this.templateName = templateName;
442 * @param event the event to set
444 public void setEvent(CldsEvent event) {
453 public String getStatus() {
460 * @param status the status to set
462 public void setStatus(String status) {
463 this.status = status;
467 * Gets blueprint text.
469 * @return the blueprint text
471 public String getBlueprintText() {
472 return blueprintText;
476 * Sets blueprint text.
478 * @param blueprintText the blueprint text
480 public void setBlueprintText(String blueprintText) {
481 this.blueprintText = blueprintText;
487 * @return the bpmn text
489 public String getBpmnText() {
496 * @param bpmnText the bpmn text
498 public void setBpmnText(String bpmnText) {
499 this.bpmnText = bpmnText;
505 * @return the image text
507 public String getImageText() {
514 * @param imageText the image text
516 public void setImageText(String imageText) {
517 this.imageText = imageText;
523 * @return the doc text
525 public String getDocText() {
532 * @param docText the doc text
534 public void setDocText(String docText) {
535 this.docText = docText;
541 * @return the type id
543 public String getTypeId() {
550 * @param typeId the type id
552 public void setTypeId(String typeId) {
553 this.typeId = typeId;
557 * Gets clds model instance list.
559 * @return the clds model instance list
561 public List<CldsModelInstance> getCldsModelInstanceList() {
562 if (cldsModelInstanceList == null) {
563 cldsModelInstanceList = new ArrayList<>();
565 return cldsModelInstanceList;
569 * Gets deployment id.
571 * @return the deployment id
573 public String getDeploymentId() {
578 * Sets deployment id.
580 * @param deploymentId the deployment id
582 public void setDeploymentId(String deploymentId) {
583 this.deploymentId = deploymentId;
587 * Gets permitted action cd.
589 * @return the permitted action cd
591 public List<String> getPermittedActionCd() {
592 return permittedActionCd;
596 * Gets error message for ui.
598 * @return the error message for ui
600 public String getErrorMessageForUi() {
601 return errorMessageForUi;
605 * Sets error message for ui.
607 * @param errorMessageForUi the error message for ui
609 public void setErrorMessageForUi(String errorMessageForUi) {
610 this.errorMessageForUi = errorMessageForUi;
614 * Gets deployment status url.
616 * @return the deployment status url
618 public String getDeploymentStatusUrl() {
619 return deploymentStatusUrl;
623 * Sets deployment status url.
625 * @param deploymentStatusUrl the deployment status url
627 public void setDeploymentStatusUrl(String deploymentStatusUrl) {
628 this.deploymentStatusUrl = deploymentStatusUrl;