summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/clamp/clds/model/CldsToscaModel.java
blob: 0123f804952052fc4bc8a2a124d9096dbc5472f3 (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 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 java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.onap.clamp.clds.client.req.policy.PolicyClient;
import org.onap.clamp.clds.config.ClampProperties;
import org.onap.clamp.clds.dao.CldsDao;
import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor;

import com.google.gson.annotations.Expose;

public class CldsToscaModel extends CldsToscaModelRevision {

	@Expose
    private String id;
	@Expose
	private String policyType;
	@Expose
	private String toscaModelName;

    /**
     * Construct.
     */
    public CldsToscaModel() {
    }

    /**
     * Creates or updates Tosca Model to DB.
     *
     * @param cldsDao The cldsDao
     * @param userId The user Id
     */
    public CldsToscaModel save(CldsDao cldsDao, ClampProperties refProp, PolicyClient policyClient, String userId) {
        CldsToscaModel cldsToscaModel = null;
        refProp.getStringList("tosca.policyTypes", ",").stream().forEach(policyType -> {
            if (StringUtils.containsIgnoreCase(this.getToscaModelName(), policyType)) {
                this.setPolicyType(policyType);
            }
        });

        ToscaYamlToJsonConvertor convertor = new ToscaYamlToJsonConvertor(cldsDao);
        this.setToscaModelJson(convertor.parseToscaYaml(this.getToscaModelYaml()));
        List<CldsToscaModel> toscaModels = cldsDao.getToscaModelByName(this.getToscaModelName());
        if (toscaModels != null && !toscaModels.isEmpty()) {
            CldsToscaModel toscaModel = toscaModels.stream().findFirst().get();
            this.setVersion(incrementVersion(toscaModel.getVersion()));
            this.setId(toscaModel.getId());
            this.setUserId(userId);
            if (refProp.getStringValue("import.tosca.model").equalsIgnoreCase("true")) {
                policyClient.importToscaModel(this);
            }
            cldsToscaModel = cldsDao.updateToscaModelWithNewVersion(this, userId);
        } else {
            this.setVersion(1);
            if (refProp.getStringValue("import.tosca.model").equalsIgnoreCase("true")) {
                policyClient.importToscaModel(this);
            }
            cldsToscaModel = cldsDao.insToscaModel(this, userId);
        }
        return cldsToscaModel;
    }

    private double incrementVersion(double curVersion) {
        return curVersion + 1;
    }

    /**
     * Get the Id.
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * Set the id.
     * @param id
     *        the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * Get the policy type.
     * @return the policyType
     */
    public String getPolicyType() {
        return policyType;
    }

    /**
     * Set the policy type.
     * @param policyType
     *        the policyType to set
     */
    public void setPolicyType(String policyType) {
        this.policyType = policyType;
    }

    /**
     * Get the tosca model name.
     * @return the toscaModelName
     */
    public String getToscaModelName() {
        return toscaModelName;
    }

    /**
     * Set the tosca model name.
     * @param toscaModelName
     *        the toscaModelName to set
     */
    public void setToscaModelName(String toscaModelName) {
        this.toscaModelName = toscaModelName;
    }

}