7ebe0edb20e838c3de36c895152269f79b7250fd
[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.dao.model.jsontype.StringJsonUserType;
43 import org.onap.clamp.loop.Loop;
44 import org.onap.clamp.policy.Policy;
45
46 @Entity
47 @Table(name = "micro_service_policies")
48 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
49 public class MicroServicePolicy implements Serializable, Policy {
50     /**
51      *
52      */
53     private static final long serialVersionUID = 6271238288583332616L;
54
55     @Expose
56     @Id
57     @Column(nullable = false, name = "name", unique = true)
58     private String name;
59
60     @Expose
61     @Type(type = "json")
62     @Column(columnDefinition = "json", name = "properties")
63     private JsonObject properties;
64
65     @Expose
66     @Column(name = "shared", nullable = false)
67     private Boolean shared;
68
69     @Column(name = "policy_tosca", nullable = false)
70     private String policyTosca;
71
72     @Expose
73     @Type(type = "json")
74     @Column(columnDefinition = "json", name = "json_representation", nullable = false)
75     private JsonObject jsonRepresentation;
76
77     @ManyToMany(mappedBy = "microServicePolicies")
78     private Set<Loop> usedByLoops = new HashSet<>();
79
80     public MicroServicePolicy() {
81         // serialization
82     }
83
84     public MicroServicePolicy(String name, String policyTosca, Boolean shared, JsonObject jsonRepresentation,
85         Set<Loop> usedByLoops) {
86         this.name = name;
87         this.policyTosca = policyTosca;
88         this.shared = shared;
89         this.jsonRepresentation = jsonRepresentation;
90         this.usedByLoops = usedByLoops;
91     }
92
93     @Override
94     public String getName() {
95         return name;
96     }
97
98     public JsonObject getProperties() {
99         return properties;
100     }
101
102     public void setProperties(JsonObject properties) {
103         this.properties = properties;
104     }
105
106     public Boolean getShared() {
107         return shared;
108     }
109
110     public void setShared(Boolean shared) {
111         this.shared = shared;
112     }
113
114     public String getPolicyTosca() {
115         return policyTosca;
116     }
117
118     public void setPolicyTosca(String policyTosca) {
119         this.policyTosca = policyTosca;
120     }
121
122     @Override
123     public JsonObject getJsonRepresentation() {
124         return jsonRepresentation;
125     }
126
127     public void setJsonRepresentation(JsonObject jsonRepresentation) {
128         this.jsonRepresentation = jsonRepresentation;
129     }
130
131     public Set<Loop> getUsedByLoops() {
132         return usedByLoops;
133     }
134
135     public void setUsedByLoops(Set<Loop> usedBy) {
136         this.usedByLoops = usedBy;
137     }
138
139     @Override
140     public int hashCode() {
141         final int prime = 31;
142         int result = 1;
143         result = prime * result + ((name == null) ? 0 : name.hashCode());
144         return result;
145     }
146
147     @Override
148     public boolean equals(Object obj) {
149         if (this == obj)
150             return true;
151         if (obj == null)
152             return false;
153         if (getClass() != obj.getClass())
154             return false;
155         MicroServicePolicy other = (MicroServicePolicy) obj;
156         if (name == null) {
157             if (other.name != null)
158                 return false;
159         } else if (!name.equals(other.name))
160             return false;
161         return true;
162     }
163
164 }