Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.drivers.server.impl.stats; |
| 18 | |
| 19 | import org.onosproject.drivers.server.stats.TimingStatistics; |
| 20 | |
| 21 | import com.google.common.base.MoreObjects; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkArgument; |
| 24 | |
| 25 | /** |
| 26 | * Default implementation for timing statistics. |
| 27 | */ |
| 28 | public final class DefaultTimingStatistics implements TimingStatistics { |
| 29 | |
| 30 | private final long deployCommandParsingTime; |
| 31 | private final long deployCommandLaunchingTime; |
| 32 | private long autoscaleTime; |
| 33 | |
| 34 | private DefaultTimingStatistics( |
| 35 | long parsingTime, |
| 36 | long launchingTime, |
| 37 | long autoscaleTime) { |
| 38 | checkArgument(parsingTime >= 0, "Parsing time is negative"); |
| 39 | checkArgument(launchingTime >= 0, "Launching time is negative"); |
| 40 | checkArgument(autoscaleTime >= 0, "Autoscale time is negative"); |
| 41 | |
| 42 | this.deployCommandParsingTime = parsingTime; |
| 43 | this.deployCommandLaunchingTime = launchingTime; |
| 44 | this.autoscaleTime = autoscaleTime; |
| 45 | } |
| 46 | |
| 47 | // Constructor for serializer |
| 48 | private DefaultTimingStatistics() { |
| 49 | this.deployCommandParsingTime = 0; |
| 50 | this.deployCommandLaunchingTime = 0; |
| 51 | this.autoscaleTime = 0; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Creates a builder for DefaultTimingStatistics object. |
| 56 | * |
| 57 | * @return builder object for DefaultTimingStatistics object |
| 58 | */ |
| 59 | public static DefaultTimingStatistics.Builder builder() { |
| 60 | return new Builder(); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public long deployCommandParsingTime() { |
| 65 | return this.deployCommandParsingTime; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public long deployCommandLaunchingTime() { |
| 70 | return this.deployCommandLaunchingTime; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public long totalDeploymentTime() { |
| 75 | return this.deployCommandParsingTime + this.deployCommandLaunchingTime; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public long autoscaleTime() { |
| 80 | return this.autoscaleTime; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public String toString() { |
| 85 | return MoreObjects.toStringHelper(this) |
| 86 | .omitNullValues() |
| 87 | .add("parsing", this.deployCommandParsingTime()) |
| 88 | .add("launching", this.deployCommandLaunchingTime()) |
| 89 | .add("total", this.totalDeploymentTime()) |
| 90 | .add("autoscale", this.autoscaleTime()) |
| 91 | .toString(); |
| 92 | } |
| 93 | |
| 94 | public static final class Builder { |
| 95 | |
| 96 | long deployCommandParsingTime; |
| 97 | long deployCommandLaunchingTime; |
| 98 | long autoscaleTime; |
| 99 | |
| 100 | private Builder() { |
| 101 | |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Sets parsing time. |
| 106 | * |
| 107 | * @param parsingTime parsing time |
| 108 | * @return builder object |
| 109 | */ |
| 110 | public Builder setParsingTime(long parsingTime) { |
| 111 | this.deployCommandParsingTime = parsingTime; |
| 112 | |
| 113 | return this; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Sets launching time. |
| 118 | * |
| 119 | * @param launchingTime launching time |
| 120 | * @return builder object |
| 121 | */ |
| 122 | public Builder setLaunchingTime(long launchingTime) { |
| 123 | this.deployCommandLaunchingTime = launchingTime; |
| 124 | |
| 125 | return this; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Sets autoscale time. |
| 130 | * |
| 131 | * @param autoscaleTime time required to autoscale |
| 132 | * @return builder object |
| 133 | */ |
| 134 | public Builder setAutoscaleTime(long autoscaleTime) { |
| 135 | this.autoscaleTime = autoscaleTime; |
| 136 | |
| 137 | return this; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Creates a DefaultTimingStatistics object. |
| 142 | * |
| 143 | * @return DefaultTimingStatistics object |
| 144 | */ |
| 145 | public DefaultTimingStatistics build() { |
| 146 | return new DefaultTimingStatistics( |
| 147 | deployCommandParsingTime, |
| 148 | deployCommandLaunchingTime, |
| 149 | autoscaleTime |
| 150 | ); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | } |