60d339cca81a84e746c3836a102c3212cfef92c6
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.models.acm.persistence.concepts;
22
23 import java.util.Map;
24 import java.util.UUID;
25 import java.util.function.UnaryOperator;
26 import javax.persistence.AttributeOverride;
27 import javax.persistence.AttributeOverrides;
28 import javax.persistence.Column;
29 import javax.persistence.Convert;
30 import javax.persistence.Entity;
31 import javax.persistence.Id;
32 import javax.persistence.Inheritance;
33 import javax.persistence.InheritanceType;
34 import javax.persistence.Lob;
35 import javax.persistence.Table;
36 import lombok.Data;
37 import lombok.EqualsAndHashCode;
38 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
39 import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
40 import org.onap.policy.common.parameters.annotations.NotNull;
41 import org.onap.policy.common.parameters.annotations.Valid;
42 import org.onap.policy.models.base.PfAuthorative;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.base.PfUtils;
45 import org.onap.policy.models.base.Validated;
46 import org.onap.policy.models.base.validation.annotations.VerifyKey;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
48
49 @Entity
50 @Table(name = "NodeTemplateState")
51 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
52 @Data
53 @EqualsAndHashCode(callSuper = false)
54 public class JpaNodeTemplateState extends Validated implements PfAuthorative<NodeTemplateState> {
55
56     @Id
57     @NotNull
58     private String nodeTemplateStateId;
59
60     @Column
61     @NotNull
62     private String compositionId;
63
64     @Column
65     private String participantId;
66
67     @VerifyKey
68     @NotNull
69     @AttributeOverrides({
70         @AttributeOverride(name = "name",    column = @Column(name = "nodeTemplate_name")),
71         @AttributeOverride(name = "version", column = @Column(name = "nodeTemplate_version"))
72     })
73     private PfConceptKey nodeTemplateId;
74
75     @Column
76     private Boolean restarting;
77
78     @Column
79     @NotNull
80     private AcTypeState state;
81
82     @Column
83     private String message;
84
85     @Lob
86     @NotNull
87     @Valid
88     @Convert(converter = StringToMapConverter.class)
89     private Map<String, Object> outProperties;
90
91     /**
92      * The Default Constructor.
93      */
94     public JpaNodeTemplateState() {
95         this(UUID.randomUUID().toString(), UUID.randomUUID().toString());
96     }
97
98     /**
99      * Constructor.
100      *
101      * @param nodeTemplateStateId the nodeTemplateStateId
102      * @param compositionId the compositionId
103      */
104     public JpaNodeTemplateState(@NotNull String nodeTemplateStateId, @NotNull String compositionId) {
105         this.nodeTemplateStateId = nodeTemplateStateId;
106         this.compositionId = compositionId;
107     }
108
109     @Override
110     public void fromAuthorative(NodeTemplateState copyConcept) {
111         this.nodeTemplateStateId = copyConcept.getNodeTemplateStateId().toString();
112         if (copyConcept.getParticipantId() != null) {
113             this.participantId = copyConcept.getParticipantId().toString();
114         }
115         this.nodeTemplateId = copyConcept.getNodeTemplateId().asConceptKey();
116         this.restarting = copyConcept.getRestarting();
117         this.state = copyConcept.getState();
118         this.message = copyConcept.getMessage();
119         this.outProperties = PfUtils.mapMap(copyConcept.getOutProperties(), UnaryOperator.identity());
120     }
121
122     @Override
123     public NodeTemplateState toAuthorative() {
124         var nodeTemplateState = new NodeTemplateState();
125         nodeTemplateState.setNodeTemplateStateId(UUID.fromString(this.nodeTemplateStateId));
126         if (this.participantId != null) {
127             nodeTemplateState.setParticipantId(UUID.fromString(this.participantId));
128         }
129         nodeTemplateState.setNodeTemplateId(new ToscaConceptIdentifier(this.nodeTemplateId));
130         nodeTemplateState.setRestarting(this.restarting);
131         nodeTemplateState.setState(this.state);
132         nodeTemplateState.setMessage(this.message);
133         nodeTemplateState.setOutProperties(PfUtils.mapMap(outProperties, UnaryOperator.identity()));
134         return nodeTemplateState;
135     }
136 }