Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | */ |
Jian Li | 7fe7eaf | 2018-12-31 17:00:33 +0900 | [diff] [blame^] | 16 | package org.onosproject.openstacktelemetry.api; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onlab.packet.IpPrefix; |
| 20 | import org.onlab.packet.TpPort; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 21 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 22 | import java.util.Objects; |
| 23 | |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 26 | /** |
| 27 | * Implementation of StatsFlowRule. |
| 28 | */ |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 29 | public final class DefaultStatsFlowRule implements StatsFlowRule { |
| 30 | private final IpPrefix srcIpPrefix; |
| 31 | private final IpPrefix dstIpPrefix; |
| 32 | private final byte ipProtocol; |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 33 | private final TpPort srcTpPort; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 34 | private final TpPort dstTpPort; |
| 35 | |
| 36 | private static final String NOT_NULL_MSG = "Element % cannot be null"; |
| 37 | |
| 38 | protected DefaultStatsFlowRule(IpPrefix srcIpPrefix, |
| 39 | IpPrefix dstIpPrefix, |
| 40 | byte ipProtoco, |
| 41 | TpPort srcTpPort, |
| 42 | TpPort dstTpPort) { |
| 43 | this.srcIpPrefix = srcIpPrefix; |
| 44 | this.dstIpPrefix = dstIpPrefix; |
| 45 | this.ipProtocol = ipProtoco; |
| 46 | this.srcTpPort = srcTpPort; |
| 47 | this.dstTpPort = dstTpPort; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public IpPrefix srcIpPrefix() { |
| 52 | return srcIpPrefix; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public IpPrefix dstIpPrefix() { |
| 57 | return dstIpPrefix; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public byte ipProtocol() { |
| 62 | return ipProtocol; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public TpPort srcTpPort() { |
| 67 | return srcTpPort; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public TpPort dstTpPort() { |
| 72 | return dstTpPort; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public String toString() { |
| 77 | return MoreObjects.toStringHelper(getClass()) |
| 78 | .add("srcIpPrefix", srcIpPrefix) |
| 79 | .add("dstIpPrefix", dstIpPrefix) |
| 80 | .add("ipProtocol", ipProtocol) |
| 81 | .add("srcTpPort", srcTpPort) |
| 82 | .add("dstTpPort", dstTpPort) |
| 83 | .toString(); |
| 84 | } |
| 85 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 86 | @Override |
| 87 | public boolean equals(Object obj) { |
| 88 | if (this == obj) { |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | if (obj instanceof DefaultStatsFlowRule) { |
| 93 | final DefaultStatsFlowRule other = (DefaultStatsFlowRule) obj; |
| 94 | return Objects.equals(this.srcIpPrefix, other.srcIpPrefix) && |
| 95 | Objects.equals(this.dstIpPrefix, other.dstIpPrefix) && |
| 96 | Objects.equals(this.srcTpPort, other.srcTpPort) && |
| 97 | Objects.equals(this.dstTpPort, other.dstTpPort) && |
| 98 | Objects.equals(this.ipProtocol, other.ipProtocol); |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public int hashCode() { |
| 105 | return Objects.hash(srcIpPrefix, dstIpPrefix, srcTpPort, dstTpPort, ipProtocol); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Obtains a default stats flow rule builder object. |
| 110 | * |
| 111 | * @return flow rule builder object |
| 112 | */ |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 113 | public static Builder builder() { |
| 114 | return new Builder(); |
| 115 | } |
| 116 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 117 | /** |
| 118 | * Obtains the builder object existing flow rule. |
| 119 | * |
| 120 | * @param flowRule flow rule |
| 121 | * @return builder object |
| 122 | */ |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 123 | public static Builder from(StatsFlowRule flowRule) { |
| 124 | return new Builder() |
| 125 | .srcIpPrefix(flowRule.srcIpPrefix()) |
| 126 | .dstIpPrefix(flowRule.dstIpPrefix()) |
| 127 | .ipProtocol(flowRule.ipProtocol()) |
| 128 | .srcTpPort(flowRule.srcTpPort()) |
| 129 | .dstTpPort(flowRule.dstTpPort()); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * A builder class for openstack flow rule. |
| 134 | */ |
| 135 | public static final class Builder implements StatsFlowRule.Builder { |
| 136 | private IpPrefix srcIpPrefix; |
| 137 | private IpPrefix dstIpPrefix; |
| 138 | private byte ipProtocol; |
| 139 | private TpPort srcTpPort; |
| 140 | private TpPort dstTpPort; |
| 141 | |
| 142 | // private constructor not intended to use from external |
| 143 | private Builder() { |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public DefaultStatsFlowRule build() { |
| 148 | checkArgument(srcIpPrefix != null, NOT_NULL_MSG, "Source IP Prefix"); |
| 149 | checkArgument(dstIpPrefix != null, NOT_NULL_MSG, "Destination IP Prefix"); |
| 150 | |
| 151 | return new DefaultStatsFlowRule(srcIpPrefix, |
| 152 | dstIpPrefix, |
| 153 | ipProtocol, |
| 154 | srcTpPort, |
| 155 | dstTpPort); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public Builder srcIpPrefix(IpPrefix srcIpPrefix) { |
| 160 | this.srcIpPrefix = srcIpPrefix; |
| 161 | return this; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public Builder dstIpPrefix(IpPrefix dstIpPrefix) { |
| 166 | this.dstIpPrefix = dstIpPrefix; |
| 167 | return this; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public Builder ipProtocol(byte ipProtocol) { |
| 172 | this.ipProtocol = ipProtocol; |
| 173 | return this; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public Builder srcTpPort(TpPort srcTpPort) { |
| 178 | this.srcTpPort = srcTpPort; |
| 179 | return this; |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public Builder dstTpPort(TpPort dstTpPort) { |
| 184 | this.dstTpPort = dstTpPort; |
| 185 | return this; |
| 186 | } |
| 187 | } |
| 188 | } |