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