Jian Li | e631216 | 2018-03-21 21:41:00 +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.openstacknode.impl; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onosproject.openstacknode.api.OpenstackPhyInterface; |
| 20 | |
| 21 | import java.util.Objects; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkArgument; |
| 24 | |
| 25 | public class DefaultOpenstackPhyInterface implements OpenstackPhyInterface { |
| 26 | |
| 27 | private final String network; |
| 28 | private final String intf; |
| 29 | |
| 30 | private static final String NOT_NULL_MSG = "% cannot be null"; |
| 31 | |
| 32 | /** |
| 33 | * A default constructor of Openstack physical interface. |
| 34 | * |
| 35 | * @param network network that this physical interface connects with |
| 36 | * @param intf name of physical interface |
| 37 | */ |
| 38 | protected DefaultOpenstackPhyInterface(String network, String intf) { |
| 39 | this.network = network; |
| 40 | this.intf = intf; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public String network() { |
| 45 | return network; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public String intf() { |
| 50 | return intf; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public boolean equals(Object obj) { |
| 55 | if (this == obj) { |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | if (obj instanceof DefaultOpenstackPhyInterface) { |
| 60 | DefaultOpenstackPhyInterface that = (DefaultOpenstackPhyInterface) obj; |
| 61 | return Objects.equals(network, that.network) && |
| 62 | Objects.equals(intf, that.intf); |
| 63 | } |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public int hashCode() { |
| 69 | return Objects.hash(network, intf); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public String toString() { |
| 74 | return MoreObjects.toStringHelper(getClass()) |
| 75 | .add("network", network) |
| 76 | .add("intf", intf) |
| 77 | .toString(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns new builder instance. |
| 82 | * |
| 83 | * @return openstack physical interface builder |
| 84 | */ |
| 85 | public static Builder builder() { |
| 86 | return new Builder(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * A builder class for openstack physical interface. |
| 91 | */ |
| 92 | public static final class Builder implements OpenstackPhyInterface.Builder { |
| 93 | |
| 94 | private String network; |
| 95 | private String intf; |
| 96 | |
| 97 | // private constructor not intended to use from external |
| 98 | private Builder() { |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public OpenstackPhyInterface build() { |
| 103 | checkArgument(network != null, NOT_NULL_MSG, "network"); |
| 104 | checkArgument(intf != null, NOT_NULL_MSG, "intf"); |
| 105 | |
| 106 | return new DefaultOpenstackPhyInterface(network, intf); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public OpenstackPhyInterface.Builder network(String network) { |
| 111 | this.network = network; |
| 112 | return this; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public OpenstackPhyInterface.Builder intf(String intf) { |
| 117 | this.intf = intf; |
| 118 | return this; |
| 119 | } |
| 120 | } |
| 121 | } |