2474da073b43920459013fb74a08ca06f69fa16a
[clamp.git] / src / main / java / org / onap / clamp / clds / model / sdc / SdcResource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-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.clds.model.sdc;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 import java.math.BigDecimal;
30 import java.util.List;
31
32 public class SdcResource implements Comparable<SdcResource> {
33
34     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcResource.class);
35     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
36
37     private String resourceInstanceName;
38     private String resourceName;
39     private String resourceInvariantUUID;
40     private String resourceVersion;
41     private String resoucreType;
42     private String resourceUuid;
43     private List<SdcArtifact> artifacts;
44
45     public String getResourceInstanceName() {
46         return resourceInstanceName;
47     }
48
49     public void setResourceInstanceName(String resourceInstanceName) {
50         this.resourceInstanceName = resourceInstanceName;
51     }
52
53     public String getResourceName() {
54         return resourceName;
55     }
56
57     public void setResourceName(String resourceName) {
58         this.resourceName = resourceName;
59     }
60
61     public String getResourceInvariantUUID() {
62         return resourceInvariantUUID;
63     }
64
65     public void setResourceInvariantUUID(String resourceInvUuid) {
66         this.resourceInvariantUUID = resourceInvUuid;
67     }
68
69     public String getResourceVersion() {
70         return resourceVersion;
71     }
72
73     public void setResourceVersion(String resourceVersion) {
74         this.resourceVersion = resourceVersion;
75     }
76
77     public String getResoucreType() {
78         return resoucreType;
79     }
80
81     public void setResoucreType(String resoucreType) {
82         this.resoucreType = resoucreType;
83     }
84
85     public String getResourceUUID() {
86         return resourceUuid;
87     }
88
89     public void setResourceUUID(String resourceUUID) {
90         this.resourceUuid = resourceUUID;
91     }
92
93     public List<SdcArtifact> getArtifacts() {
94         return artifacts;
95     }
96
97     public void setArtifacts(List<SdcArtifact> artifacts) {
98         this.artifacts = artifacts;
99     }
100
101     @Override
102     public int compareTo(SdcResource in) {
103         // Compares this object with the specified object for order.
104         // Returns a negative integer, zero, or a positive integer as this
105         // object is less than, equal to, or greater than the specified object.
106
107         // first compare based on name
108         int rtn = resourceInstanceName.compareToIgnoreCase(in.resourceInstanceName);
109
110         if (rtn == 0) {
111             BigDecimal myVersion = convertVersion(resourceVersion);
112             BigDecimal inVersion = convertVersion(in.resourceVersion);
113             rtn = myVersion.compareTo(inVersion);
114         }
115         return rtn;
116     }
117
118     @Override
119     public int hashCode() {
120         final int prime = 31;
121         int result = 1;
122         result = prime * result + ((resourceInstanceName == null) ? 0 : resourceInstanceName.hashCode());
123         result = prime * result + ((resourceVersion == null) ? 0 : resourceVersion.hashCode());
124         return result;
125     }
126
127     @Override
128     public boolean equals(Object obj) {
129         if (this == obj)
130             return true;
131         if (obj == null)
132             return false;
133         if (getClass() != obj.getClass())
134             return false;
135         SdcResource other = (SdcResource) obj;
136         if (resourceInstanceName == null) {
137             if (other.resourceInstanceName != null)
138                 return false;
139         } else if (!resourceInstanceName.equals(other.resourceInstanceName))
140             return false;
141         if (resourceVersion == null) {
142             if (other.resourceVersion != null)
143                 return false;
144         } else if (!resourceVersion.equals(other.resourceVersion))
145             return false;
146         return true;
147     }
148
149     /**
150      * Convert version String into a BigDecimal
151      *
152      * @param versionText
153      * @return
154      */
155     private BigDecimal convertVersion(String versionText) {
156         BigDecimal rtn = BigDecimal.valueOf(0.0);
157         try {
158             rtn = new BigDecimal(versionText);
159         } catch (NumberFormatException nfe) {
160             logger.warn("SDC version=" + versionText + " is not decimal for name=" + resourceInstanceName);
161         }
162         return rtn;
163     }
164 }