Added oparent to sdc main
[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
21 package org.openecomp.sdc.be.datatypes.elements;
22
23 import static java.util.Collections.emptyList;
24
25 import java.util.List;
26 import java.util.Map;
27 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
28
29 /**
30  * public class representing the component policy,
31  * described by the next properties:
32  * <p>
33  * name
34  * uniqueId
35  * type (policy type name)
36  * typeUid (policy type uniqueId)
37  * version (version)
38  * derivedFrom (policy type derivedFrom)
39  * description
40  * policyUUID
41  * invariantUUID
42  * members
43  * metadata
44  * properties
45  * targets
46  * isFromCsar
47  */
48 public class PolicyDataDefinition extends PropertyDataDefinition {
49
50     /**
51      * public constructor by default
52      */
53     public PolicyDataDefinition() {
54         super();
55     }
56
57     public PolicyDataDefinition(PropertyDataDefinition propertyDataDefinition) {
58         super(propertyDataDefinition);
59     }
60
61     /**
62      * public constructor from superclass
63      *
64      * @param policy
65      */
66     public PolicyDataDefinition(Map<String, Object> policy) {
67         super(policy);
68     }
69
70     /**
71      * public copy constructor
72      *
73      * @param other
74      */
75     public PolicyDataDefinition(PolicyDataDefinition other) {
76         super(other);
77         this.setName(other.getName());
78         this.setUniqueId(other.getUniqueId());
79         this.setPolicyTypeName(other.getPolicyTypeName());
80         this.setPolicyTypeUid(other.getPolicyTypeUid());
81         this.setVersion(other.getVersion());
82         this.setDerivedFrom(other.getDerivedFrom());
83         this.setDescription(other.getDescription());
84         this.setPolicyUUID(other.getPolicyUUID());
85         this.setInvariantUUID(other.getInvariantUUID());
86         this.setInvariantName(other.getInvariantName());
87         this.setComponentName(other.getComponentName());
88         this.setIsFromCsar(other.getIsFromCsar());
89         this.setValue(other.getValue());
90         this.setOwnerId(other.getOwnerId());
91         this.setType(other.getType());
92         this.setInstanceUniqueId(other.getInstanceUniqueId());
93         this.setInputPath(other.getInputPath());
94         if (other.getProperties() != null) {
95             this.setProperties(other.getProperties());
96         }
97         if (other.getTargets() != null) {
98             this.setTargets(other.getTargets());
99         }
100     }
101
102     private void setIsFromCsar(Boolean isFromCsar) {
103         if (isFromCsar == null) {
104             setToscaPresentationValue(JsonPresentationFields.IS_FROM_CSAR, false);
105         } else {
106             setToscaPresentationValue(JsonPresentationFields.IS_FROM_CSAR, isFromCsar);
107         }
108     }
109
110     public Boolean getIsFromCsar() {
111         Boolean isFromCsar = (Boolean) getToscaPresentationValue(JsonPresentationFields.IS_FROM_CSAR);
112         return (isFromCsar != null) ? isFromCsar : false;
113     }
114
115     public String getComponentName() {
116         return (String) getToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_NAME);
117     }
118
119     public void setComponentName(String componentName) {
120         setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_NAME, componentName);
121     }
122
123     public String getInvariantName() {
124         return (String) getToscaPresentationValue(JsonPresentationFields.CI_INVARIANT_NAME);
125     }
126
127     public void setInvariantName(Object invariantName) {
128         setToscaPresentationValue(JsonPresentationFields.CI_INVARIANT_NAME, invariantName);
129     }
130
131     public String getPolicyTypeName() {
132         return (String) getToscaPresentationValue(JsonPresentationFields.TYPE);
133     }
134
135     public void setPolicyTypeName(String policyTypeName) {
136         setToscaPresentationValue(JsonPresentationFields.TYPE, policyTypeName);
137     }
138
139     public String getPolicyTypeUid() {
140         return (String) getToscaPresentationValue(JsonPresentationFields.TYPE_UNIQUE_ID);
141     }
142
143     public void setPolicyTypeUid(String policyTypeUid) {
144         setToscaPresentationValue(JsonPresentationFields.TYPE_UNIQUE_ID, policyTypeUid);
145     }
146
147     public String getVersion() {
148         return (String) getToscaPresentationValue(JsonPresentationFields.VERSION);
149     }
150
151     public void setVersion(String version) {
152         setToscaPresentationValue(JsonPresentationFields.VERSION, version);
153     }
154
155     public String getDerivedFrom() {
156         return (String) getToscaPresentationValue(JsonPresentationFields.DERIVED_FROM);
157     }
158
159     public void setDerivedFrom(String derivedFrom) {
160         setToscaPresentationValue(JsonPresentationFields.DERIVED_FROM, derivedFrom);
161     }
162
163     public String getDescription() {
164         return (String) getToscaPresentationValue(JsonPresentationFields.DESCRIPTION);
165     }
166
167     public void setDescription(String description) {
168         setToscaPresentationValue(JsonPresentationFields.DESCRIPTION, description);
169     }
170
171     public String getPolicyUUID() {
172         return (String) getToscaPresentationValue(JsonPresentationFields.UUID);
173     }
174
175     public void setPolicyUUID(String policyUUID) {
176         setToscaPresentationValue(JsonPresentationFields.UUID, policyUUID);
177     }
178
179     public String getInvariantUUID() {
180         return (String) getToscaPresentationValue(JsonPresentationFields.INVARIANT_UUID);
181     }
182
183     public void setInvariantUUID(String invariantUUID) {
184         setToscaPresentationValue(JsonPresentationFields.INVARIANT_UUID, invariantUUID);
185     }
186
187     @SuppressWarnings("unchecked")
188     public List<PropertyDataDefinition> getProperties() {
189         return (List<PropertyDataDefinition>) getToscaPresentationValue(JsonPresentationFields.PROPERTIES);
190     }
191
192     public void setProperties(List<PropertyDataDefinition> properties) {
193         setToscaPresentationValue(JsonPresentationFields.PROPERTIES, properties);
194     }
195
196     @SuppressWarnings("unchecked")
197     public Map<PolicyTargetType, List<String>> getTargets() {
198         return (Map<PolicyTargetType, List<String>>) getToscaPresentationValue(JsonPresentationFields.TARGETS);
199     }
200
201     public void setTargets(Map<PolicyTargetType, List<String>> metadata) {
202         setToscaPresentationValue(JsonPresentationFields.TARGETS, metadata);
203     }
204
205     public List<String> resolveComponentInstanceTargets() {
206         return resolveTargetsByType(PolicyTargetType.COMPONENT_INSTANCES);
207     }
208
209     public List<String> resolveGroupTargets() {
210         return resolveTargetsByType(PolicyTargetType.GROUPS);
211     }
212
213     public boolean containsTarget(String targetId, PolicyTargetType policyTargetType) {
214         return resolveTargetsByType(policyTargetType).contains(targetId);
215     }
216
217     private List<String> resolveTargetsByType(PolicyTargetType targetType) {
218         Map<PolicyTargetType, List<String>> targets = getTargets();
219         return targets == null || !targets.containsKey(targetType) ? emptyList() : targets.get(targetType);
220     }
221
222 }