blob: 9878de0a70f2e68c0fd97d487c14f5370c111ec2 [file] [log] [blame]
Jian Liecae4382018-06-28 17:41:12 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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.openstacknetworking.impl;
17
18import com.google.common.base.Strings;
19import com.google.common.collect.ImmutableSet;
Jian Liec5c32b2018-07-13 14:28:58 +090020import com.google.common.collect.Sets;
Jian Liecae4382018-06-28 17:41:12 +090021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
27import org.onlab.packet.IpAddress;
28import org.onlab.packet.MacAddress;
Jian Li078cd202018-07-23 18:58:20 +090029import org.onosproject.cluster.ClusterService;
30import org.onosproject.cluster.LeadershipService;
31import org.onosproject.cluster.NodeId;
32import org.onosproject.core.ApplicationId;
Jian Liecae4382018-06-28 17:41:12 +090033import org.onosproject.core.CoreService;
34import org.onosproject.event.ListenerRegistry;
35import org.onosproject.net.Host;
Jian Liec5c32b2018-07-13 14:28:58 +090036import org.onosproject.net.HostLocation;
Jian Liecae4382018-06-28 17:41:12 +090037import org.onosproject.net.host.HostEvent;
38import org.onosproject.net.host.HostListener;
39import org.onosproject.net.host.HostService;
40import org.onosproject.openstacknetworking.api.Constants;
41import org.onosproject.openstacknetworking.api.InstancePort;
42import org.onosproject.openstacknetworking.api.InstancePortAdminService;
43import org.onosproject.openstacknetworking.api.InstancePortEvent;
44import org.onosproject.openstacknetworking.api.InstancePortListener;
45import org.onosproject.openstacknetworking.api.InstancePortService;
46import org.onosproject.openstacknetworking.api.InstancePortStore;
47import org.onosproject.openstacknetworking.api.InstancePortStoreDelegate;
48import org.slf4j.Logger;
49
Jian Li078cd202018-07-23 18:58:20 +090050import java.util.Objects;
Jian Liecae4382018-06-28 17:41:12 +090051import java.util.Set;
52import java.util.stream.Collectors;
53
54import static com.google.common.base.Preconditions.checkArgument;
55import static com.google.common.base.Preconditions.checkNotNull;
Jian Liec5c32b2018-07-13 14:28:58 +090056import static org.onosproject.openstacknetworking.api.Constants.ANNOTATION_NETWORK_ID;
57import static org.onosproject.openstacknetworking.api.Constants.ANNOTATION_PORT_ID;
Jian Liecae4382018-06-28 17:41:12 +090058import static org.onosproject.openstacknetworking.api.InstancePort.State.ACTIVE;
Jian Liec5c32b2018-07-13 14:28:58 +090059import static org.onosproject.openstacknetworking.api.InstancePort.State.INACTIVE;
60import static org.onosproject.openstacknetworking.api.InstancePort.State.MIGRATED;
61import static org.onosproject.openstacknetworking.api.InstancePort.State.MIGRATING;
Jian Liecae4382018-06-28 17:41:12 +090062import static org.slf4j.LoggerFactory.getLogger;
63
64/**
65 * Provides implementation of administering and interfacing instance ports.
66 * It also provides instance port events for the hosts mapped to OpenStack VM interface.
67 */
68@Service
Jian Liec5c32b2018-07-13 14:28:58 +090069@Component(immediate = true)
Jian Liecae4382018-06-28 17:41:12 +090070public class InstancePortManager
71 extends ListenerRegistry<InstancePortEvent, InstancePortListener>
72 implements InstancePortService, InstancePortAdminService {
73
74 protected final Logger log = getLogger(getClass());
75
76 private static final String MSG_INSTANCE_PORT = "Instance port %s %s";
77 private static final String MSG_CREATED = "created";
78 private static final String MSG_UPDATED = "updated";
79 private static final String MSG_REMOVED = "removed";
80
81 private static final String ERR_NULL_INSTANCE_PORT = "Instance port cannot be null";
82 private static final String ERR_NULL_INSTANCE_PORT_ID = "Instance port ID cannot be null";
83 private static final String ERR_NULL_MAC_ADDRESS = "MAC address cannot be null";
84 private static final String ERR_NULL_IP_ADDRESS = "IP address cannot be null";
85 private static final String ERR_NULL_NETWORK_ID = "Network ID cannot be null";
86
87 private static final String ERR_IN_USE = " still in use";
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected CoreService coreService;
91
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected InstancePortStore instancePortStore;
94
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jian Li078cd202018-07-23 18:58:20 +090096 protected LeadershipService leadershipService;
97
98 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected ClusterService clusterService;
100
101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jian Liecae4382018-06-28 17:41:12 +0900102 protected HostService hostService;
103
104 private final InstancePortStoreDelegate
105 delegate = new InternalInstancePortStoreDelegate();
106 private final InternalHostListener
107 hostListener = new InternalHostListener();
108
Jian Li078cd202018-07-23 18:58:20 +0900109 private ApplicationId appId;
110 private NodeId localNodeId;
111
Jian Liecae4382018-06-28 17:41:12 +0900112 @Activate
113 protected void activate() {
Jian Li078cd202018-07-23 18:58:20 +0900114 appId = coreService.registerApplication(Constants.OPENSTACK_NETWORKING_APP_ID);
115 localNodeId = clusterService.getLocalNode().id();
Jian Liecae4382018-06-28 17:41:12 +0900116 instancePortStore.setDelegate(delegate);
117 hostService.addListener(hostListener);
Jian Li078cd202018-07-23 18:58:20 +0900118 leadershipService.runForLeadership(appId.name());
119
Jian Liecae4382018-06-28 17:41:12 +0900120 log.info("Started");
121 }
122
123 @Deactivate
124 protected void deactivate() {
125 hostService.removeListener(hostListener);
126 instancePortStore.unsetDelegate(delegate);
Jian Li078cd202018-07-23 18:58:20 +0900127 leadershipService.withdraw(appId.name());
128
Jian Liecae4382018-06-28 17:41:12 +0900129 log.info("Stopped");
130 }
131
132 @Override
133 public void createInstancePort(InstancePort instancePort) {
134 checkNotNull(instancePort, ERR_NULL_INSTANCE_PORT);
135 checkArgument(!Strings.isNullOrEmpty(instancePort.portId()),
136 ERR_NULL_INSTANCE_PORT_ID);
137
138 instancePortStore.createInstancePort(instancePort);
139 log.info(String.format(MSG_INSTANCE_PORT, instancePort.portId(),
140 MSG_CREATED));
141 }
142
143 @Override
144 public void updateInstancePort(InstancePort instancePort) {
145 checkNotNull(instancePort, ERR_NULL_INSTANCE_PORT);
146 checkArgument(!Strings.isNullOrEmpty(instancePort.portId()),
147 ERR_NULL_INSTANCE_PORT_ID);
148
149 instancePortStore.updateInstancePort(instancePort);
150 log.info(String.format(MSG_INSTANCE_PORT, instancePort.portId(), MSG_UPDATED));
151 }
152
153 @Override
154 public void removeInstancePort(String portId) {
155 checkArgument(!Strings.isNullOrEmpty(portId), ERR_NULL_INSTANCE_PORT_ID);
156
157 synchronized (this) {
158 if (isInstancePortInUse(portId)) {
159 final String error =
160 String.format(MSG_INSTANCE_PORT, portId, ERR_IN_USE);
161 throw new IllegalStateException(error);
162 }
163 InstancePort instancePort = instancePortStore.removeInstancePort(portId);
164 if (instancePort != null) {
165 log.info(String.format(MSG_INSTANCE_PORT, instancePort.portId(), MSG_REMOVED));
166 }
167 }
168 }
169
170 @Override
171 public void clear() {
172 instancePortStore.clear();
173 }
174
175 @Override
176 public InstancePort instancePort(MacAddress macAddress) {
177 checkNotNull(macAddress, ERR_NULL_MAC_ADDRESS);
178
179 return instancePortStore.instancePorts().stream()
180 .filter(port -> port.macAddress().equals(macAddress))
181 .findFirst().orElse(null);
182 }
183
184 @Override
185 public InstancePort instancePort(IpAddress ipAddress, String osNetId) {
186 checkNotNull(ipAddress, ERR_NULL_IP_ADDRESS);
187 checkNotNull(osNetId, ERR_NULL_NETWORK_ID);
188
189 return instancePortStore.instancePorts().stream()
190 .filter(port -> port.networkId().equals(osNetId))
191 .filter(port -> port.ipAddress().equals(ipAddress))
192 .findFirst().orElse(null);
193 }
194
195 @Override
196 public InstancePort instancePort(String portId) {
197 checkArgument(!Strings.isNullOrEmpty(portId), ERR_NULL_INSTANCE_PORT_ID);
198
199 return instancePortStore.instancePort(portId);
200 }
201
202 @Override
203 public Set<InstancePort> instancePorts() {
204 Set<InstancePort> ports = instancePortStore.instancePorts();
205
206 return ImmutableSet.copyOf(ports);
207 }
208
209 @Override
210 public Set<InstancePort> instancePorts(String osNetId) {
211 checkNotNull(osNetId, ERR_NULL_NETWORK_ID);
212
213 Set<InstancePort> ports = instancePortStore.instancePorts().stream()
214 .filter(port -> port.networkId().equals(osNetId))
215 .collect(Collectors.toSet());
216
217 return ImmutableSet.copyOf(ports);
218 }
219
Jian Liecae4382018-06-28 17:41:12 +0900220 private boolean isInstancePortInUse(String portId) {
221 // TODO add checking logic
222 return false;
223 }
224
225 private class InternalInstancePortStoreDelegate implements InstancePortStoreDelegate {
226
227 @Override
228 public void notify(InstancePortEvent event) {
229 if (event != null) {
230 log.trace("send instance port event {}", event);
231 process(event);
232 }
233 }
234 }
235
236 /**
237 * An internal listener that listens host event generated by HostLocationTracker
238 * in DistributedHostStore. The role of this listener is to convert host event
239 * to instance port event and post to the subscribers that have interested on
240 * this type of event.
241 */
242 private class InternalHostListener implements HostListener {
243
244 @Override
245 public boolean isRelevant(HostEvent event) {
246 Host host = event.subject();
247 if (!isValidHost(host)) {
248 log.debug("Invalid host detected, ignore it {}", host);
249 return false;
250 }
Jian Li078cd202018-07-23 18:58:20 +0900251
252 // do not allow to proceed without leadership
253 NodeId leader = leadershipService.getLeader(appId.name());
254 return Objects.equals(localNodeId, leader);
Jian Liecae4382018-06-28 17:41:12 +0900255 }
256
257 @Override
258 public void event(HostEvent event) {
259 InstancePort instPort = DefaultInstancePort.from(event.subject(), ACTIVE);
260
261 switch (event.type()) {
262 case HOST_UPDATED:
263 updateInstancePort(instPort);
264 break;
265 case HOST_ADDED:
Jian Liec5c32b2018-07-13 14:28:58 +0900266 InstancePort existingPort = instancePort(instPort.portId());
267 if (existingPort == null) {
268 // first time to add instance
269 createInstancePort(instPort);
270 } else {
Jian Li46b74002018-07-15 18:39:08 +0900271 if (existingPort.state() == INACTIVE) {
Jian Liee8214a2018-07-21 20:07:28 +0900272
273 if (instPort.deviceId().equals(existingPort.deviceId())) {
274
275 // VM RESTART case
276 // if the ID of switch where VM is attached to is
277 // identical, we can assume that the VM was
278 // restarted in the same location;
279 // note that the switch port number where VM is
280 // attached can be varied per each restart
281 updateInstancePort(instPort);
282 } else {
283
284 // VM COLD MIGRATION case
285 // if the ID of switch where VM is attached to is
286 // varied, we can assume that the VM was migrated
287 // to a new location
288 updateInstancePort(instPort.updateState(MIGRATING));
289 InstancePort updated = instPort.updateState(MIGRATED);
290 updateInstancePort(updated.updatePrevLocation(
291 existingPort.deviceId(), existingPort.portNumber()));
292 }
Jian Liec5c32b2018-07-13 14:28:58 +0900293 }
294 }
Jian Liecae4382018-06-28 17:41:12 +0900295 break;
296 case HOST_REMOVED:
Jian Liec5c32b2018-07-13 14:28:58 +0900297 // we will remove instance port from persistent store,
Jian Liee8214a2018-07-21 20:07:28 +0900298 // only if we receive port removal signal from neutron.
Jian Liec5c32b2018-07-13 14:28:58 +0900299 // by default, we update the instance port state to INACTIVE
300 // to indicate the instance is terminated
301 updateInstancePort(instPort.updateState(INACTIVE));
Jian Liecae4382018-06-28 17:41:12 +0900302 break;
303 case HOST_MOVED:
Jian Liec5c32b2018-07-13 14:28:58 +0900304 Host oldHost = event.prevSubject();
305 Host currHost = event.subject();
306
307 // in the middle of VM migration
308 if (oldHost.locations().size() < currHost.locations().size()) {
309 updateInstancePort(instPort.updateState(MIGRATING));
310 }
311
312 // finish of VM migration
313 if (oldHost.locations().size() > currHost.locations().size()) {
314 Set<HostLocation> diff =
315 Sets.difference(oldHost.locations(), currHost.locations());
316 HostLocation location = diff.stream().findFirst().orElse(null);
317
318 if (location != null) {
319 InstancePort updated = instPort.updateState(MIGRATED);
Jian Li46b74002018-07-15 18:39:08 +0900320 updateInstancePort(updated.updatePrevLocation(
Jian Liec5c32b2018-07-13 14:28:58 +0900321 location.deviceId(), location.port()));
322 }
323 }
Jian Liecae4382018-06-28 17:41:12 +0900324 break;
325 default:
326 break;
327 }
328 }
329
330 private boolean isValidHost(Host host) {
331 return !host.ipAddresses().isEmpty() &&
332 host.annotations().value(ANNOTATION_NETWORK_ID) != null &&
333 host.annotations().value(ANNOTATION_PORT_ID) != null;
334 }
335 }
336}