blob: be2b810722488a152aa5255fe94727fec73012f2 [file] [log] [blame]
Hyunsun Moon699f46b2015-12-04 11:35:25 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Hyunsun Moon699f46b2015-12-04 11:35:25 -08003 *
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 */
Hyunsun Moon7f4ed9d2016-04-14 16:13:42 -070016package org.onosproject.cordvtn.api;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080017
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;
Hyunsun Moon576d6872016-04-14 19:04:23 -070022import org.openstack4j.model.network.Network;
23import org.openstack4j.model.network.Subnet;
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;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080030
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080031public final class CordService {
Hyunsun Moon699f46b2015-12-04 11:35:25 -080032
Hyunsun Moon7f4ed9d2016-04-14 16:13:42 -070033 public enum ServiceType {
Hyunsun Moon699f46b2015-12-04 11:35:25 -080034 PRIVATE,
Hyunsun Moon098cda82016-03-03 13:27:44 -080035 PUBLIC,
Hyunsun Moond52bffc2016-01-29 18:57:05 -080036 MANAGEMENT
Hyunsun Moon699f46b2015-12-04 11:35:25 -080037 }
38
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080039 private final CordServiceId id;
40 private final long segmentationId;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080041 private final ServiceType serviceType;
42 private final IpPrefix serviceIpRange;
43 private final IpAddress serviceIp;
Hyunsun Moonc71231d2015-12-16 20:53:23 -080044 private final Map<Host, IpAddress> hosts;
45 private final Set<CordServiceId> tenantServices;
Hyunsun Moondd91be22016-04-24 17:43:32 -070046 private final Set<CordServiceId> providerServices;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080047
48 /**
49 * Default constructor.
50 *
Hyunsun Moon576d6872016-04-14 19:04:23 -070051 * @param osNet OpenStack network
52 * @param osSubnet OpenStack subnet
Hyunsun Moonc71231d2015-12-16 20:53:23 -080053 * @param hosts host and tunnel ip map
54 * @param tenantServices list of tenant service ids
Hyunsun Moondd91be22016-04-24 17:43:32 -070055 * @param providerServices list of provider service ids
Hyunsun Moon699f46b2015-12-04 11:35:25 -080056 */
Hyunsun Moon576d6872016-04-14 19:04:23 -070057 public CordService(Network osNet, Subnet osSubnet,
Hyunsun Moondd91be22016-04-24 17:43:32 -070058 Map<Host, IpAddress> hosts, Set<CordServiceId> tenantServices,
59 Set<CordServiceId> providerServices) {
Hyunsun Moon576d6872016-04-14 19:04:23 -070060 this.id = CordServiceId.of(osNet.getId());
61 this.segmentationId = Long.parseLong(osNet.getProviderSegID());
62 this.serviceType = getServiceType(osNet.getName());
63 this.serviceIpRange = IpPrefix.valueOf(osSubnet.getCidr());
64 this.serviceIp = IpAddress.valueOf(osSubnet.getGateway());
Hyunsun Moonc71231d2015-12-16 20:53:23 -080065 this.hosts = hosts;
66 this.tenantServices = tenantServices;
Hyunsun Moondd91be22016-04-24 17:43:32 -070067 this.providerServices = providerServices;
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 Moondd91be22016-04-24 17:43:32 -0700133 /**
134 * Returns provider service IDs.
135 *
136 * @return list of provider service id
137 */
138 public Set<CordServiceId> providerServices() {
139 return providerServices;
140 }
141
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800142 @Override
143 public int hashCode() {
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800144 return Objects.hash(id);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800145 }
146
147 @Override
148 public boolean equals(Object obj) {
149 if (this == obj) {
150 return true;
151 }
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800152 if (!(obj instanceof CordService)) {
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800153 return false;
154 }
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800155 final CordService other = (CordService) obj;
156 return Objects.equals(this.id, other.id);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800157 }
158
159 @Override
160 public String toString() {
161 return MoreObjects.toStringHelper(this)
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800162 .add("id", id)
163 .add("segmentationId", segmentationId)
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800164 .add("serviceType", serviceType)
165 .add("serviceIpRange", serviceIpRange)
166 .add("serviceIp", serviceIp)
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800167 .add("tenantServices", tenantServices)
Hyunsun Moondd91be22016-04-24 17:43:32 -0700168 .add("providerServices", providerServices)
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800169 .toString();
170 }
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800171
172 /**
173 * Returns network type from network name.
174 * It assumes that network name contains network type.
175 *
176 * @param netName network name
Hyunsun Moond52bffc2016-01-29 18:57:05 -0800177 * @return network type, or PRIVATE if it doesn't match any type
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800178 */
Hyunsun Moondd91be22016-04-24 17:43:32 -0700179 public static ServiceType getServiceType(String netName) {
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800180 checkNotNull(netName);
181
182 String name = netName.toUpperCase();
Hyunsun Moon098cda82016-03-03 13:27:44 -0800183 if (name.contains(ServiceType.PUBLIC.toString())) {
184 return ServiceType.PUBLIC;
185 } else if (name.contains(ServiceType.MANAGEMENT.toString())) {
186 return ServiceType.MANAGEMENT;
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800187 } else {
Hyunsun Moon098cda82016-03-03 13:27:44 -0800188 return ServiceType.PRIVATE;
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800189 }
190 }
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800191}