blob: 6e20290c673a4c2ae0e0740ec6b807cd24e404a3 [file] [log] [blame]
Hyunsun Moon44aac662017-02-18 02:07:01 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Hyunsun Moon44aac662017-02-18 02:07:01 +09003 *
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.openstacknetworking.impl;
17
18import com.google.common.base.Strings;
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.MacAddress;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.Host;
23import org.onosproject.net.PortNumber;
24import org.onosproject.openstacknetworking.api.InstancePort;
25
26import java.util.Objects;
27import java.util.Optional;
28
29import static com.google.common.base.Preconditions.checkArgument;
30import 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 */
38public 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}