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 {
43 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsModel.class);
44 private static final int UUID_LENGTH = 36;
45 private static final String STATUS_DESIGN = "DESIGN";
46 private static final String STATUS_DISTRIBUTED = "DISTRIBUTED";
47 private static final String STATUS_ACTIVE = "ACTIVE";
48 private static final String STATUS_STOPPED = "STOPPED";
49 private static final String STATUS_DELETING = "DELETING";
50 private static final String STATUS_ERROR = "ERROR";
51 private 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 bpmnText;
59 private String propText;
60 private String imageText;
61 private String docText;
62 private String blueprintText;
63 private CldsEvent event;
64 private String status;
65 private List<String> permittedActionCd;
66 private List<CldsModelInstance> cldsModelInstanceList;
67 private String typeId;
68 private String typeName;
69 private String deploymentId;
72 * Construct empty model.
75 event = new CldsEvent();
81 public static CldsModel retrieve(CldsDao cldsDao, String name, boolean okIfNotFound) {
83 CldsModel model = cldsDao.getModelTemplate(name);
84 if (model.getId() == null && !okIfNotFound) {
85 throw new NotFoundException();
87 model.determineStatus();
88 model.determinePermittedActionCd();
92 public boolean canInventoryCall() {
93 boolean canCall = false;
94 /* Below checks the clds event is submit/resubmit */
96 if ((event.isActionCd(CldsEvent.ACTION_SUBMIT) || event.isActionCd(CldsEvent.ACTION_RESUBMIT))) {
105 public void save(CldsDao cldsDao, String userid) {
106 cldsDao.setModel(this, userid);
108 determinePermittedActionCd();
112 * set the status in the model
114 private void determineStatus() {
116 status = STATUS_UNKNOWN;
117 if (event == null || event.getActionCd() == null) {
118 status = STATUS_DESIGN;
119 } else if (event.isActionStateCd(CldsEvent.ACTION_STATE_ERROR)) {
120 status = STATUS_ERROR;
121 } else if (event.isActionAndStateCd(CldsEvent.ACTION_CREATE, CldsEvent.ACTION_STATE_ANY)
122 || event.isActionAndStateCd(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_STATE_ANY)
123 || event.isActionAndStateCd(CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_STATE_ANY)
124 || event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_RECEIVED)) {
125 status = STATUS_DESIGN;
126 } else if (event.isActionAndStateCd(CldsEvent.ACTION_DISTRIBUTE, CldsEvent.ACTION_STATE_RECEIVED)
127 || event.isActionAndStateCd(CldsEvent.ACTION_UNDEPLOY, CldsEvent.ACTION_STATE_RECEIVED)) {
128 status = STATUS_DISTRIBUTED;
129 } else if (event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_SENT)) {
130 status = STATUS_DELETING;
131 } else if (event.isActionAndStateCd(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_STATE_RECEIVED)
132 || event.isActionAndStateCd(CldsEvent.ACTION_RESTART, CldsEvent.ACTION_STATE_ANY)
133 || event.isActionAndStateCd(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STATE_ANY)
134 || event.isActionAndStateCd(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_STATE_ANY)) {
135 status = STATUS_ACTIVE;
136 } else if (event.isActionAndStateCd(CldsEvent.ACTION_STOP, CldsEvent.ACTION_STATE_ANY)) {
137 status = STATUS_STOPPED;
143 * Get the actionCd from current event. If none, default value is
144 * CldsEvent.ACTION_CREATE
146 private String getCurrentActionCd() {
147 // current default actionCd is CREATE
148 String actionCd = CldsEvent.ACTION_CREATE;
149 if (event != null && event.getActionCd() != null) {
150 actionCd = event.getActionCd();
156 * Get the actionStateCd from current event. If none, default value is
157 * CldsEvent.ACTION_STATE_COMPLETED
159 private String getCurrentActionStateCd() {
160 // current default actionStateCd is CREATE
161 String actionStateCd = CldsEvent.ACTION_STATE_COMPLETED;
162 if (event != null && event.getActionStateCd() != null) {
163 actionStateCd = event.getActionStateCd();
165 return actionStateCd;
169 * Determine permittedActionCd list using the actionCd from the current
170 * event. It's a states graph, given the next action that can be executed
171 * from the one that has been executed (described in the event object).
172 * ACTION_CREATE being the first one.
174 private void determinePermittedActionCd() {
175 String actionCd = getCurrentActionCd();
177 case CldsEvent.ACTION_CREATE:
178 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_TEST);
180 case CldsEvent.ACTION_SUBMIT:
181 case CldsEvent.ACTION_RESUBMIT:
182 // for 1702 delete is not currently implemented (and resubmit
183 // requires manually deleting artifact from sdc
184 permittedActionCd = Arrays.asList(CldsEvent.ACTION_RESUBMIT);
186 case CldsEvent.ACTION_DISTRIBUTE:
187 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_RESUBMIT);
189 case CldsEvent.ACTION_UNDEPLOY:
190 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY,
191 CldsEvent.ACTION_RESUBMIT);
193 case CldsEvent.ACTION_DEPLOY:
194 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UNDEPLOY,
195 CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
197 case CldsEvent.ACTION_RESTART:
198 case CldsEvent.ACTION_UPDATE:
199 // for 1702 delete is not currently implemented
200 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UPDATE,
201 CldsEvent.ACTION_STOP, CldsEvent.ACTION_UNDEPLOY);
203 case CldsEvent.ACTION_DELETE:
204 if (getCurrentActionStateCd().equals(CldsEvent.ACTION_STATE_SENT)) {
205 permittedActionCd = Arrays.asList();
207 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT);
210 case CldsEvent.ACTION_STOP:
211 // for 1702 delete is not currently implemented
212 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART,
213 CldsEvent.ACTION_UNDEPLOY);
216 logger.warn("Invalid current actionCd: " + actionCd);
221 * Validate requestedActionCd - determine permittedActionCd and then check
222 * if contained in permittedActionCd Throw IllegalArgumentException if
223 * requested actionCd is not permitted.
225 public void validateAction(String requestedActionCd) {
226 determinePermittedActionCd();
227 if (!permittedActionCd.contains(requestedActionCd)) {
228 throw new IllegalArgumentException(
229 "Invalid requestedActionCd: " + requestedActionCd + ". Given current actionCd: "
230 + getCurrentActionCd() + ", the permittedActionCd: " + permittedActionCd);
235 * Extract the UUID portion of a given full control name (controlNamePrefix
236 * + controlNameUuid). No fields are populated other than controlNamePrefix
237 * and controlNameUuid. Throws BadRequestException if length of given
238 * control name is less than UUID_LENGTH.
240 public static CldsModel createUsingControlName(String fullControlName) {
241 if (fullControlName == null || fullControlName.length() < UUID_LENGTH) {
242 throw new BadRequestException(
243 "closed loop id / control name length, " + (fullControlName != null ? fullControlName.length() : 0)
244 + ", less than the minimum of: " + UUID_LENGTH);
246 CldsModel model = new CldsModel();
247 model.setControlNamePrefix(fullControlName.substring(0, fullControlName.length() - UUID_LENGTH));
248 model.setControlNameUuid(fullControlName.substring(fullControlName.length() - UUID_LENGTH));
253 * @return the controlName (controlNamePrefix + controlNameUuid)
255 public String getControlName() {
256 return controlNamePrefix + controlNameUuid;
260 * To insert modelInstance to the database
262 public static CldsModel insertModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent, String userid) {
263 String controlName = dcaeEvent.getControlName();
264 CldsModel cldsModel = createUsingControlName(controlName);
265 cldsModel = cldsDao.getModelByUuid(cldsModel.getControlNameUuid());
266 cldsModel.determineStatus();
267 if (dcaeEvent.getCldsActionCd().equals(CldsEvent.ACTION_UNDEPLOY) || (dcaeEvent.getCldsActionCd()
268 .equals(CldsEvent.ACTION_DEPLOY)
269 && (cldsModel.getStatus().equals(STATUS_DISTRIBUTED) || cldsModel.getStatus().equals(STATUS_DESIGN)))) {
270 CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userid, dcaeEvent.getCldsActionCd(),
271 CldsEvent.ACTION_STATE_RECEIVED, null);
273 cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
280 public String getName() {
288 public void setName(String name) {
293 * @return the typeName
295 public String getTypeName() {
299 public void setTypeName(String typeName) {
300 this.typeName = typeName;
303 public String getTemplateId() {
307 public void setTemplateId(String templateId) {
308 this.templateId = templateId;
312 * @return the controlNamePrefix
314 public String getControlNamePrefix() {
315 return controlNamePrefix;
319 * @param controlNamePrefix
320 * the controlNamePrefix to set
322 public void setControlNamePrefix(String controlNamePrefix) {
323 this.controlNamePrefix = controlNamePrefix;
327 * @return the controlNameUuid
329 public String getControlNameUuid() {
330 return controlNameUuid;
334 * @param controlNameUuid
335 * the controlNameUuid to set
337 public void setControlNameUuid(String controlNameUuid) {
338 this.controlNameUuid = controlNameUuid;
342 * @return the propText
344 public String getPropText() {
350 * the propText to set
352 public void setPropText(String propText) {
353 this.propText = propText;
359 public CldsEvent getEvent() {
363 public String getId() {
367 public void setId(String id) {
371 public String getTemplateName() {
375 public void setTemplateName(String templateName) {
376 this.templateName = templateName;
383 public void setEvent(CldsEvent event) {
390 public String getStatus() {
398 public void setStatus(String status) {
399 this.status = status;
402 public String getBlueprintText() {
403 return blueprintText;
406 public void setBlueprintText(String blueprintText) {
407 this.blueprintText = blueprintText;
410 public String getBpmnText() {
414 public void setBpmnText(String bpmnText) {
415 this.bpmnText = bpmnText;
418 public String getImageText() {
422 public void setImageText(String imageText) {
423 this.imageText = imageText;
426 public String getDocText() {
430 public void setDocText(String docText) {
431 this.docText = docText;
434 public String getTypeId() {
438 public void setTypeId(String typeId) {
439 this.typeId = typeId;
442 public List<CldsModelInstance> getCldsModelInstanceList() {
443 if (cldsModelInstanceList == null) {
444 cldsModelInstanceList = new ArrayList<>();
446 return cldsModelInstanceList;
449 public String getDeploymentId() {
453 public void setDeploymentId(String deploymentId) {
454 this.deploymentId = deploymentId;