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.neutron.transcriber;
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;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
20 import org.opendaylight.neutron.spi.INeutronLoadBalancerHealthMonitorCRUD;
21 import org.opendaylight.neutron.spi.NeutronLoadBalancerHealthMonitor;
22 import org.opendaylight.neutron.spi.Neutron_ID;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.ProbeBase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.ProbeHttp;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.ProbeHttps;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.ProbeIcmp;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.ProbeTcp;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev141002.lbaas.attributes.Healthmonitors;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev141002.lbaas.attributes.healthmonitors.Healthmonitor;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev141002.lbaas.attributes.healthmonitors.HealthmonitorBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150325.Neutron;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.ServiceRegistration;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import com.google.common.collect.ImmutableBiMap;
41 public class NeutronLoadBalancerHealthMonitorInterface extends AbstractNeutronInterface<Healthmonitor, NeutronLoadBalancerHealthMonitor> implements INeutronLoadBalancerHealthMonitorCRUD {
42 private static final Logger LOGGER = LoggerFactory.getLogger(NeutronLoadBalancerHealthMonitorInterface.class);
43 private ConcurrentMap<String, NeutronLoadBalancerHealthMonitor> loadBalancerHealthMonitorDB = new ConcurrentHashMap<String, NeutronLoadBalancerHealthMonitor>();
45 private static final ImmutableBiMap<Class<? extends ProbeBase>,String> PROBE_MAP
46 = new ImmutableBiMap.Builder<Class<? extends ProbeBase>,String>()
47 .put(ProbeHttp.class,"HTTP")
48 .put(ProbeHttps.class,"HTTPS")
49 .put(ProbeIcmp.class,"ICMP")
50 .put(ProbeTcp.class,"TCP")
54 NeutronLoadBalancerHealthMonitorInterface(ProviderContext providerContext) {
55 super(providerContext);
59 public boolean neutronLoadBalancerHealthMonitorExists(String uuid) {
64 public NeutronLoadBalancerHealthMonitor getNeutronLoadBalancerHealthMonitor(String uuid) {
69 public List<NeutronLoadBalancerHealthMonitor> getAll() {
70 Set<NeutronLoadBalancerHealthMonitor> allLoadBalancerHealthMonitors = new HashSet<NeutronLoadBalancerHealthMonitor>();
71 Healthmonitors healthMonitors = readMd(createInstanceIdentifier());
72 if (healthMonitors != null) {
73 for (Healthmonitor healthMonitor : healthMonitors.getHealthmonitor()) {
74 allLoadBalancerHealthMonitors.add(fromMd(healthMonitor));
77 LOGGER.debug("Exiting getLoadBalancerHealthMonitors, Found {} OpenStackLoadBalancerHealthMonitor", allLoadBalancerHealthMonitors.size());
78 List<NeutronLoadBalancerHealthMonitor> ans = new ArrayList<NeutronLoadBalancerHealthMonitor>();
79 ans.addAll(allLoadBalancerHealthMonitors);
84 public List<NeutronLoadBalancerHealthMonitor> getAllNeutronLoadBalancerHealthMonitors() {
89 public boolean addNeutronLoadBalancerHealthMonitor(NeutronLoadBalancerHealthMonitor input) {
94 public boolean removeNeutronLoadBalancerHealthMonitor(String uuid) {
99 public boolean updateNeutronLoadBalancerHealthMonitor(String uuid, NeutronLoadBalancerHealthMonitor delta) {
100 return update(uuid, delta);
104 public boolean neutronLoadBalancerHealthMonitorInUse(String loadBalancerHealthMonitorUUID) {
105 return !exists(loadBalancerHealthMonitorUUID);
109 protected Healthmonitor toMd(String uuid) {
110 HealthmonitorBuilder healthmonitorBuilder = new HealthmonitorBuilder();
111 healthmonitorBuilder.setUuid(toUuid(uuid));
112 return healthmonitorBuilder.build();
116 protected InstanceIdentifier<Healthmonitor> createInstanceIdentifier(
117 Healthmonitor healthMonitor) {
118 return InstanceIdentifier.create(Neutron.class)
119 .child(Healthmonitors.class )
120 .child(Healthmonitor.class, healthMonitor.getKey());
123 protected InstanceIdentifier<Healthmonitors> createInstanceIdentifier() {
124 return InstanceIdentifier.create(Neutron.class)
125 .child(Healthmonitors.class);
129 protected Healthmonitor toMd(NeutronLoadBalancerHealthMonitor healthMonitor) {
130 HealthmonitorBuilder healthmonitorBuilder = new HealthmonitorBuilder();
131 healthmonitorBuilder.setAdminStateUp(healthMonitor.getLoadBalancerHealthMonitorAdminStateIsUp());
132 if (healthMonitor.getLoadBalancerHealthMonitorDelay() != null) {
133 healthmonitorBuilder.setDelay(Long.valueOf(healthMonitor.getLoadBalancerHealthMonitorDelay()));
135 if (healthMonitor.getLoadBalancerHealthMonitorExpectedCodes() != null) {
136 healthmonitorBuilder.setExpectedCodes(healthMonitor.getLoadBalancerHealthMonitorExpectedCodes());
138 if (healthMonitor.getLoadBalancerHealthMonitorHttpMethod() != null) {
139 healthmonitorBuilder.setHttpMethod(healthMonitor.getLoadBalancerHealthMonitorHttpMethod());
141 if (healthMonitor.getLoadBalancerHealthMonitorMaxRetries() != null) {
142 healthmonitorBuilder.setMaxRetries(Integer.valueOf(healthMonitor.getLoadBalancerHealthMonitorMaxRetries()));
144 if (healthMonitor.getLoadBalancerHealthMonitorPools() != null) {
145 List<Uuid> listUuid = new ArrayList<Uuid>();
146 for (Neutron_ID neutron_id : healthMonitor.getLoadBalancerHealthMonitorPools()) {
147 listUuid.add(toUuid(neutron_id.getID()));
149 healthmonitorBuilder.setPools(listUuid);
151 if (healthMonitor.getTenantID() != null) {
152 healthmonitorBuilder.setTenantId(toUuid(healthMonitor.getTenantID()));
154 if (healthMonitor.getLoadBalancerHealthMonitorTimeout() != null) {
155 healthmonitorBuilder.setTimeout(Long.valueOf(healthMonitor.getLoadBalancerHealthMonitorTimeout()));
157 if (healthMonitor.getLoadBalancerHealthMonitorType() != null) {
158 ImmutableBiMap<String, Class<? extends ProbeBase>> mapper =
160 healthmonitorBuilder.setType((Class<? extends ProbeBase>) mapper.get(healthMonitor.getLoadBalancerHealthMonitorType()));
162 if (healthMonitor.getLoadBalancerHealthMonitorUrlPath() != null) {
163 healthmonitorBuilder.setUrlPath(healthMonitor.getLoadBalancerHealthMonitorUrlPath());
165 if (healthMonitor.getID() != null) {
166 healthmonitorBuilder.setUuid(toUuid(healthMonitor.getID()));
168 LOGGER.warn("Attempting to write neutron laod balancer health monitor without UUID");
170 return healthmonitorBuilder.build();
173 protected NeutronLoadBalancerHealthMonitor fromMd(Healthmonitor healthMonitor) {
174 NeutronLoadBalancerHealthMonitor answer = new NeutronLoadBalancerHealthMonitor();
175 if (healthMonitor.isAdminStateUp() != null) {
176 answer.setLoadBalancerHealthMonitorAdminStateIsUp(healthMonitor.isAdminStateUp());
178 if (healthMonitor.getDelay() != null) {
179 answer.setLoadBalancerHealthMonitorDelay(healthMonitor.getDelay().intValue());
181 if (healthMonitor.getExpectedCodes() != null) {
182 answer.setLoadBalancerHealthMonitorExpectedCodes(healthMonitor.getExpectedCodes());
184 if (healthMonitor.getHttpMethod() != null) {
185 answer.setLoadBalancerHealthMonitorHttpMethod(healthMonitor.getHttpMethod());
187 if (healthMonitor.getMaxRetries() != null) {
188 answer.setLoadBalancerHealthMonitorMaxRetries(Integer.valueOf(healthMonitor.getMaxRetries()));
190 if (healthMonitor.getPools() != null) {
191 List<Neutron_ID> list = new ArrayList<Neutron_ID>();
192 for (Uuid id : healthMonitor.getPools()) {
193 list.add(new Neutron_ID(id.getValue()));
195 answer.setLoadBalancerHealthMonitorPools(list);
197 if (healthMonitor.getTenantId() != null) {
198 answer.setTenantID(healthMonitor.getTenantId().getValue().replace("-",""));
200 if (healthMonitor.getTimeout() != null) {
201 answer.setLoadBalancerHealthMonitorTimeout(healthMonitor.getTimeout().intValue());
203 if (healthMonitor.getType() != null) {
204 answer.setLoadBalancerHealthMonitorType(PROBE_MAP.get(healthMonitor.getType()));
206 if (healthMonitor.getUrlPath() != null) {
207 answer.setLoadBalancerHealthMonitorUrlPath(healthMonitor.getUrlPath());
209 if (healthMonitor.getUuid() != null) {
210 answer.setID(healthMonitor.getUuid().getValue());
212 LOGGER.warn("Attempting to write neutron laod balancer health monitor without UUID");
217 public static void registerNewInterface(BundleContext context,
218 ProviderContext providerContext,
219 List<ServiceRegistration<?>> registrations) {
220 NeutronLoadBalancerHealthMonitorInterface neutronLoadBalancerHealthMonitorInterface = new NeutronLoadBalancerHealthMonitorInterface(providerContext);
221 ServiceRegistration<INeutronLoadBalancerHealthMonitorCRUD> neutronLoadBalancerHealthMonitorInterfaceRegistration = context.registerService(INeutronLoadBalancerHealthMonitorCRUD.class, neutronLoadBalancerHealthMonitorInterface, null);
222 if(neutronLoadBalancerHealthMonitorInterfaceRegistration != null) {
223 registrations.add(neutronLoadBalancerHealthMonitorInterfaceRegistration);