checkstyle: TypeName
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronLoadBalancerHealthMonitorInterface.java
1 /*
2  * Copyright (c) 2014, 2015 Red Hat, Inc. and others.  All rights reserved.
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 com.google.common.collect.ImmutableBiMap;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.neutron.spi.INeutronLoadBalancerHealthMonitorCRUD;
16 import org.opendaylight.neutron.spi.NeutronID;
17 import org.opendaylight.neutron.spi.NeutronLoadBalancerHealthMonitor;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProbeBase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProbeHttp;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProbeHttps;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProbePing;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProbeTcp;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev150712.lbaas.attributes.Healthmonitors;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev150712.lbaas.attributes.healthmonitors.Healthmonitor;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev150712.lbaas.attributes.healthmonitors.HealthmonitorBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev150712.lbaas.attributes.healthmonitors.HealthmonitorKey;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public final class NeutronLoadBalancerHealthMonitorInterface
32         extends AbstractNeutronInterface<Healthmonitor, Healthmonitors, HealthmonitorKey,
33                                          NeutronLoadBalancerHealthMonitor>
34         implements INeutronLoadBalancerHealthMonitorCRUD {
35     private static final Logger LOGGER = LoggerFactory.getLogger(NeutronLoadBalancerHealthMonitorInterface.class);
36
37     private static final ImmutableBiMap<Class<? extends ProbeBase>,
38             String> PROBE_MAP = new ImmutableBiMap.Builder<Class<? extends ProbeBase>, String>()
39                     .put(ProbeHttp.class, "HTTP").put(ProbeHttps.class, "HTTPS").put(ProbePing.class, "PING")
40                     .put(ProbeTcp.class, "TCP").build();
41
42     NeutronLoadBalancerHealthMonitorInterface(DataBroker db) {
43         super(HealthmonitorBuilder.class, db);
44     }
45
46     @Override
47     protected List<Healthmonitor> getDataObjectList(Healthmonitors healthMonitors) {
48         return healthMonitors.getHealthmonitor();
49     }
50
51     @Override
52     protected Healthmonitor toMd(NeutronLoadBalancerHealthMonitor healthMonitor) {
53         final HealthmonitorBuilder healthmonitorBuilder = new HealthmonitorBuilder();
54         healthmonitorBuilder.setAdminStateUp(healthMonitor.getLoadBalancerHealthMonitorAdminStateIsUp());
55         if (healthMonitor.getLoadBalancerHealthMonitorDelay() != null) {
56             healthmonitorBuilder.setDelay(Long.valueOf(healthMonitor.getLoadBalancerHealthMonitorDelay()));
57         }
58         if (healthMonitor.getLoadBalancerHealthMonitorExpectedCodes() != null) {
59             healthmonitorBuilder.setExpectedCodes(healthMonitor.getLoadBalancerHealthMonitorExpectedCodes());
60         }
61         if (healthMonitor.getLoadBalancerHealthMonitorHttpMethod() != null) {
62             healthmonitorBuilder.setHttpMethod(healthMonitor.getLoadBalancerHealthMonitorHttpMethod());
63         }
64         if (healthMonitor.getLoadBalancerHealthMonitorMaxRetries() != null) {
65             healthmonitorBuilder.setMaxRetries(Integer.valueOf(healthMonitor.getLoadBalancerHealthMonitorMaxRetries()));
66         }
67         if (healthMonitor.getLoadBalancerHealthMonitorPools() != null) {
68             final List<Uuid> listUuid = new ArrayList<Uuid>();
69             for (final NeutronID neutronId : healthMonitor.getLoadBalancerHealthMonitorPools()) {
70                 listUuid.add(toUuid(neutronId.getID()));
71             }
72             healthmonitorBuilder.setPools(listUuid);
73         }
74         if (healthMonitor.getTenantID() != null) {
75             healthmonitorBuilder.setTenantId(toUuid(healthMonitor.getTenantID()));
76         }
77         if (healthMonitor.getLoadBalancerHealthMonitorTimeout() != null) {
78             healthmonitorBuilder.setTimeout(Long.valueOf(healthMonitor.getLoadBalancerHealthMonitorTimeout()));
79         }
80         if (healthMonitor.getLoadBalancerHealthMonitorType() != null) {
81             final ImmutableBiMap<String, Class<? extends ProbeBase>> mapper = PROBE_MAP.inverse();
82             healthmonitorBuilder
83                     .setType((Class<? extends ProbeBase>) mapper.get(healthMonitor.getLoadBalancerHealthMonitorType()));
84         }
85         if (healthMonitor.getLoadBalancerHealthMonitorUrlPath() != null) {
86             healthmonitorBuilder.setUrlPath(healthMonitor.getLoadBalancerHealthMonitorUrlPath());
87         }
88         if (healthMonitor.getID() != null) {
89             healthmonitorBuilder.setUuid(toUuid(healthMonitor.getID()));
90         } else {
91             LOGGER.warn("Attempting to write neutron laod balancer health monitor without UUID");
92         }
93         return healthmonitorBuilder.build();
94     }
95
96     protected NeutronLoadBalancerHealthMonitor fromMd(Healthmonitor healthMonitor) {
97         final NeutronLoadBalancerHealthMonitor answer = new NeutronLoadBalancerHealthMonitor();
98         if (healthMonitor.isAdminStateUp() != null) {
99             answer.setLoadBalancerHealthMonitorAdminStateIsUp(healthMonitor.isAdminStateUp());
100         }
101         if (healthMonitor.getDelay() != null) {
102             answer.setLoadBalancerHealthMonitorDelay(healthMonitor.getDelay().intValue());
103         }
104         if (healthMonitor.getExpectedCodes() != null) {
105             answer.setLoadBalancerHealthMonitorExpectedCodes(healthMonitor.getExpectedCodes());
106         }
107         if (healthMonitor.getHttpMethod() != null) {
108             answer.setLoadBalancerHealthMonitorHttpMethod(healthMonitor.getHttpMethod());
109         }
110         if (healthMonitor.getMaxRetries() != null) {
111             answer.setLoadBalancerHealthMonitorMaxRetries(Integer.valueOf(healthMonitor.getMaxRetries()));
112         }
113         if (healthMonitor.getPools() != null) {
114             final List<NeutronID> list = new ArrayList<NeutronID>();
115             for (final Uuid id : healthMonitor.getPools()) {
116                 list.add(new NeutronID(id.getValue()));
117             }
118             answer.setLoadBalancerHealthMonitorPools(list);
119         }
120         if (healthMonitor.getTenantId() != null) {
121             answer.setTenantID(healthMonitor.getTenantId());
122         }
123         if (healthMonitor.getTimeout() != null) {
124             answer.setLoadBalancerHealthMonitorTimeout(healthMonitor.getTimeout().intValue());
125         }
126         if (healthMonitor.getType() != null) {
127             answer.setLoadBalancerHealthMonitorType(PROBE_MAP.get(healthMonitor.getType()));
128         }
129         if (healthMonitor.getUrlPath() != null) {
130             answer.setLoadBalancerHealthMonitorUrlPath(healthMonitor.getUrlPath());
131         }
132         if (healthMonitor.getUuid() != null) {
133             answer.setID(healthMonitor.getUuid().getValue());
134         } else {
135             LOGGER.warn("Attempting to write neutron laod balancer health monitor without UUID");
136         }
137         return answer;
138     }
139 }