blob: 0d09bb306f5e7122b4e43ded5b7835de51ba760e [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;
22import org.onosproject.openstackswitching.OpenstackNetwork;
23import org.onosproject.openstackswitching.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,
41 PUBLIC_INDIRECT
42 }
43
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080044 private final CordServiceId id;
45 private final long segmentationId;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080046 private final ServiceType serviceType;
47 private final IpPrefix serviceIpRange;
48 private final IpAddress serviceIp;
Hyunsun Moonc71231d2015-12-16 20:53:23 -080049 private final Map<Host, IpAddress> hosts;
50 private final Set<CordServiceId> tenantServices;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080051
52 /**
53 * Default constructor.
54 *
Hyunsun Moonc71231d2015-12-16 20:53:23 -080055 * @param vNet OpenStack network
56 * @param hosts host and tunnel ip map
57 * @param tenantServices list of tenant service ids
Hyunsun Moon699f46b2015-12-04 11:35:25 -080058 */
Hyunsun Moonc71231d2015-12-16 20:53:23 -080059 public CordService(OpenstackNetwork vNet, OpenstackSubnet subnet,
60 Map<Host, IpAddress> hosts, Set<CordServiceId> tenantServices) {
61 this.id = CordServiceId.of(vNet.id());
62 this.segmentationId = Long.parseLong(vNet.segmentId());
63 this.serviceType = getServiceType(vNet.name());
64 this.serviceIpRange = IpPrefix.valueOf(subnet.cidr());
65 this.serviceIp = IpAddress.valueOf(subnet.gatewayIp());
66 this.hosts = hosts;
67 this.tenantServices = tenantServices;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080068 }
69
70 /**
71 * Returns service ID.
72 *
73 * @return service id
74 */
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080075 public CordServiceId id() {
76 return id;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080077 }
78
79 /**
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080080 * Returns segmentation ID of this service.
Hyunsun Moon699f46b2015-12-04 11:35:25 -080081 *
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080082 * @return segmentation id
Hyunsun Moon699f46b2015-12-04 11:35:25 -080083 */
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080084 public long segmentationId() {
85 return segmentationId;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080086 }
87
88 /**
89 * Returns service type.
90 *
91 * @return service type
92 */
93 public ServiceType serviceType() {
94 return serviceType;
95 }
96
97 /**
98 * Returns service IP range.
99 *
100 * @return CIDR
101 */
102 public IpPrefix serviceIpRange() {
103 return serviceIpRange;
104 }
105
106 /**
107 * Returns service IP address.
108 *
109 * @return ip address
110 */
111 public IpAddress serviceIp() {
112 return serviceIp;
113 }
114
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800115 /**
116 * Returns hosts associated with this service.
117 *
118 * @return list of hosts
119 */
120 public Map<Host, IpAddress> hosts() {
121 return hosts;
122 }
123
124 /**
125 * Returns tenant service IDs.
126 *
127 * @return list of tenant service id
128 */
129 public Set<CordServiceId> tenantServices() {
130 return tenantServices;
131 }
132
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800133 @Override
134 public int hashCode() {
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800135 return Objects.hash(id);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800136 }
137
138 @Override
139 public boolean equals(Object obj) {
140 if (this == obj) {
141 return true;
142 }
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800143 if (!(obj instanceof CordService)) {
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800144 return false;
145 }
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800146 final CordService other = (CordService) obj;
147 return Objects.equals(this.id, other.id);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800148 }
149
150 @Override
151 public String toString() {
152 return MoreObjects.toStringHelper(this)
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800153 .add("id", id)
154 .add("segmentationId", segmentationId)
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800155 .add("serviceType", serviceType)
156 .add("serviceIpRange", serviceIpRange)
157 .add("serviceIp", serviceIp)
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800158 .add("tenantServices", tenantServices)
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800159 .toString();
160 }
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800161
162 /**
163 * Returns network type from network name.
164 * It assumes that network name contains network type.
165 *
166 * @param netName network name
167 * @return network type, or null if it doesn't match any type
168 */
169 private ServiceType getServiceType(String netName) {
170 checkNotNull(netName);
171
172 String name = netName.toUpperCase();
173 if (name.contains(PRIVATE_DIRECT.toString())) {
174 return PRIVATE_DIRECT;
175 } else if (name.contains(PRIVATE_INDIRECT.toString())) {
176 return PRIVATE_INDIRECT;
177 } else if (name.contains(PUBLIC_DIRECT.toString())) {
178 return PUBLIC_DIRECT;
179 } else if (name.contains(PUBLIC_INDIRECT.toString())) {
180 return PUBLIC_INDIRECT;
181 } else if (name.contains(PRIVATE.toString())) {
182 return PRIVATE;
183 } else {
184 return null;
185 }
186 }
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800187}