Add template and tosca model entities and repositories
[clamp.git] / src / main / java / org / onap / clamp / loop / template / MicroServiceModel.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 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.loop.template;
25
26 import com.google.gson.annotations.Expose;
27
28 import java.io.Serializable;
29 import java.util.HashSet;
30 import java.util.Set;
31
32 import javax.persistence.CascadeType;
33 import javax.persistence.Column;
34 import javax.persistence.Entity;
35 import javax.persistence.FetchType;
36 import javax.persistence.Id;
37 import javax.persistence.JoinColumn;
38 import javax.persistence.JoinColumns;
39 import javax.persistence.ManyToOne;
40 import javax.persistence.OneToMany;
41 import javax.persistence.Table;
42
43 import org.onap.clamp.loop.common.AuditEntity;
44
45 /**
46  * This class represents a micro service model for a loop template.
47  */
48
49 @Entity
50 @Table(name = "micro_service_models")
51 public class MicroServiceModel extends AuditEntity implements Serializable {
52
53     /**
54      * The serial version id.
55      */
56     private static final long serialVersionUID = -286522707701376645L;
57
58     @Id
59     @Expose
60     @Column(nullable = false, name = "name", unique = true)
61     private String name;
62
63     /**
64      * This variable is used to store the type mentioned in the micro-service
65      * blueprint.
66      */
67     @Expose
68     @Column(nullable = false, name = "policy_type")
69     private String policyType;
70
71     @Column(nullable = false, name = "blueprint_yaml")
72     private String blueprint;
73
74     @Expose
75     @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
76     @JoinColumns({ @JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"),
77         @JoinColumn(name = "policy_model_version", referencedColumnName = "version") })
78     private PolicyModel policyModel;
79
80     @OneToMany(fetch = FetchType.LAZY, mappedBy = "microServiceModel", orphanRemoval = true)
81     private Set<TemplateMicroServiceModel> usedByLoopTemplates = new HashSet<>();
82
83     /**
84      * policyModel getter.
85      * 
86      * @return the policyModel
87      */
88     public PolicyModel getPolicyModel() {
89         return policyModel;
90     }
91
92     /**
93      * policyModel setter.
94      * 
95      * @param policyModel the policyModel to set
96      */
97     public void setPolicyModel(PolicyModel policyModel) {
98         this.policyModel = policyModel;
99     }
100
101     /**
102      * name getter.
103      * 
104      * @return the name
105      */
106     public String getName() {
107         return name;
108     }
109
110     /**
111      * name setter.
112      * 
113      * @param name the name to set
114      */
115     public void setName(String name) {
116         this.name = name;
117     }
118
119     /**
120      * policyType getter.
121      * 
122      * @return the policyType
123      */
124     public String getPolicyType() {
125         return policyType;
126     }
127
128     /**
129      * policyType setter.
130      * 
131      * @param policyType the policyType to set
132      */
133     public void setPolicyType(String policyType) {
134         this.policyType = policyType;
135     }
136
137     /**
138      * blueprint getter.
139      * 
140      * @return the blueprint
141      */
142     public String getBlueprint() {
143         return blueprint;
144     }
145
146     /**
147      * blueprint setter.
148      * 
149      * @param blueprint the blueprint to set
150      */
151     public void setBlueprint(String blueprint) {
152         this.blueprint = blueprint;
153     }
154
155     /**
156      * usedByLoopTemplates getter.
157      * 
158      * @return the usedByLoopTemplates
159      */
160     public Set<TemplateMicroServiceModel> getUsedByLoopTemplates() {
161         return usedByLoopTemplates;
162     }
163
164     /**
165      * Default constructor for serialization.
166      */
167     public MicroServiceModel() {
168     }
169
170     /**
171      * Constructor.
172      * 
173      * @param name        The name id
174      * @param policyType  The policy model type like
175      *                    onap.policies.controlloop.operational.common.Apex
176      * @param blueprint   The blueprint defined for dcae that contains the policy
177      *                    type to use
178      * @param policyModel The policy model for the policy type mentioned here
179      */
180     public MicroServiceModel(String name, String policyType, String blueprint, PolicyModel policyModel) {
181         this.name = name;
182         this.policyType = policyType;
183         this.blueprint = blueprint;
184         this.policyModel = policyModel;
185     }
186
187     @Override
188     public int hashCode() {
189         final int prime = 31;
190         int result = 1;
191         result = prime * result + ((name == null) ? 0 : name.hashCode());
192         return result;
193     }
194
195     @Override
196     public boolean equals(Object obj) {
197         if (this == obj) {
198             return true;
199         }
200         if (obj == null) {
201             return false;
202         }
203         if (getClass() != obj.getClass()) {
204             return false;
205         }
206         MicroServiceModel other = (MicroServiceModel) obj;
207         if (name == null) {
208             if (other.name != null) {
209                 return false;
210             }
211         } else if (!name.equals(other.name)) {
212             return false;
213         }
214         return true;
215     }
216
217 }