aboutsummaryrefslogtreecommitdiffstats
path: root/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroup.java
blob: 1d49a9b8b6947eb822f61ad1cca8a52dd7699b54 (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP Policy Model
 * ================================================================================
 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2019-2021 Nordix Foundation.
 * ================================================================================
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.models.pdp.persistence.concepts;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import org.onap.policy.common.parameters.BeanValidationResult;
import org.onap.policy.common.parameters.annotations.Min;
import org.onap.policy.common.parameters.annotations.NotBlank;
import org.onap.policy.common.parameters.annotations.NotNull;
import org.onap.policy.common.parameters.annotations.Valid;
import org.onap.policy.models.base.PfAuthorative;
import org.onap.policy.models.base.PfConcept;
import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.base.PfKey;
import org.onap.policy.models.base.PfKeyUse;
import org.onap.policy.models.base.PfReferenceKey;
import org.onap.policy.models.base.PfSearchableKey;
import org.onap.policy.models.base.PfUtils;
import org.onap.policy.models.base.validation.annotations.VerifyKey;
import org.onap.policy.models.pdp.concepts.Pdp;
import org.onap.policy.models.pdp.concepts.PdpSubGroup;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;

/**
 * Class to represent a PDP subgroup in the database.
 *
 * @author Liam Fallon (liam.fallon@est.tech)
 */
@Entity
@Table(name = "PdpSubGroup")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Data
@EqualsAndHashCode(callSuper = false)
public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGroup> {
    private static final long serialVersionUID = -357224425637789775L;

    @EmbeddedId
    @VerifyKey
    @NotNull
    private PfReferenceKey key;

    @ElementCollection
    @NotNull
    private List<@NotNull @Valid PfSearchableKey> supportedPolicyTypes;

    @ElementCollection
    @NotNull
    private List<PfConceptKey> policies;

    @Column
    @Min(0)
    private int currentInstanceCount;

    @Column
    @Min(0)
    private int desiredInstanceCount;

    @ElementCollection
    private Map<@NotNull @NotBlank String, @NotNull @NotBlank String> properties;

    // @formatter:off
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinTable (
            joinColumns = {
                @JoinColumn(name = "pdpParentKeyName",    referencedColumnName = "parentKeyName"),
                @JoinColumn(name = "pdpParentKeyVersion", referencedColumnName = "parentKeyVersion"),
                @JoinColumn(name = "pdpParentLocalName",  referencedColumnName = "parentLocalName"),
                @JoinColumn(name = "pdpLocalName",        referencedColumnName = "localName")
            }
        )
    // formatter:on
    @NotNull
    private List<@NotNull @Valid JpaPdp> pdpInstances;

    /**
     * The Default Constructor creates a {@link JpaPdpSubGroup} object with a null key.
     */
    public JpaPdpSubGroup() {
        this(new PfReferenceKey());
    }

    /**
     * The Key Constructor creates a {@link JpaPdpSubGroup} object with the given concept key.
     *
     * @param key the key
     */
    public JpaPdpSubGroup(@NonNull final PfReferenceKey key) {
        this(key, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
    }

    /**
     * The Key Constructor creates a {@link JpaPdpSubGroup} object with all mandatory fields.
     *
     * @param key the key
     * @param supportedPolicyTypes Supported policy types
     * @param policies policies deployed on this PDP subgroups
     * @param pdpInstances the PDP instances on this PDP subgroups
     */
    public JpaPdpSubGroup(@NonNull final PfReferenceKey key, @NonNull final List<PfSearchableKey> supportedPolicyTypes,
            @NonNull List<PfConceptKey> policies, @NonNull final List<JpaPdp> pdpInstances) {
        this.key = key;
        this.supportedPolicyTypes = supportedPolicyTypes;
        this.policies = policies;
        this.pdpInstances = pdpInstances;
    }

    /**
     * Copy constructor.
     *
     * @param copyConcept the concept to copy from
     */
    public JpaPdpSubGroup(@NonNull final JpaPdpSubGroup copyConcept) {
        super(copyConcept);
        this.key = new PfReferenceKey(copyConcept.key);
        this.supportedPolicyTypes = PfUtils.mapList(copyConcept.supportedPolicyTypes,
                                        PfSearchableKey::new, new ArrayList<>(0));
        this.policies = PfUtils.mapList(copyConcept.policies, PfConceptKey::new, new ArrayList<>(0));
        this.currentInstanceCount = copyConcept.currentInstanceCount;
        this.desiredInstanceCount = copyConcept.desiredInstanceCount;
        this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
        this.pdpInstances = PfUtils.mapList(copyConcept.pdpInstances, JpaPdp::new, new ArrayList<>(0));
    }

    /**
     * Authorative constructor.
     *
     * @param authorativeConcept the authorative concept to copy from
     */
    public JpaPdpSubGroup(@NonNull final PdpSubGroup authorativeConcept) {
        this.fromAuthorative(authorativeConcept);
    }

    @Override
    public PdpSubGroup toAuthorative() {
        var pdpSubgroup = new PdpSubGroup();

        pdpSubgroup.setPdpType(getKey().getLocalName());

        pdpSubgroup.setSupportedPolicyTypes(new ArrayList<>());
        for (PfSearchableKey supportedPolicyTypeKey : supportedPolicyTypes) {
            var supportedPolicyTypeIdent = new ToscaConceptIdentifier(
                    supportedPolicyTypeKey.getName(), supportedPolicyTypeKey.getVersion());
            pdpSubgroup.getSupportedPolicyTypes().add(supportedPolicyTypeIdent);
        }

        pdpSubgroup.setPolicies(new ArrayList<>());
        for (PfConceptKey policyKey : policies) {
            var toscaPolicyIdentifier = new ToscaConceptIdentifier();
            toscaPolicyIdentifier.setName(policyKey.getName());
            toscaPolicyIdentifier.setVersion(policyKey.getVersion());
            pdpSubgroup.getPolicies().add(toscaPolicyIdentifier);
        }

        pdpSubgroup.setCurrentInstanceCount(currentInstanceCount);
        pdpSubgroup.setDesiredInstanceCount(desiredInstanceCount);
        pdpSubgroup.setProperties(properties == null ? null : new LinkedHashMap<>(properties));

        pdpSubgroup.setPdpInstances(new ArrayList<>());
        for (JpaPdp jpaPdp : pdpInstances) {
            pdpSubgroup.getPdpInstances().add(jpaPdp.toAuthorative());
        }

        return pdpSubgroup;
    }

    @Override
    public void fromAuthorative(@NonNull final PdpSubGroup pdpSubgroup) {
        if (this.key == null || this.getKey().isNullKey()) {
            this.setKey(new PfReferenceKey());
            getKey().setLocalName(pdpSubgroup.getPdpType());
        }

        this.supportedPolicyTypes = new ArrayList<>();
        if (pdpSubgroup.getSupportedPolicyTypes() != null) {
            for (ToscaConceptIdentifier supportedPolicyType : pdpSubgroup.getSupportedPolicyTypes()) {
                this.supportedPolicyTypes
                        .add(new PfSearchableKey(supportedPolicyType.getName(), supportedPolicyType.getVersion()));
            }
        }

        this.policies = new ArrayList<>();
        if (pdpSubgroup.getPolicies() != null) {
            for (ToscaConceptIdentifier toscaPolicyIdentifier : pdpSubgroup.getPolicies()) {
                this.policies
                        .add(new PfConceptKey(toscaPolicyIdentifier.getName(), toscaPolicyIdentifier.getVersion()));
            }
        }
        this.currentInstanceCount = pdpSubgroup.getCurrentInstanceCount();
        this.desiredInstanceCount = pdpSubgroup.getDesiredInstanceCount();
        this.properties =
                (pdpSubgroup.getProperties() == null ? null : new LinkedHashMap<>(pdpSubgroup.getProperties()));

        this.pdpInstances = new ArrayList<>();
        if (pdpSubgroup.getPdpInstances() != null) {
            for (Pdp pdp : pdpSubgroup.getPdpInstances()) {
                var jpaPdp = new JpaPdp();
                jpaPdp.setKey(new PfReferenceKey(getKey(), pdp.getInstanceId()));
                jpaPdp.fromAuthorative(pdp);
                this.pdpInstances.add(jpaPdp);
            }
        }
    }

    @Override
    public List<PfKey> getKeys() {
        List<PfKey> keyList = getKey().getKeys();

        for (PfSearchableKey ptkey : supportedPolicyTypes) {
            keyList.add(new PfKeyUse(ptkey));
        }

        for (PfConceptKey pkey : policies) {
            keyList.add(new PfKeyUse(pkey));
        }

        for (JpaPdp jpaPdp : pdpInstances) {
            keyList.addAll(jpaPdp.getKeys());
        }


        return keyList;
    }

    @Override
    public void clean() {
        key.clean();

        for (PfSearchableKey ptkey : supportedPolicyTypes) {
            ptkey.clean();
        }

        for (PfConceptKey pkey : policies) {
            pkey.clean();
        }

        if (properties != null) {
            Map<String, String> cleanedPropertyMap = new LinkedHashMap<>();
            for (Entry<String, String> propertyEntry : properties.entrySet()) {
                cleanedPropertyMap.put(propertyEntry.getKey().trim(), propertyEntry.getValue().trim());
            }
            properties = cleanedPropertyMap;
        }

        for (JpaPdp jpaPdp : pdpInstances) {
            jpaPdp.clean();
        }
    }

    @Override
    public BeanValidationResult validate(@NonNull String fieldName) {
        BeanValidationResult result = super.validate(fieldName);

        validateKeyNotNull(result, "parent of key", key.getParentConceptKey());

        if (supportedPolicyTypes != null && supportedPolicyTypes.isEmpty()) {
            addResult(result, "supportedPolicyTypes", supportedPolicyTypes, "is empty");
        }

        return result;
    }

    @Override
    public int compareTo(final PfConcept otherConcept) {
        if (otherConcept == null) {
            return -1;
        }
        if (this == otherConcept) {
            return 0;
        }
        if (getClass() != otherConcept.getClass()) {
            return getClass().getName().compareTo(otherConcept.getClass().getName());
        }

        final JpaPdpSubGroup other = (JpaPdpSubGroup) otherConcept;
        if (!key.equals(other.key)) {
            return key.compareTo(other.key);
        }

        int result = PfUtils.compareObjects(supportedPolicyTypes, other.supportedPolicyTypes);
        if (result != 0) {
            return result;
        }

        result = PfUtils.compareObjects(policies, other.policies);
        if (result != 0) {
            return result;
        }

        if (currentInstanceCount != other.currentInstanceCount) {
            return currentInstanceCount - other.currentInstanceCount;
        }

        if (desiredInstanceCount != other.desiredInstanceCount) {
            return desiredInstanceCount - other.desiredInstanceCount;
        }

        result = PfUtils.compareObjects(properties, other.properties);
        if (result != 0) {
            return result;
        }

        return PfUtils.compareObjects(pdpInstances, other.pdpInstances);
    }
}