959e2cce4e8462453d71ecae4d3027fc7494ac7c
[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 LOG = 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         toMdIds(healthMonitor, healthmonitorBuilder);
55         healthmonitorBuilder.setAdminStateUp(healthMonitor.getLoadBalancerHealthMonitorAdminStateIsUp());
56         if (healthMonitor.getLoadBalancerHealthMonitorDelay() != null) {
57             healthmonitorBuilder.setDelay(Long.valueOf(healthMonitor.getLoadBalancerHealthMonitorDelay()));
58         }
59         if (healthMonitor.getLoadBalancerHealthMonitorExpectedCodes() != null) {
60             healthmonitorBuilder.setExpectedCodes(healthMonitor.getLoadBalancerHealthMonitorExpectedCodes());
61         }
62         if (healthMonitor.getLoadBalancerHealthMonitorHttpMethod() != null) {
63             healthmonitorBuilder.setHttpMethod(healthMonitor.getLoadBalancerHealthMonitorHttpMethod());
64         }
65         if (healthMonitor.getLoadBalancerHealthMonitorMaxRetries() != null) {
66             healthmonitorBuilder.setMaxRetries(Integer.valueOf(healthMonitor.getLoadBalancerHealthMonitorMaxRetries()));
67         }
68         if (healthMonitor.getLoadBalancerHealthMonitorPools() != null) {
69             final List<Uuid> listUuid = new ArrayList<>();
70             for (final NeutronID neutronId : healthMonitor.getLoadBalancerHealthMonitorPools()) {
71                 listUuid.add(toUuid(neutronId.getID()));
72             }
73             healthmonitorBuilder.setPools(listUuid);
74         }
75         if (healthMonitor.getLoadBalancerHealthMonitorTimeout() != null) {
76             healthmonitorBuilder.setTimeout(Long.valueOf(healthMonitor.getLoadBalancerHealthMonitorTimeout()));
77         }
78         if (healthMonitor.getLoadBalancerHealthMonitorType() != null) {
79             final ImmutableBiMap<String, Class<? extends ProbeBase>> mapper = PROBE_MAP.inverse();
80             healthmonitorBuilder
81                     .setType(mapper.get(healthMonitor.getLoadBalancerHealthMonitorType()));
82         }
83         if (healthMonitor.getLoadBalancerHealthMonitorUrlPath() != null) {
84             healthmonitorBuilder.setUrlPath(healthMonitor.getLoadBalancerHealthMonitorUrlPath());
85         }
86         return healthmonitorBuilder.build();
87     }
88
89     @Override
90     protected NeutronLoadBalancerHealthMonitor fromMd(Healthmonitor healthMonitor) {
91         final NeutronLoadBalancerHealthMonitor answer = new NeutronLoadBalancerHealthMonitor();
92         fromMdIds(healthMonitor, answer);
93         if (healthMonitor.isAdminStateUp() != null) {
94             answer.setLoadBalancerHealthMonitorAdminStateIsUp(healthMonitor.isAdminStateUp());
95         }
96         if (healthMonitor.getDelay() != null) {
97             answer.setLoadBalancerHealthMonitorDelay(healthMonitor.getDelay().intValue());
98         }
99         if (healthMonitor.getExpectedCodes() != null) {
100             answer.setLoadBalancerHealthMonitorExpectedCodes(healthMonitor.getExpectedCodes());
101         }
102         if (healthMonitor.getHttpMethod() != null) {
103             answer.setLoadBalancerHealthMonitorHttpMethod(healthMonitor.getHttpMethod());
104         }
105         if (healthMonitor.getMaxRetries() != null) {
106             answer.setLoadBalancerHealthMonitorMaxRetries(Integer.valueOf(healthMonitor.getMaxRetries()));
107         }
108         if (healthMonitor.getPools() != null) {
109             final List<NeutronID> list = new ArrayList<>();
110             for (final Uuid id : healthMonitor.getPools()) {
111                 list.add(new NeutronID(id.getValue()));
112             }
113             answer.setLoadBalancerHealthMonitorPools(list);
114         }
115         if (healthMonitor.getTimeout() != null) {
116             answer.setLoadBalancerHealthMonitorTimeout(healthMonitor.getTimeout().intValue());
117         }
118         if (healthMonitor.getType() != null) {
119             answer.setLoadBalancerHealthMonitorType(PROBE_MAP.get(healthMonitor.getType()));
120         }
121         if (healthMonitor.getUrlPath() != null) {
122             answer.setLoadBalancerHealthMonitorUrlPath(healthMonitor.getUrlPath());
123         }
124         return answer;
125     }
126 }