Docker-compose for heat integration lab
[clamp.git] / src / main / java / org / onap / clamp / clds / model / CldsModel.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  *
22  */
23
24 package org.onap.clamp.clds.model;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.databind.JsonNode;
29
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34
35 import javax.ws.rs.BadRequestException;
36 import javax.ws.rs.NotFoundException;
37
38 import org.onap.clamp.clds.dao.CldsDao;
39 import org.onap.clamp.clds.util.JacksonUtils;
40
41 /**
42  * Represent a CLDS Model.
43  */
44 public class CldsModel {
45
46     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsModel.class);
47     private static final int UUID_LENGTH = 36;
48     public static final String STATUS_DESIGN = "DESIGN";
49     public static final String STATUS_DISTRIBUTED = "DISTRIBUTED";
50     public static final String STATUS_ACTIVE = "ACTIVE";
51     public static final String STATUS_STOPPED = "STOPPED";
52     public static final String STATUS_DELETING = "DELETING";
53     public static final String STATUS_ERROR = "ERROR";
54     public static final String STATUS_UNKNOWN = "UNKNOWN";
55     private String id;
56     private String templateId;
57     private String templateName;
58     private String name;
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;
70     /**
71      * The service type Id received from DCAE by querying it
72      */
73     private String typeId;
74     private String typeName;
75     private String deploymentId;
76
77     /**
78      * Construct empty model.
79      */
80     public CldsModel() {
81         event = new CldsEvent();
82     }
83
84     /**
85      * Retrieve from DB.
86      */
87     public static CldsModel retrieve(CldsDao cldsDao, String name, boolean okIfNotFound) {
88         // get from db
89         CldsModel model = cldsDao.getModelTemplate(name);
90         if (model.getId() == null && !okIfNotFound) {
91             throw new NotFoundException();
92         }
93         model.determineStatus();
94         model.determinePermittedActionCd();
95         return model;
96     }
97
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)) {
103             canCall = true;
104         }
105         return canCall;
106     }
107
108     /**
109      * Save model to DB.
110      */
111     public CldsModel save(CldsDao cldsDao, String userid) {
112         CldsModel cldsModel = cldsDao.setModel(this, userid);
113         determineStatus();
114         determinePermittedActionCd();
115         return cldsModel;
116     }
117
118     /**
119      * set the status in the model
120      */
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;
147         }
148     }
149
150     /**
151      * Get the actionCd from current event. If none, default value is
152      * CldsEvent.ACTION_CREATE
153      */
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();
159         }
160         return actionCd;
161     }
162
163     /**
164      * Get the actionStateCd from current event. If none, default value is
165      * CldsEvent.ACTION_STATE_COMPLETED
166      */
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();
172         }
173         return actionStateCd;
174     }
175
176     /**
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.
181      */
182     private void determinePermittedActionCd() {
183         String actionCd = getCurrentActionCd();
184         switch (actionCd) {
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);
191             }
192             break;
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);
198             }
199             break;
200         case CldsEvent.ACTION_SUBMIT:
201         case CldsEvent.ACTION_RESUBMIT:
202             permittedActionCd = Arrays.asList(CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_DELETE);
203             break;
204         case CldsEvent.ACTION_SUBMITDCAE:
205             permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_DELETE);
206             break;
207         case CldsEvent.ACTION_SUBMITPOLICY:
208             permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
209             break;
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);
216             }
217             break;
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);
224             }
225             break;
226         case CldsEvent.ACTION_DEPLOY:
227             permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UNDEPLOY,
228                 CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
229             break;
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);
236             }
237             break;
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);
244             }
245             break;
246         default:
247             logger.warn("Invalid current actionCd: " + actionCd);
248         }
249     }
250
251     private boolean isSimplifiedModel() {
252         boolean result = false;
253         try {
254             if (propText != null) {
255                 JsonNode modelJson = JacksonUtils.getObjectMapperInstance().readTree(propText);
256                 JsonNode simpleModelJson = modelJson.get("simpleModel");
257                 if (simpleModelJson != null && simpleModelJson.asBoolean()) {
258                     result = true;
259                 }
260             }
261         } catch (IOException e) {
262             logger.error("Error while parsing propText json", e);
263         }
264         return result;
265     }
266
267     private boolean isPolicyOnly() {
268         boolean result = false;
269         try {
270             if (propText != null) {
271                 JsonNode modelJson = JacksonUtils.getObjectMapperInstance().readTree(propText);
272                 JsonNode policyOnlyJson = modelJson.get("policyOnly");
273                 if (policyOnlyJson != null && policyOnlyJson.asBoolean()) {
274                     result = true;
275                 }
276             }
277         } catch (IOException e) {
278             logger.error("Error while parsing propText json", e);
279         }
280         return result;
281     }
282
283     /**
284      * Validate requestedActionCd - determine permittedActionCd and then check
285      * if contained in permittedActionCd Throw IllegalArgumentException if
286      * requested actionCd is not permitted.
287      */
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);
294         }
295     }
296
297     /**
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.
302      */
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);
308         }
309         CldsModel model = new CldsModel();
310         model.setControlNamePrefix(fullControlName.substring(0, fullControlName.length() - UUID_LENGTH));
311         model.setControlNameUuid(fullControlName.substring(fullControlName.length() - UUID_LENGTH));
312         return model;
313     }
314
315     /**
316      * @return the controlName (controlNamePrefix + controlNameUuid)
317      */
318     public String getControlName() {
319         return controlNamePrefix + controlNameUuid;
320     }
321
322     /**
323      * To insert modelInstance to the database
324      */
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);
335         }
336         cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
337         return cldsModel;
338     }
339
340     /**
341      * @return the name
342      */
343     public String getName() {
344         return name;
345     }
346
347     /**
348      * @param name
349      *            the name to set
350      */
351     public void setName(String name) {
352         this.name = name;
353     }
354
355     /**
356      * @return the typeName
357      */
358     public String getTypeName() {
359         return typeName;
360     }
361
362     public void setTypeName(String typeName) {
363         this.typeName = typeName;
364     }
365
366     public String getTemplateId() {
367         return templateId;
368     }
369
370     public void setTemplateId(String templateId) {
371         this.templateId = templateId;
372     }
373
374     /**
375      * @return the controlNamePrefix
376      */
377     public String getControlNamePrefix() {
378         return controlNamePrefix;
379     }
380
381     /**
382      * @param controlNamePrefix
383      *            the controlNamePrefix to set
384      */
385     public void setControlNamePrefix(String controlNamePrefix) {
386         this.controlNamePrefix = controlNamePrefix;
387     }
388
389     /**
390      * @return the controlNameUuid
391      */
392     public String getControlNameUuid() {
393         return controlNameUuid;
394     }
395
396     /**
397      * @param controlNameUuid
398      *            the controlNameUuid to set
399      */
400     public void setControlNameUuid(String controlNameUuid) {
401         this.controlNameUuid = controlNameUuid;
402     }
403
404     /**
405      * @return the propText
406      */
407     public String getPropText() {
408         return propText;
409     }
410
411     /**
412      * @param propText
413      *            the propText to set
414      */
415     public void setPropText(String propText) {
416         this.propText = propText;
417     }
418
419     /**
420      * @return the event
421      */
422     public CldsEvent getEvent() {
423         return event;
424     }
425
426     public String getId() {
427         return id;
428     }
429
430     public void setId(String id) {
431         this.id = id;
432     }
433
434     public String getTemplateName() {
435         return templateName;
436     }
437
438     public void setTemplateName(String templateName) {
439         this.templateName = templateName;
440     }
441
442     /**
443      * @param event
444      *            the event to set
445      */
446     public void setEvent(CldsEvent event) {
447         this.event = event;
448     }
449
450     /**
451      * @return the status
452      */
453     public String getStatus() {
454         return status;
455     }
456
457     /**
458      * @param status
459      *            the status to set
460      */
461     public void setStatus(String status) {
462         this.status = status;
463     }
464
465     public String getBlueprintText() {
466         return blueprintText;
467     }
468
469     public void setBlueprintText(String blueprintText) {
470         this.blueprintText = blueprintText;
471     }
472
473     public String getBpmnText() {
474         return bpmnText;
475     }
476
477     public void setBpmnText(String bpmnText) {
478         this.bpmnText = bpmnText;
479     }
480
481     public String getImageText() {
482         return imageText;
483     }
484
485     public void setImageText(String imageText) {
486         this.imageText = imageText;
487     }
488
489     public String getDocText() {
490         return docText;
491     }
492
493     public void setDocText(String docText) {
494         this.docText = docText;
495     }
496
497     public String getTypeId() {
498         return typeId;
499     }
500
501     public void setTypeId(String typeId) {
502         this.typeId = typeId;
503     }
504
505     public List<CldsModelInstance> getCldsModelInstanceList() {
506         if (cldsModelInstanceList == null) {
507             cldsModelInstanceList = new ArrayList<>();
508         }
509         return cldsModelInstanceList;
510     }
511
512     public String getDeploymentId() {
513         return deploymentId;
514     }
515
516     public void setDeploymentId(String deploymentId) {
517         this.deploymentId = deploymentId;
518     }
519
520     public List<String> getPermittedActionCd() {
521         return permittedActionCd;
522     }
523 }