2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping;
10 import static com.google.common.base.Preconditions.checkNotNull;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.List;
19 import java.util.concurrent.ExecutionException;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
23 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
24 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.groupbasedpolicy.neutron.gbp.util.NeutronGbpIidFactory;
27 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
28 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils.ForwardingCtx;
29 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.NeutronUtils;
30 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.Utils;
31 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
32 import org.opendaylight.groupbasedpolicy.util.IidFactory;
33 import org.opendaylight.neutron.spi.INeutronPortAware;
34 import org.opendaylight.neutron.spi.NeutronPort;
35 import org.opendaylight.neutron.spi.NeutronSecurityGroup;
36 import org.opendaylight.neutron.spi.NeutronSecurityRule;
37 import org.opendaylight.neutron.spi.Neutron_IPs;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2FloodDomainId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Name;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.UniqueId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInputBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterL3PrefixEndpointInput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterL3PrefixEndpointInputBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInputBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3Address;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3AddressBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.l3.prefix.fields.EndpointL3Gateways;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.l3.prefix.fields.EndpointL3GatewaysBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Key;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3PrefixKey;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L2;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L2Builder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L3;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L3Builder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.gbp.by.neutron.mappings.endpoints.by.floating.ip.ports.EndpointByFloatingIpPort;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.gbp.by.neutron.mappings.endpoints.by.ports.EndpointByPort;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.gbp.by.neutron.mappings.endpoints.by.router._interface.ports.EndpointByRouterInterfacePort;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.gbp.by.neutron.mappings.endpoints.by.router.gateway.ports.EndpointByRouterGatewayPort;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.floating.ip.ports.by.endpoints.FloatingIpPortByEndpoint;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.ports.by.endpoints.PortByEndpoint;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.router._interface.ports.by.endpoints.RouterInterfacePortByEndpoint;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.router.gateway.ports.by.endpoints.RouterGatewayPortByEndpoint;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContextInput;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContextInputBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroup;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Subnet;
80 import org.opendaylight.yangtools.yang.common.RpcResult;
81 import org.slf4j.Logger;
82 import org.slf4j.LoggerFactory;
84 import com.google.common.base.Function;
85 import com.google.common.base.Optional;
86 import com.google.common.base.Strings;
87 import com.google.common.collect.Collections2;
88 import com.google.common.collect.ImmutableList;
90 public class NeutronPortAware implements INeutronPortAware {
92 public static final Logger LOG = LoggerFactory.getLogger(NeutronPortAware.class);
93 private static final String DEVICE_OWNER_DHCP = "network:dhcp";
94 private static final String DEVICE_OWNER_ROUTER_IFACE = "network:router_interface";
95 private static final String DEVICE_OWNER_ROUTER_GATEWAY = "network:router_gateway";
96 private static final String DEVICE_OWNER_FLOATING_IP = "network:floatingip";
97 private static final int DHCP_CLIENT_PORT = 68;
98 private static final int DHCP_SERVER_PORT = 67;
99 private final DataBroker dataProvider;
100 private final EndpointService epService;
101 private final static Map<String, UniqueId> floatingIpPortByDeviceId = new HashMap<>();
103 public NeutronPortAware(DataBroker dataProvider, EndpointService epService) {
104 this.dataProvider = checkNotNull(dataProvider);
105 this.epService = checkNotNull(epService);
109 * @see org.opendaylight.neutron.spi.INeutronPortAware#canCreatePort(org.opendaylight.neutron.spi.NeutronPort)
112 public int canCreatePort(NeutronPort port) {
113 LOG.trace("canCreatePort - {}", port);
114 // TODO Li msunal this has to be rewrite when OFOverlay renderer will support l3-endpoints.
115 List<Neutron_IPs> fixedIPs = port.getFixedIPs();
116 if (fixedIPs != null && fixedIPs.size() > 1) {
117 LOG.warn("Neutron mapper does not support multiple IPs on the same port.");
118 return StatusCode.BAD_REQUEST;
120 return StatusCode.OK;
124 * @see org.opendaylight.neutron.spi.INeutronPortAware#neutronPortCreated(org.opendaylight.neutron.spi.NeutronPort)
127 public void neutronPortCreated(NeutronPort port) {
128 LOG.trace("neutronPortCreated - {}", port);
129 if (isRouterInterfacePort(port)) {
130 LOG.trace("Port is router interface - {} does nothing. {} handles router iface.",
131 NeutronPortAware.class.getSimpleName(), NeutronRouterAware.class.getSimpleName());
134 if (isRouterGatewayPort(port)) {
135 LOG.trace("Port is router gateway - {} does nothing. {} handles router iface.",
136 NeutronPortAware.class.getSimpleName(), NeutronRouterAware.class.getSimpleName());
139 if (isFloatingIpPort(port)) {
140 LOG.trace("Port is floating ip - {} device id - {}", port.getID(), port.getDeviceID());
141 floatingIpPortByDeviceId.put(port.getDeviceID(), new UniqueId(port.getID()));
144 ReadWriteTransaction rwTx = dataProvider.newReadWriteTransaction();
145 TenantId tenantId = new TenantId(Utils.normalizeUuid(port.getTenantID()));
146 if (isDhcpPort(port)) {
147 LOG.trace("Port is DHCP port. - {}", port.getID());
148 List<NeutronSecurityRule> dhcpSecRules = createDhcpSecRules(port, null, rwTx);
149 if (dhcpSecRules == null) {
154 for (NeutronSecurityRule dhcpSecRule : dhcpSecRules) {
155 boolean isDhcpSecRuleAdded = NeutronSecurityRuleAware.addNeutronSecurityRule(dhcpSecRule, rwTx);
156 if (!isDhcpSecRuleAdded) {
162 List<NeutronSecurityGroup> secGroups = port.getSecurityGroups();
163 if (secGroups != null) {
164 for (NeutronSecurityGroup secGroup : secGroups) {
165 EndpointGroupId epgId = new EndpointGroupId(secGroup.getSecurityGroupUUID());
166 Optional<EndpointGroup> potentialEpg = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
167 IidFactory.endpointGroupIid(tenantId, epgId), rwTx);
168 if (!potentialEpg.isPresent()) {
169 boolean isSecGroupCreated = NeutronSecurityGroupAware.addNeutronSecurityGroup(secGroup, rwTx);
170 if (!isSecGroupCreated) {
174 if (containsSecRuleWithRemoteSecGroup(secGroup)) {
175 List<NeutronSecurityRule> dhcpSecRules = createDhcpSecRules(port, epgId, rwTx);
176 if (dhcpSecRules == null) {
180 List<NeutronSecurityRule> routerSecRules = NeutronRouterAware.createRouterSecRules(port, epgId, rwTx);
181 if (routerSecRules == null) {
187 List<NeutronSecurityRule> secRules = secGroup.getSecurityRules();
188 if (secRules != null) {
189 for (NeutronSecurityRule secRule : secRules) {
190 NeutronSecurityRuleAware.addNeutronSecurityRule(secRule, rwTx);
197 boolean isNeutronPortCreated = addNeutronPort(port, rwTx, epService);
198 if (!isNeutronPortCreated) {
203 DataStoreHelper.submitToDs(rwTx);
206 public static boolean addNeutronPort(NeutronPort port, ReadWriteTransaction rwTx, EndpointService epService) {
207 TenantId tenantId = new TenantId(Utils.normalizeUuid(port.getTenantID()));
208 L2FloodDomainId l2FdId = new L2FloodDomainId(port.getNetworkUUID());
209 ForwardingCtx fwCtx = MappingUtils.createForwardingContext(tenantId, l2FdId, rwTx);
210 boolean isFwCtxValid = validateForwardingCtx(fwCtx);
214 EndpointKey epKey = new EndpointKey(fwCtx.getL2BridgeDomain().getId(), new MacAddress(port.getMacAddress()));
215 addNeutronGbpMapping(port, epKey, rwTx);
218 RegisterEndpointInput registerEpRpcInput = createRegisterEndpointInput(port, fwCtx);
220 RpcResult<Void> rpcResult = epService.registerEndpoint(registerEpRpcInput).get();
221 if (!rpcResult.isSuccessful()) {
222 LOG.warn("Illegal state - RPC registerEndpoint failed. Input of RPC: {}", registerEpRpcInput);
225 } catch (InterruptedException | ExecutionException e) {
226 LOG.error("addPort - RPC invocation failed.", e);
232 private static void addNeutronGbpMapping(NeutronPort port, EndpointKey epKey, ReadWriteTransaction rwTx) {
233 UniqueId portId = new UniqueId(port.getID());
234 if (isRouterInterfacePort(port)) {
235 LOG.trace("Adding RouterInterfacePort-Endpoint mapping for port {} and endpoint {}", port.getID(), epKey);
236 EndpointByRouterInterfacePort endpointByPort = MappingFactory.createEndpointByRouterInterfacePort(epKey,
238 rwTx.put(LogicalDatastoreType.OPERATIONAL, NeutronGbpIidFactory.endpointByRouterInterfacePortIid(portId),
239 endpointByPort, true);
240 RouterInterfacePortByEndpoint portByEndpoint = MappingFactory.createRouterInterfacePortByEndpoint(portId,
242 rwTx.put(LogicalDatastoreType.OPERATIONAL,
243 NeutronGbpIidFactory.routerInterfacePortByEndpointIid(epKey.getL2Context(), epKey.getMacAddress()),
244 portByEndpoint, true);
245 } else if (isRouterGatewayPort(port)) {
246 LOG.trace("Adding RouterGatewayPort-Endpoint mapping for port {} and endpoint {}", port.getID(), epKey);
247 EndpointByRouterGatewayPort endpointByPort = MappingFactory.createEndpointByRouterGatewayPort(epKey, portId);
248 rwTx.put(LogicalDatastoreType.OPERATIONAL, NeutronGbpIidFactory.endpointByRouterGatewayPortIid(portId),
249 endpointByPort, true);
250 RouterGatewayPortByEndpoint portByEndpoint = MappingFactory.createRouterGatewayPortByEndpoint(portId, epKey);
251 rwTx.put(LogicalDatastoreType.OPERATIONAL,
252 NeutronGbpIidFactory.routerGatewayPortByEndpointIid(epKey.getL2Context(), epKey.getMacAddress()),
253 portByEndpoint, true);
254 } else if (isFloatingIpPort(port)) {
255 LOG.trace("Adding FloatingIpPort-Endpoint mapping for port {} and endpoint {}", port.getID(), epKey);
256 EndpointByFloatingIpPort endpointByPort = MappingFactory.createEndpointByFloatingIpPort(epKey, portId);
257 rwTx.put(LogicalDatastoreType.OPERATIONAL, IidFactory.endpointByFloatingIpPortIid(portId),
258 endpointByPort, true);
259 FloatingIpPortByEndpoint portByEndpoint = MappingFactory.createFloatingIpPortByEndpoint(portId, epKey);
260 rwTx.put(LogicalDatastoreType.OPERATIONAL,
261 IidFactory.floatingIpPortByEndpointIid(epKey.getL2Context(), epKey.getMacAddress()),
262 portByEndpoint, true);
264 LOG.trace("Adding Port-Endpoint mapping for port {} (device owner {}) and endpoint {}", port.getID(),
265 port.getDeviceOwner(), epKey);
266 EndpointByPort endpointByPort = MappingFactory.createEndpointByPort(epKey, portId);
267 rwTx.put(LogicalDatastoreType.OPERATIONAL, NeutronGbpIidFactory.endpointByPortIid(portId), endpointByPort, true);
268 PortByEndpoint portByEndpoint = MappingFactory.createPortByEndpoint(portId, epKey);
269 rwTx.put(LogicalDatastoreType.OPERATIONAL,
270 NeutronGbpIidFactory.portByEndpointIid(epKey.getL2Context(), epKey.getMacAddress()), portByEndpoint, true);
274 public static boolean addL3PrefixEndpoint(L3ContextId l3ContextId, IpPrefix ipPrefix, IpAddress ipAddress, TenantId tenantId,
275 ReadWriteTransaction rwTx, EndpointService epService) {
277 EndpointL3PrefixKey epL3PrefixKey = new EndpointL3PrefixKey( ipPrefix, l3ContextId);
279 EndpointL3Key epL3Key = null;
280 List<EndpointL3Key> l3Gateways = new ArrayList<>();
281 if (ipAddress != null) {
282 epL3Key = new EndpointL3Key(ipAddress, l3ContextId);
283 l3Gateways.add(epL3Key);
288 RegisterL3PrefixEndpointInput registerL3PrefixEpRpcInput = createRegisterL3PrefixEndpointInput(epL3PrefixKey, l3Gateways,tenantId);
290 RpcResult<Void> rpcResult = epService.registerL3PrefixEndpoint(registerL3PrefixEpRpcInput).get();
291 if (!rpcResult.isSuccessful()) {
292 LOG.warn("Illegal state - RPC registerEndpoint failed. Input of RPC: {}", registerL3PrefixEpRpcInput);
295 } catch (InterruptedException | ExecutionException e) {
296 LOG.error("addPort - RPC invocation failed.", e);
303 private static boolean validateForwardingCtx(ForwardingCtx fwCtx) {
304 if (fwCtx.getL2FloodDomain() == null) {
305 LOG.warn("Illegal state - l2-flood-domain does not exist.");
308 if (fwCtx.getL2BridgeDomain() == null) {
309 LOG.warn("Illegal state - l2-bridge-domain does not exist.");
312 if (fwCtx.getL3Context() == null) {
313 LOG.warn("Illegal state - l3-context does not exist.");
319 private List<NeutronSecurityRule> createDhcpSecRules(NeutronPort port, EndpointGroupId consumerEpgId, ReadTransaction rTx) {
320 TenantId tenantId = new TenantId(Utils.normalizeUuid(port.getTenantID()));
321 Neutron_IPs firstIp = MappingUtils.getFirstIp(port.getFixedIPs());
322 if (firstIp == null) {
323 LOG.warn("Illegal state - DHCP port does not have an IP address.");
326 SubnetId dhcpSubnetId = new SubnetId(firstIp.getSubnetUUID());
327 Optional<Subnet> potentialSubnet = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
328 IidFactory.subnetIid(tenantId, dhcpSubnetId), rTx);
329 if (!potentialSubnet.isPresent()) {
330 LOG.warn("Illegal state - Subnet {} where is DHCP port does not exist.", dhcpSubnetId.getValue());
333 IpPrefix ipSubnet = potentialSubnet.get().getIpPrefix();
334 NeutronSecurityRule dhcpRuleEgress = createDhcpSecRule(port.getID(), tenantId, ipSubnet, consumerEpgId, true);
335 NeutronSecurityRule dhcpRuleIngress = createDhcpSecRule(port.getID(), tenantId, ipSubnet, consumerEpgId, false);
336 return ImmutableList.of(dhcpRuleEgress, dhcpRuleIngress);
339 private NeutronSecurityRule createDhcpSecRule(String ruleUuid, TenantId tenantId, IpPrefix ipSubnet, EndpointGroupId consumerEpgId,
341 NeutronSecurityRule dhcpSecRule = new NeutronSecurityRule();
342 dhcpSecRule.setSecurityRuleGroupID(MappingUtils.EPG_DHCP_ID.getValue());
343 dhcpSecRule.setSecurityRuleTenantID(tenantId.getValue());
344 dhcpSecRule.setSecurityRuleRemoteIpPrefix(Utils.getStringIpPrefix(ipSubnet));
345 if (consumerEpgId != null) {
346 dhcpSecRule.setSecurityRemoteGroupID(consumerEpgId.getValue());
349 dhcpSecRule.setSecurityRuleUUID(NeutronUtils.EGRESS + "__" + ruleUuid);
350 dhcpSecRule.setSecurityRuleDirection(NeutronUtils.EGRESS);
351 dhcpSecRule.setSecurityRulePortMin(DHCP_CLIENT_PORT);
352 dhcpSecRule.setSecurityRulePortMax(DHCP_CLIENT_PORT);
354 dhcpSecRule.setSecurityRuleUUID(NeutronUtils.INGRESS + "__" + ruleUuid);
355 dhcpSecRule.setSecurityRuleDirection(NeutronUtils.INGRESS);
356 dhcpSecRule.setSecurityRulePortMin(DHCP_SERVER_PORT);
357 dhcpSecRule.setSecurityRulePortMax(DHCP_SERVER_PORT);
359 dhcpSecRule.setSecurityRuleProtocol(NeutronUtils.UDP);
360 if (ipSubnet.getIpv4Prefix() != null) {
361 dhcpSecRule.setSecurityRuleEthertype(NeutronUtils.IPv4);
363 dhcpSecRule.setSecurityRuleEthertype(NeutronUtils.IPv6);
369 * @see org.opendaylight.neutron.spi.INeutronPortAware#canUpdatePort(org.opendaylight.neutron.spi.NeutronPort,
370 * org.opendaylight.neutron.spi.NeutronPort)
373 public int canUpdatePort(NeutronPort delta, NeutronPort original) {
374 LOG.trace("canUpdatePort - delta: {} original: {}", delta, original);
375 if (delta.getFixedIPs() == null || delta.getFixedIPs().isEmpty()) {
376 return StatusCode.OK;
378 // TODO Li msunal this has to be rewrite when OFOverlay renderer will support l3-endpoints.
379 List<Neutron_IPs> fixedIPs = delta.getFixedIPs();
380 if (fixedIPs != null && fixedIPs.size() > 1) {
381 LOG.warn("Neutron mapper does not support multiple IPs on the same port.");
382 return StatusCode.BAD_REQUEST;
384 return StatusCode.OK;
388 * @see org.opendaylight.neutron.spi.INeutronPortAware#neutronPortUpdated(org.opendaylight.neutron.spi.NeutronPort)
391 public void neutronPortUpdated(NeutronPort port) {
392 LOG.trace("neutronPortUpdated - {}", port);
393 if (isRouterInterfacePort(port)) {
394 LOG.trace("Port is router interface - {} does nothing. {} handles router iface.",
395 NeutronPortAware.class.getSimpleName(), NeutronRouterAware.class.getSimpleName());
398 if (isRouterGatewayPort(port)) {
399 LOG.trace("Port is router gateway - {}", port.getID());
402 if (isFloatingIpPort(port)) {
403 LOG.trace("Port is floating ip - {}", port.getID());
406 if (Strings.isNullOrEmpty(port.getTenantID())) {
407 LOG.trace("REMOVE ME: Tenant is null - {}", port.getID());
411 ReadOnlyTransaction rTx = dataProvider.newReadOnlyTransaction();
412 TenantId tenantId = new TenantId(Utils.normalizeUuid(port.getTenantID()));
413 MacAddress macAddress = new MacAddress(port.getMacAddress());
414 L2FloodDomainId l2FdId = new L2FloodDomainId(port.getNetworkUUID());
415 ForwardingCtx fwCtx = MappingUtils.createForwardingContext(tenantId, l2FdId, rTx);
416 boolean isFwCtxValid = validateForwardingCtx(fwCtx);
422 Optional<Endpoint> potentionalEp = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
423 IidFactory.endpointIid(fwCtx.getL2BridgeDomain().getId(), macAddress), rTx);
424 if (!potentionalEp.isPresent()) {
425 LOG.warn("Illegal state - endpoint {} does not exist.", new EndpointKey(fwCtx.getL2BridgeDomain().getId(),
431 Endpoint ep = potentionalEp.get();
432 if (isEpIpDifferentThanPortFixedIp(ep, port) || isEpgDifferentThanSecGrp(ep, port)) {
433 UnregisterEndpointInput unregisterEpRpcInput = createUnregisterEndpointInput(ep);
434 RegisterEndpointInput registerEpRpcInput = createRegisterEndpointInput(port, fwCtx);
436 RpcResult<Void> rpcResult = epService.unregisterEndpoint(unregisterEpRpcInput).get();
437 if (!rpcResult.isSuccessful()) {
438 LOG.warn("Illegal state - RPC unregisterEndpoint failed. Input of RPC: {}", unregisterEpRpcInput);
442 rpcResult = epService.registerEndpoint(registerEpRpcInput).get();
443 if (!rpcResult.isSuccessful()) {
444 LOG.warn("Illegal state - RPC registerEndpoint failed. Input of RPC: {}", registerEpRpcInput);
448 } catch (InterruptedException | ExecutionException e) {
449 LOG.error("addPort - RPC invocation failed.", e);
457 private boolean isEpIpDifferentThanPortFixedIp(Endpoint ep, NeutronPort port) {
458 List<L3Address> l3Addresses = ep.getL3Address();
459 List<Neutron_IPs> fixedIPs = port.getFixedIPs();
460 if ((l3Addresses == null || l3Addresses.isEmpty()) && (fixedIPs == null || fixedIPs.isEmpty())) {
463 if (l3Addresses != null && !l3Addresses.isEmpty() && fixedIPs != null && !fixedIPs.isEmpty()) {
464 if (fixedIPs.get(0).getIpAddress().equals(Utils.getStringIpAddress(l3Addresses.get(0).getIpAddress()))) {
471 private boolean isEpgDifferentThanSecGrp(Endpoint ep, NeutronPort port) {
472 List<EndpointGroupId> epgIds = ep.getEndpointGroups();
473 List<NeutronSecurityGroup> secGroups = port.getSecurityGroups();
474 if ((epgIds == null || epgIds.isEmpty()) && (secGroups == null || secGroups.isEmpty())) {
477 if (epgIds != null && !epgIds.isEmpty() && secGroups != null && !secGroups.isEmpty()) {
478 if (epgIds.size() != secGroups.size()) {
481 Collection<EndpointGroupId> epgIdsFromSecGroups = Collections2.transform(secGroups,
482 new Function<NeutronSecurityGroup, EndpointGroupId>() {
485 public EndpointGroupId apply(NeutronSecurityGroup input) {
486 return new EndpointGroupId(input.getSecurityGroupUUID());
489 // order independent equals
490 Set<EndpointGroupId> one = new HashSet<>(epgIds);
491 Set<EndpointGroupId> two = new HashSet<>(epgIdsFromSecGroups);
492 if (one.equals(two)) {
500 * @see org.opendaylight.neutron.spi.INeutronPortAware#canDeletePort(org.opendaylight.neutron.spi.NeutronPort)
503 public int canDeletePort(NeutronPort port) {
504 LOG.trace("canDeletePort - {}", port);
505 // nothing to consider
506 return StatusCode.OK;
510 * @see org.opendaylight.neutron.spi.INeutronPortAware#neutronPortDeleted(org.opendaylight.neutron.spi.NeutronPort)
513 public void neutronPortDeleted(NeutronPort port) {
514 LOG.trace("neutronPortDeleted - {}", port);
515 if (isRouterInterfacePort(port)) {
516 LOG.trace("Port is router interface - {} does nothing. {} handles router iface.",
517 NeutronPortAware.class.getSimpleName(), NeutronRouterAware.class.getSimpleName());
520 if (isRouterGatewayPort(port)) {
521 LOG.trace("Port is router gateway - {} does nothing. {} handles router iface.",
522 NeutronPortAware.class.getSimpleName(), NeutronRouterAware.class.getSimpleName());
525 if (isFloatingIpPort(port)) {
526 LOG.trace("Port is floating ip - {} device id - {}", port.getID(), port.getDeviceID());
527 floatingIpPortByDeviceId.remove(port.getDeviceID());
529 ReadWriteTransaction rwTx = dataProvider.newReadWriteTransaction();
530 TenantId tenantId = new TenantId(Utils.normalizeUuid(port.getTenantID()));
531 L2FloodDomainId l2FdId = new L2FloodDomainId(port.getNetworkUUID());
532 ForwardingCtx fwCtx = MappingUtils.createForwardingContext(tenantId, l2FdId, rwTx);
533 boolean isFwCtxValid = validateForwardingCtx(fwCtx);
539 EndpointKey epKey = new EndpointKey(fwCtx.getL2BridgeDomain().getId(), new MacAddress(port.getMacAddress()));
540 deleteNeutronGbpMapping(port, epKey, rwTx);
541 UnregisterEndpointInput unregisterEpRpcInput = createUnregisterEndpointInput(port, fwCtx);
543 RpcResult<Void> rpcResult = epService.unregisterEndpoint(unregisterEpRpcInput).get();
544 if (!rpcResult.isSuccessful()) {
545 LOG.warn("Illegal state - RPC unregisterEndpoint failed. Input of RPC: {}", unregisterEpRpcInput);
547 } catch (InterruptedException | ExecutionException e) {
548 LOG.error("addPort - RPC invocation failed.", e);
553 private static void deleteNeutronGbpMapping(NeutronPort port, EndpointKey epKey, ReadWriteTransaction rwTx) {
554 UniqueId portId = new UniqueId(port.getID());
555 if (isRouterInterfacePort(port)) {
556 LOG.trace("Adding RouterInterfacePort-Endpoint mapping for port {} and endpoint {}", port.getID(), epKey);
557 DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
558 IidFactory.endpointByRouterInterfacePortIid(portId), rwTx);
559 DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
560 IidFactory.routerInterfacePortByEndpointIid(epKey.getL2Context(), epKey.getMacAddress()), rwTx);
561 } else if (isRouterGatewayPort(port)) {
562 LOG.trace("Adding RouterGatewayPort-Endpoint mapping for port {} and endpoint {}", port.getID(), epKey);
563 DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
564 IidFactory.endpointByRouterGatewayPortIid(portId), rwTx);
565 DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
566 IidFactory.routerGatewayPortByEndpointIid(epKey.getL2Context(), epKey.getMacAddress()), rwTx);
567 } else if (isFloatingIpPort(port)) {
568 LOG.trace("Adding FloatingIpPort-Endpoint mapping for port {} and endpoint {}", port.getID(), epKey);
569 DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
570 IidFactory.endpointByFloatingIpPortIid(portId), rwTx);
571 DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
572 IidFactory.floatingIpPortByEndpointIid(epKey.getL2Context(), epKey.getMacAddress()), rwTx);
574 LOG.trace("Adding Port-Endpoint mapping for port {} (device owner {}) and endpoint {}", port.getID(),
575 port.getDeviceOwner(), epKey);
576 DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL, IidFactory.endpointByPortIid(portId), rwTx);
577 DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
578 IidFactory.portByEndpointIid(epKey.getL2Context(), epKey.getMacAddress()), rwTx);
582 private static RegisterL3PrefixEndpointInput createRegisterL3PrefixEndpointInput(EndpointL3PrefixKey key, List<EndpointL3Key> endpointL3Keys, TenantId tenantId) {
583 List<EndpointGroupId> epgIds = new ArrayList<>();
584 // each EP has to be in EPG ANY, except dhcp and router
585 epgIds.add(MappingUtils.EPG_ANY_ID);
587 List<EndpointL3Gateways> l3Gateways = new ArrayList<EndpointL3Gateways>();
588 for (EndpointL3Key epL3Key : endpointL3Keys) {
589 EndpointL3Gateways l3Gateway = new EndpointL3GatewaysBuilder().setIpAddress(epL3Key.getIpAddress())
590 .setL3Context(epL3Key.getL3Context())
592 l3Gateways.add(l3Gateway);
594 RegisterL3PrefixEndpointInputBuilder inputBuilder = new RegisterL3PrefixEndpointInputBuilder()
595 .setL3Context(key.getL3Context())
596 .setIpPrefix(key.getIpPrefix())
597 .setEndpointGroups(epgIds)
599 .setEndpointL3Gateways(l3Gateways)
600 .setTimestamp(System.currentTimeMillis());
601 return inputBuilder.build();
604 private static RegisterEndpointInput createRegisterEndpointInput(NeutronPort port, ForwardingCtx fwCtx) {
605 List<EndpointGroupId> epgIds = new ArrayList<>();
606 // each EP has to be in EPG ANY, except dhcp and router
607 if (isDhcpPort(port)) {
608 epgIds.add(MappingUtils.EPG_DHCP_ID);
609 } else if (!containsSecRuleWithRemoteSecGroup(port.getSecurityGroups())) {
610 epgIds.add(MappingUtils.EPG_ANY_ID);
613 List<NeutronSecurityGroup> securityGroups = port.getSecurityGroups();
614 if ((securityGroups == null || securityGroups.isEmpty())) {
615 if (!isDhcpPort(port)) {
617 "Port {} does not contain any security group. The port should belong to 'default' security group at least.",
621 for (NeutronSecurityGroup secGrp : securityGroups) {
622 epgIds.add(new EndpointGroupId(secGrp.getSecurityGroupUUID()));
625 LocationType locationType = LocationType.Internal;
626 if(isRouterGatewayPort(port)) {
627 locationType = LocationType.External;
629 RegisterEndpointInputBuilder inputBuilder = new RegisterEndpointInputBuilder().setL2Context(
630 fwCtx.getL2BridgeDomain().getId())
631 .setMacAddress(new MacAddress(port.getMacAddress()))
632 .setTenant(new TenantId(Utils.normalizeUuid(port.getTenantID())))
633 .setEndpointGroups(epgIds)
634 .addAugmentation(OfOverlayContextInput.class,
635 new OfOverlayContextInputBuilder()
636 .setPortName(createTapPortName(port))
637 .setLocationType(locationType)
639 .setTimestamp(System.currentTimeMillis());
640 List<Neutron_IPs> fixedIPs = port.getFixedIPs();
641 // TODO Li msunal this getting of just first IP has to be rewrite when OFOverlay renderer
642 // will support l3-endpoints. Then we will register L2 and L3 endpoints separately.
643 Neutron_IPs firstIp = MappingUtils.getFirstIp(fixedIPs);
644 if (firstIp != null) {
645 inputBuilder.setNetworkContainment(new SubnetId(firstIp.getSubnetUUID()));
646 L3Address l3Address = new L3AddressBuilder().setIpAddress(Utils.createIpAddress(firstIp.getIpAddress()))
647 .setL3Context(fwCtx.getL3Context().getId())
649 inputBuilder.setL3Address(ImmutableList.of(l3Address));
651 if (!Strings.isNullOrEmpty(port.getName())) {
654 return inputBuilder.build();
657 private static boolean containsSecRuleWithRemoteSecGroup(List<NeutronSecurityGroup> secGroups) {
658 if (secGroups == null) {
661 for (NeutronSecurityGroup secGroup : secGroups) {
662 boolean containsSecRuleWithRemoteSecGroup = containsSecRuleWithRemoteSecGroup(secGroup);
663 if (containsSecRuleWithRemoteSecGroup) {
670 private static boolean containsSecRuleWithRemoteSecGroup(NeutronSecurityGroup secGroup) {
671 List<NeutronSecurityRule> secRules = secGroup.getSecurityRules();
672 if (secRules == null) {
675 for (NeutronSecurityRule secRule : secRules) {
676 if (!Strings.isNullOrEmpty(secRule.getSecurityRemoteGroupID())) {
683 private static Name createTapPortName(NeutronPort port) {
684 return new Name("tap" + port.getID().substring(0, 11));
687 private static boolean isDhcpPort(NeutronPort port) {
688 return DEVICE_OWNER_DHCP.equals(port.getDeviceOwner());
691 private static boolean isRouterInterfacePort(NeutronPort port) {
692 return DEVICE_OWNER_ROUTER_IFACE.equals(port.getDeviceOwner());
695 private static boolean isRouterGatewayPort(NeutronPort port) {
696 return DEVICE_OWNER_ROUTER_GATEWAY.equals(port.getDeviceOwner());
699 private static boolean isFloatingIpPort(NeutronPort port) {
700 return DEVICE_OWNER_FLOATING_IP.equals(port.getDeviceOwner());
703 private UnregisterEndpointInput createUnregisterEndpointInput(Endpoint ep) {
704 UnregisterEndpointInputBuilder inputBuilder = new UnregisterEndpointInputBuilder();
705 L2 l2Ep = new L2Builder().setL2Context(ep.getL2Context()).setMacAddress(ep.getMacAddress()).build();
706 inputBuilder.setL2(ImmutableList.of(l2Ep));
707 // TODO Li msunal this has to be rewrite when OFOverlay renderer will support l3-endpoints.
708 // Endpoint probably will not have l3-addresses anymore, because L2 and L3 endpoints should
709 // be registered separately.
710 if (ep.getL3Address() != null && !ep.getL3Address().isEmpty()) {
711 List<L3> l3Eps = new ArrayList<>();
712 for (L3Address ip : ep.getL3Address()) {
713 l3Eps.add(new L3Builder().setL3Context(ip.getL3Context()).setIpAddress(ip.getIpAddress()).build());
715 inputBuilder.setL3(l3Eps);
717 return inputBuilder.build();
720 private UnregisterEndpointInput createUnregisterEndpointInput(NeutronPort port, ForwardingCtx fwCtx) {
721 UnregisterEndpointInputBuilder inputBuilder = new UnregisterEndpointInputBuilder();
722 L2 l2Ep = new L2Builder().setL2Context(fwCtx.getL2BridgeDomain().getId())
723 .setMacAddress(new MacAddress(port.getMacAddress()))
725 inputBuilder.setL2(ImmutableList.of(l2Ep));
726 // TODO Li msunal this has to be rewrite when OFOverlay renderer will support l3-endpoints.
727 // Endpoint probably will not have l3-addresses anymore, because L2 and L3 endpoints should
728 // be registered separately.
729 if (port.getFixedIPs() != null && !port.getFixedIPs().isEmpty()) {
730 inputBuilder.setL3(createL3s(port.getFixedIPs(), fwCtx.getL3Context().getId()));
732 return inputBuilder.build();
735 private List<L3> createL3s(List<Neutron_IPs> neutronIps, L3ContextId l3ContextId) {
736 List<L3> l3s = new ArrayList<>();
737 for (Neutron_IPs fixedIp : neutronIps) {
738 String ip = fixedIp.getIpAddress();
739 L3 l3 = new L3Builder().setIpAddress(Utils.createIpAddress(ip)).setL3Context(l3ContextId).build();
745 public static UniqueId getFloatingIpPortIdByDeviceId(String deviceId) {
746 return floatingIpPortByDeviceId.get(deviceId);