aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/clamp/clds/model/CldsModel.java
blob: af4d6c6664e964f61505966763e404cf39580c92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
 *                             reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 * 
 */

package org.onap.clamp.clds.model;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.fasterxml.jackson.databind.JsonNode;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.ws.rs.BadRequestException;
import javax.ws.rs.NotFoundException;

import org.onap.clamp.clds.dao.CldsDao;
import org.onap.clamp.clds.util.JacksonUtils;

/**
 * Represent a CLDS Model.
 */
public class CldsModel {

    private static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsModel.class);
    private static final int UUID_LENGTH = 36;
    private static final String STATUS_DESIGN = "DESIGN";
    private static final String STATUS_DISTRIBUTED = "DISTRIBUTED";
    private static final String STATUS_ACTIVE = "ACTIVE";
    private static final String STATUS_STOPPED = "STOPPED";
    private static final String STATUS_DELETING = "DELETING";
    private static final String STATUS_ERROR = "ERROR";
    private static final String STATUS_UNKNOWN = "UNKNOWN";
    private String id;
    private String templateId;
    private String templateName;
    private String name;
    private String controlNamePrefix;
    private String controlNameUuid;
    private String bpmnText;
    private String propText;
    private String imageText;
    private String docText;
    private String blueprintText;
    private CldsEvent event;
    private String status;
    private List<String> permittedActionCd;
    private List<CldsModelInstance> cldsModelInstanceList;
    /**
     * The service type Id received from DCAE by querying it
     */
    private String typeId;
    private String typeName;
    private String deploymentId;

    /**
     * Construct empty model.
     */
    public CldsModel() {
        event = new CldsEvent();
    }

    /**
     * Retrieve from DB.
     */
    public static CldsModel retrieve(CldsDao cldsDao, String name, boolean okIfNotFound) {
        // get from db
        CldsModel model = cldsDao.getModelTemplate(name);
        if (model.getId() == null && !okIfNotFound) {
            throw new NotFoundException();
        }
        model.determineStatus();
        model.determinePermittedActionCd();
        return model;
    }

    public boolean canInventoryCall() {
        boolean canCall = false;
        /* Below checks the clds event is submit/resubmit */
        if ((event.isActionCd(CldsEvent.ACTION_SUBMIT) || event.isActionCd(CldsEvent.ACTION_RESUBMIT)
                || event.isActionCd(CldsEvent.ACTION_SUBMITDCAE))) {
            canCall = true;
        }
        return canCall;
    }

    /**
     * Save model to DB.
     */
    public CldsModel save(CldsDao cldsDao, String userid) {
        CldsModel cldsModel = cldsDao.setModel(this, userid);
        determineStatus();
        determinePermittedActionCd();
        return cldsModel;
    }

    /**
     * set the status in the model
     */
    private void determineStatus() {
        status = STATUS_UNKNOWN;
        if (event == null || event.getActionCd() == null) {
            status = STATUS_DESIGN;
        } else if (event.isActionStateCd(CldsEvent.ACTION_STATE_ERROR)) {
            status = STATUS_ERROR;
        } else if (event.isActionAndStateCd(CldsEvent.ACTION_CREATE, CldsEvent.ACTION_STATE_ANY)
                || event.isActionAndStateCd(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_STATE_ANY)
                || event.isActionAndStateCd(CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_STATE_ANY)
                || event.isActionAndStateCd(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_STATE_ANY)
                || event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_RECEIVED)) {
            status = STATUS_DESIGN;
        } else if (event.isActionAndStateCd(CldsEvent.ACTION_DISTRIBUTE, CldsEvent.ACTION_STATE_RECEIVED)
                || event.isActionAndStateCd(CldsEvent.ACTION_UNDEPLOY, CldsEvent.ACTION_STATE_RECEIVED)) {
            status = STATUS_DISTRIBUTED;
        } else if (event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_SENT)) {
            status = STATUS_DELETING;
        } else if (event.isActionAndStateCd(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_STATE_RECEIVED)
                || event.isActionAndStateCd(CldsEvent.ACTION_RESTART, CldsEvent.ACTION_STATE_ANY)
                || event.isActionAndStateCd(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STATE_ANY)
                || event.isActionAndStateCd(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_STATE_ANY)) {
            status = STATUS_ACTIVE;
        } else if (event.isActionAndStateCd(CldsEvent.ACTION_STOP, CldsEvent.ACTION_STATE_ANY)) {
            status = STATUS_STOPPED;
        }
    }

    /**
     * Get the actionCd from current event. If none, default value is
     * CldsEvent.ACTION_CREATE
     */
    private String getCurrentActionCd() {
        // current default actionCd is CREATE
        String actionCd = CldsEvent.ACTION_CREATE;
        if (event != null && event.getActionCd() != null) {
            actionCd = event.getActionCd();
        }
        return actionCd;
    }

    /**
     * Get the actionStateCd from current event. If none, default value is
     * CldsEvent.ACTION_STATE_COMPLETED
     */
    private String getCurrentActionStateCd() {
        // current default actionStateCd is CREATE
        String actionStateCd = CldsEvent.ACTION_STATE_COMPLETED;
        if (event != null && event.getActionStateCd() != null) {
            actionStateCd = event.getActionStateCd();
        }
        return actionStateCd;
    }

    /**
     * Determine permittedActionCd list using the actionCd from the current
     * event. It's a states graph, given the next action that can be executed
     * from the one that has been executed (described in the event object).
     * ACTION_CREATE being the first one.
     */
    private void determinePermittedActionCd() {
        String actionCd = getCurrentActionCd();
        switch (actionCd) {
            case CldsEvent.ACTION_CREATE:
                permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_TEST);
                if (isSimplifiedModel()) {
                    permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_TEST);
                }
                break;
            case CldsEvent.ACTION_SUBMIT:
            case CldsEvent.ACTION_RESUBMIT:
                // for 1702 delete is not currently implemented (and resubmit
                // requires manually deleting artifact from sdc
                permittedActionCd = Arrays.asList(CldsEvent.ACTION_RESUBMIT);
                break;
            case CldsEvent.ACTION_SUBMITDCAE:
                permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE);
                break;
            case CldsEvent.ACTION_DISTRIBUTE:
                permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_RESUBMIT);
                if (isSimplifiedModel()) {
                    permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_SUBMITDCAE);
                }
                break;
            case CldsEvent.ACTION_UNDEPLOY:
                permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY,
                        CldsEvent.ACTION_RESUBMIT);
                if (isSimplifiedModel()) {
                    permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY,
                            CldsEvent.ACTION_SUBMITDCAE);
                }
                break;
            case CldsEvent.ACTION_DEPLOY:
                permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UNDEPLOY,
                        CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
                break;
            case CldsEvent.ACTION_RESTART:
            case CldsEvent.ACTION_UPDATE:
                // for 1702 delete is not currently implemented
                permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UPDATE,
                        CldsEvent.ACTION_STOP, CldsEvent.ACTION_UNDEPLOY);
                break;
            case CldsEvent.ACTION_DELETE:
                if (getCurrentActionStateCd().equals(CldsEvent.ACTION_STATE_SENT)) {
                    permittedActionCd = Arrays.asList();
                } else {
                    permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT);
                }
                break;
            case CldsEvent.ACTION_STOP:
                // for 1702 delete is not currently implemented
                permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_RESTART,
                        CldsEvent.ACTION_UNDEPLOY);
                break;
            default:
                logger.warn("Invalid current actionCd: " + actionCd);
        }
    }

    private boolean isSimplifiedModel() {
        boolean result = false;
        try {
            if (propText != null) {
                JsonNode modelJson = JacksonUtils.getObjectMapperInstance().readTree(propText);
                JsonNode simpleModelJson = modelJson.get("simpleModel");
                if (simpleModelJson != null && simpleModelJson.asBoolean()) {
                    result = true;
                }
            }
        } catch (IOException e) {
            logger.error("Error while parsing propText json", e);
        }
        return result;
    }

    /**
     * Validate requestedActionCd - determine permittedActionCd and then check
     * if contained in permittedActionCd Throw IllegalArgumentException if
     * requested actionCd is not permitted.
     */
    public void validateAction(String requestedActionCd) {
        determinePermittedActionCd();
        if (!permittedActionCd.contains(requestedActionCd)) {
            throw new IllegalArgumentException(
                    "Invalid requestedActionCd: " + requestedActionCd + ".  Given current actionCd: "
                            + getCurrentActionCd() + ", the permittedActionCd: " + permittedActionCd);
        }
    }

    /**
     * Extract the UUID portion of a given full control name (controlNamePrefix
     * + controlNameUuid). No fields are populated other than controlNamePrefix
     * and controlNameUuid. Throws BadRequestException if length of given
     * control name is less than UUID_LENGTH.
     */
    public static CldsModel createUsingControlName(String fullControlName) {
        if (fullControlName == null || fullControlName.length() < UUID_LENGTH) {
            throw new BadRequestException(
                    "closed loop id / control name length, " + (fullControlName != null ? fullControlName.length() : 0)
                            + ", less than the minimum of: " + UUID_LENGTH);
        }
        CldsModel model = new CldsModel();
        model.setControlNamePrefix(fullControlName.substring(0, fullControlName.length() - UUID_LENGTH));
        model.setControlNameUuid(fullControlName.substring(fullControlName.length() - UUID_LENGTH));
        return model;
    }

    /**
     * @return the controlName (controlNamePrefix + controlNameUuid)
     */
    public String getControlName() {
        return controlNamePrefix + controlNameUuid;
    }

    /**
     * To insert modelInstance to the database
     */
    public static CldsModel insertModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent, String userid) {
        String controlName = dcaeEvent.getControlName();
        CldsModel cldsModel = createUsingControlName(controlName);
        cldsModel = cldsDao.getModelByUuid(cldsModel.getControlNameUuid());
        cldsModel.determineStatus();
        if (dcaeEvent.getCldsActionCd().equals(CldsEvent.ACTION_UNDEPLOY) || (dcaeEvent.getCldsActionCd()
                .equals(CldsEvent.ACTION_DEPLOY)
                && (cldsModel.getStatus().equals(STATUS_DISTRIBUTED) || cldsModel.getStatus().equals(STATUS_DESIGN)))) {
            CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userid, dcaeEvent.getCldsActionCd(),
                    CldsEvent.ACTION_STATE_RECEIVED, null);
        }
        cldsDao.insModelInstance(cldsModel, dcaeEvent.getInstances());
        return cldsModel;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the typeName
     */
    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public String getTemplateId() {
        return templateId;
    }

    public void setTemplateId(String templateId) {
        this.templateId = templateId;
    }

    /**
     * @return the controlNamePrefix
     */
    public String getControlNamePrefix() {
        return controlNamePrefix;
    }

    /**
     * @param controlNamePrefix
     *            the controlNamePrefix to set
     */
    public void setControlNamePrefix(String controlNamePrefix) {
        this.controlNamePrefix = controlNamePrefix;
    }

    /**
     * @return the controlNameUuid
     */
    public String getControlNameUuid() {
        return controlNameUuid;
    }

    /**
     * @param controlNameUuid
     *            the controlNameUuid to set
     */
    public void setControlNameUuid(String controlNameUuid) {
        this.controlNameUuid = controlNameUuid;
    }

    /**
     * @return the propText
     */
    public String getPropText() {
        return propText;
    }

    /**
     * @param propText
     *            the propText to set
     */
    public void setPropText(String propText) {
        this.propText = propText;
    }

    /**
     * @return the event
     */
    public CldsEvent getEvent() {
        return event;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTemplateName() {
        return templateName;
    }

    public void setTemplateName(String templateName) {
        this.templateName = templateName;
    }

    /**
     * @param event
     *            the event to set
     */
    public void setEvent(CldsEvent event) {
        this.event = event;
    }

    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @param status
     *            the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }

    public String getBlueprintText() {
        return blueprintText;
    }

    public void setBlueprintText(String blueprintText) {
        this.blueprintText = blueprintText;
    }

    public String getBpmnText() {
        return bpmnText;
    }

    public void setBpmnText(String bpmnText) {
        this.bpmnText = bpmnText;
    }

    public String getImageText() {
        return imageText;
    }

    public void setImageText(String imageText) {
        this.imageText = imageText;
    }

    public String getDocText() {
        return docText;
    }

    public void setDocText(String docText) {
        this.docText = docText;
    }

    public String getTypeId() {
        return typeId;
    }

    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }

    public List<CldsModelInstance> getCldsModelInstanceList() {
        if (cldsModelInstanceList == null) {
            cldsModelInstanceList = new ArrayList<>();
        }
        return cldsModelInstanceList;
    }

    public String getDeploymentId() {
        return deploymentId;
    }

    public void setDeploymentId(String deploymentId) {
        this.deploymentId = deploymentId;
    }

    public List<String> getPermittedActionCd() {
        return permittedActionCd;
    }
}