2 * Copyright (C) 2014 Red Hat, Inc.
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.neutron.transcriber;
11 import java.lang.reflect.Method;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map.Entry;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
21 import org.opendaylight.neutron.spi.INeutronLoadBalancerPoolCRUD;
22 import org.opendaylight.neutron.spi.NeutronLoadBalancerPool;
23 import org.opendaylight.neutron.spi.NeutronLoadBalancerPoolMember;
24 import org.opendaylight.neutron.spi.NeutronLoadBalancer_SessionPersistence;
25 import org.opendaylight.neutron.spi.Neutron_ID;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev141002.PoolAttrs.Protocol;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev141002.lbaas.attributes.Pool;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev141002.lbaas.attributes.pool.Pools;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev141002.lbaas.attributes.pool.PoolsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev141002.pool.attrs.SessionPersistenceBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150325.Neutron;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class NeutronLoadBalancerPoolInterface extends AbstractNeutronInterface<Pools, NeutronLoadBalancerPool> implements INeutronLoadBalancerPoolCRUD {
38 private static final Logger LOGGER = LoggerFactory.getLogger(NeutronLoadBalancerPoolInterface.class);
39 private ConcurrentMap<String, NeutronLoadBalancerPool> loadBalancerPoolDB = new ConcurrentHashMap<String, NeutronLoadBalancerPool>();
42 NeutronLoadBalancerPoolInterface(ProviderContext providerContext) {
43 super(providerContext);
46 // this method uses reflection to update an object from it's delta.
48 private boolean overwrite(Object target, Object delta) {
49 Method[] methods = target.getClass().getMethods();
51 for (Method toMethod : methods) {
52 if (toMethod.getDeclaringClass().equals(target.getClass())
53 && toMethod.getName().startsWith("set")) {
55 String toName = toMethod.getName();
56 String fromName = toName.replace("set", "get");
59 Method fromMethod = delta.getClass().getMethod(fromName);
60 Object value = fromMethod.invoke(delta, (Object[]) null);
62 toMethod.invoke(target, value);
64 } catch (Exception e) {
65 LOGGER.error(e.getMessage());
74 public boolean neutronLoadBalancerPoolExists(String uuid) {
75 return loadBalancerPoolDB.containsKey(uuid);
79 public NeutronLoadBalancerPool getNeutronLoadBalancerPool(String uuid) {
80 if (!neutronLoadBalancerPoolExists(uuid)) {
81 LOGGER.debug("No LoadBalancerPool has Been Defined");
84 return loadBalancerPoolDB.get(uuid);
88 public List<NeutronLoadBalancerPool> getAllNeutronLoadBalancerPools() {
89 Set<NeutronLoadBalancerPool> allLoadBalancerPools = new HashSet<NeutronLoadBalancerPool>();
90 for (Entry<String, NeutronLoadBalancerPool> entry : loadBalancerPoolDB.entrySet()) {
91 NeutronLoadBalancerPool loadBalancerPool = entry.getValue();
92 allLoadBalancerPools.add(loadBalancerPool);
94 LOGGER.debug("Exiting getLoadBalancerPools, Found {} OpenStackLoadBalancerPool", allLoadBalancerPools.size());
95 List<NeutronLoadBalancerPool> ans = new ArrayList<NeutronLoadBalancerPool>();
96 ans.addAll(allLoadBalancerPools);
101 public boolean addNeutronLoadBalancerPool(NeutronLoadBalancerPool input) {
102 if (neutronLoadBalancerPoolExists(input.getLoadBalancerPoolID())) {
105 loadBalancerPoolDB.putIfAbsent(input.getLoadBalancerPoolID(), input);
106 //TODO: add code to find INeutronLoadBalancerPoolAware services and call newtorkCreated on them
111 public boolean removeNeutronLoadBalancerPool(String uuid) {
112 if (!neutronLoadBalancerPoolExists(uuid)) {
115 loadBalancerPoolDB.remove(uuid);
116 //TODO: add code to find INeutronLoadBalancerPoolAware services and call newtorkDeleted on them
121 public boolean updateNeutronLoadBalancerPool(String uuid, NeutronLoadBalancerPool delta) {
122 if (!neutronLoadBalancerPoolExists(uuid)) {
125 NeutronLoadBalancerPool target = loadBalancerPoolDB.get(uuid);
126 return overwrite(target, delta);
130 public boolean neutronLoadBalancerPoolInUse(String loadBalancerPoolUUID) {
131 return !neutronLoadBalancerPoolExists(loadBalancerPoolUUID);
135 protected Pools toMd(String uuid) {
136 PoolsBuilder poolsBuilder = new PoolsBuilder();
137 poolsBuilder.setUuid(toUuid(uuid));
138 return poolsBuilder.build();
142 protected InstanceIdentifier<Pools> createInstanceIdentifier(Pools pools) {
143 return InstanceIdentifier.create(Neutron.class)
145 .child(Pools.class, pools.getKey());
149 protected Pools toMd(NeutronLoadBalancerPool pools) {
150 PoolsBuilder poolsBuilder = new PoolsBuilder();
151 poolsBuilder.setAdminStateUp(pools.getLoadBalancerPoolAdminIsStateIsUp());
152 if (pools.getLoadBalancerPoolDescription() != null) {
153 poolsBuilder.setDescr(pools.getLoadBalancerPoolDescription());
155 if (pools.getNeutronLoadBalancerPoolHealthMonitorID() != null) {
156 List<Uuid> listHealthMonitor = new ArrayList<Uuid>();
157 listHealthMonitor.add(toUuid(pools.getNeutronLoadBalancerPoolHealthMonitorID()));
158 poolsBuilder.setHealthmonitorIds(listHealthMonitor);
160 if (pools.getLoadBalancerPoolLbAlgorithm() != null) {
161 poolsBuilder.setLbAlgorithm(pools.getLoadBalancerPoolLbAlgorithm());
163 if (pools.getLoadBalancerPoolListeners() != null) {
164 List<Uuid> listListener = new ArrayList<Uuid>();
165 for (Neutron_ID neutron_id : pools.getLoadBalancerPoolListeners()) {
166 listListener.add(toUuid(neutron_id.getID()));
168 poolsBuilder.setListeners(listListener);
170 if (pools.getLoadBalancerPoolMembers() != null) {
171 List<Uuid> listMember = new ArrayList<Uuid>();
172 for (NeutronLoadBalancerPoolMember laodBalancerPoolMember : pools.getLoadBalancerPoolMembers()) {
173 listMember.add(toUuid(laodBalancerPoolMember.getPoolMemberID()));
176 poolsBuilder.setMembers(listMember);
178 if (pools.getLoadBalancerPoolName() != null) {
179 poolsBuilder.setName(pools.getLoadBalancerPoolName());
181 if (pools.getLoadBalancerPoolProtocol() != null) {
182 poolsBuilder.setProtocol(Protocol.valueOf(pools.getLoadBalancerPoolProtocol()));
184 if (pools.getLoadBalancerPoolSessionPersistence() != null) {
185 NeutronLoadBalancer_SessionPersistence sessionPersistence = pools.getLoadBalancerPoolSessionPersistence();
186 SessionPersistenceBuilder sessionPersistenceBuilder = new SessionPersistenceBuilder();
187 sessionPersistenceBuilder.setCookieName(sessionPersistence.getCookieName());
188 sessionPersistenceBuilder.setType(sessionPersistence.getType());
189 poolsBuilder.setSessionPersistence(sessionPersistenceBuilder.build());
191 if (pools.getLoadBalancerPoolTenantID() != null) {
192 poolsBuilder.setTenantId(toUuid(pools.getLoadBalancerPoolTenantID()));
194 if (pools.getLoadBalancerPoolID() != null) {
195 poolsBuilder.setUuid(toUuid(pools.getLoadBalancerPoolID()));
197 LOGGER.warn("Attempting to write neutron load balancer pool without UUID");
199 return poolsBuilder.build();