24f17dc0ce91d06e12a3e3b0dab613870b2237d0
[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 jakarta.persistence.AttributeOverride;
24 import jakarta.persistence.Column;
25 import jakarta.persistence.Convert;
26 import jakarta.persistence.Entity;
27 import jakarta.persistence.Id;
28 import jakarta.persistence.Inheritance;
29 import jakarta.persistence.InheritanceType;
30 import jakarta.persistence.Lob;
31 import jakarta.persistence.Table;
32 import java.util.Map;
33 import java.util.UUID;
34 import java.util.function.UnaryOperator;
35 import lombok.Data;
36 import lombok.EqualsAndHashCode;
37 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
38 import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
39 import org.onap.policy.common.parameters.annotations.NotNull;
40 import org.onap.policy.common.parameters.annotations.Valid;
41 import org.onap.policy.models.base.PfAuthorative;
42 import org.onap.policy.models.base.PfConceptKey;
43 import org.onap.policy.models.base.PfUtils;
44 import org.onap.policy.models.base.Validated;
45 import org.onap.policy.models.base.validation.annotations.VerifyKey;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47
48 @Entity
49 @Table(name = "NodeTemplateState")
50 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
51 @Data
52 @EqualsAndHashCode(callSuper = false)
53 public class JpaNodeTemplateState extends Validated implements PfAuthorative<NodeTemplateState> {
54
55     @Id
56     @NotNull
57     private String nodeTemplateStateId;
58
59     @Column
60     @NotNull
61     private String compositionId;
62
63     @Column
64     private String participantId;
65
66     @VerifyKey
67     @NotNull
68     @AttributeOverride(name = "name",    column = @Column(name = "nodeTemplate_name"))
69     @AttributeOverride(name = "version", column = @Column(name = "nodeTemplate_version"))
70     private PfConceptKey nodeTemplateId;
71
72     @Column
73     private Boolean restarting;
74
75     @Column
76     @NotNull
77     private AcTypeState state;
78
79     @Column
80     private String message;
81
82     @Lob
83     @NotNull
84     @Valid
85     @Convert(converter = StringToMapConverter.class)
86     @Column(length = 100000)
87     private Map<String, Object> outProperties;
88
89     /**
90      * The Default Constructor.
91      */
92     public JpaNodeTemplateState() {
93         this(UUID.randomUUID().toString(), UUID.randomUUID().toString());
94     }
95
96     /**
97      * Constructor.
98      *
99      * @param nodeTemplateStateId the nodeTemplateStateId
100      * @param compositionId the compositionId
101      */
102     public JpaNodeTemplateState(@NotNull String nodeTemplateStateId, @NotNull String compositionId) {
103         this.nodeTemplateStateId = nodeTemplateStateId;
104         this.compositionId = compositionId;
105     }
106
107     @Override
108     public void fromAuthorative(NodeTemplateState copyConcept) {
109         this.nodeTemplateStateId = copyConcept.getNodeTemplateStateId().toString();
110         if (copyConcept.getParticipantId() != null) {
111             this.participantId = copyConcept.getParticipantId().toString();
112         }
113         this.nodeTemplateId = copyConcept.getNodeTemplateId().asConceptKey();
114         this.restarting = copyConcept.getRestarting();
115         this.state = copyConcept.getState();
116         this.message = copyConcept.getMessage();
117         this.outProperties = PfUtils.mapMap(copyConcept.getOutProperties(), UnaryOperator.identity());
118     }
119
120     @Override
121     public NodeTemplateState toAuthorative() {
122         var nodeTemplateState = new NodeTemplateState();
123         nodeTemplateState.setNodeTemplateStateId(UUID.fromString(this.nodeTemplateStateId));
124         if (this.participantId != null) {
125             nodeTemplateState.setParticipantId(UUID.fromString(this.participantId));
126         }
127         nodeTemplateState.setNodeTemplateId(new ToscaConceptIdentifier(this.nodeTemplateId));
128         nodeTemplateState.setRestarting(this.restarting);
129         nodeTemplateState.setState(this.state);
130         nodeTemplateState.setMessage(this.message);
131         nodeTemplateState.setOutProperties(PfUtils.mapMap(outProperties, UnaryOperator.identity()));
132         return nodeTemplateState;
133     }
134 }