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 || event.isActionAndStateCd(CldsEvent.ACTION_SUBMITPOLICY, CldsEvent.ACTION_STATE_ANY)) {
144 status = STATUS_ACTIVE;
145 } else if (event.isActionAndStateCd(CldsEvent.ACTION_STOP, CldsEvent.ACTION_STATE_ANY)) {
146 status = STATUS_STOPPED;
151 * Get the actionCd from current event. If none, default value is
152 * CldsEvent.ACTION_CREATE
154 private String getCurrentActionCd() {
155 // current default actionCd is CREATE
156 String actionCd = CldsEvent.ACTION_CREATE;
157 if (event != null && event.getActionCd() != null) {
158 actionCd = event.getActionCd();
164 * Get the actionStateCd from current event. If none, default value is
165 * CldsEvent.ACTION_STATE_COMPLETED
167 private String getCurrentActionStateCd() {
168 // current default actionStateCd is CREATE
169 String actionStateCd = CldsEvent.ACTION_STATE_COMPLETED;
170 if (event != null && event.getActionStateCd() != null) {
171 actionStateCd = event.getActionStateCd();
173 return actionStateCd;
177 * Determine permittedActionCd list using the actionCd from the current
178 * event. It's a states graph, given the next action that can be executed
179 * from the one that has been executed (described in the event object).
180 * ACTION_CREATE being the first one.
182 private void determinePermittedActionCd() {
183 String actionCd = getCurrentActionCd();
185 case CldsEvent.ACTION_CREATE:
186 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_TEST,
187 CldsEvent.ACTION_DELETE);
188 if (isSimplifiedModel()) {
189 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_SUBMITPOLICY,
190 CldsEvent.ACTION_TEST, CldsEvent.ACTION_DELETE);
193 case CldsEvent.ACTION_MODIFY:
194 permittedActionCd = Arrays.asList(CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_DELETE);
195 if (isSimplifiedModel()) {
196 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_SUBMITPOLICY,
197 CldsEvent.ACTION_DELETE);
200 case CldsEvent.ACTION_SUBMIT:
201 case CldsEvent.ACTION_RESUBMIT:
202 permittedActionCd = Arrays.asList(CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_DELETE);
204 case CldsEvent.ACTION_SUBMITDCAE:
205 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_DELETE);
207 case CldsEvent.ACTION_SUBMITPOLICY:
208 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
210 case CldsEvent.ACTION_DISTRIBUTE:
211 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_RESUBMIT,
212 CldsEvent.ACTION_DELETE);
213 if (isSimplifiedModel()) {
214 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_SUBMITDCAE,
215 CldsEvent.ACTION_DELETE);
218 case CldsEvent.ACTION_UNDEPLOY:
219 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY,
220 CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_DELETE);
221 if (isSimplifiedModel()) {
222 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY,
223 CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_DELETE);
226 case CldsEvent.ACTION_DEPLOY:
227 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UNDEPLOY,
228 CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
230 case CldsEvent.ACTION_RESTART:
231 case CldsEvent.ACTION_UPDATE:
232 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UPDATE,
233 CldsEvent.ACTION_STOP, CldsEvent.ACTION_UNDEPLOY);
234 if (isPolicyOnly()) {
235 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
238 case CldsEvent.ACTION_STOP:
239 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART,
240 CldsEvent.ACTION_UNDEPLOY);
241 if (isPolicyOnly()) {
242 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART,
243 CldsEvent.ACTION_DELETE);
247 logger.warn("Invalid current actionCd: " + actionCd);
251 private boolean isSimplifiedModel() {
252 boolean result = false;
254 if (propText != null) {
255 JsonNode modelJson = JacksonUtils.getObjectMapperInstance().readTree(propText);
256 JsonNode simpleModelJson = modelJson.get("simpleModel");
257 if (simpleModelJson != null && simpleModelJson.asBoolean()) {
261 } catch (IOException e) {
262 logger.error("Error while parsing propText json", e);
267 private boolean isPolicyOnly() {
268 boolean result = false;
270 if (propText != null) {
271 JsonNode modelJson = JacksonUtils.getObjectMapperInstance().readTree(propText);
272 JsonNode policyOnlyJson = modelJson.get("policyOnly");
273 if (policyOnlyJson != null && policyOnlyJson.asBoolean()) {
277 } catch (IOException e) {
278 logger.error("Error while parsing propText json", e);
284 * Validate requestedActionCd - determine permittedActionCd and then check
285 * if contained in permittedActionCd Throw IllegalArgumentException if
286 * requested actionCd is not permitted.
288 public void validateAction(String requestedActionCd) {
289 determinePermittedActionCd();
290 if (!permittedActionCd.contains(requestedActionCd)) {
291 throw new IllegalArgumentException(
292 "Invalid requestedActionCd: " + requestedActionCd + ". Given current actionCd: "
293 + getCurrentActionCd() + ", the permittedActionCd: " + permittedActionCd);
298 * Extract the UUID portion of a given full control name (controlNamePrefix
299 * + controlNameUuid). No fields are populated other than controlNamePrefix
300 * and controlNameUuid. Throws BadRequestException if length of given
301 * control name is less than UUID_LENGTH.
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
325 public static CldsModel insertModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent, String userid) {
326 String controlName = dcaeEvent.getControlName();
327 CldsModel cldsModel = createUsingControlName(controlName);
328 cldsModel = cldsDao.getModelByUuid(cldsModel.getControlNameUuid());
329 cldsModel.determineStatus();
330 if (dcaeEvent.getCldsActionCd().equals(CldsEvent.ACTION_UNDEPLOY) || (dcaeEvent.getCldsActionCd()
331 .equals(CldsEvent.ACTION_DEPLOY)
332 && (cldsModel.getStatus().equals(STATUS_DISTRIBUTED) || cldsModel.getStatus().equals(STATUS_DESIGN)))) {
333 CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userid, dcaeEvent.getCldsActionCd(),
334 CldsEvent.ACTION_STATE_RECEIVED, null);
336 cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
343 public String getName() {
351 public void setName(String name) {
356 * @return the typeName
358 public String getTypeName() {
362 public void setTypeName(String typeName) {
363 this.typeName = typeName;
366 public String getTemplateId() {
370 public void setTemplateId(String templateId) {
371 this.templateId = templateId;
375 * @return the controlNamePrefix
377 public String getControlNamePrefix() {
378 return controlNamePrefix;
382 * @param controlNamePrefix
383 * the controlNamePrefix to set
385 public void setControlNamePrefix(String controlNamePrefix) {
386 this.controlNamePrefix = controlNamePrefix;
390 * @return the controlNameUuid
392 public String getControlNameUuid() {
393 return controlNameUuid;
397 * @param controlNameUuid
398 * the controlNameUuid to set
400 public void setControlNameUuid(String controlNameUuid) {
401 this.controlNameUuid = controlNameUuid;
405 * @return the propText
407 public String getPropText() {
413 * the propText to set
415 public void setPropText(String propText) {
416 this.propText = propText;
422 public CldsEvent getEvent() {
426 public String getId() {
430 public void setId(String id) {
434 public String getTemplateName() {
438 public void setTemplateName(String templateName) {
439 this.templateName = templateName;
446 public void setEvent(CldsEvent event) {
453 public String getStatus() {
461 public void setStatus(String status) {
462 this.status = status;
465 public String getBlueprintText() {
466 return blueprintText;
469 public void setBlueprintText(String blueprintText) {
470 this.blueprintText = blueprintText;
473 public String getBpmnText() {
477 public void setBpmnText(String bpmnText) {
478 this.bpmnText = bpmnText;
481 public String getImageText() {
485 public void setImageText(String imageText) {
486 this.imageText = imageText;
489 public String getDocText() {
493 public void setDocText(String docText) {
494 this.docText = docText;
497 public String getTypeId() {
501 public void setTypeId(String typeId) {
502 this.typeId = typeId;
505 public List<CldsModelInstance> getCldsModelInstanceList() {
506 if (cldsModelInstanceList == null) {
507 cldsModelInstanceList = new ArrayList<>();
509 return cldsModelInstanceList;
512 public String getDeploymentId() {
516 public void setDeploymentId(String deploymentId) {
517 this.deploymentId = deploymentId;
520 public List<String> getPermittedActionCd() {
521 return permittedActionCd;