Fix the new tosca converter
[clamp.git] / src / main / java / org / onap / clamp / loop / service / Service.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.loop.service;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.gson.JsonObject;
29 import com.google.gson.annotations.Expose;
30 import java.io.Serializable;
31 import javax.persistence.Column;
32 import javax.persistence.Entity;
33 import javax.persistence.Id;
34 import javax.persistence.Table;
35 import javax.persistence.Transient;
36 import org.hibernate.annotations.Type;
37 import org.hibernate.annotations.TypeDef;
38 import org.hibernate.annotations.TypeDefs;
39 import org.onap.clamp.clds.util.JsonUtils;
40 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
41
42 @Entity
43 @Table(name = "services")
44 @TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)})
45 public class Service implements Serializable {
46
47     /**
48      * The serial version id.
49      */
50     private static final long serialVersionUID = 1331119060272760758L;
51
52     @Transient
53     private static final EELFLogger logger = EELFManager.getInstance().getLogger(Service.class);
54
55     @Id
56     @Column(name = "service_uuid", unique = true)
57     private String serviceUuid;
58
59     @Column(nullable = false, name = "name")
60     private String name;
61
62     @Column(name = "version")
63     private String version;
64
65     @Expose
66     @Type(type = "json")
67     @Column(columnDefinition = "json", name = "service_details")
68     private JsonObject serviceDetails;
69
70     @Expose
71     @Type(type = "json")
72     @Column(columnDefinition = "json", name = "resource_details")
73     private JsonObject resourceDetails;
74
75     /**
76      * Default constructor for serialization.
77      */
78     public Service() {
79     }
80
81     /**
82      * Constructor with string.
83      */
84     public Service(String serviceDetails, String resourceDetails) {
85         JsonObject serviceDetailsJson = JsonUtils.GSON.fromJson(serviceDetails, JsonObject.class);
86         this.name = serviceDetailsJson.get("name").getAsString();
87         this.serviceUuid = serviceDetailsJson.get("UUID").getAsString();
88         this.serviceDetails = serviceDetailsJson;
89         this.resourceDetails = JsonUtils.GSON.fromJson(resourceDetails, JsonObject.class);
90     }
91
92     /**
93      * Constructor with Json Object.
94      */
95     public Service(JsonObject serviceDetails, JsonObject resourceDetails, String version) {
96         this.name = serviceDetails.get("name").getAsString();
97         this.serviceUuid = serviceDetails.get("UUID").getAsString();
98         this.serviceDetails = serviceDetails;
99         this.resourceDetails = resourceDetails;
100         this.version = version;
101     }
102
103     public String getServiceUuid() {
104         return serviceUuid;
105     }
106
107     public JsonObject getServiceDetails() {
108         return serviceDetails;
109     }
110
111     public JsonObject getResourceDetails() {
112         return resourceDetails;
113     }
114
115     public JsonObject getResourceByType(String type) {
116         return (JsonObject) resourceDetails.get(type);
117     }
118
119     /**
120      * Name getter.
121      *
122      * @return the name
123      */
124     public String getName() {
125         return name;
126     }
127
128     /**
129      * Version getter.
130      *
131      * @return the version
132      */
133     public String getVersion() {
134         return version;
135     }
136
137     @Override
138     public int hashCode() {
139         final int prime = 31;
140         int result = 1;
141         result = prime * result + ((serviceUuid == null) ? 0 : serviceUuid.hashCode());
142         return result;
143     }
144
145     @Override
146     public boolean equals(Object obj) {
147         if (this == obj) {
148             return true;
149         }
150         if (obj == null) {
151             return false;
152         }
153         if (getClass() != obj.getClass()) {
154             return false;
155         }
156         Service other = (Service) obj;
157         if (serviceUuid == null) {
158             if (other.serviceUuid != null) {
159                 return false;
160             }
161         }
162         else if (!serviceUuid.equals(other.serviceUuid)) {
163             return false;
164         }
165         return true;
166     }
167
168 }