Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 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.openstacknetworking.impl; |
| 17 | |
| 18 | import com.google.common.base.Strings; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.Host; |
| 23 | import org.onosproject.net.PortNumber; |
| 24 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 25 | |
| 26 | import java.util.Objects; |
| 27 | import java.util.Optional; |
| 28 | |
| 29 | import static com.google.common.base.Preconditions.checkArgument; |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * Implementation of instance port based on host subsystem. |
| 34 | * Basically, HostBasedInstancePort is just a wrapper of a host, which helps |
| 35 | * mapping between OpenStack port and the OVS port and retrieving information |
| 36 | * such as IP address, location, and so on. |
| 37 | */ |
| 38 | public final class HostBasedInstancePort implements InstancePort { |
| 39 | |
| 40 | static final String ANNOTATION_NETWORK_ID = "networkId"; |
| 41 | static final String ANNOTATION_PORT_ID = "portId"; |
| 42 | static final String ANNOTATION_CREATE_TIME = "createTime"; |
| 43 | |
| 44 | private final Host host; |
| 45 | |
| 46 | /** |
| 47 | * Default constructor. |
| 48 | * |
| 49 | * @param instance host object of this instance |
| 50 | */ |
| 51 | private HostBasedInstancePort(Host instance) { |
| 52 | this.host = instance; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns new instance. |
| 57 | * |
| 58 | * @param host host object of this instance |
| 59 | * @return instance |
| 60 | */ |
| 61 | public static HostBasedInstancePort of(Host host) { |
| 62 | checkNotNull(host); |
| 63 | checkArgument(!Strings.isNullOrEmpty(host.annotations().value(ANNOTATION_NETWORK_ID))); |
| 64 | checkArgument(!Strings.isNullOrEmpty(host.annotations().value(ANNOTATION_PORT_ID))); |
| 65 | checkArgument(!Strings.isNullOrEmpty(host.annotations().value(ANNOTATION_CREATE_TIME))); |
| 66 | |
| 67 | return new HostBasedInstancePort(host); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public String networkId() { |
| 72 | return host.annotations().value(ANNOTATION_NETWORK_ID); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public String portId() { |
| 77 | return host.annotations().value(ANNOTATION_PORT_ID); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public MacAddress macAddress() { |
| 82 | return host.mac(); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public IpAddress ipAddress() { |
| 87 | Optional<IpAddress> ipAddr = host.ipAddresses().stream().findFirst(); |
| 88 | return ipAddr.orElse(null); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public DeviceId deviceId() { |
| 93 | return host.location().deviceId(); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public PortNumber portNumber() { |
| 98 | return host.location().port(); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public String toString() { |
| 103 | return host.toString(); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public boolean equals(Object obj) { |
| 108 | if (this == obj) { |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | if (obj instanceof HostBasedInstancePort) { |
| 113 | HostBasedInstancePort that = (HostBasedInstancePort) obj; |
| 114 | if (Objects.equals(this.portId(), that.portId())) { |
| 115 | return true; |
| 116 | } |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public int hashCode() { |
| 123 | return Objects.hash(portId()); |
| 124 | } |
| 125 | } |