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