blob: f7039c387156b55f4142b3b5f3adb066885def60 [file] [log] [blame]
Hyunsun Moond0e932a2015-09-15 22:39:16 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Hyunsun Moond0e932a2015-09-15 22:39:16 -07003 *
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 Moon7f4ed9d2016-04-14 16:13:42 -070016package org.onosproject.cordvtn.impl;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070017
Hyunsun Moon1d3eac92016-02-03 00:11:11 -080018import com.google.common.collect.Lists;
Hyunsun Moonae39ae82016-02-17 15:02:06 -080019import com.google.common.collect.Maps;
Hyunsun Moon1f145552015-10-08 22:25:30 -070020import com.google.common.collect.Sets;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070021import 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;
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -080027import org.onlab.packet.Ethernet;
Hyunsun Moon1d3eac92016-02-03 00:11:11 -080028import org.onlab.packet.Ip4Address;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -080029import org.onlab.packet.IpAddress;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080030import org.onlab.packet.MacAddress;
31import org.onlab.packet.VlanId;
Hyunsun Moon7f4ed9d2016-04-14 16:13:42 -070032import org.onosproject.cordvtn.api.CordService;
Hyunsun Moondd91be22016-04-24 17:43:32 -070033import org.onosproject.cordvtn.api.CordService.ServiceType;
Hyunsun Moon7f4ed9d2016-04-14 16:13:42 -070034import org.onosproject.cordvtn.api.CordServiceId;
35import org.onosproject.cordvtn.api.CordVtnConfig;
36import org.onosproject.cordvtn.api.CordVtnNode;
37import org.onosproject.cordvtn.api.CordVtnService;
Hyunsun Moon1f145552015-10-08 22:25:30 -070038import org.onosproject.core.ApplicationId;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070039import org.onosproject.core.CoreService;
Hyunsun Moon1d3eac92016-02-03 00:11:11 -080040import org.onosproject.dhcp.DhcpService;
Hyunsun Moonc71231d2015-12-16 20:53:23 -080041import org.onosproject.mastership.MastershipService;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080042import org.onosproject.net.ConnectPoint;
Hyunsun Moond772f342015-10-28 20:28:16 -070043import org.onosproject.net.DefaultAnnotations;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070044import org.onosproject.net.Host;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -080045import org.onosproject.net.HostId;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080046import org.onosproject.net.HostLocation;
Hyunsun Moon8539b042015-11-07 22:08:43 -080047import org.onosproject.net.Port;
Hyunsun Moon746956f2016-01-24 21:47:06 -080048import org.onosproject.net.config.ConfigFactory;
49import org.onosproject.net.config.NetworkConfigEvent;
50import org.onosproject.net.config.NetworkConfigListener;
51import org.onosproject.net.config.NetworkConfigRegistry;
52import org.onosproject.net.config.NetworkConfigService;
53import org.onosproject.net.config.basics.SubjectFactories;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070054import org.onosproject.net.device.DeviceService;
Hyunsun Moonc71231d2015-12-16 20:53:23 -080055import org.onosproject.net.flow.FlowRuleService;
56import org.onosproject.net.group.GroupService;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080057import org.onosproject.net.host.DefaultHostDescription;
58import org.onosproject.net.host.HostDescription;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070059import org.onosproject.net.host.HostEvent;
60import org.onosproject.net.host.HostListener;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080061import org.onosproject.net.host.HostProvider;
62import org.onosproject.net.host.HostProviderRegistry;
63import org.onosproject.net.host.HostProviderService;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070064import org.onosproject.net.host.HostService;
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -080065import org.onosproject.net.packet.PacketContext;
66import org.onosproject.net.packet.PacketProcessor;
67import org.onosproject.net.packet.PacketService;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080068import org.onosproject.net.provider.AbstractProvider;
69import org.onosproject.net.provider.ProviderId;
Hyunsun Moondd91be22016-04-24 17:43:32 -070070import org.onosproject.xosclient.api.XosAccess;
71import org.onosproject.xosclient.api.XosClientService;
Hyunsun Moon576d6872016-04-14 19:04:23 -070072import org.openstack4j.api.OSClient;
73import org.openstack4j.api.exceptions.AuthenticationException;
74import org.openstack4j.model.identity.Access;
75import org.openstack4j.model.network.Network;
76import org.openstack4j.model.network.Subnet;
77import org.openstack4j.openstack.OSFactory;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070078import org.slf4j.Logger;
79
Hyunsun Moon1d3eac92016-02-03 00:11:11 -080080import java.util.List;
Hyunsun Moon1f145552015-10-08 22:25:30 -070081import java.util.Map;
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080082import java.util.Objects;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -080083import java.util.Set;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070084import java.util.concurrent.ExecutorService;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -080085import java.util.stream.Collectors;
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080086import java.util.stream.StreamSupport;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070087
Hyunsun Moon1f145552015-10-08 22:25:30 -070088import static com.google.common.base.Preconditions.checkNotNull;
Hyunsun Moon746956f2016-01-24 21:47:06 -080089import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070090import static org.onlab.util.Tools.groupedThreads;
Hyunsun Moondd91be22016-04-24 17:43:32 -070091import static org.onosproject.cordvtn.api.CordService.getServiceType;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070092import static org.slf4j.LoggerFactory.getLogger;
93
94/**
Hyunsun Moon9f0814b2015-11-04 17:34:35 -080095 * Provisions virtual tenant networks with service chaining capability
96 * in OpenStack environment.
Hyunsun Moond0e932a2015-09-15 22:39:16 -070097 */
98@Component(immediate = true)
99@Service
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800100public class CordVtn extends AbstractProvider implements CordVtnService, HostProvider {
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700101
102 protected final Logger log = getLogger(getClass());
103
104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected CoreService coreService;
106
107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moon746956f2016-01-24 21:47:06 -0800108 protected NetworkConfigRegistry configRegistry;
109
110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
111 protected NetworkConfigService configService;
112
113 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800114 protected HostProviderRegistry hostProviderRegistry;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700115
116 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700117 protected DeviceService deviceService;
118
119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
120 protected HostService hostService;
121
Hyunsun Moon1f145552015-10-08 22:25:30 -0700122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800123 protected FlowRuleService flowRuleService;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800124
125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800126 protected PacketService packetService;
127
128 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800129 protected MastershipService mastershipService;
130
131 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
132 protected GroupService groupService;
133
134 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800135 protected DhcpService dhcpService;
136
Hyunsun Moondd91be22016-04-24 17:43:32 -0700137 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
138 protected XosClientService xosClient;
139
Hyunsun Moon746956f2016-01-24 21:47:06 -0800140 private final ConfigFactory configFactory =
141 new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, CordVtnConfig.class, "cordvtn") {
142 @Override
143 public CordVtnConfig createConfig() {
144 return new CordVtnConfig();
145 }
146 };
147
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800148 private static final String DEFAULT_TUNNEL = "vxlan";
149 private static final String SERVICE_ID = "serviceId";
Hyunsun Moon6d247342016-02-12 12:48:47 -0800150 private static final String OPENSTACK_PORT_ID = "openstackPortId";
151 private static final String DATA_PLANE_IP = "dataPlaneIp";
152 private static final String DATA_PLANE_INTF = "dataPlaneIntf";
153 private static final String S_TAG = "stag";
Hyunsun Moon98025542016-03-08 04:36:02 -0800154 private static final String VSG_HOST_ID = "vsgHostId";
Hyunsun Moond35420f2016-03-08 21:59:13 -0800155 private static final String CREATED_TIME = "createdTime";
Hyunsun Moon6d247342016-02-12 12:48:47 -0800156
157 private static final Ip4Address DEFAULT_DNS = Ip4Address.valueOf("8.8.8.8");
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800158
Hyunsun Moon746956f2016-01-24 21:47:06 -0800159 private final ExecutorService eventExecutor =
160 newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtn", "event-handler"));
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700161
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800162 private final PacketProcessor packetProcessor = new InternalPacketProcessor();
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800163 private final HostListener hostListener = new InternalHostListener();
Hyunsun Moon746956f2016-01-24 21:47:06 -0800164 private final NetworkConfigListener configListener = new InternalConfigListener();
Hyunsun Moon2b530322015-09-23 13:24:35 -0700165
Hyunsun Moon746956f2016-01-24 21:47:06 -0800166 private ApplicationId appId;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800167 private HostProviderService hostProvider;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800168 private CordVtnRuleInstaller ruleInstaller;
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800169 private CordVtnArpProxy arpProxy;
Hyunsun Moon576d6872016-04-14 19:04:23 -0700170
171 private volatile Access osAccess = null;
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800172 private volatile MacAddress privateGatewayMac = MacAddress.NONE;
Hyunsun Moon8539b042015-11-07 22:08:43 -0800173
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800174 /**
175 * Creates an cordvtn host location provider.
176 */
177 public CordVtn() {
178 super(new ProviderId("host", CORDVTN_APP_ID));
Hyunsun Moon8539b042015-11-07 22:08:43 -0800179 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700180
181 @Activate
182 protected void activate() {
Hyunsun Moon746956f2016-01-24 21:47:06 -0800183 appId = coreService.registerApplication("org.onosproject.cordvtn");
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800184 ruleInstaller = new CordVtnRuleInstaller(appId, flowRuleService,
185 deviceService,
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800186 groupService,
Hyunsun Moonfae776d2016-03-08 18:07:52 -0800187 configRegistry,
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800188 DEFAULT_TUNNEL);
189
Hyunsun Moon9cf43db2016-02-12 15:59:53 -0800190 arpProxy = new CordVtnArpProxy(appId, packetService, hostService);
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800191 packetService.addProcessor(packetProcessor, PacketProcessor.director(0));
192 arpProxy.requestPacket();
193
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700194 hostService.addListener(hostListener);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800195 hostProvider = hostProviderRegistry.register(this);
Hyunsun Moon2b530322015-09-23 13:24:35 -0700196
Hyunsun Moon746956f2016-01-24 21:47:06 -0800197 configRegistry.registerConfigFactory(configFactory);
198 configService.addListener(configListener);
Hyunsun Moon746956f2016-01-24 21:47:06 -0800199
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700200 log.info("Started");
201 }
202
203 @Deactivate
204 protected void deactivate() {
Hyunsun Moon746956f2016-01-24 21:47:06 -0800205 hostProviderRegistry.unregister(this);
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700206 hostService.removeListener(hostListener);
Hyunsun Moon746956f2016-01-24 21:47:06 -0800207
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800208 packetService.removeProcessor(packetProcessor);
Hyunsun Moon2b530322015-09-23 13:24:35 -0700209
Hyunsun Moon746956f2016-01-24 21:47:06 -0800210 configRegistry.unregisterConfigFactory(configFactory);
211 configService.removeListener(configListener);
Hyunsun Moon2b530322015-09-23 13:24:35 -0700212
Hyunsun Moon746956f2016-01-24 21:47:06 -0800213 eventExecutor.shutdown();
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700214 log.info("Stopped");
215 }
216
217 @Override
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800218 public void triggerProbe(Host host) {
219 /*
220 * Note: In CORD deployment, we assume that all hosts are configured.
221 * Therefore no probe is required.
222 */
Hyunsun Moonb219fc42016-01-14 03:42:47 -0800223 }
224
225 @Override
Hyunsun Moon640f183e2016-02-10 17:02:37 -0800226 public void createServiceDependency(CordServiceId tServiceId, CordServiceId pServiceId,
227 boolean isBidirectional) {
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800228 CordService tService = getCordService(tServiceId);
229 CordService pService = getCordService(pServiceId);
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800230
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800231 if (tService == null || pService == null) {
232 log.error("Failed to create CordService for {}", tServiceId.id());
233 return;
234 }
235
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800236 log.info("Service dependency from {} to {} created.", tService.id().id(), pService.id().id());
Hyunsun Moon640f183e2016-02-10 17:02:37 -0800237 ruleInstaller.populateServiceDependencyRules(tService, pService, isBidirectional);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800238 }
239
240 @Override
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800241 public void removeServiceDependency(CordServiceId tServiceId, CordServiceId pServiceId) {
242 CordService tService = getCordService(tServiceId);
243 CordService pService = getCordService(pServiceId);
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800244
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800245 if (tService == null || pService == null) {
246 log.error("Failed to create CordService for {}", tServiceId.id());
247 return;
248 }
249
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800250 log.info("Service dependency from {} to {} removed.", tService.id().id(), pService.id().id());
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800251 ruleInstaller.removeServiceDependencyRules(tService, pService);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800252 }
253
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800254 @Override
255 public void addServiceVm(CordVtnNode node, ConnectPoint connectPoint) {
Hyunsun Moon576d6872016-04-14 19:04:23 -0700256 checkNotNull(osAccess, "OpenStack access is not set");
257
258 OSClient osClient = OSFactory.clientFromAccess(osAccess);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800259 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
Hyunsun Moon576d6872016-04-14 19:04:23 -0700260 org.openstack4j.model.network.Port osPort = osClient.networking().port().list()
261 .stream()
262 .filter(p -> p.getId().contains(getPortName(port).substring(3)))
263 .findFirst().orElse(null);
264 if (osPort == null) {
265 log.warn("Failed to get OpenStack port for {}", getPortName(port));
Hyunsun Moon8539b042015-11-07 22:08:43 -0800266 return;
267 }
268
Hyunsun Moon576d6872016-04-14 19:04:23 -0700269 MacAddress mac = MacAddress.valueOf(osPort.getMacAddress());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800270 HostId hostId = HostId.hostId(mac);
Hyunsun Moon8539b042015-11-07 22:08:43 -0800271
Hyunsun Moond35420f2016-03-08 21:59:13 -0800272 Host existingHost = hostService.getHost(hostId);
273 if (existingHost != null) {
274 String serviceId = existingHost.annotations().value(SERVICE_ID);
Hyunsun Moon576d6872016-04-14 19:04:23 -0700275 if (serviceId == null || !serviceId.equals(osPort.getNetworkId())) {
Hyunsun Moond35420f2016-03-08 21:59:13 -0800276 // this host is not injected by cordvtn or a stale host, remove it
277 hostProvider.hostVanished(existingHost.id());
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800278 }
Hyunsun Moon8539b042015-11-07 22:08:43 -0800279 }
280
Hyunsun Moond35420f2016-03-08 21:59:13 -0800281 // Included CREATED_TIME to annotation intentionally to trigger HOST_UPDATED
282 // event so that the flow rule population for this host can happen.
283 // This ensures refreshing data plane by pushing network config always make
284 // the data plane synced.
Hyunsun Moon576d6872016-04-14 19:04:23 -0700285 Set<IpAddress> fixedIps = osPort.getFixedIps().stream()
286 .map(ip -> IpAddress.valueOf(ip.getIpAddress()))
287 .collect(Collectors.toSet());
288
Hyunsun Moon6d247342016-02-12 12:48:47 -0800289 DefaultAnnotations.Builder annotations = DefaultAnnotations.builder()
Hyunsun Moon576d6872016-04-14 19:04:23 -0700290 .set(SERVICE_ID, osPort.getNetworkId())
291 .set(OPENSTACK_PORT_ID, osPort.getId())
Hyunsun Moon6d247342016-02-12 12:48:47 -0800292 .set(DATA_PLANE_IP, node.dpIp().ip().toString())
Hyunsun Moond35420f2016-03-08 21:59:13 -0800293 .set(DATA_PLANE_INTF, node.dpIntf())
294 .set(CREATED_TIME, String.valueOf(System.currentTimeMillis()));
Hyunsun Moon6d247342016-02-12 12:48:47 -0800295
Hyunsun Moon576d6872016-04-14 19:04:23 -0700296 String serviceVlan = getServiceVlan(osPort);
Hyunsun Moon6d247342016-02-12 12:48:47 -0800297 if (serviceVlan != null) {
298 annotations.set(S_TAG, serviceVlan);
299 }
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800300
301 HostDescription hostDesc = new DefaultHostDescription(
302 mac,
303 VlanId.NONE,
304 new HostLocation(connectPoint, System.currentTimeMillis()),
Hyunsun Moon576d6872016-04-14 19:04:23 -0700305 fixedIps,
Hyunsun Moon6d247342016-02-12 12:48:47 -0800306 annotations.build());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800307
308 hostProvider.hostDetected(hostId, hostDesc, false);
Hyunsun Moon8539b042015-11-07 22:08:43 -0800309 }
310
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800311 @Override
312 public void removeServiceVm(ConnectPoint connectPoint) {
Hyunsun Moon2a225162016-02-17 19:00:50 -0800313 hostService.getConnectedHosts(connectPoint)
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800314 .stream()
Hyunsun Moon2a225162016-02-17 19:00:50 -0800315 .forEach(host -> hostProvider.hostVanished(host.id()));
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800316 }
317
Hyunsun Moon6d247342016-02-12 12:48:47 -0800318 @Override
319 public void updateVirtualSubscriberGateways(HostId vSgHostId, String serviceVlan,
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800320 Map<IpAddress, MacAddress> vSgs) {
Hyunsun Moon98025542016-03-08 04:36:02 -0800321 Host vSgHost = hostService.getHost(vSgHostId);
322 if (vSgHost == null || !vSgHost.annotations().value(S_TAG).equals(serviceVlan)) {
Hyunsun Moon6d247342016-02-12 12:48:47 -0800323 log.debug("Invalid vSG updates for {}", serviceVlan);
324 return;
325 }
326
Hyunsun Moon98025542016-03-08 04:36:02 -0800327 log.info("Updates vSGs in {} with {}", vSgHost.id(), vSgs.toString());
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800328 vSgs.entrySet().stream()
Hyunsun Moon98025542016-03-08 04:36:02 -0800329 .filter(entry -> hostService.getHostsByMac(entry.getValue()).isEmpty())
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800330 .forEach(entry -> addVirtualSubscriberGateway(
Hyunsun Moon98025542016-03-08 04:36:02 -0800331 vSgHost,
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800332 entry.getKey(),
333 entry.getValue(),
334 serviceVlan));
335
Hyunsun Moon98025542016-03-08 04:36:02 -0800336 hostService.getConnectedHosts(vSgHost.location()).stream()
337 .filter(host -> !host.mac().equals(vSgHost.mac()))
Hyunsun Moon2a225162016-02-17 19:00:50 -0800338 .filter(host -> !vSgs.values().contains(host.mac()))
339 .forEach(host -> {
340 log.info("Removed vSG {}", host.toString());
341 hostProvider.hostVanished(host.id());
342 });
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800343 }
344
345 /**
346 * Adds virtual subscriber gateway to the system.
347 *
348 * @param vSgHost host virtual machine of this vSG
349 * @param vSgIp vSG ip address
350 * @param vSgMac vSG mac address
351 * @param serviceVlan service vlan
352 */
Hyunsun Moon2a225162016-02-17 19:00:50 -0800353 private void addVirtualSubscriberGateway(Host vSgHost, IpAddress vSgIp, MacAddress vSgMac,
354 String serviceVlan) {
Hyunsun Moon98025542016-03-08 04:36:02 -0800355 log.info("vSG with IP({}) MAC({}) added", vSgIp.toString(), vSgMac.toString());
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800356
Hyunsun Moon98025542016-03-08 04:36:02 -0800357 HostId hostId = HostId.hostId(vSgMac);
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800358 DefaultAnnotations.Builder annotations = DefaultAnnotations.builder()
Hyunsun Moon98025542016-03-08 04:36:02 -0800359 .set(S_TAG, serviceVlan)
Hyunsun Moond35420f2016-03-08 21:59:13 -0800360 .set(VSG_HOST_ID, vSgHost.id().toString())
361 .set(CREATED_TIME, String.valueOf(System.currentTimeMillis()));
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800362
363 HostDescription hostDesc = new DefaultHostDescription(
364 vSgMac,
365 VlanId.NONE,
366 vSgHost.location(),
367 Sets.newHashSet(vSgIp),
368 annotations.build());
369
370 hostProvider.hostDetected(hostId, hostDesc, false);
Hyunsun Moon6d247342016-02-12 12:48:47 -0800371 }
372
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800373 /**
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800374 * Returns public ip addresses of vSGs running inside a give vSG host.
375 *
376 * @param vSgHost vSG host
377 * @return map of ip and mac address, or empty map
378 */
379 private Map<IpAddress, MacAddress> getSubscriberGateways(Host vSgHost) {
Hyunsun Moon576d6872016-04-14 19:04:23 -0700380 checkNotNull(osAccess, "OpenStack access is not set");
381
382 String osPortId = vSgHost.annotations().value(OPENSTACK_PORT_ID);
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800383 String serviceVlan = vSgHost.annotations().value(S_TAG);
384
Hyunsun Moon576d6872016-04-14 19:04:23 -0700385 OSClient osClient = OSFactory.clientFromAccess(osAccess);
386 org.openstack4j.model.network.Port osPort = osClient.networking().port().get(osPortId);
387 if (osPort == null) {
388 log.warn("Failed to get OpenStack port {} for VM {}", osPortId, vSgHost.id());
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800389 return Maps.newHashMap();
390 }
391
Hyunsun Moon576d6872016-04-14 19:04:23 -0700392 if (!serviceVlan.equals(getServiceVlan(osPort))) {
393 log.error("Host({}) s-tag does not match with OpenStack port s-tag", vSgHost.id());
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800394 return Maps.newHashMap();
395 }
396
Hyunsun Moon576d6872016-04-14 19:04:23 -0700397 Map<IpAddress, MacAddress> addressPairs = Maps.newHashMap();
398 osPort.getAllowedAddressPairs()
399 .stream().forEach(p -> addressPairs.put(
400 IpAddress.valueOf(p.getIpAddress()),
401 MacAddress.valueOf(p.getMacAddress())));
402
403 return addressPairs;
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800404 }
405
406 /**
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800407 * Returns CordService by service ID.
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800408 *
409 * @param serviceId service id
410 * @return cord service, or null if it fails to get network from OpenStack
411 */
412 private CordService getCordService(CordServiceId serviceId) {
Hyunsun Moon576d6872016-04-14 19:04:23 -0700413 checkNotNull(osAccess, "OpenStack access is not set");
414
415 OSClient osClient = OSFactory.clientFromAccess(osAccess);
416 Network osNet = osClient.networking().network().get(serviceId.id());
417 if (osNet == null) {
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800418 log.warn("Couldn't find OpenStack network for service {}", serviceId.id());
419 return null;
420 }
421
Hyunsun Moondd91be22016-04-24 17:43:32 -0700422 return getCordService(osNet);
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800423 }
424
425 /**
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800426 * Returns CordService by OpenStack network.
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800427 *
Hyunsun Moon576d6872016-04-14 19:04:23 -0700428 * @param osNet OpenStack network
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800429 * @return cord service
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800430 */
Hyunsun Moon576d6872016-04-14 19:04:23 -0700431 private CordService getCordService(Network osNet) {
432 checkNotNull(osNet);
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800433
Hyunsun Moon576d6872016-04-14 19:04:23 -0700434 CordServiceId serviceId = CordServiceId.of(osNet.getId());
435 // here it assumes all cord service networks has only one subnet
436 Subnet osSubnet = osNet.getNeutronSubnets().stream()
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800437 .findFirst()
438 .orElse(null);
Hyunsun Moon576d6872016-04-14 19:04:23 -0700439 if (osSubnet == null) {
440 log.warn("Couldn't find OpenStack subnet for service {}", serviceId.id());
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800441 return null;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800442 }
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800443
Hyunsun Moon576d6872016-04-14 19:04:23 -0700444 Map<Host, IpAddress> hosts = getHostsWithOpenstackNetwork(osNet)
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800445 .stream()
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800446 .collect(Collectors.toMap(host -> host, this::getTunnelIp));
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800447
Hyunsun Moondd91be22016-04-24 17:43:32 -0700448 ServiceType serviceType = getServiceType(osNet.getName());
449 // allows working without XOS for now
450 Set<CordServiceId> tServices = Sets.newHashSet();
451 Set<CordServiceId> pServices = Sets.newHashSet();
452
453 if (xosClient.access() != null && serviceType != ServiceType.MANAGEMENT) {
454 tServices = xosClient.vtnServiceApi().getTenantServices(serviceId.id())
455 .stream()
456 .map(CordServiceId::of)
457 .collect(Collectors.toSet());
458
459 pServices = xosClient.vtnServiceApi().getProviderServices(serviceId.id())
460 .stream()
461 .map(CordServiceId::of)
462 .collect(Collectors.toSet());
463 }
464
465 return new CordService(osNet, osSubnet, hosts, tServices, pServices);
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800466 }
467
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800468 /**
469 * Returns IP address for tunneling for a given host.
470 *
471 * @param host host
Hyunsun Moon6d247342016-02-12 12:48:47 -0800472 * @return ip address, or null
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800473 */
474 private IpAddress getTunnelIp(Host host) {
Hyunsun Moon6d247342016-02-12 12:48:47 -0800475 String ip = host.annotations().value(DATA_PLANE_IP);
476 return ip == null ? null : IpAddress.valueOf(ip);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800477 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700478
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800479 /**
480 * Returns port name.
481 *
482 * @param port port
483 * @return port name
484 */
485 private String getPortName(Port port) {
486 return port.annotations().value("portName");
487 }
Hyunsun Moon8539b042015-11-07 22:08:43 -0800488
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800489 /**
Hyunsun Moon6d247342016-02-12 12:48:47 -0800490 * Returns s-tag from a given OpenStack port.
491 *
Hyunsun Moon576d6872016-04-14 19:04:23 -0700492 * @param osPort openstack port
Hyunsun Moon6d247342016-02-12 12:48:47 -0800493 * @return s-tag string
494 */
Hyunsun Moon576d6872016-04-14 19:04:23 -0700495 private String getServiceVlan(org.openstack4j.model.network.Port osPort) {
496 checkNotNull(osPort);
Hyunsun Moon6d247342016-02-12 12:48:47 -0800497
Hyunsun Moon576d6872016-04-14 19:04:23 -0700498 String portName = osPort.getName();
499 if (portName != null && portName.startsWith(S_TAG)) {
500 return portName.split("-")[1];
Hyunsun Moon6d247342016-02-12 12:48:47 -0800501 } else {
502 return null;
503 }
504 }
505
506 /**
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800507 * Returns service ID of this host.
508 *
509 * @param host host
510 * @return service id, or null if not found
511 */
512 private String getServiceId(Host host) {
513 return host.annotations().value(SERVICE_ID);
514 }
515
516 /**
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800517 * Returns hosts associated with a given OpenStack network.
518 *
Hyunsun Moon576d6872016-04-14 19:04:23 -0700519 * @param osNet openstack network
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800520 * @return set of hosts
521 */
Hyunsun Moon576d6872016-04-14 19:04:23 -0700522 private Set<Host> getHostsWithOpenstackNetwork(Network osNet) {
523 checkNotNull(osNet);
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700524
Hyunsun Moon576d6872016-04-14 19:04:23 -0700525 String osNetId = osNet.getId();
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800526 return StreamSupport.stream(hostService.getHosts().spliterator(), false)
Hyunsun Moon576d6872016-04-14 19:04:23 -0700527 .filter(host -> Objects.equals(osNetId, getServiceId(host)))
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800528 .collect(Collectors.toSet());
Hyunsun Moon6d247342016-02-12 12:48:47 -0800529 }
530
531 /**
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800532 * Registers static DHCP lease for a given host.
533 *
534 * @param host host
535 * @param service cord service
536 */
537 private void registerDhcpLease(Host host, CordService service) {
538 List<Ip4Address> options = Lists.newArrayList();
539 options.add(Ip4Address.makeMaskPrefix(service.serviceIpRange().prefixLength()));
540 options.add(service.serviceIp().getIp4Address());
541 options.add(service.serviceIp().getIp4Address());
542 options.add(DEFAULT_DNS);
543
544 log.debug("Set static DHCP mapping for {}", host.mac());
545 dhcpService.setStaticMapping(host.mac(),
546 host.ipAddresses().stream().findFirst().get().getIp4Address(),
547 true,
548 options);
549 }
550
551 /**
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800552 * Handles VM detected situation.
553 *
554 * @param host host
555 */
556 private void serviceVmAdded(Host host) {
Hyunsun Moon576d6872016-04-14 19:04:23 -0700557 checkNotNull(osAccess, "OpenStack access is not set");
558
Hyunsun Moon98025542016-03-08 04:36:02 -0800559 String serviceVlan = host.annotations().value(S_TAG);
560 if (serviceVlan != null) {
561 virtualSubscriberGatewayAdded(host, serviceVlan);
562 }
563
Hyunsun Moon576d6872016-04-14 19:04:23 -0700564 String osNetId = host.annotations().value(SERVICE_ID);
565 if (osNetId == null) {
Hyunsun Moon2a225162016-02-17 19:00:50 -0800566 // ignore this host, it is not the service VM, or it's a vSG
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800567 return;
568 }
569
Hyunsun Moon576d6872016-04-14 19:04:23 -0700570 OSClient osClient = OSFactory.clientFromAccess(osAccess);
571 Network osNet = osClient.networking().network().get(osNetId);
572 if (osNet == null) {
Hyunsun Moond35420f2016-03-08 21:59:13 -0800573 log.warn("Failed to get OpenStack network {} for VM {}.",
Hyunsun Moon576d6872016-04-14 19:04:23 -0700574 osNetId, host.id());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800575 return;
576 }
577
Hyunsun Moond35420f2016-03-08 21:59:13 -0800578 log.info("VM is detected, MAC: {} IP: {}",
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800579 host.mac(),
580 host.ipAddresses().stream().findFirst().get());
581
Hyunsun Moon576d6872016-04-14 19:04:23 -0700582 CordService service = getCordService(osNet);
Hyunsun Moond52bffc2016-01-29 18:57:05 -0800583 if (service == null) {
Hyunsun Moondd91be22016-04-24 17:43:32 -0700584 log.warn("Failed to get CordService for {}", osNet.getName());
Hyunsun Moond52bffc2016-01-29 18:57:05 -0800585 return;
586 }
587
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800588 switch (service.serviceType()) {
589 case MANAGEMENT:
590 ruleInstaller.populateManagementNetworkRules(host, service);
591 break;
592 case PRIVATE:
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800593 arpProxy.addGateway(service.serviceIp(), privateGatewayMac);
Hyunsun Moon098cda82016-03-03 13:27:44 -0800594 case PUBLIC:
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800595 default:
Hyunsun Moondd91be22016-04-24 17:43:32 -0700596 // TODO get bidirectional information from XOS once XOS supports
597 service.tenantServices().stream().forEach(
598 tServiceId -> createServiceDependency(tServiceId, service.id(), true));
599 service.providerServices().stream().forEach(
600 pServiceId -> createServiceDependency(service.id(), pServiceId, true));
601
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800602 ruleInstaller.updateServiceGroup(service);
603 // sends gratuitous ARP here for the case of adding existing VMs
604 // when ONOS or cordvtn app is restarted
605 arpProxy.sendGratuitousArpForGateway(service.serviceIp(), Sets.newHashSet(host));
606 break;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800607 }
608
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800609 registerDhcpLease(host, service);
Hyunsun Moon576d6872016-04-14 19:04:23 -0700610 ruleInstaller.populateBasicConnectionRules(host, getTunnelIp(host), osNet);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800611 }
612
613 /**
614 * Handles VM removed situation.
615 *
616 * @param host host
617 */
618 private void serviceVmRemoved(Host host) {
Hyunsun Moon576d6872016-04-14 19:04:23 -0700619 checkNotNull(osAccess, "OpenStack access is not set");
620
Hyunsun Moon98025542016-03-08 04:36:02 -0800621 String serviceVlan = host.annotations().value(S_TAG);
622 if (serviceVlan != null) {
623 virtualSubscriberGatewayRemoved(host);
624 }
625
Hyunsun Moon576d6872016-04-14 19:04:23 -0700626 String osNetId = host.annotations().value(SERVICE_ID);
627 if (osNetId == null) {
Hyunsun Moon2a225162016-02-17 19:00:50 -0800628 // ignore it, it's not the service VM or it's a vSG
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800629 return;
630 }
631
Hyunsun Moon576d6872016-04-14 19:04:23 -0700632 OSClient osClient = OSFactory.clientFromAccess(osAccess);
633 Network osNet = osClient.networking().network().get(osNetId);
634 if (osNet == null) {
Hyunsun Moond35420f2016-03-08 21:59:13 -0800635 log.warn("Failed to get OpenStack network {} for VM {}",
Hyunsun Moon576d6872016-04-14 19:04:23 -0700636 osNetId, host.id());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800637 return;
638 }
639
Hyunsun Moond35420f2016-03-08 21:59:13 -0800640 log.info("VM is vanished, MAC: {} IP: {}",
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800641 host.mac(),
642 host.ipAddresses().stream().findFirst().get());
643
644 ruleInstaller.removeBasicConnectionRules(host);
Hyunsun Moonfb631b42016-02-03 14:44:06 -0800645 dhcpService.removeStaticMapping(host.mac());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800646
Hyunsun Moon576d6872016-04-14 19:04:23 -0700647 CordService service = getCordService(osNet);
Hyunsun Moond52bffc2016-01-29 18:57:05 -0800648 if (service == null) {
649 return;
650 }
651
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800652 switch (service.serviceType()) {
653 case MANAGEMENT:
654 ruleInstaller.removeManagementNetworkRules(host, service);
655 break;
656 case PRIVATE:
Hyunsun Moon576d6872016-04-14 19:04:23 -0700657 if (getHostsWithOpenstackNetwork(osNet).isEmpty()) {
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800658 arpProxy.removeGateway(service.serviceIp());
659 }
Hyunsun Moon098cda82016-03-03 13:27:44 -0800660 case PUBLIC:
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800661 default:
Hyunsun Moondd91be22016-04-24 17:43:32 -0700662 if (!service.tenantServices().isEmpty()) {
663 ruleInstaller.updateServiceGroup(service);
664 }
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800665 break;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700666 }
667 }
668
Hyunsun Moon98025542016-03-08 04:36:02 -0800669
670 /**
671 * Handles virtual subscriber gateway VM or container.
672 *
673 * @param host new host with stag, it can be vsg VM or vsg
674 * @param serviceVlan service vlan
675 */
676 private void virtualSubscriberGatewayAdded(Host host, String serviceVlan) {
677 Map<IpAddress, MacAddress> vSgs;
678 Host vSgHost;
679
680 String vSgHostId = host.annotations().value(VSG_HOST_ID);
681 if (vSgHostId == null) {
682 log.debug("vSG VM detected {}", host.id());
683
684 vSgHost = host;
685 vSgs = getSubscriberGateways(vSgHost);
686 vSgs.entrySet().stream().forEach(entry -> addVirtualSubscriberGateway(
687 vSgHost,
688 entry.getKey(),
689 entry.getValue(),
690 serviceVlan));
691 } else {
692 vSgHost = hostService.getHost(HostId.hostId(vSgHostId));
693 if (vSgHost == null) {
694 return;
695 }
696
697 log.debug("vSG detected {}", host.id());
698 vSgs = getSubscriberGateways(vSgHost);
699 }
700
701 ruleInstaller.populateSubscriberGatewayRules(vSgHost, vSgs.keySet());
702 }
703
704 /**
705 * Handles virtual subscriber gateway removed.
706 *
707 * @param vSg vsg host to remove
708 */
709 private void virtualSubscriberGatewayRemoved(Host vSg) {
710 String vSgHostId = vSg.annotations().value(VSG_HOST_ID);
711 if (vSgHostId == null) {
712 return;
713 }
714
715 Host vSgHost = hostService.getHost(HostId.hostId(vSgHostId));
716 if (vSgHost == null) {
717 return;
718 }
719
720 log.info("vSG removed {}", vSg.id());
721 Map<IpAddress, MacAddress> vSgs = getSubscriberGateways(vSgHost);
722 ruleInstaller.populateSubscriberGatewayRules(vSgHost, vSgs.keySet());
723 }
724
Hyunsun Moon746956f2016-01-24 21:47:06 -0800725 /**
726 * Sets service network gateway MAC address and sends out gratuitous ARP to all
727 * VMs to update the gateway MAC address.
728 *
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800729 * @param newMac mac address to update
Hyunsun Moon746956f2016-01-24 21:47:06 -0800730 */
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800731 private void setPrivateGatewayMac(MacAddress newMac) {
Hyunsun Moondd91be22016-04-24 17:43:32 -0700732 checkNotNull(osAccess, "OpenStack access is not set");
733
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800734 if (newMac == null || newMac.equals(privateGatewayMac)) {
735 // no updates, do nothing
736 return;
Hyunsun Moon746956f2016-01-24 21:47:06 -0800737 }
738
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800739 privateGatewayMac = newMac;
740 log.debug("Set service gateway MAC address to {}", privateGatewayMac.toString());
741
Hyunsun Moondd91be22016-04-24 17:43:32 -0700742 OSClient osClient = OSFactory.clientFromAccess(osAccess);
743 List<Network> vNets = Lists.newArrayList(osClient.networking().network().list().iterator());
Hyunsun Moon746956f2016-01-24 21:47:06 -0800744
745 vNets.stream().forEach(vNet -> {
Hyunsun Moondd91be22016-04-24 17:43:32 -0700746 CordService service = getCordService(vNet);
Hyunsun Moon746956f2016-01-24 21:47:06 -0800747 if (service != null) {
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800748 arpProxy.addGateway(service.serviceIp(), privateGatewayMac);
749 arpProxy.sendGratuitousArpForGateway(service.serviceIp(), service.hosts().keySet());
Hyunsun Moon746956f2016-01-24 21:47:06 -0800750 }
751 });
752 }
753
754 /**
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800755 * Sets public gateway MAC address.
756 *
757 * @param publicGateways gateway ip and mac address pairs
758 */
759 private void setPublicGatewayMac(Map<IpAddress, MacAddress> publicGateways) {
760 publicGateways.entrySet()
761 .stream()
762 .forEach(entry -> {
763 arpProxy.addGateway(entry.getKey(), entry.getValue());
Hyunsun Moon576d6872016-04-14 19:04:23 -0700764 log.debug("Added public gateway IP {}, MAC {}",
765 entry.getKey().toString(), entry.getValue().toString());
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800766 });
767 // TODO notice gateway MAC change to VMs holds this gateway IP
768 }
769
770 /**
Hyunsun Moon576d6872016-04-14 19:04:23 -0700771 * Sets OpenStack access information.
772 * Access is the entity returned when authenticated and provides a singleton client
773 * between multiple threads.
774 *
775 * @param osConfig openstack config
776 */
777 private void setOpenstackAccess(CordVtnConfig.OpenStackConfig osConfig) {
Hyunsun Moondd91be22016-04-24 17:43:32 -0700778 checkNotNull(osConfig, "OpenStack access is not configured");
779
Hyunsun Moon576d6872016-04-14 19:04:23 -0700780 log.debug("Get OpenStack access with Endpoint: {} Tenant: {} User: {} Passwd: {}",
781 osConfig.endpoint(),
782 osConfig.tenant(),
783 osConfig.user(),
784 osConfig.password());
785 try {
786 osAccess = OSFactory.builder()
787 .endpoint(osConfig.endpoint())
788 .credentials(osConfig.user(), osConfig.password())
789 .tenantName(osConfig.tenant())
790 .authenticate()
791 .getAccess();
792 } catch (AuthenticationException e) {
793 log.error("Failed to get OpenStack Access");
794 }
795 }
796
797 /**
Hyunsun Moondd91be22016-04-24 17:43:32 -0700798 * Sets XOS access information.
799 *
800 * @param xosAccess xos access
801 */
802 private void setXosAccess(XosAccess xosAccess) {
803 checkNotNull(xosAccess, "XOS access is not configured");
804
805 log.debug("Set XOS access with Endpoint: {} User: {} Passwd: {}",
806 xosAccess.endpoint(),
807 xosAccess.username(),
808 xosAccess.password());
809
810 xosClient.setAccess(xosAccess);
811 }
812
813 /**
Hyunsun Moon746956f2016-01-24 21:47:06 -0800814 * Updates configurations.
815 */
816 private void readConfiguration() {
817 CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class);
818 if (config == null) {
819 log.debug("No configuration found");
820 return;
821 }
822
Hyunsun Moondd91be22016-04-24 17:43:32 -0700823 setXosAccess(config.xosAccess());
Hyunsun Moon576d6872016-04-14 19:04:23 -0700824 setOpenstackAccess(config.openstackConfig());
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800825 setPrivateGatewayMac(config.privateGatewayMac());
826 setPublicGatewayMac(config.publicGateways());
Hyunsun Moon576d6872016-04-14 19:04:23 -0700827 }
Hyunsun Moon746956f2016-01-24 21:47:06 -0800828
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700829 private class InternalHostListener implements HostListener {
830
831 @Override
832 public void event(HostEvent event) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800833 Host host = event.subject();
Hyunsun Moond35420f2016-03-08 21:59:13 -0800834 if (!mastershipService.isLocalMaster(host.location().deviceId())) {
835 // do not allow to proceed without mastership
836 return;
837 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700838
839 switch (event.type()) {
Hyunsun Moond35420f2016-03-08 21:59:13 -0800840 case HOST_UPDATED:
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700841 case HOST_ADDED:
Hyunsun Moond35420f2016-03-08 21:59:13 -0800842 eventExecutor.submit(() -> serviceVmAdded(host));
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700843 break;
844 case HOST_REMOVED:
Hyunsun Moond35420f2016-03-08 21:59:13 -0800845 eventExecutor.submit(() -> serviceVmRemoved(host));
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700846 break;
847 default:
848 break;
849 }
850 }
851 }
852
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800853 private class InternalPacketProcessor implements PacketProcessor {
854
855 @Override
856 public void process(PacketContext context) {
857 if (context.isHandled()) {
858 return;
859 }
860
861 Ethernet ethPacket = context.inPacket().parsed();
Hyunsun Moon746956f2016-01-24 21:47:06 -0800862 if (ethPacket == null || ethPacket.getEtherType() != Ethernet.TYPE_ARP) {
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800863 return;
864 }
865
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800866 arpProxy.processArpPacket(context, ethPacket);
Hyunsun Moon746956f2016-01-24 21:47:06 -0800867 }
868 }
869
870 private class InternalConfigListener implements NetworkConfigListener {
871
872 @Override
873 public void event(NetworkConfigEvent event) {
874 if (!event.configClass().equals(CordVtnConfig.class)) {
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800875 return;
876 }
877
Hyunsun Moon746956f2016-01-24 21:47:06 -0800878 switch (event.type()) {
879 case CONFIG_ADDED:
880 case CONFIG_UPDATED:
881 log.info("Network configuration changed");
882 eventExecutor.execute(CordVtn.this::readConfiguration);
883 break;
884 default:
885 break;
886 }
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800887 }
888 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700889}