blob: f8776309c3a9a1f3b7d8c0ae24039a8fd205371f [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
Jian Lidfba7392016-01-22 16:46:58 -080056 * @param subnet OpenStack subnet
Hyunsun Moonc71231d2015-12-16 20:53:23 -080057 * @param hosts host and tunnel ip map
58 * @param tenantServices list of tenant service ids
Hyunsun Moon699f46b2015-12-04 11:35:25 -080059 */
Hyunsun Moonc71231d2015-12-16 20:53:23 -080060 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 Moon699f46b2015-12-04 11:35:25 -080069 }
70
71 /**
72 * Returns service ID.
73 *
74 * @return service id
75 */
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080076 public CordServiceId id() {
77 return id;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080078 }
79
80 /**
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080081 * Returns segmentation ID of this service.
Hyunsun Moon699f46b2015-12-04 11:35:25 -080082 *
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080083 * @return segmentation id
Hyunsun Moon699f46b2015-12-04 11:35:25 -080084 */
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080085 public long segmentationId() {
86 return segmentationId;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080087 }
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 Moonc71231d2015-12-16 20:53:23 -0800116 /**
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 Moon699f46b2015-12-04 11:35:25 -0800134 @Override
135 public int hashCode() {
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800136 return Objects.hash(id);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800137 }
138
139 @Override
140 public boolean equals(Object obj) {
141 if (this == obj) {
142 return true;
143 }
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800144 if (!(obj instanceof CordService)) {
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800145 return false;
146 }
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800147 final CordService other = (CordService) obj;
148 return Objects.equals(this.id, other.id);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800149 }
150
151 @Override
152 public String toString() {
153 return MoreObjects.toStringHelper(this)
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800154 .add("id", id)
155 .add("segmentationId", segmentationId)
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800156 .add("serviceType", serviceType)
157 .add("serviceIpRange", serviceIpRange)
158 .add("serviceIp", serviceIp)
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800159 .add("tenantServices", tenantServices)
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800160 .toString();
161 }
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800162
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 Moon699f46b2015-12-04 11:35:25 -0800188}