2 * Copyright (c) 2014, 2015 Red Hat, 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
9 package org.opendaylight.netvirt.openstack.netvirt.translator.crud.impl;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map.Entry;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronLoadBalancer;
20 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.INeutronLoadBalancerCRUD;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev150712.lbaas.attributes.Loadbalancers;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev150712.lbaas.attributes.loadbalancers.Loadbalancer;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev150712.lbaas.attributes.loadbalancers.LoadbalancerBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.framework.ServiceRegistration;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * TODO: Migrate this to consume the MD-SAL data store, so that it can read all the data from data store.
34 * No need to worry about the write/update related methods here. OVSDB net-virt will use these CRUD Interface
35 * only for reading. We will cleanup these interface/methods later.
37 public class NeutronLoadBalancerInterface extends AbstractNeutronInterface<Loadbalancer, NeutronLoadBalancer> implements INeutronLoadBalancerCRUD {
38 private static final Logger LOGGER = LoggerFactory.getLogger(NeutronLoadBalancerInterface.class);
39 private ConcurrentMap<String, NeutronLoadBalancer> loadBalancerDB = new ConcurrentHashMap<>();
42 NeutronLoadBalancerInterface(final DataBroker dataBroker) {
47 public boolean neutronLoadBalancerExists(String uuid) {
48 return loadBalancerDB.containsKey(uuid);
52 public NeutronLoadBalancer getNeutronLoadBalancer(String uuid) {
53 if (!neutronLoadBalancerExists(uuid)) {
54 LOGGER.debug("No LoadBalancer Have Been Defined");
57 return loadBalancerDB.get(uuid);
61 public List<NeutronLoadBalancer> getAllNeutronLoadBalancers() {
62 Set<NeutronLoadBalancer> allLoadBalancers = new HashSet<>();
63 for (Entry<String, NeutronLoadBalancer> entry : loadBalancerDB.entrySet()) {
64 NeutronLoadBalancer loadBalancer = entry.getValue();
65 allLoadBalancers.add(loadBalancer);
67 LOGGER.debug("Exiting getLoadBalancers, Found {} OpenStackLoadBalancer", allLoadBalancers.size());
68 List<NeutronLoadBalancer> ans = new ArrayList<>();
69 ans.addAll(allLoadBalancers);
74 public boolean addNeutronLoadBalancer(NeutronLoadBalancer input) {
75 if (neutronLoadBalancerExists(input.getID())) {
78 loadBalancerDB.putIfAbsent(input.getID(), input);
79 //TODO: add code to find INeutronLoadBalancerAware services and call newtorkCreated on them
84 public boolean removeNeutronLoadBalancer(String uuid) {
85 if (!neutronLoadBalancerExists(uuid)) {
88 loadBalancerDB.remove(uuid);
89 //TODO: add code to find INeutronLoadBalancerAware services and call newtorkDeleted on them
94 public boolean updateNeutronLoadBalancer(String uuid, NeutronLoadBalancer delta) {
95 if (!neutronLoadBalancerExists(uuid)) {
98 NeutronLoadBalancer target = loadBalancerDB.get(uuid);
99 return overwrite(target, delta);
103 public boolean neutronLoadBalancerInUse(String loadBalancerUUID) {
104 return !neutronLoadBalancerExists(loadBalancerUUID);
108 protected Loadbalancer toMd(String uuid) {
109 LoadbalancerBuilder loadBalancersBuilder = new LoadbalancerBuilder();
110 loadBalancersBuilder.setUuid(toUuid(uuid));
111 return loadBalancersBuilder.build();
115 protected InstanceIdentifier<Loadbalancer> createInstanceIdentifier(
116 Loadbalancer loadBalancer) {
117 return InstanceIdentifier.create(Neutron.class)
118 .child(Loadbalancers.class)
119 .child(Loadbalancer.class, loadBalancer.getKey());
123 protected Loadbalancer toMd(NeutronLoadBalancer loadBalancer) {
124 LoadbalancerBuilder loadBalancersBuilder = new LoadbalancerBuilder();
125 loadBalancersBuilder.setAdminStateUp(loadBalancer.getLoadBalancerAdminStateUp());
126 if (loadBalancer.getLoadBalancerName() != null) {
127 loadBalancersBuilder.setName(loadBalancer.getLoadBalancerName());
129 if (loadBalancer.getLoadBalancerStatus() != null) {
130 loadBalancersBuilder.setStatus(loadBalancer.getLoadBalancerStatus());
132 if (loadBalancer.getLoadBalancerTenantID() != null) {
133 loadBalancersBuilder.setTenantId(toUuid(loadBalancer.getLoadBalancerTenantID()));
135 if (loadBalancer.getLoadBalancerVipAddress() != null) {
136 loadBalancersBuilder.setVipAddress(new IpAddress(loadBalancer.getLoadBalancerVipAddress().toCharArray()));
138 if (loadBalancer.getLoadBalancerVipSubnetID() != null) {
139 loadBalancersBuilder.setVipSubnetId(toUuid(loadBalancer.getLoadBalancerVipSubnetID()));
141 if (loadBalancer.getID() != null) {
142 loadBalancersBuilder.setUuid(toUuid(loadBalancer.getID()));
144 LOGGER.warn("Attempting to write neutron load balancer without UUID");
146 return loadBalancersBuilder.build();
149 public static void registerNewInterface(BundleContext context,
150 final DataBroker dataBroker,
151 List<ServiceRegistration<?>> registrations) {
152 NeutronLoadBalancerInterface neutronLoadBalancerInterface = new NeutronLoadBalancerInterface(dataBroker);
153 ServiceRegistration<INeutronLoadBalancerCRUD> neutronLoadBalancerInterfaceRegistration = context.registerService(INeutronLoadBalancerCRUD.class, neutronLoadBalancerInterface, null);
154 if(neutronLoadBalancerInterfaceRegistration != null) {
155 registrations.add(neutronLoadBalancerInterfaceRegistration);