blob: a373ff0b61d5a6e348690cf96a1a1134352bb65b [file] [log] [blame]
Hyunsun Moonb974fca2016-06-30 21:20:39 -07001/*
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 Moonb3eb84d2016-07-27 19:10:52 -070016package org.onosproject.openstacknetworking;
Hyunsun Moonb974fca2016-06-30 21:20:39 -070017
18import org.onlab.osgi.DefaultServiceDirectory;
19import org.onlab.osgi.ServiceDirectory;
20import org.onlab.packet.Ip4Address;
21import org.onlab.util.Tools;
Hyunsun Moonb974fca2016-06-30 21:20:39 -070022import org.onosproject.core.CoreService;
23import org.onosproject.mastership.MastershipService;
24import org.onosproject.net.Host;
25import org.onosproject.net.host.HostEvent;
26import org.onosproject.net.host.HostListener;
27import org.onosproject.net.host.HostService;
sanghofb3b5012016-11-10 15:47:53 +090028import org.onosproject.openstackinterface.OpenstackInterfaceService;
29import org.onosproject.openstackinterface.OpenstackRouter;
30import org.onosproject.openstackinterface.OpenstackSubnet;
Hyunsun Moonb974fca2016-06-30 21:20:39 -070031import org.slf4j.Logger;
32
33import java.util.Objects;
34import java.util.Optional;
35import java.util.Set;
36import java.util.concurrent.ExecutorService;
37import java.util.stream.Collectors;
38
39import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
40import static org.onlab.util.Tools.groupedThreads;
sangho6032f342016-07-07 14:32:03 +090041import static org.onosproject.openstacknetworking.Constants.*;
Hyunsun Moonb974fca2016-06-30 21:20:39 -070042import static org.slf4j.LoggerFactory.getLogger;
43
44/**
45 * Provides abstract virtual machine handler.
46 */
47public 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;
sanghofb3b5012016-11-10 15:47:53 +090056 protected OpenstackInterfaceService openstackService;
Hyunsun Moonb974fca2016-06-30 21:20:39 -070057
Hyunsun Moonb974fca2016-06-30 21:20:39 -070058 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);
sanghofb3b5012016-11-10 15:47:53 +090064 openstackService = services.get(OpenstackInterfaceService.class);
Hyunsun Moonb974fca2016-06-30 21:20:39 -070065 hostService = services.get(HostService.class);
Hyunsun Moonb974fca2016-06-30 21:20:39 -070066 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 Moonb3eb84d2016-07-27 19:10:52 -070078 /**
79 * Performs any action when a host is detected.
80 *
81 * @param host detected host
82 */
83 protected abstract void hostDetected(Host host);
Hyunsun Moonb974fca2016-06-30 21:20:39 -070084
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070085 /**
86 * Performs any action when a host is removed.
87 *
88 * @param host removed host
89 */
90 protected abstract void hostRemoved(Host host);
Hyunsun Moonb974fca2016-06-30 21:20:39 -070091
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
sanghofb3b5012016-11-10 15:47:53 +0900115 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 Moonb974fca2016-06-30 21:20:39 -0700137 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}