Reformat common-be
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / datatypes / elements / PolicyDataDefinition.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.datatypes.elements;
21
22 import static java.util.Collections.emptyList;
23
24 import java.util.List;
25 import java.util.Map;
26 import lombok.NoArgsConstructor;
27 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
28
29 /**
30  * public class representing the component policy, described by the next properties:
31  * <p>
32  * name uniqueId type (policy type name) typeUid (policy type uniqueId) version (version) derivedFrom (policy type derivedFrom) description policyUUID
33  * invariantUUID members metadata properties targets isFromCsar
34  */
35 @NoArgsConstructor
36 public class PolicyDataDefinition extends PropertyDataDefinition {
37
38     public PolicyDataDefinition(PropertyDataDefinition propertyDataDefinition) {
39         super(propertyDataDefinition);
40     }
41
42     /**
43      * public constructor from superclass
44      *
45      * @param policy
46      */
47     public PolicyDataDefinition(Map<String, Object> policy) {
48         super(policy);
49     }
50
51     /**
52      * public copy constructor
53      *
54      * @param other
55      */
56     public PolicyDataDefinition(PolicyDataDefinition other) {
57         super(other);
58         this.setName(other.getName());
59         this.setUniqueId(other.getUniqueId());
60         this.setPolicyTypeName(other.getPolicyTypeName());
61         this.setPolicyTypeUid(other.getPolicyTypeUid());
62         this.setVersion(other.getVersion());
63         this.setDerivedFrom(other.getDerivedFrom());
64         this.setDescription(other.getDescription());
65         this.setPolicyUUID(other.getPolicyUUID());
66         this.setInvariantUUID(other.getInvariantUUID());
67         this.setInvariantName(other.getInvariantName());
68         this.setComponentName(other.getComponentName());
69         this.setIsFromCsar(other.getIsFromCsar());
70         this.setValue(other.getValue());
71         this.setOwnerId(other.getOwnerId());
72         this.setType(other.getType());
73         this.setInstanceUniqueId(other.getInstanceUniqueId());
74         this.setInputPath(other.getInputPath());
75         if (other.getProperties() != null) {
76             this.setProperties(other.getProperties());
77         }
78         if (other.getTargets() != null) {
79             this.setTargets(other.getTargets());
80         }
81     }
82
83     public Boolean getIsFromCsar() {
84         Boolean isFromCsar = (Boolean) getToscaPresentationValue(JsonPresentationFields.IS_FROM_CSAR);
85         return (isFromCsar != null) ? isFromCsar : false;
86     }
87
88     private void setIsFromCsar(Boolean isFromCsar) {
89         if (isFromCsar == null) {
90             setToscaPresentationValue(JsonPresentationFields.IS_FROM_CSAR, false);
91         } else {
92             setToscaPresentationValue(JsonPresentationFields.IS_FROM_CSAR, isFromCsar);
93         }
94     }
95
96     public String getComponentName() {
97         return (String) getToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_NAME);
98     }
99
100     public void setComponentName(String componentName) {
101         setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_NAME, componentName);
102     }
103
104     public String getInvariantName() {
105         return (String) getToscaPresentationValue(JsonPresentationFields.CI_INVARIANT_NAME);
106     }
107
108     public void setInvariantName(Object invariantName) {
109         setToscaPresentationValue(JsonPresentationFields.CI_INVARIANT_NAME, invariantName);
110     }
111
112     public String getPolicyTypeName() {
113         return (String) getToscaPresentationValue(JsonPresentationFields.TYPE);
114     }
115
116     public void setPolicyTypeName(String policyTypeName) {
117         setToscaPresentationValue(JsonPresentationFields.TYPE, policyTypeName);
118     }
119
120     public String getPolicyTypeUid() {
121         return (String) getToscaPresentationValue(JsonPresentationFields.TYPE_UNIQUE_ID);
122     }
123
124     public void setPolicyTypeUid(String policyTypeUid) {
125         setToscaPresentationValue(JsonPresentationFields.TYPE_UNIQUE_ID, policyTypeUid);
126     }
127
128     @Override
129     public String getVersion() {
130         return (String) getToscaPresentationValue(JsonPresentationFields.VERSION);
131     }
132
133     public void setVersion(String version) {
134         setToscaPresentationValue(JsonPresentationFields.VERSION, version);
135     }
136
137     public String getDerivedFrom() {
138         return (String) getToscaPresentationValue(JsonPresentationFields.DERIVED_FROM);
139     }
140
141     public void setDerivedFrom(String derivedFrom) {
142         setToscaPresentationValue(JsonPresentationFields.DERIVED_FROM, derivedFrom);
143     }
144
145     @Override
146     public String getDescription() {
147         return (String) getToscaPresentationValue(JsonPresentationFields.DESCRIPTION);
148     }
149
150     @Override
151     public void setDescription(String description) {
152         setToscaPresentationValue(JsonPresentationFields.DESCRIPTION, description);
153     }
154
155     public String getPolicyUUID() {
156         return (String) getToscaPresentationValue(JsonPresentationFields.UUID);
157     }
158
159     public void setPolicyUUID(String policyUUID) {
160         setToscaPresentationValue(JsonPresentationFields.UUID, policyUUID);
161     }
162
163     public String getInvariantUUID() {
164         return (String) getToscaPresentationValue(JsonPresentationFields.INVARIANT_UUID);
165     }
166
167     public void setInvariantUUID(String invariantUUID) {
168         setToscaPresentationValue(JsonPresentationFields.INVARIANT_UUID, invariantUUID);
169     }
170
171     @SuppressWarnings("unchecked")
172     public List<PropertyDataDefinition> getProperties() {
173         return (List<PropertyDataDefinition>) getToscaPresentationValue(JsonPresentationFields.PROPERTIES);
174     }
175
176     public void setProperties(List<PropertyDataDefinition> properties) {
177         setToscaPresentationValue(JsonPresentationFields.PROPERTIES, properties);
178     }
179
180     @SuppressWarnings("unchecked")
181     public Map<PolicyTargetType, List<String>> getTargets() {
182         return (Map<PolicyTargetType, List<String>>) getToscaPresentationValue(JsonPresentationFields.TARGETS);
183     }
184
185     public void setTargets(Map<PolicyTargetType, List<String>> metadata) {
186         setToscaPresentationValue(JsonPresentationFields.TARGETS, metadata);
187     }
188
189     public List<String> resolveComponentInstanceTargets() {
190         return resolveTargetsByType(PolicyTargetType.COMPONENT_INSTANCES);
191     }
192
193     public List<String> resolveGroupTargets() {
194         return resolveTargetsByType(PolicyTargetType.GROUPS);
195     }
196
197     public boolean containsTarget(String targetId, PolicyTargetType policyTargetType) {
198         return resolveTargetsByType(policyTargetType).contains(targetId);
199     }
200
201     private List<String> resolveTargetsByType(PolicyTargetType targetType) {
202         Map<PolicyTargetType, List<String>> targets = getTargets();
203         return targets == null || !targets.containsKey(targetType) ? emptyList() : targets.get(targetType);
204     }
205 }