blob: d7b7a8a0a57c04774724a597edf4fb3f6ad7dd33 [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 */
16package org.onosproject.openstacknetworking.switching;
17
18import org.onlab.osgi.DefaultServiceDirectory;
19import org.onlab.osgi.ServiceDirectory;
20import org.onlab.packet.Ip4Address;
21import org.onlab.util.Tools;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.core.CoreService;
24import org.onosproject.mastership.MastershipService;
25import org.onosproject.net.Host;
26import org.onosproject.net.host.HostEvent;
27import org.onosproject.net.host.HostListener;
28import org.onosproject.net.host.HostService;
sangho6032f342016-07-07 14:32:03 +090029import org.onosproject.openstacknetworking.Constants;
Hyunsun Moonb974fca2016-06-30 21:20:39 -070030import org.slf4j.Logger;
31
32import java.util.Objects;
33import java.util.Optional;
34import java.util.Set;
35import java.util.concurrent.ExecutorService;
36import java.util.stream.Collectors;
37
38import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
39import static org.onlab.util.Tools.groupedThreads;
sangho6032f342016-07-07 14:32:03 +090040import static org.onosproject.openstacknetworking.Constants.*;
Hyunsun Moonb974fca2016-06-30 21:20:39 -070041import static org.slf4j.LoggerFactory.getLogger;
42
43/**
44 * Provides abstract virtual machine handler.
45 */
46public abstract class AbstractVmHandler {
47 protected final Logger log = getLogger(getClass());
48
49 protected final ExecutorService eventExecutor = newSingleThreadScheduledExecutor(
50 groupedThreads(this.getClass().getSimpleName(), "event-handler"));
51
52 protected CoreService coreService;
53 protected MastershipService mastershipService;
54 protected HostService hostService;
55
56 protected ApplicationId appId;
57 protected HostListener hostListener = new InternalHostListener();
58
59 protected void activate() {
60 ServiceDirectory services = new DefaultServiceDirectory();
61 coreService = services.get(CoreService.class);
62 mastershipService = services.get(MastershipService.class);
63 hostService = services.get(HostService.class);
64
65 appId = coreService.registerApplication(Constants.APP_ID);
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
78 abstract void hostDetected(Host host);
79
80 abstract void hostRemoved(Host host);
81
82 protected boolean isValidHost(Host host) {
83 return !host.ipAddresses().isEmpty() &&
84 host.annotations().value(VXLAN_ID) != null &&
85 host.annotations().value(NETWORK_ID) != null &&
86 host.annotations().value(TENANT_ID) != null &&
87 host.annotations().value(PORT_ID) != null;
88 }
89
90 protected Set<Host> getVmsInDifferentCnode(Host host) {
91 return Tools.stream(hostService.getHosts())
92 .filter(h -> !h.location().deviceId().equals(host.location().deviceId()))
93 .filter(this::isValidHost)
94 .filter(h -> Objects.equals(getVni(h), getVni(host)))
95 .collect(Collectors.toSet());
96 }
97
98 protected Optional<Host> getVmByPortId(String portId) {
99 return Tools.stream(hostService.getHosts())
100 .filter(this::isValidHost)
101 .filter(host -> host.annotations().value(PORT_ID).equals(portId))
102 .findFirst();
103 }
104
105 protected Ip4Address getIp(Host host) {
106 return host.ipAddresses().stream().findFirst().get().getIp4Address();
107 }
108
109 protected String getVni(Host host) {
110 return host.annotations().value(VXLAN_ID);
111 }
112
113 protected String getTenantId(Host host) {
114 return host.annotations().value(TENANT_ID);
115 }
116
117 private class InternalHostListener implements HostListener {
118
119 @Override
120 public void event(HostEvent event) {
121 Host host = event.subject();
122 if (!mastershipService.isLocalMaster(host.location().deviceId())) {
123 // do not allow to proceed without mastership
124 return;
125 }
126
127 if (!isValidHost(host)) {
128 log.debug("Invalid host event, ignore it {}", host);
129 return;
130 }
131
132 switch (event.type()) {
133 case HOST_UPDATED:
134 case HOST_ADDED:
135 eventExecutor.execute(() -> hostDetected(host));
136 break;
137 case HOST_REMOVED:
138 eventExecutor.execute(() -> hostRemoved(host));
139 break;
140 default:
141 break;
142 }
143 }
144 }
145}