2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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;
48 public static final String STATUS_DESIGN = "DESIGN";
49 public static final String STATUS_DISTRIBUTED = "DISTRIBUTED";
50 public static final String STATUS_ACTIVE = "ACTIVE";
51 public static final String STATUS_STOPPED = "STOPPED";
52 public static final String STATUS_DELETING = "DELETING";
53 public static final String STATUS_ERROR = "ERROR";
54 public static final String STATUS_UNKNOWN = "UNKNOWN";
56 private String templateId;
57 private String templateName;
59 private String controlNamePrefix;
60 private String controlNameUuid;
61 private String bpmnText;
62 private String propText;
63 private String imageText;
64 private String docText;
65 private String blueprintText;
66 private CldsEvent event;
67 private String status;
68 private List<String> permittedActionCd;
69 private List<CldsModelInstance> cldsModelInstanceList;
70 // This is a transient value used to return the failure message to UI
71 private String errorMessageForUi;
73 * The service type Id received from DCAE by querying it
75 private String typeId;
76 private String typeName;
77 private String deploymentId;
78 private String deploymentStatusUrl;
80 // Set default handlers but this can be changed if needed.
81 private static StatusHandler statusHandler = new StatusHandlerImpl();
82 private static ActionsHandler actionsHandler = new ActionsHandlerImpl();
84 public static synchronized void setStatusHandler(StatusHandler statHandler) {
85 statusHandler = statHandler;
88 public static synchronized void setActionsHandler(ActionsHandler cdHandler) {
89 actionsHandler = cdHandler;
93 * Construct empty model.
96 event = new CldsEvent();
102 public static CldsModel retrieve(CldsDao cldsDao, String name, boolean okIfNotFound) {
104 CldsModel model = cldsDao.getModelTemplate(name);
105 if (model.getId() == null && !okIfNotFound) {
106 throw new NotFoundException();
108 model.determineStatus();
109 model.determinePermittedActionCd();
113 public boolean canDcaeInventoryCall() {
114 boolean canCall = false;
115 /* Below checks the clds event is submit/resubmit/distribute */
116 if (event.isActionCd(CldsEvent.ACTION_SUBMIT) || event.isActionCd(CldsEvent.ACTION_RESUBMIT)
117 || event.isActionCd(CldsEvent.ACTION_DISTRIBUTE) || event.isActionCd(CldsEvent.ACTION_SUBMITDCAE)) {
126 public CldsModel save(CldsDao cldsDao, String userid) {
127 CldsModel cldsModel = cldsDao.setModel(this, userid);
129 determinePermittedActionCd();
134 * set the status in the model
136 public void determineStatus() {
137 status = statusHandler.determineStatusOnLastEvent(event);
141 * Determine permittedActionCd list using the actionCd from the current event.
142 * It's a states graph, given the next action that can be executed from the one
143 * that has been executed (described in the event object). ACTION_CREATE being
146 public void determinePermittedActionCd() {
147 permittedActionCd = actionsHandler.determinePermittedActionsOnLastEvent(event, propText);
151 * Validate requestedActionCd - determine permittedActionCd and then check if
152 * contained in permittedActionCd Throw IllegalArgumentException if requested
153 * actionCd is not permitted.
155 public void validateAction(String requestedActionCd) {
156 determinePermittedActionCd();
157 if (!permittedActionCd.contains(requestedActionCd)) {
158 throw new IllegalArgumentException(
159 "Invalid requestedActionCd: " + requestedActionCd + ". Given current actionCd: "
160 + actionsHandler.getCurrentActionCd(event) + ", the permittedActionCd: " + permittedActionCd);
165 * Extract the UUID portion of a given full control name (controlNamePrefix +
166 * controlNameUuid). No fields are populated other than controlNamePrefix and
167 * controlNameUuid. Throws BadRequestException if length of given control name
168 * is less than UUID_LENGTH.
170 public static CldsModel createUsingControlName(String fullControlName) {
171 if (fullControlName == null || fullControlName.length() < UUID_LENGTH) {
172 throw new BadRequestException(
173 "closed loop id / control name length, " + (fullControlName != null ? fullControlName.length() : 0)
174 + ", less than the minimum of: " + UUID_LENGTH);
176 CldsModel model = new CldsModel();
177 model.setControlNamePrefix(fullControlName.substring(0, fullControlName.length() - UUID_LENGTH));
178 model.setControlNameUuid(fullControlName.substring(fullControlName.length() - UUID_LENGTH));
183 * @return the controlName (controlNamePrefix + controlNameUuid)
185 public String getControlName() {
186 return controlNamePrefix + controlNameUuid;
190 * To insert modelInstance to the database
192 public static CldsModel insertModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent, String userid) {
193 String controlName = dcaeEvent.getControlName();
194 CldsModel cldsModel = createUsingControlName(controlName);
195 cldsModel = cldsDao.getModelByUuid(cldsModel.getControlNameUuid());
196 cldsModel.determineStatus();
197 if (dcaeEvent.getCldsActionCd().equals(CldsEvent.ACTION_UNDEPLOY)
198 || (dcaeEvent.getCldsActionCd().equals(CldsEvent.ACTION_DEPLOY)
199 && (cldsModel.getStatus().equals(STATUS_DISTRIBUTED) || cldsModel.getStatus().equals(STATUS_DESIGN)))) {
200 CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userid, dcaeEvent.getCldsActionCd(),
201 CldsEvent.ACTION_STATE_RECEIVED, null);
203 cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
210 public String getName() {
218 public void setName(String name) {
223 * @return the typeName
225 public String getTypeName() {
229 public void setTypeName(String typeName) {
230 this.typeName = typeName;
233 public String getTemplateId() {
237 public void setTemplateId(String templateId) {
238 this.templateId = templateId;
242 * @return the controlNamePrefix
244 public String getControlNamePrefix() {
245 return controlNamePrefix;
249 * @param controlNamePrefix
250 * the controlNamePrefix to set
252 public void setControlNamePrefix(String controlNamePrefix) {
253 this.controlNamePrefix = controlNamePrefix;
257 * @return the controlNameUuid
259 public String getControlNameUuid() {
260 return controlNameUuid;
264 * @param controlNameUuid
265 * the controlNameUuid to set
267 public void setControlNameUuid(String controlNameUuid) {
268 this.controlNameUuid = controlNameUuid;
272 * @return the propText
274 public String getPropText() {
280 * the propText to set
282 public void setPropText(String propText) {
283 this.propText = propText;
289 public CldsEvent getEvent() {
293 public String getId() {
297 public void setId(String id) {
301 public String getTemplateName() {
305 public void setTemplateName(String templateName) {
306 this.templateName = templateName;
313 public void setEvent(CldsEvent event) {
320 public String getStatus() {
328 public void setStatus(String status) {
329 this.status = status;
332 public String getBlueprintText() {
333 return blueprintText;
336 public void setBlueprintText(String blueprintText) {
337 this.blueprintText = blueprintText;
340 public String getBpmnText() {
344 public void setBpmnText(String bpmnText) {
345 this.bpmnText = bpmnText;
348 public String getImageText() {
352 public void setImageText(String imageText) {
353 this.imageText = imageText;
356 public String getDocText() {
360 public void setDocText(String docText) {
361 this.docText = docText;
364 public String getTypeId() {
368 public void setTypeId(String typeId) {
369 this.typeId = typeId;
372 public List<CldsModelInstance> getCldsModelInstanceList() {
373 if (cldsModelInstanceList == null) {
374 cldsModelInstanceList = new ArrayList<>();
376 return cldsModelInstanceList;
379 public String getDeploymentId() {
383 public void setDeploymentId(String deploymentId) {
384 this.deploymentId = deploymentId;
387 public List<String> getPermittedActionCd() {
388 return permittedActionCd;
391 public String getErrorMessageForUi() {
392 return errorMessageForUi;
395 public void setErrorMessageForUi(String errorMessageForUi) {
396 this.errorMessageForUi = errorMessageForUi;
399 public String getDeploymentStatusUrl() {
400 return deploymentStatusUrl;
403 public void setDeploymentStatusUrl(String deploymentStatusUrl) {
404 this.deploymentStatusUrl = deploymentStatusUrl;