Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / common / src / main / java / org / commscope / tr069adapter / common / deviceversion / DeviceVersion.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : tr-069-adapter
4  * =================================================================================================
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.
6  * =================================================================================================
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
9  * may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ===============LICENSE_END=======================================================================
17  */
18
19 package org.commscope.tr069adapter.common.deviceversion;
20
21 import java.io.Serializable;
22 import java.util.Comparator;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class DeviceVersion implements Serializable, Comparable<DeviceVersion> {
28
29   /**
30    * 
31    */
32   private static final Logger LOG = LoggerFactory.getLogger(DeviceVersion.class);
33   private static final long serialVersionUID = -7251276716604249440L;
34   private int svMajorVersion = 0;
35   private int svMinorVersion = 0;
36   private int svPatchVersion = 0;
37   private String swVersion;
38   private boolean isGenericVersion = false;
39   private boolean isHwRegex = false;
40   private boolean isSwRegex = false;
41
42   public DeviceVersion(String swVersion, String hwVersion) {
43     super();
44     setSwVersion(swVersion);
45     this.hwVersion = hwVersion;
46   }
47
48   public DeviceVersion(String swVersion, String hwVersion, boolean isSwRegex, boolean isHwRegex) {
49     super();
50     this.hwVersion = hwVersion;
51     this.swVersion = swVersion;
52     this.isHwRegex = isHwRegex;
53     this.isSwRegex = isSwRegex;
54     if (!isSwRegex) {
55       setSwVersion(swVersion);
56     }
57   }
58
59   public String getSwVersion() {
60     if (!isSwRegex)
61       return svMajorVersion + "." + svMinorVersion + "." + svPatchVersion;
62     else
63       return this.swVersion;
64   }
65
66   public boolean isHwRegex() {
67     return isHwRegex;
68   }
69
70   public boolean isSwRegex() {
71     return isSwRegex;
72   }
73
74   private void setSwVersion(String swVersion) {
75     if (swVersion.indexOf(".") > 0) {
76       String[] verArray = swVersion.split("\\.");
77
78       for (int i = 0; i < verArray.length; i++) {
79
80         if (verArray[i].equals("*")) {
81           verArray[i] = "0";
82         }
83       }
84       try {
85         svMajorVersion = Integer.parseInt(verArray[0]);
86         svMinorVersion = Integer.parseInt(verArray[1]);
87         svPatchVersion = Integer.parseInt(verArray[2]);
88       } catch (Exception e) {
89         LOG.error("Software Version setting has failed. {}", e.toString());
90       }
91
92     } else if (swVersion.indexOf("x") > 0) {
93       swVersion = "*";
94     } else if (swVersion.equals("*")) {
95       isGenericVersion = true;
96     }
97
98   }
99
100   public String getHwVersion() {
101     return hwVersion;
102   }
103
104   private String hwVersion;
105
106   public int getSvMajorVersion() {
107     return svMajorVersion;
108   }
109
110   public int getSvMinorVersion() {
111     return svMinorVersion;
112   }
113
114   public int getSvPatchVersion() {
115     return svPatchVersion;
116   }
117
118   private long deviceTypeId;
119
120   public long getDeviceTypeId() {
121     return deviceTypeId;
122   }
123
124   public boolean isGenericVersion() {
125     return isGenericVersion;
126   }
127
128   public static final Comparator<DeviceVersion> softwareComparator =
129       new Comparator<DeviceVersion>() {
130         @Override
131         public int compare(DeviceVersion d1, DeviceVersion d2) {
132           if (d1.getSvMajorVersion() != d2.getSvMajorVersion()) {
133             return (d1.getSvMajorVersion() - d2.getSvMajorVersion());
134           } else if (d1.getSvMinorVersion() != d2.getSvMinorVersion()) {
135             return d1.getSvMinorVersion() - d2.getSvMinorVersion();
136           } else {
137             return d1.getSvPatchVersion() - d2.getSvPatchVersion();
138           }
139         }
140       };
141
142   @Override
143   public int compareTo(DeviceVersion o) {
144     if (deviceTypeId != o.deviceTypeId)
145       return -1;
146
147     if (isSwRegex) {
148       return this.hashCode() - o.hashCode();
149     } else {
150       if (svMajorVersion != o.svMajorVersion) {
151         return (svMajorVersion - o.svMajorVersion);
152       } else if (svMinorVersion != o.svMinorVersion) {
153         return svMinorVersion - o.svMinorVersion;
154       } else if (svPatchVersion != o.svPatchVersion) {
155         return svPatchVersion - o.svPatchVersion;
156       } else {
157         return hwVersion.compareToIgnoreCase(o.hwVersion);
158       }
159     }
160   }
161
162   @Override
163   public boolean equals(Object obj) {
164     if (this == obj)
165       return true;
166     if (obj == null)
167       return false;
168     if (getClass() != obj.getClass())
169       return false;
170     DeviceVersion other = (DeviceVersion) obj;
171     if (deviceTypeId != other.deviceTypeId)
172       return false;
173     if (hwVersion == null && other.hwVersion != null) {
174       return false;
175     }
176     if (isGenericVersion != other.isGenericVersion)
177       return false;
178     if (isHwRegex != other.isHwRegex)
179       return false;
180     if (isSwRegex != other.isSwRegex)
181       return false;
182     if (svMajorVersion != other.svMajorVersion)
183       return false;
184     if (svMinorVersion != other.svMinorVersion)
185       return false;
186     if (svPatchVersion != other.svPatchVersion)
187       return false;
188     if (swVersion == null) {
189       if (other.swVersion != null)
190         return false;
191     }
192     return true;
193   }
194
195   @Override
196   public int hashCode() {
197     final int prime = 31;
198     int result = 1;
199     result = prime * result + (int) (deviceTypeId ^ (deviceTypeId >>> 32));
200     result = prime * result + ((hwVersion == null) ? 0 : hwVersion.hashCode());
201     result = prime * result + (isGenericVersion ? 1231 : 1237);
202     result = prime * result + (isHwRegex ? 1231 : 1237);
203     result = prime * result + (isSwRegex ? 1241 : 1247);
204     result = prime * result + svMajorVersion;
205     result = prime * result + svMinorVersion;
206     result = prime * result + svPatchVersion;
207     return result;
208   }
209 }