blob: 0fe253d1347ef5f3d0abba0188442dda11d20f1d [file] [log] [blame]
Georgios Katsikas83600982017-05-28 20:41:45 +02001/*
2 * Copyright 2017-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 */
16
17package org.onosproject.drivers.server.impl.stats;
18
Georgios Katsikasfda66742018-07-31 20:18:14 +020019import org.onosproject.drivers.server.stats.MonitoringUnit;
Georgios Katsikas83600982017-05-28 20:41:45 +020020import org.onosproject.drivers.server.stats.TimingStatistics;
21
Georgios Katsikasfda66742018-07-31 20:18:14 +020022import com.google.common.base.Strings;
Georgios Katsikas83600982017-05-28 20:41:45 +020023import com.google.common.base.MoreObjects;
24
25import static com.google.common.base.Preconditions.checkArgument;
Georgios Katsikasfda66742018-07-31 20:18:14 +020026import static com.google.common.base.Preconditions.checkNotNull;
27import static org.onosproject.drivers.server.stats.MonitoringUnit.LatencyUnit;
Georgios Katsikas83600982017-05-28 20:41:45 +020028
29/**
30 * Default implementation for timing statistics.
31 */
32public final class DefaultTimingStatistics implements TimingStatistics {
33
Georgios Katsikasfda66742018-07-31 20:18:14 +020034 private static final LatencyUnit DEF_UNIT = LatencyUnit.NANO_SECOND;
35
36 private final MonitoringUnit unit;
Georgios Katsikas83600982017-05-28 20:41:45 +020037 private final long deployCommandParsingTime;
38 private final long deployCommandLaunchingTime;
39 private long autoscaleTime;
40
41 private DefaultTimingStatistics(
Georgios Katsikasfda66742018-07-31 20:18:14 +020042 MonitoringUnit unit,
Georgios Katsikas83600982017-05-28 20:41:45 +020043 long parsingTime,
44 long launchingTime,
45 long autoscaleTime) {
Georgios Katsikasfda66742018-07-31 20:18:14 +020046 checkNotNull(unit, "Time statistics unit is null");
Georgios Katsikas83600982017-05-28 20:41:45 +020047 checkArgument(parsingTime >= 0, "Parsing time is negative");
48 checkArgument(launchingTime >= 0, "Launching time is negative");
49 checkArgument(autoscaleTime >= 0, "Autoscale time is negative");
50
Georgios Katsikasfda66742018-07-31 20:18:14 +020051 this.unit = unit;
Georgios Katsikas83600982017-05-28 20:41:45 +020052 this.deployCommandParsingTime = parsingTime;
53 this.deployCommandLaunchingTime = launchingTime;
54 this.autoscaleTime = autoscaleTime;
55 }
56
57 // Constructor for serializer
58 private DefaultTimingStatistics() {
Georgios Katsikasfda66742018-07-31 20:18:14 +020059 this.unit = null;
Georgios Katsikas83600982017-05-28 20:41:45 +020060 this.deployCommandParsingTime = 0;
61 this.deployCommandLaunchingTime = 0;
62 this.autoscaleTime = 0;
63 }
64
65 /**
66 * Creates a builder for DefaultTimingStatistics object.
67 *
68 * @return builder object for DefaultTimingStatistics object
69 */
70 public static DefaultTimingStatistics.Builder builder() {
71 return new Builder();
72 }
73
74 @Override
Georgios Katsikasfda66742018-07-31 20:18:14 +020075 public MonitoringUnit unit() {
76 return this.unit;
77 }
78
79 @Override
Georgios Katsikas83600982017-05-28 20:41:45 +020080 public long deployCommandParsingTime() {
81 return this.deployCommandParsingTime;
82 }
83
84 @Override
85 public long deployCommandLaunchingTime() {
86 return this.deployCommandLaunchingTime;
87 }
88
89 @Override
90 public long totalDeploymentTime() {
91 return this.deployCommandParsingTime + this.deployCommandLaunchingTime;
92 }
93
94 @Override
95 public long autoscaleTime() {
96 return this.autoscaleTime;
97 }
98
99 @Override
100 public String toString() {
101 return MoreObjects.toStringHelper(this)
102 .omitNullValues()
Georgios Katsikasfda66742018-07-31 20:18:14 +0200103 .add("unit", this.unit().toString())
104 .add("parsingTime", this.deployCommandParsingTime())
105 .add("launchingTime", this.deployCommandLaunchingTime())
106 .add("deploymentTime", this.totalDeploymentTime())
107 .add("autoScaleTime", this.autoscaleTime())
Georgios Katsikas83600982017-05-28 20:41:45 +0200108 .toString();
109 }
110
111 public static final class Builder {
112
Georgios Katsikasfda66742018-07-31 20:18:14 +0200113 MonitoringUnit unit = DEF_UNIT;
Georgios Katsikas83600982017-05-28 20:41:45 +0200114 long deployCommandParsingTime;
115 long deployCommandLaunchingTime;
116 long autoscaleTime;
117
118 private Builder() {
119
120 }
121
122 /**
Georgios Katsikasfda66742018-07-31 20:18:14 +0200123 * Sets time statistics unit.
124 *
125 * @param unitStr time statistics unit as a string
126 * @return builder object
127 */
128 public Builder setUnit(String unitStr) {
129 if (!Strings.isNullOrEmpty(unitStr)) {
130 this.unit = LatencyUnit.getByName(unitStr);
131 }
132
133 return this;
134 }
135
136 /**
Georgios Katsikas83600982017-05-28 20:41:45 +0200137 * Sets parsing time.
138 *
139 * @param parsingTime parsing time
140 * @return builder object
141 */
142 public Builder setParsingTime(long parsingTime) {
143 this.deployCommandParsingTime = parsingTime;
144
145 return this;
146 }
147
148 /**
149 * Sets launching time.
150 *
151 * @param launchingTime launching time
152 * @return builder object
153 */
154 public Builder setLaunchingTime(long launchingTime) {
155 this.deployCommandLaunchingTime = launchingTime;
156
157 return this;
158 }
159
160 /**
161 * Sets autoscale time.
162 *
163 * @param autoscaleTime time required to autoscale
164 * @return builder object
165 */
166 public Builder setAutoscaleTime(long autoscaleTime) {
167 this.autoscaleTime = autoscaleTime;
168
169 return this;
170 }
171
172 /**
173 * Creates a DefaultTimingStatistics object.
174 *
175 * @return DefaultTimingStatistics object
176 */
177 public DefaultTimingStatistics build() {
178 return new DefaultTimingStatistics(
Georgios Katsikasfda66742018-07-31 20:18:14 +0200179 unit,
Georgios Katsikas83600982017-05-28 20:41:45 +0200180 deployCommandParsingTime,
181 deployCommandLaunchingTime,
182 autoscaleTime
183 );
184 }
185 }
186
187}