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/distribute */
101 if (event.isActionCd(CldsEvent.ACTION_SUBMIT) || event.isActionCd(CldsEvent.ACTION_RESUBMIT)
102 || event.isActionCd(CldsEvent.ACTION_DISTRIBUTE) || event.isActionCd(CldsEvent.ACTION_SUBMITDCAE)) {
111 public CldsModel save(CldsDao cldsDao, String userid) {
112 CldsModel cldsModel = cldsDao.setModel(this, userid);
114 determinePermittedActionCd();
119 * set the status in the model
121 private void determineStatus() {
122 status = STATUS_UNKNOWN;
123 if (event == null || event.getActionCd() == null) {
124 status = STATUS_DESIGN;
125 } else if (event.isActionStateCd(CldsEvent.ACTION_STATE_ERROR)) {
126 status = STATUS_ERROR;
127 } else if (event.isActionAndStateCd(CldsEvent.ACTION_CREATE, CldsEvent.ACTION_STATE_ANY)
128 || event.isActionAndStateCd(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_STATE_ANY)
129 || event.isActionAndStateCd(CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_STATE_ANY)
130 || event.isActionAndStateCd(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_STATE_ANY)
131 || event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_RECEIVED)
132 || event.isActionAndStateCd(CldsEvent.ACTION_MODIFY, CldsEvent.ACTION_STATE_ANY)) {
133 status = STATUS_DESIGN;
134 } else if (event.isActionAndStateCd(CldsEvent.ACTION_DISTRIBUTE, CldsEvent.ACTION_STATE_RECEIVED)
135 || event.isActionAndStateCd(CldsEvent.ACTION_UNDEPLOY, CldsEvent.ACTION_STATE_RECEIVED)) {
136 status = STATUS_DISTRIBUTED;
137 } else if (event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_SENT)) {
138 status = STATUS_DELETING;
139 } else if (event.isActionAndStateCd(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_STATE_RECEIVED)
140 || event.isActionAndStateCd(CldsEvent.ACTION_RESTART, CldsEvent.ACTION_STATE_ANY)
141 || event.isActionAndStateCd(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STATE_ANY)
142 || event.isActionAndStateCd(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_STATE_ANY)) {
143 status = STATUS_ACTIVE;
144 } else if (event.isActionAndStateCd(CldsEvent.ACTION_STOP, CldsEvent.ACTION_STATE_ANY)) {
145 status = STATUS_STOPPED;
150 * Get the actionCd from current event. If none, default value is
151 * CldsEvent.ACTION_CREATE
153 private String getCurrentActionCd() {
154 // current default actionCd is CREATE
155 String actionCd = CldsEvent.ACTION_CREATE;
156 if (event != null && event.getActionCd() != null) {
157 actionCd = event.getActionCd();
163 * Get the actionStateCd from current event. If none, default value is
164 * CldsEvent.ACTION_STATE_COMPLETED
166 private String getCurrentActionStateCd() {
167 // current default actionStateCd is CREATE
168 String actionStateCd = CldsEvent.ACTION_STATE_COMPLETED;
169 if (event != null && event.getActionStateCd() != null) {
170 actionStateCd = event.getActionStateCd();
172 return actionStateCd;
176 * Determine permittedActionCd list using the actionCd from the current
177 * event. It's a states graph, given the next action that can be executed
178 * from the one that has been executed (described in the event object).
179 * ACTION_CREATE being the first one.
181 private void determinePermittedActionCd() {
182 String actionCd = getCurrentActionCd();
184 case CldsEvent.ACTION_CREATE:
185 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_TEST,
186 CldsEvent.ACTION_DELETE);
187 if (isSimplifiedModel()) {
188 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_TEST,
189 CldsEvent.ACTION_DELETE);
192 case CldsEvent.ACTION_MODIFY:
193 permittedActionCd = Arrays.asList(CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_DELETE);
194 if (isSimplifiedModel()) {
195 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_DELETE);
198 case CldsEvent.ACTION_SUBMIT:
199 case CldsEvent.ACTION_RESUBMIT:
200 permittedActionCd = Arrays.asList(CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_DELETE);
202 case CldsEvent.ACTION_SUBMITDCAE:
203 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_DELETE);
205 case CldsEvent.ACTION_DISTRIBUTE:
206 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_RESUBMIT,
207 CldsEvent.ACTION_DELETE);
208 if (isSimplifiedModel()) {
209 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_SUBMITDCAE,
210 CldsEvent.ACTION_DELETE);
213 case CldsEvent.ACTION_UNDEPLOY:
214 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY,
215 CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_DELETE);
216 if (isSimplifiedModel()) {
217 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY,
218 CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_DELETE);
221 case CldsEvent.ACTION_DEPLOY:
222 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UNDEPLOY,
223 CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
225 case CldsEvent.ACTION_RESTART:
226 case CldsEvent.ACTION_UPDATE:
227 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UPDATE,
228 CldsEvent.ACTION_STOP, CldsEvent.ACTION_UNDEPLOY);
230 case CldsEvent.ACTION_STOP:
231 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART,
232 CldsEvent.ACTION_UNDEPLOY);
235 logger.warn("Invalid current actionCd: " + actionCd);
239 private boolean isSimplifiedModel() {
240 boolean result = false;
242 if (propText != null) {
243 JsonNode modelJson = JacksonUtils.getObjectMapperInstance().readTree(propText);
244 JsonNode simpleModelJson = modelJson.get("simpleModel");
245 if (simpleModelJson != null && simpleModelJson.asBoolean()) {
249 } catch (IOException e) {
250 logger.error("Error while parsing propText json", e);
256 * Validate requestedActionCd - determine permittedActionCd and then check
257 * if contained in permittedActionCd Throw IllegalArgumentException if
258 * requested actionCd is not permitted.
260 public void validateAction(String requestedActionCd) {
261 determinePermittedActionCd();
262 if (!permittedActionCd.contains(requestedActionCd)) {
263 throw new IllegalArgumentException(
264 "Invalid requestedActionCd: " + requestedActionCd + ". Given current actionCd: "
265 + getCurrentActionCd() + ", the permittedActionCd: " + permittedActionCd);
270 * Extract the UUID portion of a given full control name (controlNamePrefix
271 * + controlNameUuid). No fields are populated other than controlNamePrefix
272 * and controlNameUuid. Throws BadRequestException if length of given
273 * control name is less than UUID_LENGTH.
275 public static CldsModel createUsingControlName(String fullControlName) {
276 if (fullControlName == null || fullControlName.length() < UUID_LENGTH) {
277 throw new BadRequestException(
278 "closed loop id / control name length, " + (fullControlName != null ? fullControlName.length() : 0)
279 + ", less than the minimum of: " + UUID_LENGTH);
281 CldsModel model = new CldsModel();
282 model.setControlNamePrefix(fullControlName.substring(0, fullControlName.length() - UUID_LENGTH));
283 model.setControlNameUuid(fullControlName.substring(fullControlName.length() - UUID_LENGTH));
288 * @return the controlName (controlNamePrefix + controlNameUuid)
290 public String getControlName() {
291 return controlNamePrefix + controlNameUuid;
295 * To insert modelInstance to the database
297 public static CldsModel insertModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent, String userid) {
298 String controlName = dcaeEvent.getControlName();
299 CldsModel cldsModel = createUsingControlName(controlName);
300 cldsModel = cldsDao.getModelByUuid(cldsModel.getControlNameUuid());
301 cldsModel.determineStatus();
302 if (dcaeEvent.getCldsActionCd().equals(CldsEvent.ACTION_UNDEPLOY) || (dcaeEvent.getCldsActionCd()
303 .equals(CldsEvent.ACTION_DEPLOY)
304 && (cldsModel.getStatus().equals(STATUS_DISTRIBUTED) || cldsModel.getStatus().equals(STATUS_DESIGN)))) {
305 CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userid, dcaeEvent.getCldsActionCd(),
306 CldsEvent.ACTION_STATE_RECEIVED, null);
308 cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
315 public String getName() {
323 public void setName(String name) {
328 * @return the typeName
330 public String getTypeName() {
334 public void setTypeName(String typeName) {
335 this.typeName = typeName;
338 public String getTemplateId() {
342 public void setTemplateId(String templateId) {
343 this.templateId = templateId;
347 * @return the controlNamePrefix
349 public String getControlNamePrefix() {
350 return controlNamePrefix;
354 * @param controlNamePrefix
355 * the controlNamePrefix to set
357 public void setControlNamePrefix(String controlNamePrefix) {
358 this.controlNamePrefix = controlNamePrefix;
362 * @return the controlNameUuid
364 public String getControlNameUuid() {
365 return controlNameUuid;
369 * @param controlNameUuid
370 * the controlNameUuid to set
372 public void setControlNameUuid(String controlNameUuid) {
373 this.controlNameUuid = controlNameUuid;
377 * @return the propText
379 public String getPropText() {
385 * the propText to set
387 public void setPropText(String propText) {
388 this.propText = propText;
394 public CldsEvent getEvent() {
398 public String getId() {
402 public void setId(String id) {
406 public String getTemplateName() {
410 public void setTemplateName(String templateName) {
411 this.templateName = templateName;
418 public void setEvent(CldsEvent event) {
425 public String getStatus() {
433 public void setStatus(String status) {
434 this.status = status;
437 public String getBlueprintText() {
438 return blueprintText;
441 public void setBlueprintText(String blueprintText) {
442 this.blueprintText = blueprintText;
445 public String getBpmnText() {
449 public void setBpmnText(String bpmnText) {
450 this.bpmnText = bpmnText;
453 public String getImageText() {
457 public void setImageText(String imageText) {
458 this.imageText = imageText;
461 public String getDocText() {
465 public void setDocText(String docText) {
466 this.docText = docText;
469 public String getTypeId() {
473 public void setTypeId(String typeId) {
474 this.typeId = typeId;
477 public List<CldsModelInstance> getCldsModelInstanceList() {
478 if (cldsModelInstanceList == null) {
479 cldsModelInstanceList = new ArrayList<>();
481 return cldsModelInstanceList;
484 public String getDeploymentId() {
488 public void setDeploymentId(String deploymentId) {
489 this.deploymentId = deploymentId;
492 public List<String> getPermittedActionCd() {
493 return permittedActionCd;