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;
28 import com.fasterxml.jackson.databind.JsonNode;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
35 import javax.ws.rs.BadRequestException;
36 import javax.ws.rs.NotFoundException;
38 import org.onap.clamp.clds.dao.CldsDao;
39 import org.onap.clamp.clds.util.JacksonUtils;
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 private static final String STATUS_DESIGN = "DESIGN";
49 private static final String STATUS_DISTRIBUTED = "DISTRIBUTED";
50 private static final String STATUS_ACTIVE = "ACTIVE";
51 private static final String STATUS_STOPPED = "STOPPED";
52 private static final String STATUS_DELETING = "DELETING";
53 private static final String STATUS_ERROR = "ERROR";
54 private 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;
71 * The service type Id received from DCAE by querying it
73 private String typeId;
74 private String typeName;
75 private String deploymentId;
78 * Construct empty model.
81 event = new CldsEvent();
87 public static CldsModel retrieve(CldsDao cldsDao, String name, boolean okIfNotFound) {
89 CldsModel model = cldsDao.getModelTemplate(name);
90 if (model.getId() == null && !okIfNotFound) {
91 throw new NotFoundException();
93 model.determineStatus();
94 model.determinePermittedActionCd();
98 public boolean canInventoryCall() {
99 boolean canCall = false;
100 /* Below checks the clds event is submit/resubmit */
101 if ((event.isActionCd(CldsEvent.ACTION_SUBMIT) || event.isActionCd(CldsEvent.ACTION_RESUBMIT)
102 || event.isActionCd(CldsEvent.ACTION_SUBMITDCAE))) {
111 public void save(CldsDao cldsDao, String userid) {
112 cldsDao.setModel(this, userid);
114 determinePermittedActionCd();
118 * set the status in the model
120 private void determineStatus() {
121 status = STATUS_UNKNOWN;
122 if (event == null || event.getActionCd() == null) {
123 status = STATUS_DESIGN;
124 } else if (event.isActionStateCd(CldsEvent.ACTION_STATE_ERROR)) {
125 status = STATUS_ERROR;
126 } else if (event.isActionAndStateCd(CldsEvent.ACTION_CREATE, CldsEvent.ACTION_STATE_ANY)
127 || event.isActionAndStateCd(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_STATE_ANY)
128 || event.isActionAndStateCd(CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_STATE_ANY)
129 || event.isActionAndStateCd(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_STATE_ANY)
130 || event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_RECEIVED)) {
131 status = STATUS_DESIGN;
132 } else if (event.isActionAndStateCd(CldsEvent.ACTION_DISTRIBUTE, CldsEvent.ACTION_STATE_RECEIVED)
133 || event.isActionAndStateCd(CldsEvent.ACTION_UNDEPLOY, CldsEvent.ACTION_STATE_RECEIVED)) {
134 status = STATUS_DISTRIBUTED;
135 } else if (event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_SENT)) {
136 status = STATUS_DELETING;
137 } else if (event.isActionAndStateCd(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_STATE_RECEIVED)
138 || event.isActionAndStateCd(CldsEvent.ACTION_RESTART, CldsEvent.ACTION_STATE_ANY)
139 || event.isActionAndStateCd(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STATE_ANY)
140 || event.isActionAndStateCd(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_STATE_ANY)) {
141 status = STATUS_ACTIVE;
142 } else if (event.isActionAndStateCd(CldsEvent.ACTION_STOP, CldsEvent.ACTION_STATE_ANY)) {
143 status = STATUS_STOPPED;
148 * Get the actionCd from current event. If none, default value is
149 * CldsEvent.ACTION_CREATE
151 private String getCurrentActionCd() {
152 // current default actionCd is CREATE
153 String actionCd = CldsEvent.ACTION_CREATE;
154 if (event != null && event.getActionCd() != null) {
155 actionCd = event.getActionCd();
161 * Get the actionStateCd from current event. If none, default value is
162 * CldsEvent.ACTION_STATE_COMPLETED
164 private String getCurrentActionStateCd() {
165 // current default actionStateCd is CREATE
166 String actionStateCd = CldsEvent.ACTION_STATE_COMPLETED;
167 if (event != null && event.getActionStateCd() != null) {
168 actionStateCd = event.getActionStateCd();
170 return actionStateCd;
174 * Determine permittedActionCd list using the actionCd from the current
175 * event. It's a states graph, given the next action that can be executed
176 * from the one that has been executed (described in the event object).
177 * ACTION_CREATE being the first one.
179 private void determinePermittedActionCd() {
180 String actionCd = getCurrentActionCd();
182 case CldsEvent.ACTION_CREATE:
183 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_TEST);
184 if (isSimplifiedModel()) {
185 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_TEST);
188 case CldsEvent.ACTION_SUBMIT:
189 case CldsEvent.ACTION_RESUBMIT:
190 // for 1702 delete is not currently implemented (and resubmit
191 // requires manually deleting artifact from sdc
192 permittedActionCd = Arrays.asList(CldsEvent.ACTION_RESUBMIT);
194 case CldsEvent.ACTION_SUBMITDCAE:
195 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE);
197 case CldsEvent.ACTION_DISTRIBUTE:
198 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_RESUBMIT);
199 if (isSimplifiedModel()) {
200 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_SUBMITDCAE);
203 case CldsEvent.ACTION_UNDEPLOY:
204 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY,
205 CldsEvent.ACTION_RESUBMIT);
206 if (isSimplifiedModel()) {
207 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY,
208 CldsEvent.ACTION_SUBMITDCAE);
211 case CldsEvent.ACTION_DEPLOY:
212 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UNDEPLOY,
213 CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
215 case CldsEvent.ACTION_RESTART:
216 case CldsEvent.ACTION_UPDATE:
217 // for 1702 delete is not currently implemented
218 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UPDATE,
219 CldsEvent.ACTION_STOP, CldsEvent.ACTION_UNDEPLOY);
221 case CldsEvent.ACTION_DELETE:
222 if (getCurrentActionStateCd().equals(CldsEvent.ACTION_STATE_SENT)) {
223 permittedActionCd = Arrays.asList();
225 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT);
228 case CldsEvent.ACTION_STOP:
229 // for 1702 delete is not currently implemented
230 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART,
231 CldsEvent.ACTION_UNDEPLOY);
234 logger.warn("Invalid current actionCd: " + actionCd);
238 private boolean isSimplifiedModel() {
239 boolean result = false;
241 if (propText != null) {
242 JsonNode modelJson = JacksonUtils.getObjectMapperInstance().readTree(propText);
243 JsonNode simpleModelJson = modelJson.get("simpleModel");
244 if (simpleModelJson != null && simpleModelJson.asBoolean()) {
248 } catch (IOException e) {
249 logger.error("Error while parsing propText json", e);
255 * Validate requestedActionCd - determine permittedActionCd and then check
256 * if contained in permittedActionCd Throw IllegalArgumentException if
257 * requested actionCd is not permitted.
259 public void validateAction(String requestedActionCd) {
260 determinePermittedActionCd();
261 if (!permittedActionCd.contains(requestedActionCd)) {
262 throw new IllegalArgumentException(
263 "Invalid requestedActionCd: " + requestedActionCd + ". Given current actionCd: "
264 + getCurrentActionCd() + ", the permittedActionCd: " + permittedActionCd);
269 * Extract the UUID portion of a given full control name (controlNamePrefix
270 * + controlNameUuid). No fields are populated other than controlNamePrefix
271 * and controlNameUuid. Throws BadRequestException if length of given
272 * control name is less than UUID_LENGTH.
274 public static CldsModel createUsingControlName(String fullControlName) {
275 if (fullControlName == null || fullControlName.length() < UUID_LENGTH) {
276 throw new BadRequestException(
277 "closed loop id / control name length, " + (fullControlName != null ? fullControlName.length() : 0)
278 + ", less than the minimum of: " + UUID_LENGTH);
280 CldsModel model = new CldsModel();
281 model.setControlNamePrefix(fullControlName.substring(0, fullControlName.length() - UUID_LENGTH));
282 model.setControlNameUuid(fullControlName.substring(fullControlName.length() - UUID_LENGTH));
287 * @return the controlName (controlNamePrefix + controlNameUuid)
289 public String getControlName() {
290 return controlNamePrefix + controlNameUuid;
294 * To insert modelInstance to the database
296 public static CldsModel insertModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent, String userid) {
297 String controlName = dcaeEvent.getControlName();
298 CldsModel cldsModel = createUsingControlName(controlName);
299 cldsModel = cldsDao.getModelByUuid(cldsModel.getControlNameUuid());
300 cldsModel.determineStatus();
301 if (dcaeEvent.getCldsActionCd().equals(CldsEvent.ACTION_UNDEPLOY) || (dcaeEvent.getCldsActionCd()
302 .equals(CldsEvent.ACTION_DEPLOY)
303 && (cldsModel.getStatus().equals(STATUS_DISTRIBUTED) || cldsModel.getStatus().equals(STATUS_DESIGN)))) {
304 CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userid, dcaeEvent.getCldsActionCd(),
305 CldsEvent.ACTION_STATE_RECEIVED, null);
307 cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
314 public String getName() {
322 public void setName(String name) {
327 * @return the typeName
329 public String getTypeName() {
333 public void setTypeName(String typeName) {
334 this.typeName = typeName;
337 public String getTemplateId() {
341 public void setTemplateId(String templateId) {
342 this.templateId = templateId;
346 * @return the controlNamePrefix
348 public String getControlNamePrefix() {
349 return controlNamePrefix;
353 * @param controlNamePrefix
354 * the controlNamePrefix to set
356 public void setControlNamePrefix(String controlNamePrefix) {
357 this.controlNamePrefix = controlNamePrefix;
361 * @return the controlNameUuid
363 public String getControlNameUuid() {
364 return controlNameUuid;
368 * @param controlNameUuid
369 * the controlNameUuid to set
371 public void setControlNameUuid(String controlNameUuid) {
372 this.controlNameUuid = controlNameUuid;
376 * @return the propText
378 public String getPropText() {
384 * the propText to set
386 public void setPropText(String propText) {
387 this.propText = propText;
393 public CldsEvent getEvent() {
397 public String getId() {
401 public void setId(String id) {
405 public String getTemplateName() {
409 public void setTemplateName(String templateName) {
410 this.templateName = templateName;
417 public void setEvent(CldsEvent event) {
424 public String getStatus() {
432 public void setStatus(String status) {
433 this.status = status;
436 public String getBlueprintText() {
437 return blueprintText;
440 public void setBlueprintText(String blueprintText) {
441 this.blueprintText = blueprintText;
444 public String getBpmnText() {
448 public void setBpmnText(String bpmnText) {
449 this.bpmnText = bpmnText;
452 public String getImageText() {
456 public void setImageText(String imageText) {
457 this.imageText = imageText;
460 public String getDocText() {
464 public void setDocText(String docText) {
465 this.docText = docText;
468 public String getTypeId() {
472 public void setTypeId(String typeId) {
473 this.typeId = typeId;
476 public List<CldsModelInstance> getCldsModelInstanceList() {
477 if (cldsModelInstanceList == null) {
478 cldsModelInstanceList = new ArrayList<>();
480 return cldsModelInstanceList;
483 public String getDeploymentId() {
487 public void setDeploymentId(String deploymentId) {
488 this.deploymentId = deploymentId;
491 public List<String> getPermittedActionCd() {
492 return permittedActionCd;