blob: 015ac73d6ae1d308ee832cb9585d9e3d80a93f05 [file] [log] [blame]
jaegonkimdcf7c822019-02-06 15:00:14 +09001/*
2 * Copyright 2019-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ofoverlay.impl.util;
17
18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20
21import java.util.regex.Matcher;
22import java.util.regex.Pattern;
23
24/**
25 * Class for OVS Version.
26 */
27public final class OvsVersion {
28 protected static final Logger log = LoggerFactory.getLogger(OvsVersion.class);
29
30 private int[] versionElements = new int[] {0, 0, 0, Integer.MAX_VALUE};
31 private int depth = 0;
32
33 /**
34 * Constructor for OvsVersion.
35 * @param ovsVerStr OVS version string
36 */
37 private OvsVersion(String ovsVerStr) {
38
39 // Supporting
40 // 1.2.3 (depth = 3, public release)
41 // 1.2.3.4 (depth = 4, beta release)
42 Matcher m = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)(\\.(\\d+))?").matcher(ovsVerStr);
43
44 if (!m.matches()) {
45 throw new IllegalArgumentException("Malformed OVS version");
46 }
47
48 versionElements[0] = Integer.parseInt(m.group(1));
49 versionElements[1] = Integer.parseInt(m.group(2));
50 versionElements[2] = Integer.parseInt(m.group(3));
51
52 if (m.group(4) == null) {
53 depth = 3;
54 return;
55 }
56
57 versionElements[3] = Integer.parseInt(m.group(5));
58 depth = 4;
59 }
60
61 /**
62 * Builder for OvsVersion.
63 * @param ovsVersionStr OVS version string
64 * @return ovs version
65 */
66 public static OvsVersion build(String ovsVersionStr) {
67 try {
68 return new OvsVersion(ovsVersionStr);
69 } catch (IllegalArgumentException e) {
70 log.error("Exception Occurred {}", e);
71 return null;
72 }
73 }
74
75 private int get(int level) {
76 return versionElements[level];
77 }
78
79 private int compare(OvsVersion tgt) {
80 //Comparison example
81 // 2.7.0 < 2.7.2
82 // 2.7.0 > 2.6.9.12
83 // 2.7.0 > 2.7.0.0 (because 2.7.0 is public release)
84 for (int i = 0; i < versionElements.length; i++) {
85 if (versionElements[i] == tgt.get(i)) {
86 continue;
87 } else if (versionElements[i] < tgt.get(i)) {
88 return (i + 1) * -1;
89 } else {
90 return (i + 1);
91 }
92 }
93 return 0;
94 }
95
96 /**
97 * Returns whether this OVS version is equal to the target OVS version.
98 * @param tgt taret OVS version
99 * @return whether this OVS version is equal to the target OVS version
100 */
101 public boolean isEqOf(OvsVersion tgt) {
102 return (compare(tgt) == 0);
103 }
104
105 /**
106 * Returns whether this OVS version is prior to the target OVS version.
107 * @param tgt taret OVS version
108 * @return whether this OVS version is prior to the target OVS version
109 */
110 public boolean isPriorOf(OvsVersion tgt) {
111 return (compare(tgt) < 0);
112 }
113
114 /**
115 * Returns whether this OVS version is prior or equal to the target OVS version.
116 * @param tgt taret OVS version
117 * @return whether this OVS version is prior or equal to the target OVS version
118 */
119 public boolean isPriorOrEqOf(OvsVersion tgt) {
120 return (compare(tgt) <= 0);
121 }
122
123 /**
124 * Returns whether this OVS version is later to the target OVS version.
125 * @param tgt taret OVS version
126 * @return whether this OVS version is later to the target OVS version
127 */
128 public boolean isLaterOf(OvsVersion tgt) {
129 return (compare(tgt) > 0);
130 }
131
132 /**
133 * Returns whether this OVS version is later or equal to the target OVS version.
134 * @param tgt taret OVS version
135 * @return whether this OVS version is later or equal to the target OVS version
136 */
137 public boolean isLaterOrEqOf(OvsVersion tgt) {
138 return (compare(tgt) >= 0);
139 }
140
141 @Override
142 public String toString() {
143
144 StringBuilder strbuild = new StringBuilder();
145 strbuild.append(versionElements[0]);
146
147 for (int i = 1; i < depth; i++) {
148 strbuild.append(".");
149 strbuild.append(versionElements[i]);
150 }
151 return strbuild.toString();
152 }
153
154}