Daniel Park | d02d7bd | 2018-08-23 23:04:31 +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 | 2a2d26c | 2018-10-08 11:29:31 +0900 | [diff] [blame] | 16 | package org.onosproject.openstacknode.api; |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame] | 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.collect.ImmutableList; |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame] | 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.Collection; |
| 23 | import java.util.Objects; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkArgument; |
| 26 | |
| 27 | /** |
| 28 | * Implementation of dpdk config. |
| 29 | */ |
| 30 | public final class DefaultDpdkConfig implements DpdkConfig { |
| 31 | |
| 32 | private final DatapathType datapathType; |
| 33 | private final String socketDir; |
| 34 | private final Collection<DpdkInterface> dpdkIntfs; |
| 35 | |
| 36 | private static final String NOT_NULL_MSG = "% cannot be null"; |
| 37 | |
| 38 | private DefaultDpdkConfig(DatapathType datapathType, |
| 39 | String socketDir, |
| 40 | Collection<DpdkInterface> dpdkIntfs) { |
| 41 | this.datapathType = datapathType; |
| 42 | this.socketDir = socketDir; |
| 43 | this.dpdkIntfs = dpdkIntfs; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public boolean equals(Object o) { |
| 48 | if (this == o) { |
| 49 | return true; |
| 50 | } |
| 51 | if (!(o instanceof DefaultDpdkConfig)) { |
| 52 | return false; |
| 53 | } |
| 54 | DefaultDpdkConfig that = (DefaultDpdkConfig) o; |
| 55 | return datapathType == that.datapathType && |
| 56 | Objects.equals(socketDir, that.socketDir) && |
| 57 | Objects.equals(dpdkIntfs, that.dpdkIntfs); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public int hashCode() { |
| 62 | return Objects.hash(datapathType, |
| 63 | socketDir, |
| 64 | dpdkIntfs); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public String toString() { |
| 69 | return MoreObjects.toStringHelper(this) |
| 70 | .add("datapathType", datapathType) |
| 71 | .add("socketDir", socketDir) |
| 72 | .add("dpdkIntfs", dpdkIntfs) |
| 73 | .toString(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns the data path type. |
| 78 | * |
| 79 | * @return data path type; normal or netdev |
| 80 | */ |
| 81 | @Override |
| 82 | public DatapathType datapathType() { |
| 83 | return datapathType; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns socket directory which dpdk port bound to. |
| 88 | * |
| 89 | * @return socket directory |
| 90 | */ |
| 91 | @Override |
| 92 | public String socketDir() { |
| 93 | return socketDir; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns a collection of dpdk interfaces. |
| 98 | * |
| 99 | * @return dpdk interfaces |
| 100 | */ |
| 101 | @Override |
| 102 | public Collection<DpdkInterface> dpdkIntfs() { |
| 103 | if (dpdkIntfs == null) { |
| 104 | return new ArrayList<>(); |
| 105 | } |
| 106 | return ImmutableList.copyOf(dpdkIntfs); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns new builder instance. |
| 111 | * |
| 112 | * @return dpdk config builder instance |
| 113 | */ |
| 114 | public static Builder builder() { |
| 115 | return new Builder(); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Builder of DpdkConfig instance. |
| 120 | */ |
| 121 | public static final class Builder implements DpdkConfig.Builder { |
| 122 | private DatapathType datapathType; |
| 123 | private String socketDir; |
| 124 | private Collection<DpdkInterface> dpdkIntfs; |
| 125 | |
| 126 | private Builder() { |
| 127 | |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public DpdkConfig build() { |
| 132 | checkArgument(datapathType != null, NOT_NULL_MSG, "datapathType"); |
| 133 | return new DefaultDpdkConfig(datapathType, |
| 134 | socketDir, |
| 135 | dpdkIntfs); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public Builder datapathType(DatapathType datapathType) { |
| 140 | this.datapathType = datapathType; |
| 141 | return this; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public Builder socketDir(String socketDir) { |
| 146 | this.socketDir = socketDir; |
| 147 | return this; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public Builder dpdkIntfs(Collection<DpdkInterface> dpdkIntfs) { |
| 152 | this.dpdkIntfs = dpdkIntfs; |
| 153 | return this; |
| 154 | } |
| 155 | } |
| 156 | } |