blob: 49ed560aef94642148844f098209e7d3cb42b11e [file] [log] [blame]
Hyunsun Moon699f46b2015-12-04 11:35:25 -08001/*
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 */
16package org.onosproject.cordvtn;
17
18import com.google.common.base.MoreObjects;
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.IpPrefix;
Hyunsun Moonc71231d2015-12-16 20:53:23 -080021import org.onosproject.net.Host;
sangho93447f12016-02-24 00:33:22 +090022import org.onosproject.openstackinterface.OpenstackNetwork;
23import org.onosproject.openstackinterface.OpenstackSubnet;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080024
Hyunsun Moonc71231d2015-12-16 20:53:23 -080025import java.util.Map;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080026import java.util.Objects;
Hyunsun Moonc71231d2015-12-16 20:53:23 -080027import java.util.Set;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30import static org.onosproject.cordvtn.CordService.ServiceType.*;
31import static org.onosproject.cordvtn.CordService.ServiceType.PRIVATE;
32import static org.onosproject.cordvtn.CordService.ServiceType.PUBLIC_INDIRECT;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080033
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080034public final class CordService {
Hyunsun Moon699f46b2015-12-04 11:35:25 -080035
36 enum ServiceType {
37 PRIVATE,
38 PRIVATE_DIRECT,
39 PRIVATE_INDIRECT,
40 PUBLIC_DIRECT,
Hyunsun Moond52bffc2016-01-29 18:57:05 -080041 PUBLIC_INDIRECT,
42 MANAGEMENT
Hyunsun Moon699f46b2015-12-04 11:35:25 -080043 }
44
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080045 private final CordServiceId id;
46 private final long segmentationId;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080047 private final ServiceType serviceType;
48 private final IpPrefix serviceIpRange;
49 private final IpAddress serviceIp;
Hyunsun Moonc71231d2015-12-16 20:53:23 -080050 private final Map<Host, IpAddress> hosts;
51 private final Set<CordServiceId> tenantServices;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080052
53 /**
54 * Default constructor.
55 *
Hyunsun Moonc71231d2015-12-16 20:53:23 -080056 * @param vNet OpenStack network
Jian Lidfba7392016-01-22 16:46:58 -080057 * @param subnet OpenStack subnet
Hyunsun Moonc71231d2015-12-16 20:53:23 -080058 * @param hosts host and tunnel ip map
59 * @param tenantServices list of tenant service ids
Hyunsun Moon699f46b2015-12-04 11:35:25 -080060 */
Hyunsun Moonc71231d2015-12-16 20:53:23 -080061 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 Moon699f46b2015-12-04 11:35:25 -080070 }
71
72 /**
73 * Returns service ID.
74 *
75 * @return service id
76 */
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080077 public CordServiceId id() {
78 return id;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080079 }
80
81 /**
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080082 * Returns segmentation ID of this service.
Hyunsun Moon699f46b2015-12-04 11:35:25 -080083 *
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080084 * @return segmentation id
Hyunsun Moon699f46b2015-12-04 11:35:25 -080085 */
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080086 public long segmentationId() {
87 return segmentationId;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080088 }
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 Moonc71231d2015-12-16 20:53:23 -0800117 /**
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 Moon699f46b2015-12-04 11:35:25 -0800135 @Override
136 public int hashCode() {
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800137 return Objects.hash(id);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800138 }
139
140 @Override
141 public boolean equals(Object obj) {
142 if (this == obj) {
143 return true;
144 }
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800145 if (!(obj instanceof CordService)) {
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800146 return false;
147 }
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800148 final CordService other = (CordService) obj;
149 return Objects.equals(this.id, other.id);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800150 }
151
152 @Override
153 public String toString() {
154 return MoreObjects.toStringHelper(this)
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800155 .add("id", id)
156 .add("segmentationId", segmentationId)
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800157 .add("serviceType", serviceType)
158 .add("serviceIpRange", serviceIpRange)
159 .add("serviceIp", serviceIp)
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800160 .add("tenantServices", tenantServices)
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800161 .toString();
162 }
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800163
164 /**
165 * Returns network type from network name.
166 * It assumes that network name contains network type.
167 *
168 * @param netName network name
Hyunsun Moond52bffc2016-01-29 18:57:05 -0800169 * @return network type, or PRIVATE if it doesn't match any type
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800170 */
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 Moond52bffc2016-01-29 18:57:05 -0800183 } else if (name.contains(MANAGEMENT.toString())) {
184 return MANAGEMENT;
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800185 } else {
Hyunsun Moond52bffc2016-01-29 18:57:05 -0800186 return PRIVATE;
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800187 }
188 }
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800189}