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