Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | */ |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 16 | package org.onosproject.openstacknetworking; |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 17 | |
| 18 | import org.onlab.osgi.DefaultServiceDirectory; |
| 19 | import org.onlab.osgi.ServiceDirectory; |
| 20 | import org.onlab.packet.Ip4Address; |
| 21 | import org.onlab.util.Tools; |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 22 | import org.onosproject.core.CoreService; |
| 23 | import org.onosproject.mastership.MastershipService; |
| 24 | import org.onosproject.net.Host; |
| 25 | import org.onosproject.net.host.HostEvent; |
| 26 | import org.onosproject.net.host.HostListener; |
| 27 | import org.onosproject.net.host.HostService; |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 28 | import org.onosproject.openstackinterface.OpenstackInterfaceService; |
| 29 | import org.onosproject.openstackinterface.OpenstackRouter; |
| 30 | import org.onosproject.openstackinterface.OpenstackSubnet; |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | |
| 33 | import java.util.Objects; |
| 34 | import java.util.Optional; |
| 35 | import java.util.Set; |
| 36 | import java.util.concurrent.ExecutorService; |
| 37 | import java.util.stream.Collectors; |
| 38 | |
| 39 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 40 | import static org.onlab.util.Tools.groupedThreads; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 41 | import static org.onosproject.openstacknetworking.Constants.*; |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 42 | import static org.slf4j.LoggerFactory.getLogger; |
| 43 | |
| 44 | /** |
| 45 | * Provides abstract virtual machine handler. |
| 46 | */ |
| 47 | public abstract class AbstractVmHandler { |
| 48 | protected final Logger log = getLogger(getClass()); |
| 49 | |
| 50 | protected final ExecutorService eventExecutor = newSingleThreadScheduledExecutor( |
| 51 | groupedThreads(this.getClass().getSimpleName(), "event-handler")); |
| 52 | |
| 53 | protected CoreService coreService; |
| 54 | protected MastershipService mastershipService; |
| 55 | protected HostService hostService; |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 56 | protected OpenstackInterfaceService openstackService; |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 57 | |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 58 | protected HostListener hostListener = new InternalHostListener(); |
| 59 | |
| 60 | protected void activate() { |
| 61 | ServiceDirectory services = new DefaultServiceDirectory(); |
| 62 | coreService = services.get(CoreService.class); |
| 63 | mastershipService = services.get(MastershipService.class); |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 64 | openstackService = services.get(OpenstackInterfaceService.class); |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 65 | hostService = services.get(HostService.class); |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 66 | hostService.addListener(hostListener); |
| 67 | |
| 68 | log.info("Started"); |
| 69 | } |
| 70 | |
| 71 | protected void deactivate() { |
| 72 | hostService.removeListener(hostListener); |
| 73 | eventExecutor.shutdown(); |
| 74 | |
| 75 | log.info("Stopped"); |
| 76 | } |
| 77 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 78 | /** |
| 79 | * Performs any action when a host is detected. |
| 80 | * |
| 81 | * @param host detected host |
| 82 | */ |
| 83 | protected abstract void hostDetected(Host host); |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 84 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Performs any action when a host is removed. |
| 87 | * |
| 88 | * @param host removed host |
| 89 | */ |
| 90 | protected abstract void hostRemoved(Host host); |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 91 | |
| 92 | protected boolean isValidHost(Host host) { |
| 93 | return !host.ipAddresses().isEmpty() && |
| 94 | host.annotations().value(VXLAN_ID) != null && |
| 95 | host.annotations().value(NETWORK_ID) != null && |
| 96 | host.annotations().value(TENANT_ID) != null && |
| 97 | host.annotations().value(PORT_ID) != null; |
| 98 | } |
| 99 | |
| 100 | protected Set<Host> getVmsInDifferentCnode(Host host) { |
| 101 | return Tools.stream(hostService.getHosts()) |
| 102 | .filter(h -> !h.location().deviceId().equals(host.location().deviceId())) |
| 103 | .filter(this::isValidHost) |
| 104 | .filter(h -> Objects.equals(getVni(h), getVni(host))) |
| 105 | .collect(Collectors.toSet()); |
| 106 | } |
| 107 | |
| 108 | protected Optional<Host> getVmByPortId(String portId) { |
| 109 | return Tools.stream(hostService.getHosts()) |
| 110 | .filter(this::isValidHost) |
| 111 | .filter(host -> host.annotations().value(PORT_ID).equals(portId)) |
| 112 | .findFirst(); |
| 113 | } |
| 114 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 115 | protected Set<Host> getHosts(OpenstackSubnet osSubnet) { |
| 116 | return Tools.stream(hostService.getHosts()) |
| 117 | .filter(host -> host.annotations().value(SUBNET_ID).equals(osSubnet.id())) |
| 118 | .collect(Collectors.toSet()); |
| 119 | } |
| 120 | |
| 121 | protected Optional<OpenstackRouter> getRouter(Host host) { |
| 122 | return openstackService.routers().stream() |
| 123 | .filter(router -> routableSubNets(router.id()).stream() |
| 124 | .filter(subnet -> subnet.id().equals(host.annotations().value(SUBNET_ID))) |
| 125 | .findAny().isPresent()) |
| 126 | .findAny(); |
| 127 | } |
| 128 | |
| 129 | protected Set<OpenstackSubnet> routableSubNets(String osRouterId) { |
| 130 | return openstackService.ports().stream() |
| 131 | .filter(p -> p.deviceOwner().equals(DEVICE_OWNER_ROUTER_INTERFACE) && |
| 132 | p.deviceId().equals(osRouterId)) |
| 133 | .map(p -> openstackService.subnet(p.fixedIps().keySet().stream().findFirst().get())) |
| 134 | .collect(Collectors.toSet()); |
| 135 | } |
| 136 | |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 137 | protected Ip4Address getIp(Host host) { |
| 138 | return host.ipAddresses().stream().findFirst().get().getIp4Address(); |
| 139 | } |
| 140 | |
| 141 | protected String getVni(Host host) { |
| 142 | return host.annotations().value(VXLAN_ID); |
| 143 | } |
| 144 | |
| 145 | protected String getTenantId(Host host) { |
| 146 | return host.annotations().value(TENANT_ID); |
| 147 | } |
| 148 | |
| 149 | private class InternalHostListener implements HostListener { |
| 150 | |
| 151 | @Override |
| 152 | public void event(HostEvent event) { |
| 153 | Host host = event.subject(); |
| 154 | if (!mastershipService.isLocalMaster(host.location().deviceId())) { |
| 155 | // do not allow to proceed without mastership |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | if (!isValidHost(host)) { |
| 160 | log.debug("Invalid host event, ignore it {}", host); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | switch (event.type()) { |
| 165 | case HOST_UPDATED: |
| 166 | case HOST_ADDED: |
| 167 | eventExecutor.execute(() -> hostDetected(host)); |
| 168 | break; |
| 169 | case HOST_REMOVED: |
| 170 | eventExecutor.execute(() -> hostRemoved(host)); |
| 171 | break; |
| 172 | default: |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |