Fix check style issues
[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     @Type(type = "json")
64     @Column(columnDefinition = "json", name = "properties")
65     private JsonObject properties;
66
67     @Expose
68     @Column(name = "shared", nullable = false)
69     private Boolean shared;
70
71     @Column(columnDefinition = "MEDIUMTEXT", name = "policy_tosca", nullable = false)
72     private String policyTosca;
73
74     @Expose
75     @Type(type = "json")
76     @Column(columnDefinition = "json", name = "json_representation", nullable = false)
77     private JsonObject jsonRepresentation;
78
79     @ManyToMany(mappedBy = "microServicePolicies")
80     private Set<Loop> usedByLoops = new HashSet<>();
81
82     public MicroServicePolicy() {
83         // serialization
84     }
85
86     /**
87      * The constructor.
88      * @param name The name of the MicroService
89      * @param policyTosca The policy Tosca of the MicroService
90      * @param shared The flag indicate whether the MicroService is shared
91      * @param usedByLoops The list of loops that uses this MicroService
92      */
93     public MicroServicePolicy(String name, String policyTosca, Boolean shared, Set<Loop> usedByLoops) {
94         this.name = name;
95         this.policyTosca = policyTosca;
96         this.shared = shared;
97         this.jsonRepresentation = JsonUtils.GSON_JPA_MODEL
98             .fromJson(new ToscaYamlToJsonConvertor(null).parseToscaYaml(policyTosca), JsonObject.class);
99         this.usedByLoops = usedByLoops;
100     }
101
102     /**
103      * The constructor.
104      * @param name The name of the MicroService
105      * @param policyTosca The policy Tosca of the MicroService
106      * @param shared The flag indicate whether the MicroService is shared
107      * @param jsonRepresentation The UI representation in json format
108      * @param usedByLoops The list of loops that uses this MicroService
109      */
110     public MicroServicePolicy(String name, String policyTosca, Boolean shared, JsonObject jsonRepresentation,
111         Set<Loop> usedByLoops) {
112         this.name = name;
113         this.policyTosca = policyTosca;
114         this.shared = shared;
115         this.usedByLoops = usedByLoops;
116         this.jsonRepresentation = jsonRepresentation;
117     }
118
119     @Override
120     public String getName() {
121         return name;
122     }
123
124     public JsonObject getProperties() {
125         return properties;
126     }
127
128     public void setProperties(JsonObject properties) {
129         this.properties = properties;
130     }
131
132     public Boolean getShared() {
133         return shared;
134     }
135
136     public void setShared(Boolean shared) {
137         this.shared = shared;
138     }
139
140     public String getPolicyTosca() {
141         return policyTosca;
142     }
143
144     public void setPolicyTosca(String policyTosca) {
145         this.policyTosca = policyTosca;
146     }
147
148     @Override
149     public JsonObject getJsonRepresentation() {
150         return jsonRepresentation;
151     }
152
153     public void setJsonRepresentation(JsonObject jsonRepresentation) {
154         this.jsonRepresentation = jsonRepresentation;
155     }
156
157     public Set<Loop> getUsedByLoops() {
158         return usedByLoops;
159     }
160
161     public void setUsedByLoops(Set<Loop> usedBy) {
162         this.usedByLoops = usedBy;
163     }
164
165     @Override
166     public int hashCode() {
167         final int prime = 31;
168         int result = 1;
169         result = prime * result + ((name == null) ? 0 : name.hashCode());
170         return result;
171     }
172
173     @Override
174     public boolean equals(Object obj) {
175         if (this == obj) {
176             return true;
177         }
178         if (obj == null) {
179             return false;
180         }
181         if (getClass() != obj.getClass()) {
182             return false;
183         }
184         MicroServicePolicy other = (MicroServicePolicy) obj;
185         if (name == null) {
186             if (other.name != null) {
187                 return false;
188             }
189         } else if (!name.equals(other.name)) {
190             return false;
191         }
192         return true;
193     }
194
195 }