Merge "Add md-transcribing for LoadBalancePool."
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronLoadBalancerPoolInterface.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
3  *
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
7  */
8
9 package org.opendaylight.neutron.transcriber;
10
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;
16 import java.util.Set;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19
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;
36
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>();
40
41
42     NeutronLoadBalancerPoolInterface(ProviderContext providerContext) {
43         super(providerContext);
44     }
45
46     // this method uses reflection to update an object from it's delta.
47
48     private boolean overwrite(Object target, Object delta) {
49         Method[] methods = target.getClass().getMethods();
50
51         for (Method toMethod : methods) {
52             if (toMethod.getDeclaringClass().equals(target.getClass())
53                     && toMethod.getName().startsWith("set")) {
54
55                 String toName = toMethod.getName();
56                 String fromName = toName.replace("set", "get");
57
58                 try {
59                     Method fromMethod = delta.getClass().getMethod(fromName);
60                     Object value = fromMethod.invoke(delta, (Object[]) null);
61                     if (value != null) {
62                         toMethod.invoke(target, value);
63                     }
64                 } catch (Exception e) {
65                     e.printStackTrace();
66                     return false;
67                 }
68             }
69         }
70         return true;
71     }
72
73     @Override
74     public boolean neutronLoadBalancerPoolExists(String uuid) {
75         return loadBalancerPoolDB.containsKey(uuid);
76     }
77
78     @Override
79     public NeutronLoadBalancerPool getNeutronLoadBalancerPool(String uuid) {
80         if (!neutronLoadBalancerPoolExists(uuid)) {
81             logger.debug("No LoadBalancerPool has Been Defined");
82             return null;
83         }
84         return loadBalancerPoolDB.get(uuid);
85     }
86
87     @Override
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);
93         }
94         logger.debug("Exiting getLoadBalancerPools, Found {} OpenStackLoadBalancerPool", allLoadBalancerPools.size());
95         List<NeutronLoadBalancerPool> ans = new ArrayList<NeutronLoadBalancerPool>();
96         ans.addAll(allLoadBalancerPools);
97         return ans;
98     }
99
100     @Override
101     public boolean addNeutronLoadBalancerPool(NeutronLoadBalancerPool input) {
102         if (neutronLoadBalancerPoolExists(input.getLoadBalancerPoolID())) {
103             return false;
104         }
105         loadBalancerPoolDB.putIfAbsent(input.getLoadBalancerPoolID(), input);
106         //TODO: add code to find INeutronLoadBalancerPoolAware services and call newtorkCreated on them
107         return true;
108     }
109
110     @Override
111     public boolean removeNeutronLoadBalancerPool(String uuid) {
112         if (!neutronLoadBalancerPoolExists(uuid)) {
113             return false;
114         }
115         loadBalancerPoolDB.remove(uuid);
116         //TODO: add code to find INeutronLoadBalancerPoolAware services and call newtorkDeleted on them
117         return true;
118     }
119
120     @Override
121     public boolean updateNeutronLoadBalancerPool(String uuid, NeutronLoadBalancerPool delta) {
122         if (!neutronLoadBalancerPoolExists(uuid)) {
123             return false;
124         }
125         NeutronLoadBalancerPool target = loadBalancerPoolDB.get(uuid);
126         return overwrite(target, delta);
127     }
128
129     @Override
130     public boolean neutronLoadBalancerPoolInUse(String loadBalancerPoolUUID) {
131         return !neutronLoadBalancerPoolExists(loadBalancerPoolUUID);
132     }
133
134     @Override
135     protected Pools toMd(String uuid) {
136         PoolsBuilder poolsBuilder = new PoolsBuilder();
137         poolsBuilder.setUuid(toUuid(uuid));
138         return poolsBuilder.build();
139     }
140
141     @Override
142     protected InstanceIdentifier<Pools> createInstanceIdentifier(Pools pools) {
143         return InstanceIdentifier.create(Neutron.class)
144                 .child(Pool.class)
145                 .child(Pools.class, pools.getKey());
146     }
147
148     @Override
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());
154         }
155         if (pools.getNeutronLoadBalancerPoolHealthMonitorID() != null) {
156             List<Uuid> listHealthMonitor = new ArrayList<Uuid>();
157             listHealthMonitor.add(toUuid(pools.getNeutronLoadBalancerPoolHealthMonitorID()));
158             poolsBuilder.setHealthmonitorIds(listHealthMonitor);
159         }
160         if (pools.getLoadBalancerPoolLbAlgorithm() != null) {
161             poolsBuilder.setLbAlgorithm(pools.getLoadBalancerPoolLbAlgorithm());
162         }
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()));
167             }
168             poolsBuilder.setListeners(listListener);
169         }
170         if (pools.getLoadBalancerPoolMembers() != null) {
171             List<Uuid> listMember = new ArrayList<Uuid>();
172             for (NeutronLoadBalancerPoolMember laodBalancerPoolMember : pools.getLoadBalancerPoolMembers()) {
173                 listMember.add(toUuid(laodBalancerPoolMember.getPoolMemberID()));
174
175             }
176             poolsBuilder.setMembers(listMember);
177         }
178         if (pools.getLoadBalancerPoolName() != null) {
179             poolsBuilder.setName(pools.getLoadBalancerPoolName());
180         }
181         if (pools.getLoadBalancerPoolProtocol() != null) {
182             poolsBuilder.setProtocol(Protocol.valueOf(pools.getLoadBalancerPoolProtocol()));
183         }
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());
190         }
191         if (pools.getLoadBalancerPoolTenantID() != null) {
192             poolsBuilder.setTenantId(toUuid(pools.getLoadBalancerPoolTenantID()));
193         }
194         if (pools.getLoadBalancerPoolID() != null) {
195             poolsBuilder.setUuid(toUuid(pools.getLoadBalancerPoolID()));
196         } else {
197             logger.warn("Attempting to write neutron load balancer pool without UUID");
198         }
199         return poolsBuilder.build();
200     }
201 }