Merge "Add updateGlobalPropertiesJson controller"
[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      *
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     public MicroServicePolicy(String name, String policyTosca, Boolean shared, Set<Loop> usedByLoops) {
87         this.name = name;
88         this.policyTosca = policyTosca;
89         this.shared = shared;
90         this.jsonRepresentation = JsonUtils.GSON_JPA_MODEL
91             .fromJson(new ToscaYamlToJsonConvertor(null).parseToscaYaml(policyTosca), JsonObject.class);
92         this.usedByLoops = usedByLoops;
93     }
94
95     public MicroServicePolicy(String name, String policyTosca, Boolean shared, JsonObject jsonRepresentation,
96         Set<Loop> usedByLoops) {
97         this.name = name;
98         this.policyTosca = policyTosca;
99         this.shared = shared;
100         this.usedByLoops = usedByLoops;
101         this.jsonRepresentation = jsonRepresentation;
102     }
103
104     @Override
105     public String getName() {
106         return name;
107     }
108
109     public JsonObject getProperties() {
110         return properties;
111     }
112
113     public void setProperties(JsonObject properties) {
114         this.properties = properties;
115     }
116
117     public Boolean getShared() {
118         return shared;
119     }
120
121     public void setShared(Boolean shared) {
122         this.shared = shared;
123     }
124
125     public String getPolicyTosca() {
126         return policyTosca;
127     }
128
129     public void setPolicyTosca(String policyTosca) {
130         this.policyTosca = policyTosca;
131     }
132
133     @Override
134     public JsonObject getJsonRepresentation() {
135         return jsonRepresentation;
136     }
137
138     public void setJsonRepresentation(JsonObject jsonRepresentation) {
139         this.jsonRepresentation = jsonRepresentation;
140     }
141
142     public Set<Loop> getUsedByLoops() {
143         return usedByLoops;
144     }
145
146     public void setUsedByLoops(Set<Loop> usedBy) {
147         this.usedByLoops = usedBy;
148     }
149
150     @Override
151     public int hashCode() {
152         final int prime = 31;
153         int result = 1;
154         result = prime * result + ((name == null) ? 0 : name.hashCode());
155         return result;
156     }
157
158     @Override
159     public boolean equals(Object obj) {
160         if (this == obj)
161             return true;
162         if (obj == null)
163             return false;
164         if (getClass() != obj.getClass())
165             return false;
166         MicroServicePolicy other = (MicroServicePolicy) obj;
167         if (name == null) {
168             if (other.name != null)
169                 return false;
170         } else if (!name.equals(other.name))
171             return false;
172         return true;
173     }
174
175 }