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 | */ |
| 16 | package org.onosproject.openstacktelemetry.impl; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onlab.packet.IpPrefix; |
| 20 | import org.onlab.packet.TpPort; |
| 21 | import org.onosproject.openstacktelemetry.api.StatsFlowRule; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkArgument; |
| 24 | |
| 25 | public final class DefaultStatsFlowRule implements StatsFlowRule { |
| 26 | private final IpPrefix srcIpPrefix; |
| 27 | private final IpPrefix dstIpPrefix; |
| 28 | private final byte ipProtocol; |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 29 | private final TpPort srcTpPort; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 30 | private final TpPort dstTpPort; |
| 31 | |
| 32 | private static final String NOT_NULL_MSG = "Element % cannot be null"; |
| 33 | |
| 34 | protected DefaultStatsFlowRule(IpPrefix srcIpPrefix, |
| 35 | IpPrefix dstIpPrefix, |
| 36 | byte ipProtoco, |
| 37 | TpPort srcTpPort, |
| 38 | TpPort dstTpPort) { |
| 39 | this.srcIpPrefix = srcIpPrefix; |
| 40 | this.dstIpPrefix = dstIpPrefix; |
| 41 | this.ipProtocol = ipProtoco; |
| 42 | this.srcTpPort = srcTpPort; |
| 43 | this.dstTpPort = dstTpPort; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public IpPrefix srcIpPrefix() { |
| 48 | return srcIpPrefix; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public IpPrefix dstIpPrefix() { |
| 53 | return dstIpPrefix; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public byte ipProtocol() { |
| 58 | return ipProtocol; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public TpPort srcTpPort() { |
| 63 | return srcTpPort; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public TpPort dstTpPort() { |
| 68 | return dstTpPort; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public String toString() { |
| 73 | return MoreObjects.toStringHelper(getClass()) |
| 74 | .add("srcIpPrefix", srcIpPrefix) |
| 75 | .add("dstIpPrefix", dstIpPrefix) |
| 76 | .add("ipProtocol", ipProtocol) |
| 77 | .add("srcTpPort", srcTpPort) |
| 78 | .add("dstTpPort", dstTpPort) |
| 79 | .toString(); |
| 80 | } |
| 81 | |
| 82 | public static Builder builder() { |
| 83 | return new Builder(); |
| 84 | } |
| 85 | |
| 86 | public static Builder from(StatsFlowRule flowRule) { |
| 87 | return new Builder() |
| 88 | .srcIpPrefix(flowRule.srcIpPrefix()) |
| 89 | .dstIpPrefix(flowRule.dstIpPrefix()) |
| 90 | .ipProtocol(flowRule.ipProtocol()) |
| 91 | .srcTpPort(flowRule.srcTpPort()) |
| 92 | .dstTpPort(flowRule.dstTpPort()); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * A builder class for openstack flow rule. |
| 97 | */ |
| 98 | public static final class Builder implements StatsFlowRule.Builder { |
| 99 | private IpPrefix srcIpPrefix; |
| 100 | private IpPrefix dstIpPrefix; |
| 101 | private byte ipProtocol; |
| 102 | private TpPort srcTpPort; |
| 103 | private TpPort dstTpPort; |
| 104 | |
| 105 | // private constructor not intended to use from external |
| 106 | private Builder() { |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public DefaultStatsFlowRule build() { |
| 111 | checkArgument(srcIpPrefix != null, NOT_NULL_MSG, "Source IP Prefix"); |
| 112 | checkArgument(dstIpPrefix != null, NOT_NULL_MSG, "Destination IP Prefix"); |
| 113 | |
| 114 | return new DefaultStatsFlowRule(srcIpPrefix, |
| 115 | dstIpPrefix, |
| 116 | ipProtocol, |
| 117 | srcTpPort, |
| 118 | dstTpPort); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public Builder srcIpPrefix(IpPrefix srcIpPrefix) { |
| 123 | this.srcIpPrefix = srcIpPrefix; |
| 124 | return this; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public Builder dstIpPrefix(IpPrefix dstIpPrefix) { |
| 129 | this.dstIpPrefix = dstIpPrefix; |
| 130 | return this; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public Builder ipProtocol(byte ipProtocol) { |
| 135 | this.ipProtocol = ipProtocol; |
| 136 | return this; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public Builder srcTpPort(TpPort srcTpPort) { |
| 141 | this.srcTpPort = srcTpPort; |
| 142 | return this; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public Builder dstTpPort(TpPort dstTpPort) { |
| 147 | this.dstTpPort = dstTpPort; |
| 148 | return this; |
| 149 | } |
| 150 | } |
| 151 | } |