Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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.cordvtn; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.IpPrefix; |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 21 | import org.onosproject.net.Host; |
| 22 | import org.onosproject.openstackswitching.OpenstackNetwork; |
| 23 | import org.onosproject.openstackswitching.OpenstackSubnet; |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 24 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 25 | import java.util.Map; |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 26 | import java.util.Objects; |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 27 | import java.util.Set; |
| 28 | |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | import static org.onosproject.cordvtn.CordService.ServiceType.*; |
| 31 | import static org.onosproject.cordvtn.CordService.ServiceType.PRIVATE; |
| 32 | import static org.onosproject.cordvtn.CordService.ServiceType.PUBLIC_INDIRECT; |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 33 | |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 34 | public final class CordService { |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 35 | |
| 36 | enum ServiceType { |
| 37 | PRIVATE, |
| 38 | PRIVATE_DIRECT, |
| 39 | PRIVATE_INDIRECT, |
| 40 | PUBLIC_DIRECT, |
| 41 | PUBLIC_INDIRECT |
| 42 | } |
| 43 | |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 44 | private final CordServiceId id; |
| 45 | private final long segmentationId; |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 46 | private final ServiceType serviceType; |
| 47 | private final IpPrefix serviceIpRange; |
| 48 | private final IpAddress serviceIp; |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 49 | private final Map<Host, IpAddress> hosts; |
| 50 | private final Set<CordServiceId> tenantServices; |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Default constructor. |
| 54 | * |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 55 | * @param vNet OpenStack network |
Jian Li | 248e920 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 56 | * @param subnet OpenStack subnet |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 57 | * @param hosts host and tunnel ip map |
| 58 | * @param tenantServices list of tenant service ids |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 59 | */ |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 60 | public CordService(OpenstackNetwork vNet, OpenstackSubnet subnet, |
| 61 | Map<Host, IpAddress> hosts, Set<CordServiceId> tenantServices) { |
| 62 | this.id = CordServiceId.of(vNet.id()); |
| 63 | this.segmentationId = Long.parseLong(vNet.segmentId()); |
| 64 | this.serviceType = getServiceType(vNet.name()); |
| 65 | this.serviceIpRange = IpPrefix.valueOf(subnet.cidr()); |
| 66 | this.serviceIp = IpAddress.valueOf(subnet.gatewayIp()); |
| 67 | this.hosts = hosts; |
| 68 | this.tenantServices = tenantServices; |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns service ID. |
| 73 | * |
| 74 | * @return service id |
| 75 | */ |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 76 | public CordServiceId id() { |
| 77 | return id; |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 81 | * Returns segmentation ID of this service. |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 82 | * |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 83 | * @return segmentation id |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 84 | */ |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 85 | public long segmentationId() { |
| 86 | return segmentationId; |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns service type. |
| 91 | * |
| 92 | * @return service type |
| 93 | */ |
| 94 | public ServiceType serviceType() { |
| 95 | return serviceType; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Returns service IP range. |
| 100 | * |
| 101 | * @return CIDR |
| 102 | */ |
| 103 | public IpPrefix serviceIpRange() { |
| 104 | return serviceIpRange; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns service IP address. |
| 109 | * |
| 110 | * @return ip address |
| 111 | */ |
| 112 | public IpAddress serviceIp() { |
| 113 | return serviceIp; |
| 114 | } |
| 115 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 116 | /** |
| 117 | * Returns hosts associated with this service. |
| 118 | * |
| 119 | * @return list of hosts |
| 120 | */ |
| 121 | public Map<Host, IpAddress> hosts() { |
| 122 | return hosts; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns tenant service IDs. |
| 127 | * |
| 128 | * @return list of tenant service id |
| 129 | */ |
| 130 | public Set<CordServiceId> tenantServices() { |
| 131 | return tenantServices; |
| 132 | } |
| 133 | |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 134 | @Override |
| 135 | public int hashCode() { |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 136 | return Objects.hash(id); |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public boolean equals(Object obj) { |
| 141 | if (this == obj) { |
| 142 | return true; |
| 143 | } |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 144 | if (!(obj instanceof CordService)) { |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 145 | return false; |
| 146 | } |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 147 | final CordService other = (CordService) obj; |
| 148 | return Objects.equals(this.id, other.id); |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public String toString() { |
| 153 | return MoreObjects.toStringHelper(this) |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 154 | .add("id", id) |
| 155 | .add("segmentationId", segmentationId) |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 156 | .add("serviceType", serviceType) |
| 157 | .add("serviceIpRange", serviceIpRange) |
| 158 | .add("serviceIp", serviceIp) |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 159 | .add("tenantServices", tenantServices) |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 160 | .toString(); |
| 161 | } |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | * Returns network type from network name. |
| 165 | * It assumes that network name contains network type. |
| 166 | * |
| 167 | * @param netName network name |
| 168 | * @return network type, or null if it doesn't match any type |
| 169 | */ |
| 170 | private ServiceType getServiceType(String netName) { |
| 171 | checkNotNull(netName); |
| 172 | |
| 173 | String name = netName.toUpperCase(); |
| 174 | if (name.contains(PRIVATE_DIRECT.toString())) { |
| 175 | return PRIVATE_DIRECT; |
| 176 | } else if (name.contains(PRIVATE_INDIRECT.toString())) { |
| 177 | return PRIVATE_INDIRECT; |
| 178 | } else if (name.contains(PUBLIC_DIRECT.toString())) { |
| 179 | return PUBLIC_DIRECT; |
| 180 | } else if (name.contains(PUBLIC_INDIRECT.toString())) { |
| 181 | return PUBLIC_INDIRECT; |
| 182 | } else if (name.contains(PRIVATE.toString())) { |
| 183 | return PRIVATE; |
| 184 | } else { |
| 185 | return null; |
| 186 | } |
| 187 | } |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 188 | } |