b92ff5f712640eeed6e801da1c4da9e6d23a3eec
[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     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";
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 */
101         if ((event.isActionCd(CldsEvent.ACTION_SUBMIT) || event.isActionCd(CldsEvent.ACTION_RESUBMIT)
102                 || event.isActionCd(CldsEvent.ACTION_SUBMITDCAE))) {
103             canCall = true;
104         }
105         return canCall;
106     }
107
108     /**
109      * Save model to DB.
110      */
111     public void save(CldsDao cldsDao, String userid) {
112         cldsDao.setModel(this, userid);
113         determineStatus();
114         determinePermittedActionCd();
115     }
116
117     /**
118      * set the status in the model
119      */
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;
144         }
145     }
146
147     /**
148      * Get the actionCd from current event. If none, default value is
149      * CldsEvent.ACTION_CREATE
150      */
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();
156         }
157         return actionCd;
158     }
159
160     /**
161      * Get the actionStateCd from current event. If none, default value is
162      * CldsEvent.ACTION_STATE_COMPLETED
163      */
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();
169         }
170         return actionStateCd;
171     }
172
173     /**
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.
178      */
179     private void determinePermittedActionCd() {
180         String actionCd = getCurrentActionCd();
181         switch (actionCd) {
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);
186                 }
187                 break;
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);
193                 break;
194             case CldsEvent.ACTION_SUBMITDCAE:
195                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE);
196                 break;
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);
201                 }
202                 break;
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);
209                 }
210                 break;
211             case CldsEvent.ACTION_DEPLOY:
212                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UNDEPLOY,
213                         CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
214                 break;
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);
220                 break;
221             case CldsEvent.ACTION_DELETE:
222                 if (getCurrentActionStateCd().equals(CldsEvent.ACTION_STATE_SENT)) {
223                     permittedActionCd = Arrays.asList();
224                 } else {
225                     permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT);
226                 }
227                 break;
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);
232                 break;
233             default:
234                 logger.warn("Invalid current actionCd: " + actionCd);
235         }
236     }
237
238     private boolean isSimplifiedModel() {
239         boolean result = false;
240         try {
241             if (propText != null) {
242                 JsonNode modelJson = JacksonUtils.getObjectMapperInstance().readTree(propText);
243                 JsonNode simpleModelJson = modelJson.get("simpleModel");
244                 if (simpleModelJson != null && simpleModelJson.asBoolean()) {
245                     result = true;
246                 }
247             }
248         } catch (IOException e) {
249             logger.error("Error while parsing propText json", e);
250         }
251         return result;
252     }
253
254     /**
255      * Validate requestedActionCd - determine permittedActionCd and then check
256      * if contained in permittedActionCd Throw IllegalArgumentException if
257      * requested actionCd is not permitted.
258      */
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);
265         }
266     }
267
268     /**
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.
273      */
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);
279         }
280         CldsModel model = new CldsModel();
281         model.setControlNamePrefix(fullControlName.substring(0, fullControlName.length() - UUID_LENGTH));
282         model.setControlNameUuid(fullControlName.substring(fullControlName.length() - UUID_LENGTH));
283         return model;
284     }
285
286     /**
287      * @return the controlName (controlNamePrefix + controlNameUuid)
288      */
289     public String getControlName() {
290         return controlNamePrefix + controlNameUuid;
291     }
292
293     /**
294      * To insert modelInstance to the database
295      */
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);
306         }
307         cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
308         return cldsModel;
309     }
310
311     /**
312      * @return the name
313      */
314     public String getName() {
315         return name;
316     }
317
318     /**
319      * @param name
320      *            the name to set
321      */
322     public void setName(String name) {
323         this.name = name;
324     }
325
326     /**
327      * @return the typeName
328      */
329     public String getTypeName() {
330         return typeName;
331     }
332
333     public void setTypeName(String typeName) {
334         this.typeName = typeName;
335     }
336
337     public String getTemplateId() {
338         return templateId;
339     }
340
341     public void setTemplateId(String templateId) {
342         this.templateId = templateId;
343     }
344
345     /**
346      * @return the controlNamePrefix
347      */
348     public String getControlNamePrefix() {
349         return controlNamePrefix;
350     }
351
352     /**
353      * @param controlNamePrefix
354      *            the controlNamePrefix to set
355      */
356     public void setControlNamePrefix(String controlNamePrefix) {
357         this.controlNamePrefix = controlNamePrefix;
358     }
359
360     /**
361      * @return the controlNameUuid
362      */
363     public String getControlNameUuid() {
364         return controlNameUuid;
365     }
366
367     /**
368      * @param controlNameUuid
369      *            the controlNameUuid to set
370      */
371     public void setControlNameUuid(String controlNameUuid) {
372         this.controlNameUuid = controlNameUuid;
373     }
374
375     /**
376      * @return the propText
377      */
378     public String getPropText() {
379         return propText;
380     }
381
382     /**
383      * @param propText
384      *            the propText to set
385      */
386     public void setPropText(String propText) {
387         this.propText = propText;
388     }
389
390     /**
391      * @return the event
392      */
393     public CldsEvent getEvent() {
394         return event;
395     }
396
397     public String getId() {
398         return id;
399     }
400
401     public void setId(String id) {
402         this.id = id;
403     }
404
405     public String getTemplateName() {
406         return templateName;
407     }
408
409     public void setTemplateName(String templateName) {
410         this.templateName = templateName;
411     }
412
413     /**
414      * @param event
415      *            the event to set
416      */
417     public void setEvent(CldsEvent event) {
418         this.event = event;
419     }
420
421     /**
422      * @return the status
423      */
424     public String getStatus() {
425         return status;
426     }
427
428     /**
429      * @param status
430      *            the status to set
431      */
432     public void setStatus(String status) {
433         this.status = status;
434     }
435
436     public String getBlueprintText() {
437         return blueprintText;
438     }
439
440     public void setBlueprintText(String blueprintText) {
441         this.blueprintText = blueprintText;
442     }
443
444     public String getBpmnText() {
445         return bpmnText;
446     }
447
448     public void setBpmnText(String bpmnText) {
449         this.bpmnText = bpmnText;
450     }
451
452     public String getImageText() {
453         return imageText;
454     }
455
456     public void setImageText(String imageText) {
457         this.imageText = imageText;
458     }
459
460     public String getDocText() {
461         return docText;
462     }
463
464     public void setDocText(String docText) {
465         this.docText = docText;
466     }
467
468     public String getTypeId() {
469         return typeId;
470     }
471
472     public void setTypeId(String typeId) {
473         this.typeId = typeId;
474     }
475
476     public List<CldsModelInstance> getCldsModelInstanceList() {
477         if (cldsModelInstanceList == null) {
478             cldsModelInstanceList = new ArrayList<>();
479         }
480         return cldsModelInstanceList;
481     }
482
483     public String getDeploymentId() {
484         return deploymentId;
485     }
486
487     public void setDeploymentId(String deploymentId) {
488         this.deploymentId = deploymentId;
489     }
490
491     public List<String> getPermittedActionCd() {
492         return permittedActionCd;
493     }
494 }