c2c60c9c0e62c5df3d7a036425f12b561a8be718
[clamp.git] / src / main / java / org / onap / clamp / policy / microservice / MicroServicePolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.policy.microservice;
25
26 import com.google.gson.JsonObject;
27 import com.google.gson.annotations.Expose;
28
29 import java.io.Serializable;
30 import java.util.HashSet;
31 import java.util.Set;
32
33 import javax.persistence.Column;
34 import javax.persistence.Entity;
35 import javax.persistence.Id;
36 import javax.persistence.ManyToMany;
37 import javax.persistence.Table;
38
39 import org.hibernate.annotations.Type;
40 import org.hibernate.annotations.TypeDef;
41 import org.hibernate.annotations.TypeDefs;
42 import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor;
43 import org.onap.clamp.clds.util.JsonUtils;
44 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
45 import org.onap.clamp.loop.Loop;
46 import org.onap.clamp.policy.Policy;
47
48 @Entity
49 @Table(name = "micro_service_policies")
50 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
51 public class MicroServicePolicy implements Serializable, Policy {
52     /**
53      * The serial version ID.
54      */
55     private static final long serialVersionUID = 6271238288583332616L;
56
57     @Expose
58     @Id
59     @Column(nullable = false, name = "name", unique = true)
60     private String name;
61
62     @Expose
63     @Column(nullable = false, name = "model_type")
64     private String modelType;
65
66     @Expose
67     @Column(nullable = false, name = "blueprint_name")
68     private String blueprintName;
69
70     @Expose
71     @Type(type = "json")
72     @Column(columnDefinition = "json", name = "properties")
73     private JsonObject properties;
74
75     @Expose
76     @Column(name = "shared", nullable = false)
77     private Boolean shared;
78
79     @Column(columnDefinition = "MEDIUMTEXT", name = "policy_tosca", nullable = false)
80     private String policyTosca;
81
82     @Expose
83     @Type(type = "json")
84     @Column(columnDefinition = "json", name = "json_representation", nullable = false)
85     private JsonObject jsonRepresentation;
86
87     @ManyToMany(mappedBy = "microServicePolicies")
88     private Set<Loop> usedByLoops = new HashSet<>();
89
90     public MicroServicePolicy() {
91         // serialization
92     }
93
94     /**
95      * The constructor.
96      * @param name The name of the MicroService
97      * @param type The model type of the MicroService
98      * @param blueprintName The name in the blueprint
99      * @param policyTosca The policy Tosca of the MicroService
100      * @param shared The flag indicate whether the MicroService is shared
101      * @param usedByLoops The list of loops that uses this MicroService
102      */
103     public MicroServicePolicy(String name, String modelType, String policyTosca, Boolean shared, Set<Loop> usedByLoops, String blueprintName) {
104         this.name = name;
105         this.modelType = modelType;
106         this.policyTosca = policyTosca;
107         this.shared = shared;
108         this.jsonRepresentation = JsonUtils.GSON_JPA_MODEL
109             .fromJson(new ToscaYamlToJsonConvertor(null).parseToscaYaml(policyTosca), JsonObject.class);
110         this.usedByLoops = usedByLoops;
111         this.blueprintName = blueprintName;
112     }
113
114     /**
115      * The constructor.
116      * @param name The name of the MicroService
117      * @param type The model type of the MicroService
118      * @param blueprintName The name in the blueprint
119      * @param policyTosca The policy Tosca of the MicroService
120      * @param shared The flag indicate whether the MicroService is shared
121      * @param jsonRepresentation The UI representation in json format
122      * @param usedByLoops The list of loops that uses this MicroService
123      */
124     public MicroServicePolicy(String name, String modelType, String policyTosca, Boolean shared, JsonObject jsonRepresentation,
125         Set<Loop> usedByLoops, String blueprintName) {
126         this.name = name;
127         this.modelType = modelType;
128         this.policyTosca = policyTosca;
129         this.shared = shared;
130         this.usedByLoops = usedByLoops;
131         this.jsonRepresentation = jsonRepresentation;
132         this.blueprintName = blueprintName;
133     }
134
135     @Override
136     public String getName() {
137         return name;
138     }
139
140     public String getModelType() {
141         return modelType;
142     }
143
144     public String getBlueprintName() {
145         return blueprintName;
146     }
147
148     public JsonObject getProperties() {
149         return properties;
150     }
151
152     public void setProperties(JsonObject properties) {
153         this.properties = properties;
154     }
155
156     public Boolean getShared() {
157         return shared;
158     }
159
160     public void setShared(Boolean shared) {
161         this.shared = shared;
162     }
163
164     public String getPolicyTosca() {
165         return policyTosca;
166     }
167
168     public void setPolicyTosca(String policyTosca) {
169         this.policyTosca = policyTosca;
170     }
171
172     @Override
173     public JsonObject getJsonRepresentation() {
174         return jsonRepresentation;
175     }
176
177     public void setJsonRepresentation(JsonObject jsonRepresentation) {
178         this.jsonRepresentation = jsonRepresentation;
179     }
180
181     public Set<Loop> getUsedByLoops() {
182         return usedByLoops;
183     }
184
185     public void setUsedByLoops(Set<Loop> usedBy) {
186         this.usedByLoops = usedBy;
187     }
188
189     @Override
190     public int hashCode() {
191         final int prime = 31;
192         int result = 1;
193         result = prime * result + ((name == null) ? 0 : name.hashCode());
194         return result;
195     }
196
197     @Override
198     public boolean equals(Object obj) {
199         if (this == obj) {
200             return true;
201         }
202         if (obj == null) {
203             return false;
204         }
205         if (getClass() != obj.getClass()) {
206             return false;
207         }
208         MicroServicePolicy other = (MicroServicePolicy) obj;
209         if (name == null) {
210             if (other.name != null) {
211                 return false;
212             }
213         } else if (!name.equals(other.name)) {
214             return false;
215         }
216         return true;
217     }
218
219 }