blob: 9120fa6dd0e3f05312e5c060ada8c3f1f6b79658 [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;
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
33 enum ServiceType {
34 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 Moon699f46b2015-12-04 11:35:25 -080046
47 /**
48 * Default constructor.
49 *
Hyunsun Moonc71231d2015-12-16 20:53:23 -080050 * @param vNet OpenStack network
Jian Lidfba7392016-01-22 16:46:58 -080051 * @param subnet OpenStack subnet
Hyunsun Moonc71231d2015-12-16 20:53:23 -080052 * @param hosts host and tunnel ip map
53 * @param tenantServices list of tenant service ids
Hyunsun Moon699f46b2015-12-04 11:35:25 -080054 */
Hyunsun Moonc71231d2015-12-16 20:53:23 -080055 public CordService(OpenstackNetwork vNet, OpenstackSubnet subnet,
56 Map<Host, IpAddress> hosts, Set<CordServiceId> tenantServices) {
57 this.id = CordServiceId.of(vNet.id());
58 this.segmentationId = Long.parseLong(vNet.segmentId());
59 this.serviceType = getServiceType(vNet.name());
60 this.serviceIpRange = IpPrefix.valueOf(subnet.cidr());
61 this.serviceIp = IpAddress.valueOf(subnet.gatewayIp());
62 this.hosts = hosts;
63 this.tenantServices = tenantServices;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080064 }
65
66 /**
67 * Returns service ID.
68 *
69 * @return service id
70 */
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080071 public CordServiceId id() {
72 return id;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080073 }
74
75 /**
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080076 * Returns segmentation ID of this service.
Hyunsun Moon699f46b2015-12-04 11:35:25 -080077 *
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080078 * @return segmentation id
Hyunsun Moon699f46b2015-12-04 11:35:25 -080079 */
Hyunsun Moonbfc47d12015-12-07 14:06:28 -080080 public long segmentationId() {
81 return segmentationId;
Hyunsun Moon699f46b2015-12-04 11:35:25 -080082 }
83
84 /**
85 * Returns service type.
86 *
87 * @return service type
88 */
89 public ServiceType serviceType() {
90 return serviceType;
91 }
92
93 /**
94 * Returns service IP range.
95 *
96 * @return CIDR
97 */
98 public IpPrefix serviceIpRange() {
99 return serviceIpRange;
100 }
101
102 /**
103 * Returns service IP address.
104 *
105 * @return ip address
106 */
107 public IpAddress serviceIp() {
108 return serviceIp;
109 }
110
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800111 /**
112 * Returns hosts associated with this service.
113 *
114 * @return list of hosts
115 */
116 public Map<Host, IpAddress> hosts() {
117 return hosts;
118 }
119
120 /**
121 * Returns tenant service IDs.
122 *
123 * @return list of tenant service id
124 */
125 public Set<CordServiceId> tenantServices() {
126 return tenantServices;
127 }
128
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800129 @Override
130 public int hashCode() {
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800131 return Objects.hash(id);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800132 }
133
134 @Override
135 public boolean equals(Object obj) {
136 if (this == obj) {
137 return true;
138 }
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800139 if (!(obj instanceof CordService)) {
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800140 return false;
141 }
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800142 final CordService other = (CordService) obj;
143 return Objects.equals(this.id, other.id);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800144 }
145
146 @Override
147 public String toString() {
148 return MoreObjects.toStringHelper(this)
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800149 .add("id", id)
150 .add("segmentationId", segmentationId)
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800151 .add("serviceType", serviceType)
152 .add("serviceIpRange", serviceIpRange)
153 .add("serviceIp", serviceIp)
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800154 .add("tenantServices", tenantServices)
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800155 .toString();
156 }
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800157
158 /**
159 * Returns network type from network name.
160 * It assumes that network name contains network type.
161 *
162 * @param netName network name
Hyunsun Moond52bffc2016-01-29 18:57:05 -0800163 * @return network type, or PRIVATE if it doesn't match any type
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800164 */
165 private ServiceType getServiceType(String netName) {
166 checkNotNull(netName);
167
168 String name = netName.toUpperCase();
Hyunsun Moon098cda82016-03-03 13:27:44 -0800169 if (name.contains(ServiceType.PUBLIC.toString())) {
170 return ServiceType.PUBLIC;
171 } else if (name.contains(ServiceType.MANAGEMENT.toString())) {
172 return ServiceType.MANAGEMENT;
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800173 } else {
Hyunsun Moon098cda82016-03-03 13:27:44 -0800174 return ServiceType.PRIVATE;
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800175 }
176 }
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800177}