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 com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.List;
33 import javax.ws.rs.BadRequestException;
34 import javax.ws.rs.NotFoundException;
36 import org.onap.clamp.clds.dao.CldsDao;
39 * Represent a CLDS Model.
41 public class CldsModel {
42 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsModel.class);
43 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
45 private static final int UUID_LENGTH = 36;
47 public static final String STATUS_DESIGN = "DESIGN";
48 public static final String STATUS_DISTRIBUTED = "DISTRIBUTED";
49 public static final String STATUS_ACTIVE = "ACTIVE";
50 public static final String STATUS_STOPPED = "STOPPED";
51 public static final String STATUS_DELETING = "DELETING";
52 public static final String STATUS_ERROR = "ERROR"; // manual
55 public static final String STATUS_UNKNOWN = "UNKNOWN";
58 private String templateId;
59 private String templateName;
61 private String controlNamePrefix;
62 private String controlNameUuid;
63 private String bpmnId;
64 private String bpmnUserid;
65 private String bpmnText;
66 private String propId;
67 private String propUserid;
68 private String propText;
69 private String imageId;
70 private String imageUserid;
71 private String imageText;
73 private String docUserid;
74 private String docText;
75 private String blueprintId;
76 private String blueprintUserid;
77 private String blueprintText;
78 private CldsEvent event;
79 private String status;
80 private List<String> permittedActionCd;
81 private List<CldsModelInstance> cldsModelInstanceList;
83 private String typeId;
84 private String typeName;
86 private String dispatcherResponse;
88 private String deploymentId;
90 private boolean userAuthorizedToUpdate;
93 * Construct empty model.
96 event = new CldsEvent();
106 public static CldsModel retrieve(CldsDao cldsDao, String name, boolean okIfNotFound) {
108 CldsModel model = cldsDao.getModelTemplate(name);
109 if (model.getId() == null && !okIfNotFound) {
110 throw new NotFoundException();
112 model.determineStatus();
113 model.determinePermittedActionCd();
117 public boolean canInventoryCall() {
118 boolean canCall = false;
119 /* Below checks the clds event is submit/resubmit */
121 if ((event.isActionCd(CldsEvent.ACTION_SUBMIT) || event.isActionCd(CldsEvent.ACTION_RESUBMIT))) {
133 public void save(CldsDao cldsDao, String userid) {
134 cldsDao.setModel(this, userid);
136 determinePermittedActionCd();
140 * Insert a new event for the new action. Throw IllegalArgumentException if
141 * requested actionCd is not permitted.
146 * @param actionStateCd
148 public void insEvent(CldsDao cldsDao, String userid, String actionCd, String actionStateCd) {
149 validateAction(actionCd);
150 event = CldsEvent.insEvent(cldsDao, this, userid, actionCd, actionStateCd, null);
152 determinePermittedActionCd();
156 * Update event with processInstanceId
159 * @param processInstanceId
161 public void updEvent(CldsDao cldsDao, String processInstanceId) {
162 cldsDao.updEvent(event.getId(), processInstanceId);
166 * set the status in the model
168 private void determineStatus() {
170 status = STATUS_UNKNOWN;
171 if (event == null || event.getActionCd() == null) {
172 status = STATUS_DESIGN;
173 } else if (event.isActionStateCd(CldsEvent.ACTION_STATE_ERROR)) {
174 status = STATUS_ERROR;
175 } else if (event.isActionAndStateCd(CldsEvent.ACTION_CREATE, CldsEvent.ACTION_STATE_ANY)
176 || event.isActionAndStateCd(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_STATE_ANY)
177 || event.isActionAndStateCd(CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_STATE_ANY)
178 || event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_RECEIVED)) {
179 status = STATUS_DESIGN;
180 } else if (event.isActionAndStateCd(CldsEvent.ACTION_DISTRIBUTE, CldsEvent.ACTION_STATE_RECEIVED)
181 || event.isActionAndStateCd(CldsEvent.ACTION_UNDEPLOY, CldsEvent.ACTION_STATE_RECEIVED)) {
182 status = STATUS_DISTRIBUTED;
183 } else if (event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_SENT)) {
184 status = STATUS_DELETING;
185 } else if (event.isActionAndStateCd(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_STATE_RECEIVED)
186 || event.isActionAndStateCd(CldsEvent.ACTION_RESTART, CldsEvent.ACTION_STATE_ANY)
187 || event.isActionAndStateCd(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STATE_ANY)
188 || event.isActionAndStateCd(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_STATE_ANY)) {
189 status = STATUS_ACTIVE;
190 } else if (event.isActionAndStateCd(CldsEvent.ACTION_STOP, CldsEvent.ACTION_STATE_ANY)) {
191 status = STATUS_STOPPED;
197 * Get the actionCd from current event. If none, default value is
198 * CldsEvent.ACTION_CREATE
202 private String getCurrentActionCd() {
203 // current default actionCd is CREATE
204 String actionCd = CldsEvent.ACTION_CREATE;
205 if (event != null && event.getActionCd() != null) {
206 actionCd = event.getActionCd();
212 * Get the actionStateCd from current event. If none, default value is
213 * CldsEvent.ACTION_STATE_COMPLETED
217 private String getCurrentActionStateCd() {
218 // current default actionStateCd is CREATE
219 String actionStateCd = CldsEvent.ACTION_STATE_COMPLETED;
220 if (event != null && event.getActionStateCd() != null) {
221 actionStateCd = event.getActionStateCd();
223 return actionStateCd;
227 * Determine permittedActionCd list using the actionCd from the current
228 * event. It's a states graph, given the next action that can be executed
229 * from the one that has been executed (described in the event object).
230 * ACTION_CREATE being the first one.
232 private void determinePermittedActionCd() {
233 String actionCd = getCurrentActionCd();
235 case CldsEvent.ACTION_CREATE:
236 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_TEST);
238 case CldsEvent.ACTION_SUBMIT:
239 case CldsEvent.ACTION_RESUBMIT:
240 // for 1702 delete is not currently implemented (and resubmit
241 // requires manually deleting artifact from sdc
242 permittedActionCd = Arrays.asList(CldsEvent.ACTION_RESUBMIT);
244 case CldsEvent.ACTION_DISTRIBUTE:
245 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_RESUBMIT);
247 case CldsEvent.ACTION_UNDEPLOY:
248 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY,
249 CldsEvent.ACTION_RESUBMIT);
251 case CldsEvent.ACTION_DEPLOY:
252 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UNDEPLOY,
253 CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
255 case CldsEvent.ACTION_RESTART:
256 case CldsEvent.ACTION_UPDATE:
257 // for 1702 delete is not currently implemented
258 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UPDATE,
259 CldsEvent.ACTION_STOP, CldsEvent.ACTION_UNDEPLOY);
261 case CldsEvent.ACTION_DELETE:
262 if (getCurrentActionStateCd().equals(CldsEvent.ACTION_STATE_SENT)) {
263 permittedActionCd = Arrays.asList();
265 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT);
268 case CldsEvent.ACTION_STOP:
269 // for 1702 delete is not currently implemented
270 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART,
271 CldsEvent.ACTION_UNDEPLOY);
274 logger.warn("Invalid current actionCd: " + actionCd);
279 * Validate requestedActionCd - determine permittedActionCd and then check
280 * if contained in permittedActionCd Throw IllegalArgumentException if
281 * requested actionCd is not permitted.
283 * @param requestedActionCd
285 public void validateAction(String requestedActionCd) {
286 determinePermittedActionCd();
287 if (!permittedActionCd.contains(requestedActionCd)) {
288 throw new IllegalArgumentException(
289 "Invalid requestedActionCd: " + requestedActionCd + ". Given current actionCd: "
290 + getCurrentActionCd() + ", the permittedActionCd: " + permittedActionCd);
295 * Extract the UUID portion of a given full control name (controlNamePrefix
296 * + controlNameUuid). No fields are populated other than controlNamePrefix
297 * and controlNameUuid. Throws BadRequestException if length of given
298 * control name is less than UUID_LENGTH.
300 * @param fullControlName
303 public static CldsModel createUsingControlName(String fullControlName) {
304 if (fullControlName == null || fullControlName.length() < UUID_LENGTH) {
305 throw new BadRequestException(
306 "closed loop id / control name length, " + (fullControlName != null ? fullControlName.length() : 0)
307 + ", less than the minimum of: " + UUID_LENGTH);
309 CldsModel model = new CldsModel();
310 model.setControlNamePrefix(fullControlName.substring(0, fullControlName.length() - UUID_LENGTH));
311 model.setControlNameUuid(fullControlName.substring(fullControlName.length() - UUID_LENGTH));
316 * @return the controlName (controlNamePrefix + controlNameUuid)
318 public String getControlName() {
319 return controlNamePrefix + controlNameUuid;
323 * To insert modelInstance to the database
328 public static CldsModel insertModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent, String userid) {
329 String controlName = dcaeEvent.getControlName();
330 CldsModel cldsModel = createUsingControlName(controlName);
331 cldsModel = cldsDao.getModelByUuid(cldsModel.getControlNameUuid());
332 cldsModel.determineStatus();
333 if (dcaeEvent.getCldsActionCd().equals(CldsEvent.ACTION_UNDEPLOY) || (dcaeEvent.getCldsActionCd()
334 .equals(CldsEvent.ACTION_DEPLOY)
335 && (cldsModel.getStatus().equals(STATUS_DISTRIBUTED) || cldsModel.getStatus().equals(STATUS_DESIGN)))) {
336 CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userid, dcaeEvent.getCldsActionCd(),
337 CldsEvent.ACTION_STATE_RECEIVED, null);
339 cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
344 * To remove modelInstance from the database This method is defunct - DCAE
345 * Proxy will not undeploy individual instances. It will send an empty list
346 * of deployed instances to indicate all have been removed. Or it will send
347 * an updated list to indicate those that are still deployed with any not on
348 * the list considered undeployed.
353 @SuppressWarnings("unused")
354 private static CldsModel removeModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent) {
355 String controlName = dcaeEvent.getControlName();
356 // cldsModel = cldsDao.delModelInstance(cldsModel.getControlNameUuid(),
357 // dcaeEvent.getInstances() );
358 return createUsingControlName(controlName);
364 public String getName() {
372 public void setName(String name) {
377 * @return the typeName
379 public String getTypeName() {
385 * the typeName to set
387 public void setTypeName(String typeName) {
388 this.typeName = typeName;
391 public String getTemplateId() {
395 public void setTemplateId(String templateId) {
396 this.templateId = templateId;
400 * @return the controlNamePrefix
402 public String getControlNamePrefix() {
403 return controlNamePrefix;
407 * @param controlNamePrefix
408 * the controlNamePrefix to set
410 public void setControlNamePrefix(String controlNamePrefix) {
411 this.controlNamePrefix = controlNamePrefix;
415 * @return the controlNameUuid
417 public String getControlNameUuid() {
418 return controlNameUuid;
422 * @param controlNameUuid
423 * the controlNameUuid to set
425 public void setControlNameUuid(String controlNameUuid) {
426 this.controlNameUuid = controlNameUuid;
430 * @return the propUserid
432 public String getPropUserid() {
438 * the propUserid to set
440 public void setPropUserid(String propUserid) {
441 this.propUserid = propUserid;
445 * @return the propText
447 public String getPropText() {
453 * the propText to set
455 public void setPropText(String propText) {
456 this.propText = propText;
462 public CldsEvent getEvent() {
466 public String getId() {
470 public void setId(String id) {
474 public String getTemplateName() {
478 public void setTemplateName(String templateName) {
479 this.templateName = templateName;
482 public String getPropId() {
486 public void setPropId(String propId) {
487 this.propId = propId;
494 public void setEvent(CldsEvent event) {
501 public String getStatus() {
509 public void setStatus(String status) {
510 this.status = status;
514 * @return the permittedActionCd
516 public List<String> getPermittedActionCd() {
517 return permittedActionCd;
521 * @param permittedActionCd
522 * the permittedActionCd to set
524 public void setPermittedActionCd(List<String> permittedActionCd) {
525 this.permittedActionCd = permittedActionCd;
528 public String getBlueprintId() {
532 public void setBlueprintId(String blueprintId) {
533 this.blueprintId = blueprintId;
536 public String getBlueprintUserid() {
537 return blueprintUserid;
540 public void setBlueprintUserid(String blueprintUserid) {
541 this.blueprintUserid = blueprintUserid;
544 public String getBlueprintText() {
545 return blueprintText;
548 public void setBlueprintText(String blueprintText) {
549 this.blueprintText = blueprintText;
552 public String getBpmnId() {
556 public void setBpmnId(String bpmnId) {
557 this.bpmnId = bpmnId;
560 public String getBpmnUserid() {
564 public void setBpmnUserid(String bpmnUserid) {
565 this.bpmnUserid = bpmnUserid;
568 public String getBpmnText() {
572 public void setBpmnText(String bpmnText) {
573 this.bpmnText = bpmnText;
576 public String getImageId() {
580 public void setImageId(String imageId) {
581 this.imageId = imageId;
584 public String getImageUserid() {
588 public void setImageUserid(String imageUserid) {
589 this.imageUserid = imageUserid;
592 public String getImageText() {
596 public void setImageText(String imageText) {
597 this.imageText = imageText;
600 public String getDocId() {
604 public void setDocId(String docId) {
608 public String getDocUserid() {
612 public void setDocUserid(String docUserid) {
613 this.docUserid = docUserid;
616 public String getDocText() {
620 public void setDocText(String docText) {
621 this.docText = docText;
624 public String getTypeId() {
628 public void setTypeId(String typeId) {
629 this.typeId = typeId;
632 public List<CldsModelInstance> getCldsModelInstanceList() {
633 if (cldsModelInstanceList == null) {
634 cldsModelInstanceList = new ArrayList<>();
636 return cldsModelInstanceList;
639 public void setCldsModelInstanceList(List<CldsModelInstance> cldsModelInstanceList) {
640 this.cldsModelInstanceList = cldsModelInstanceList;
643 public void setDispatcherResponse(String dispatcherResponse) {
644 this.dispatcherResponse = dispatcherResponse;
648 public String getDispatcherResponse() {
649 return this.dispatcherResponse;
652 public String getDeploymentId() {
656 public void setDeploymentId(String deploymentId) {
657 this.deploymentId = deploymentId;
660 public boolean isUserAuthorizedToUpdate() {
661 return userAuthorizedToUpdate;
664 public void setUserAuthorizedToUpdate(boolean userAuthorizedToUpdate) {
665 this.userAuthorizedToUpdate = userAuthorizedToUpdate;