2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ===================================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.clamp.clds.model;
26 import org.onap.clamp.clds.dao.CldsDao;
27 import org.jboss.resteasy.spi.BadRequestException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import javax.ws.rs.NotFoundException;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
37 * Represent a CLDS Model.
39 public class CldsModel {
40 private static final Logger logger = LoggerFactory.getLogger(CldsModel.class);
42 private static final int UUID_LENGTH = 36;
44 public static final String STATUS_DESIGN = "DESIGN";
45 public static final String STATUS_DISTRIBUTED = "DISTRIBUTED";
46 public static final String STATUS_ACTIVE = "ACTIVE";
47 public static final String STATUS_STOPPED = "STOPPED";
48 public static final String STATUS_DELETING = "DELETING";
49 public static final String STATUS_ERROR = "ERROR"; // manual intervention required
50 public static final String STATUS_UNKNOWN = "UNKNOWN";
53 private String templateId;
54 private String templateName;
56 private String controlNamePrefix;
57 private String controlNameUuid;
58 private String bpmnId;
59 private String bpmnUserid;
60 private String bpmnText;
61 private String propId;
62 private String propUserid;
63 private String propText;
64 private String imageId;
65 private String imageUserid;
66 private String imageText;
68 private String docUserid;
69 private String docText;
70 private String blueprintId;
71 private String blueprintUserid;
72 private String blueprintText;
73 private CldsEvent event;
74 private String status;
75 private List<String> permittedActionCd;
76 private List<CldsModelInstance> cldsModelInstanceList;
79 * Construct empty model.
82 event = new CldsEvent();
92 public static CldsModel retrieve(CldsDao cldsDao, String name, boolean okIfNotFound) {
94 CldsModel model = cldsDao.getModelTemplate(name);
95 if (model.getId() == null && !okIfNotFound) {
96 throw new NotFoundException();
98 model.determineStatus();
99 model.determinePermittedActionCd();
109 public void save(CldsDao cldsDao, String userid) {
110 cldsDao.setModel(this, userid);
112 determinePermittedActionCd();
116 * Insert a new event for the new action.
117 * Throw IllegalArgumentException if requested actionCd is not permitted.
122 * @param actionStateCd
124 public void insEvent(CldsDao cldsDao, String userid, String actionCd, String actionStateCd) {
125 validateAction(actionCd);
126 event = CldsEvent.insEvent(cldsDao, this, actionCd, actionStateCd, null);
128 determinePermittedActionCd();
132 * Update event with processInstanceId
135 * @param processInstanceId
137 public void updEvent(CldsDao cldsDao, String processInstanceId) {
138 cldsDao.updEvent(event.getId(), processInstanceId);
142 * set the status in the model
144 private void determineStatus() {
146 status = STATUS_UNKNOWN;
147 if (event == null || event.getActionCd() == null) {
148 status = STATUS_DESIGN;
149 } else if (event.isActionStateCd(CldsEvent.ACTION_STATE_ERROR)) {
150 status = STATUS_ERROR;
151 } else if (event.isActionAndStateCd(CldsEvent.ACTION_CREATE, CldsEvent.ACTION_STATE_ANY) ||
152 event.isActionAndStateCd(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_STATE_ANY) ||
153 event.isActionAndStateCd(CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_STATE_ANY) ||
154 event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_RECEIVED)) {
155 status = STATUS_DESIGN;
156 } else if (event.isActionAndStateCd(CldsEvent.ACTION_DISTRIBUTE, CldsEvent.ACTION_STATE_RECEIVED) ||
157 event.isActionAndStateCd(CldsEvent.ACTION_UNDEPLOY, CldsEvent.ACTION_STATE_RECEIVED)) {
158 status = STATUS_DISTRIBUTED;
159 } else if (event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_SENT)) {
160 status = STATUS_DELETING;
161 } else if (event.isActionAndStateCd(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_STATE_RECEIVED) ||
162 event.isActionAndStateCd(CldsEvent.ACTION_RESTART, CldsEvent.ACTION_STATE_ANY) ||
163 event.isActionAndStateCd(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STATE_ANY)) {
164 status = STATUS_ACTIVE;
165 } else if (event.isActionAndStateCd(CldsEvent.ACTION_STOP, CldsEvent.ACTION_STATE_ANY)) {
166 status = STATUS_STOPPED;
172 * Get the actionCd from current event. If none, default value is CldsEvent.ACTION_CREATE
176 private String getCurrentActionCd() {
177 // current default actionCd is CREATE
178 String actionCd = CldsEvent.ACTION_CREATE;
179 if (event != null && event.getActionCd() != null) {
180 actionCd = event.getActionCd();
186 * Get the actionStateCd from current event. If none, default value is CldsEvent.ACTION_STATE_COMPLETED
190 private String getCurrentActionStateCd() {
191 // current default actionStateCd is CREATE
192 String actionStateCd = CldsEvent.ACTION_STATE_COMPLETED;
193 if (event != null && event.getActionStateCd() != null) {
194 actionStateCd = event.getActionStateCd();
196 return actionStateCd;
200 * Determine permittedActionCd list using the actionCd from the current event.
202 private void determinePermittedActionCd() {
203 String actionCd = getCurrentActionCd();
205 case CldsEvent.ACTION_CREATE:
206 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT);
208 case CldsEvent.ACTION_SUBMIT:
209 case CldsEvent.ACTION_RESUBMIT:
210 // for 1702 delete is not currently implemented (and resubmit requires manually deleting artifact from asdc
211 permittedActionCd = Arrays.asList(CldsEvent.ACTION_RESUBMIT);
213 case CldsEvent.ACTION_DISTRIBUTE:
214 case CldsEvent.ACTION_UNDEPLOY:
215 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE);
217 case CldsEvent.ACTION_DEPLOY:
218 case CldsEvent.ACTION_RESTART:
219 case CldsEvent.ACTION_UPDATE:
220 // for 1702 delete is not currently implemented
221 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
223 case CldsEvent.ACTION_DELETE:
224 if (getCurrentActionStateCd().equals(CldsEvent.ACTION_STATE_SENT)) {
225 permittedActionCd = Arrays.asList();
227 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT);
230 case CldsEvent.ACTION_STOP:
231 // for 1702 delete is not currently implemented
232 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART);
235 logger.warn("Invalid current actionCd: " + actionCd);
240 * Validate requestedActionCd - determine permittedActionCd and then check if contained in permittedActionCd
241 * Throw IllegalArgumentException if requested actionCd is not permitted.
243 * @param requestedActionCd
245 public void validateAction(String requestedActionCd) {
246 determinePermittedActionCd();
247 if (!permittedActionCd.contains(requestedActionCd)) {
248 throw new IllegalArgumentException("Invalid requestedActionCd: " + requestedActionCd + ". Given current actionCd: " + getCurrentActionCd() + ", the permittedActionCd: " + permittedActionCd);
253 * Extract the UUID portion of a given full control name (controlNamePrefix + controlNameUuid).
254 * No fields are populated other than controlNamePrefix and controlNameUuid.
255 * Throws BadRequestException if length of given control name is less than UUID_LENGTH.
257 * @param fullControlName
260 public static CldsModel createUsingControlName(String fullControlName) {
264 if (fullControlName != null) {
265 len = fullControlName.length();
267 if (len < UUID_LENGTH) {
268 throw new BadRequestException("closed loop id / control name length, " + len + ", less than the minimum of: " + UUID_LENGTH);
270 CldsModel model = new CldsModel();
271 model.setControlNamePrefix(fullControlName.substring(0, len - UUID_LENGTH));
272 model.setControlNameUuid(fullControlName.substring(len - UUID_LENGTH));
277 * @return the controlName (controlNamePrefix + controlNameUuid)
279 public String getControlName() {
280 return controlNamePrefix + controlNameUuid;
284 * To insert modelInstance to the database
289 public static CldsModel insertModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent, String userid) {
290 String controlName = dcaeEvent.getControlName();
291 CldsModel cldsModel = createUsingControlName(controlName);
292 cldsModel = cldsDao.getModelByUuid(cldsModel.getControlNameUuid());
293 cldsModel.determineStatus();
294 if (dcaeEvent.getCldsActionCd().equals(CldsEvent.ACTION_UNDEPLOY) ||
295 (dcaeEvent.getCldsActionCd().equals(CldsEvent.ACTION_DEPLOY) && (cldsModel.getStatus().equals(STATUS_DISTRIBUTED) || cldsModel.getStatus().equals(STATUS_DESIGN)))) {
296 CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userid, dcaeEvent.getCldsActionCd(), CldsEvent.ACTION_STATE_RECEIVED, null);
298 cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
303 * To remove modelInstance from the database
304 * This method is defunct - DCAE Proxy will not undeploy individual instances. It will send an empty list of
305 * deployed instances to indicate all have been removed. Or it will send an updated list to indicate those that
306 * are still deployed with any not on the list considered undeployed.
311 @SuppressWarnings("unused")
312 private static CldsModel removeModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent) {
313 String controlName = dcaeEvent.getControlName();
314 //cldsModel = cldsDao.delModelInstance(cldsModel.getControlNameUuid(), dcaeEvent.getInstances() );
315 return createUsingControlName(controlName);
321 public String getName() {
326 * @param name the name to set
328 public void setName(String name) {
332 public String getTemplateId() {
336 public void setTemplateId(String templateId) {
337 this.templateId = templateId;
341 * @return the controlNamePrefix
343 public String getControlNamePrefix() {
344 return controlNamePrefix;
348 * @param controlNamePrefix the controlNamePrefix to set
350 public void setControlNamePrefix(String controlNamePrefix) {
351 this.controlNamePrefix = controlNamePrefix;
355 * @return the controlNameUuid
357 public String getControlNameUuid() {
358 return controlNameUuid;
362 * @param controlNameUuid the controlNameUuid to set
364 public void setControlNameUuid(String controlNameUuid) {
365 this.controlNameUuid = controlNameUuid;
370 * @return the propUserid
372 public String getPropUserid() {
377 * @param propUserid the propUserid to set
379 public void setPropUserid(String propUserid) {
380 this.propUserid = propUserid;
384 * @return the propText
386 public String getPropText() {
391 * @param propText the propText to set
393 public void setPropText(String propText) {
394 this.propText = propText;
400 public CldsEvent getEvent() {
404 public String getId() {
408 public void setId(String id) {
412 public String getTemplateName() {
416 public void setTemplateName(String templateName) {
417 this.templateName = templateName;
420 public String getPropId() {
424 public void setPropId(String propId) {
425 this.propId = propId;
429 * @param event the event to set
431 public void setEvent(CldsEvent event) {
438 public String getStatus() {
443 * @param status the status to set
445 public void setStatus(String status) {
446 this.status = status;
450 * @return the permittedActionCd
452 public List<String> getPermittedActionCd() {
453 return permittedActionCd;
457 * @param permittedActionCd the permittedActionCd to set
459 public void setPermittedActionCd(List<String> permittedActionCd) {
460 this.permittedActionCd = permittedActionCd;
463 public String getBlueprintId() {
467 public void setBlueprintId(String blueprintId) {
468 this.blueprintId = blueprintId;
471 public String getBlueprintUserid() {
472 return blueprintUserid;
475 public void setBlueprintUserid(String blueprintUserid) {
476 this.blueprintUserid = blueprintUserid;
479 public String getBlueprintText() {
480 return blueprintText;
483 public void setBlueprintText(String blueprintText) {
484 this.blueprintText = blueprintText;
487 public String getBpmnId() {
491 public void setBpmnId(String bpmnId) {
492 this.bpmnId = bpmnId;
495 public String getBpmnUserid() {
499 public void setBpmnUserid(String bpmnUserid) {
500 this.bpmnUserid = bpmnUserid;
503 public String getBpmnText() {
507 public void setBpmnText(String bpmnText) {
508 this.bpmnText = bpmnText;
511 public String getImageId() {
515 public void setImageId(String imageId) {
516 this.imageId = imageId;
519 public String getImageUserid() {
523 public void setImageUserid(String imageUserid) {
524 this.imageUserid = imageUserid;
527 public String getImageText() {
531 public void setImageText(String imageText) {
532 this.imageText = imageText;
535 public String getDocId() {
539 public void setDocId(String docId) {
543 public String getDocUserid() {
547 public void setDocUserid(String docUserid) {
548 this.docUserid = docUserid;
551 public String getDocText() {
555 public void setDocText(String docText) {
556 this.docText = docText;
559 public List<CldsModelInstance> getCldsModelInstanceList() {
560 if (cldsModelInstanceList == null) {
561 cldsModelInstanceList = new ArrayList<>();
563 return cldsModelInstanceList;
566 public void setCldsModelInstanceList(List<CldsModelInstance> cldsModelInstanceList) {
567 this.cldsModelInstanceList = cldsModelInstanceList;