2 * Copyright (c) 2013, 2015 IBM Corporation 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
9 package org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
17 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronFloatingIP;
18 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronFloatingIPCRUD;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.Floatingips;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.Floatingip;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.FloatingipBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.osgi.framework.BundleContext;
26 import org.osgi.framework.ServiceRegistration;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class NeutronFloatingIPInterface extends AbstractNeutronInterface<Floatingip, NeutronFloatingIP> implements INeutronFloatingIPCRUD {
31 private static final Logger LOGGER = LoggerFactory.getLogger(NeutronFloatingIPInterface.class);
33 NeutronFloatingIPInterface(ProviderContext providerContext) {
34 super(providerContext);
37 // IfNBFloatingIPCRUD interface methods
40 public boolean floatingIPExists(String uuid) {
41 Floatingip fip = readMd(createInstanceIdentifier(toMd(uuid)));
46 public NeutronFloatingIP getFloatingIP(String uuid) {
47 Floatingip fip = readMd(createInstanceIdentifier(toMd(uuid)));
55 public List<NeutronFloatingIP> getAllFloatingIPs() {
56 Set<NeutronFloatingIP> allIPs = new HashSet<>();
57 Floatingips fips = readMd(createInstanceIdentifier());
59 for (Floatingip fip: fips.getFloatingip()) {
60 allIPs.add(fromMd(fip));
63 LOGGER.debug("Exiting getAllFloatingIPs, Found {} FloatingIPs", allIPs.size());
64 List<NeutronFloatingIP> ans = new ArrayList<>();
70 public boolean addFloatingIP(NeutronFloatingIP input) {
71 if (floatingIPExists(input.getID())) {
78 public boolean removeFloatingIP(String uuid) {
79 NeutronFloatingIP fip = getFloatingIP(uuid);
83 return removeMd(toMd(uuid));
87 public boolean updateFloatingIP(String uuid, NeutronFloatingIP delta) {
88 NeutronFloatingIP target = getFloatingIP(uuid);
92 delta.setPortUUID(target.getPortUUID());
93 delta.setFixedIPAddress(target.getFixedIPAddress());
94 return updateMd(delta);
98 protected Floatingip toMd(String uuid) {
99 FloatingipBuilder floatingipBuilder = new FloatingipBuilder();
100 floatingipBuilder.setUuid(toUuid(uuid));
101 return floatingipBuilder.build();
105 protected Floatingip toMd(NeutronFloatingIP floatingIp) {
106 FloatingipBuilder floatingipBuilder = new FloatingipBuilder();
107 if (floatingIp.getFixedIPAddress() != null) {
108 floatingipBuilder.setFixedIpAddress(new IpAddress(floatingIp.getFixedIPAddress().toCharArray()));
110 if(floatingIp.getFloatingIPAddress() != null) {
111 floatingipBuilder.setFloatingIpAddress(new IpAddress(floatingIp.getFloatingIPAddress().toCharArray()));
113 if (floatingIp.getFloatingNetworkUUID() != null) {
114 floatingipBuilder.setFloatingNetworkId(toUuid(floatingIp.getFloatingNetworkUUID()));
116 if (floatingIp.getPortUUID() != null) {
117 floatingipBuilder.setPortId(toUuid(floatingIp.getPortUUID()));
119 if (floatingIp.getRouterUUID() != null) {
120 floatingipBuilder.setRouterId(toUuid(floatingIp.getRouterUUID()));
122 if (floatingIp.getStatus() != null) {
123 floatingipBuilder.setStatus(floatingIp.getStatus());
125 if (floatingIp.getTenantUUID() != null) {
126 floatingipBuilder.setTenantId(toUuid(floatingIp.getTenantUUID()));
128 if (floatingIp.getID() != null) {
129 floatingipBuilder.setUuid(toUuid(floatingIp.getID()));
132 LOGGER.warn("Attempting to write neutron floating IP without UUID");
134 return floatingipBuilder.build();
137 protected NeutronFloatingIP fromMd(Floatingip fip) {
138 NeutronFloatingIP result = new NeutronFloatingIP();
139 result.setID(String.valueOf(fip.getUuid().getValue()));
140 if (fip.getFloatingNetworkId() != null) {
141 result.setFloatingNetworkUUID(String.valueOf(fip.getFloatingNetworkId().getValue()));
143 if (fip.getPortId() != null) {
144 result.setPortUUID(String.valueOf(fip.getPortId().getValue()));
146 if (fip.getFixedIpAddress() != null ) {
147 result.setFixedIPAddress(String.valueOf(fip.getFixedIpAddress().getValue()));
149 if (fip.getFloatingIpAddress() != null) {
150 result.setFloatingIPAddress(String.valueOf(fip.getFloatingIpAddress().getValue()));
152 if (fip.getTenantId() != null) {
153 result.setTenantUUID(String.valueOf(fip.getTenantId().getValue()));
155 if (fip.getRouterId() != null) {
156 result.setRouterUUID(String.valueOf(fip.getRouterId().getValue()));
158 result.setStatus(fip.getStatus());
163 protected InstanceIdentifier<Floatingip> createInstanceIdentifier(
165 return InstanceIdentifier.create(Neutron.class)
166 .child(Floatingips.class)
167 .child(Floatingip.class,item.getKey());
170 protected InstanceIdentifier<Floatingips> createInstanceIdentifier() {
171 return InstanceIdentifier.create(Neutron.class)
172 .child(Floatingips.class);
175 public static void registerNewInterface(BundleContext context,
176 ProviderContext providerContext,
177 List<ServiceRegistration<?>> registrations) {
178 NeutronFloatingIPInterface neutronFloatingIPInterface = new NeutronFloatingIPInterface(providerContext);
179 ServiceRegistration<INeutronFloatingIPCRUD> neutronFloatingIPInterfaceRegistration = context.registerService(INeutronFloatingIPCRUD.class, neutronFloatingIPInterface, null);
180 if (neutronFloatingIPInterfaceRegistration != null) {
181 registrations.add(neutronFloatingIPInterfaceRegistration);