2c178eb8b7fb1e7bc7166b18e4ad7b0001062f63
[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
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import javax.ws.rs.BadRequestException;
33 import javax.ws.rs.NotFoundException;
34
35 import org.onap.clamp.clds.dao.CldsDao;
36 import org.onap.clamp.clds.model.actions.ActionsHandler;
37 import org.onap.clamp.clds.model.actions.ActionsHandlerImpl;
38 import org.onap.clamp.clds.model.status.StatusHandler;
39 import org.onap.clamp.clds.model.status.StatusHandlerImpl;
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     // This is a transient value used to return the failure message to UI
71     private String errorMessageForUi;
72     /**
73      * The service type Id received from DCAE by querying it
74      */
75     private String typeId;
76     private String typeName;
77     private String deploymentId;
78     private String deploymentStatusUrl;
79
80     // Set default handlers but this can be changed if needed.
81     private static StatusHandler statusHandler = new StatusHandlerImpl();
82     private static ActionsHandler actionsHandler = new ActionsHandlerImpl();
83
84     public static synchronized void setStatusHandler(StatusHandler statHandler) {
85         statusHandler = statHandler;
86     }
87
88     public static synchronized void setActionsHandler(ActionsHandler cdHandler) {
89         actionsHandler = cdHandler;
90     }
91
92     /**
93      * Construct empty model.
94      */
95     public CldsModel() {
96         event = new CldsEvent();
97     }
98
99     /**
100      * Retrieve from DB.
101      */
102     public static CldsModel retrieve(CldsDao cldsDao, String name, boolean okIfNotFound) {
103         // get from db
104         CldsModel model = cldsDao.getModelTemplate(name);
105         if (model.getId() == null && !okIfNotFound) {
106             throw new NotFoundException();
107         }
108         model.determineStatus();
109         model.determinePermittedActionCd();
110         return model;
111     }
112
113     public boolean canDcaeInventoryCall() {
114         boolean canCall = false;
115         /* Below checks the clds event is submit/resubmit/distribute */
116         if (event.isActionCd(CldsEvent.ACTION_SUBMIT) || event.isActionCd(CldsEvent.ACTION_RESUBMIT)
117             || event.isActionCd(CldsEvent.ACTION_DISTRIBUTE) || event.isActionCd(CldsEvent.ACTION_SUBMITDCAE)) {
118             canCall = true;
119         }
120         return canCall;
121     }
122
123     /**
124      * Save model to DB.
125      */
126     public CldsModel save(CldsDao cldsDao, String userid) {
127         CldsModel cldsModel = cldsDao.setModel(this, userid);
128         determineStatus();
129         determinePermittedActionCd();
130         return cldsModel;
131     }
132
133     /**
134      * set the status in the model
135      */
136     public void determineStatus() {
137         status = statusHandler.determineStatusOnLastEvent(event);
138     }
139
140     /**
141      * Determine permittedActionCd list using the actionCd from the current event.
142      * It's a states graph, given the next action that can be executed from the one
143      * that has been executed (described in the event object). ACTION_CREATE being
144      * the first one.
145      */
146     public void determinePermittedActionCd() {
147         permittedActionCd = actionsHandler.determinePermittedActionsOnLastEvent(event, propText);
148     }
149
150     /**
151      * Validate requestedActionCd - determine permittedActionCd and then check if
152      * contained in permittedActionCd Throw IllegalArgumentException if requested
153      * actionCd is not permitted.
154      */
155     public void validateAction(String requestedActionCd) {
156         determinePermittedActionCd();
157         if (!permittedActionCd.contains(requestedActionCd)) {
158             throw new IllegalArgumentException(
159                 "Invalid requestedActionCd: " + requestedActionCd + ".  Given current actionCd: "
160                     + actionsHandler.getCurrentActionCd(event) + ", the permittedActionCd: " + permittedActionCd);
161         }
162     }
163
164     /**
165      * Extract the UUID portion of a given full control name (controlNamePrefix +
166      * controlNameUuid). No fields are populated other than controlNamePrefix and
167      * controlNameUuid. Throws BadRequestException if length of given control name
168      * is less than UUID_LENGTH.
169      */
170     public static CldsModel createUsingControlName(String fullControlName) {
171         if (fullControlName == null || fullControlName.length() < UUID_LENGTH) {
172             throw new BadRequestException(
173                 "closed loop id / control name length, " + (fullControlName != null ? fullControlName.length() : 0)
174                     + ", less than the minimum of: " + UUID_LENGTH);
175         }
176         CldsModel model = new CldsModel();
177         model.setControlNamePrefix(fullControlName.substring(0, fullControlName.length() - UUID_LENGTH));
178         model.setControlNameUuid(fullControlName.substring(fullControlName.length() - UUID_LENGTH));
179         return model;
180     }
181
182     /**
183      * @return the controlName (controlNamePrefix + controlNameUuid)
184      */
185     public String getControlName() {
186         return controlNamePrefix + controlNameUuid;
187     }
188
189     /**
190      * To insert modelInstance to the database
191      */
192     public static CldsModel insertModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent, String userid) {
193         String controlName = dcaeEvent.getControlName();
194         CldsModel cldsModel = createUsingControlName(controlName);
195         cldsModel = cldsDao.getModelByUuid(cldsModel.getControlNameUuid());
196         cldsModel.determineStatus();
197         if (dcaeEvent.getCldsActionCd().equals(CldsEvent.ACTION_UNDEPLOY)
198             || (dcaeEvent.getCldsActionCd().equals(CldsEvent.ACTION_DEPLOY)
199                 && (cldsModel.getStatus().equals(STATUS_DISTRIBUTED) || cldsModel.getStatus().equals(STATUS_DESIGN)))) {
200             CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userid, dcaeEvent.getCldsActionCd(),
201                 CldsEvent.ACTION_STATE_RECEIVED, null);
202         }
203         cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
204         return cldsModel;
205     }
206
207     /**
208      * @return the name
209      */
210     public String getName() {
211         return name;
212     }
213
214     /**
215      * @param name
216      *        the name to set
217      */
218     public void setName(String name) {
219         this.name = name;
220     }
221
222     /**
223      * @return the typeName
224      */
225     public String getTypeName() {
226         return typeName;
227     }
228
229     public void setTypeName(String typeName) {
230         this.typeName = typeName;
231     }
232
233     public String getTemplateId() {
234         return templateId;
235     }
236
237     public void setTemplateId(String templateId) {
238         this.templateId = templateId;
239     }
240
241     /**
242      * @return the controlNamePrefix
243      */
244     public String getControlNamePrefix() {
245         return controlNamePrefix;
246     }
247
248     /**
249      * @param controlNamePrefix
250      *        the controlNamePrefix to set
251      */
252     public void setControlNamePrefix(String controlNamePrefix) {
253         this.controlNamePrefix = controlNamePrefix;
254     }
255
256     /**
257      * @return the controlNameUuid
258      */
259     public String getControlNameUuid() {
260         return controlNameUuid;
261     }
262
263     /**
264      * @param controlNameUuid
265      *        the controlNameUuid to set
266      */
267     public void setControlNameUuid(String controlNameUuid) {
268         this.controlNameUuid = controlNameUuid;
269     }
270
271     /**
272      * @return the propText
273      */
274     public String getPropText() {
275         return propText;
276     }
277
278     /**
279      * @param propText
280      *        the propText to set
281      */
282     public void setPropText(String propText) {
283         this.propText = propText;
284     }
285
286     /**
287      * @return the event
288      */
289     public CldsEvent getEvent() {
290         return event;
291     }
292
293     public String getId() {
294         return id;
295     }
296
297     public void setId(String id) {
298         this.id = id;
299     }
300
301     public String getTemplateName() {
302         return templateName;
303     }
304
305     public void setTemplateName(String templateName) {
306         this.templateName = templateName;
307     }
308
309     /**
310      * @param event
311      *        the event to set
312      */
313     public void setEvent(CldsEvent event) {
314         this.event = event;
315     }
316
317     /**
318      * @return the status
319      */
320     public String getStatus() {
321         return status;
322     }
323
324     /**
325      * @param status
326      *        the status to set
327      */
328     public void setStatus(String status) {
329         this.status = status;
330     }
331
332     public String getBlueprintText() {
333         return blueprintText;
334     }
335
336     public void setBlueprintText(String blueprintText) {
337         this.blueprintText = blueprintText;
338     }
339
340     public String getBpmnText() {
341         return bpmnText;
342     }
343
344     public void setBpmnText(String bpmnText) {
345         this.bpmnText = bpmnText;
346     }
347
348     public String getImageText() {
349         return imageText;
350     }
351
352     public void setImageText(String imageText) {
353         this.imageText = imageText;
354     }
355
356     public String getDocText() {
357         return docText;
358     }
359
360     public void setDocText(String docText) {
361         this.docText = docText;
362     }
363
364     public String getTypeId() {
365         return typeId;
366     }
367
368     public void setTypeId(String typeId) {
369         this.typeId = typeId;
370     }
371
372     public List<CldsModelInstance> getCldsModelInstanceList() {
373         if (cldsModelInstanceList == null) {
374             cldsModelInstanceList = new ArrayList<>();
375         }
376         return cldsModelInstanceList;
377     }
378
379     public String getDeploymentId() {
380         return deploymentId;
381     }
382
383     public void setDeploymentId(String deploymentId) {
384         this.deploymentId = deploymentId;
385     }
386
387     public List<String> getPermittedActionCd() {
388         return permittedActionCd;
389     }
390
391     public String getErrorMessageForUi() {
392         return errorMessageForUi;
393     }
394
395     public void setErrorMessageForUi(String errorMessageForUi) {
396         this.errorMessageForUi = errorMessageForUi;
397     }
398
399     public String getDeploymentStatusUrl() {
400         return deploymentStatusUrl;
401     }
402
403     public void setDeploymentStatusUrl(String deploymentStatusUrl) {
404         this.deploymentStatusUrl = deploymentStatusUrl;
405     }
406 }