Checkstyle imisc issues fix(Transcriber)
[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.NeutronLoadBalancerHealthMonitor;
17 import org.opendaylight.neutron.spi.Neutron_ID;
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.rev150712.Neutron;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class NeutronLoadBalancerHealthMonitorInterface
33         extends AbstractNeutronInterface<Healthmonitor, Healthmonitors, 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(db);
44     }
45
46     @Override
47     protected List<Healthmonitor> getDataObjectList(Healthmonitors healthMonitors) {
48         return healthMonitors.getHealthmonitor();
49     }
50
51     @Override
52     protected InstanceIdentifier<Healthmonitor> createInstanceIdentifier(Healthmonitor healthMonitor) {
53         return InstanceIdentifier.create(Neutron.class).child(Healthmonitors.class).child(Healthmonitor.class,
54                 healthMonitor.getKey());
55     }
56
57     @Override
58     protected InstanceIdentifier<Healthmonitors> createInstanceIdentifier() {
59         return InstanceIdentifier.create(Neutron.class).child(Healthmonitors.class);
60     }
61
62     @Override
63     protected Healthmonitor toMd(NeutronLoadBalancerHealthMonitor healthMonitor) {
64         final HealthmonitorBuilder healthmonitorBuilder = new HealthmonitorBuilder();
65         healthmonitorBuilder.setAdminStateUp(healthMonitor.getLoadBalancerHealthMonitorAdminStateIsUp());
66         if (healthMonitor.getLoadBalancerHealthMonitorDelay() != null) {
67             healthmonitorBuilder.setDelay(Long.valueOf(healthMonitor.getLoadBalancerHealthMonitorDelay()));
68         }
69         if (healthMonitor.getLoadBalancerHealthMonitorExpectedCodes() != null) {
70             healthmonitorBuilder.setExpectedCodes(healthMonitor.getLoadBalancerHealthMonitorExpectedCodes());
71         }
72         if (healthMonitor.getLoadBalancerHealthMonitorHttpMethod() != null) {
73             healthmonitorBuilder.setHttpMethod(healthMonitor.getLoadBalancerHealthMonitorHttpMethod());
74         }
75         if (healthMonitor.getLoadBalancerHealthMonitorMaxRetries() != null) {
76             healthmonitorBuilder.setMaxRetries(Integer.valueOf(healthMonitor.getLoadBalancerHealthMonitorMaxRetries()));
77         }
78         if (healthMonitor.getLoadBalancerHealthMonitorPools() != null) {
79             final List<Uuid> listUuid = new ArrayList<Uuid>();
80             for (final Neutron_ID neutron_id : healthMonitor.getLoadBalancerHealthMonitorPools()) {
81                 listUuid.add(toUuid(neutron_id.getID()));
82             }
83             healthmonitorBuilder.setPools(listUuid);
84         }
85         if (healthMonitor.getTenantID() != null) {
86             healthmonitorBuilder.setTenantId(toUuid(healthMonitor.getTenantID()));
87         }
88         if (healthMonitor.getLoadBalancerHealthMonitorTimeout() != null) {
89             healthmonitorBuilder.setTimeout(Long.valueOf(healthMonitor.getLoadBalancerHealthMonitorTimeout()));
90         }
91         if (healthMonitor.getLoadBalancerHealthMonitorType() != null) {
92             final ImmutableBiMap<String, Class<? extends ProbeBase>> mapper = PROBE_MAP.inverse();
93             healthmonitorBuilder
94                     .setType((Class<? extends ProbeBase>) mapper.get(healthMonitor.getLoadBalancerHealthMonitorType()));
95         }
96         if (healthMonitor.getLoadBalancerHealthMonitorUrlPath() != null) {
97             healthmonitorBuilder.setUrlPath(healthMonitor.getLoadBalancerHealthMonitorUrlPath());
98         }
99         if (healthMonitor.getID() != null) {
100             healthmonitorBuilder.setUuid(toUuid(healthMonitor.getID()));
101         } else {
102             LOGGER.warn("Attempting to write neutron laod balancer health monitor without UUID");
103         }
104         return healthmonitorBuilder.build();
105     }
106
107     @Override
108     protected Healthmonitor toMd(String uuid) {
109         final HealthmonitorBuilder healthmonitorBuilder = new HealthmonitorBuilder();
110         healthmonitorBuilder.setUuid(toUuid(uuid));
111         return healthmonitorBuilder.build();
112     }
113
114     protected NeutronLoadBalancerHealthMonitor fromMd(Healthmonitor healthMonitor) {
115         final NeutronLoadBalancerHealthMonitor answer = new NeutronLoadBalancerHealthMonitor();
116         if (healthMonitor.isAdminStateUp() != null) {
117             answer.setLoadBalancerHealthMonitorAdminStateIsUp(healthMonitor.isAdminStateUp());
118         }
119         if (healthMonitor.getDelay() != null) {
120             answer.setLoadBalancerHealthMonitorDelay(healthMonitor.getDelay().intValue());
121         }
122         if (healthMonitor.getExpectedCodes() != null) {
123             answer.setLoadBalancerHealthMonitorExpectedCodes(healthMonitor.getExpectedCodes());
124         }
125         if (healthMonitor.getHttpMethod() != null) {
126             answer.setLoadBalancerHealthMonitorHttpMethod(healthMonitor.getHttpMethod());
127         }
128         if (healthMonitor.getMaxRetries() != null) {
129             answer.setLoadBalancerHealthMonitorMaxRetries(Integer.valueOf(healthMonitor.getMaxRetries()));
130         }
131         if (healthMonitor.getPools() != null) {
132             final List<Neutron_ID> list = new ArrayList<Neutron_ID>();
133             for (final Uuid id : healthMonitor.getPools()) {
134                 list.add(new Neutron_ID(id.getValue()));
135             }
136             answer.setLoadBalancerHealthMonitorPools(list);
137         }
138         if (healthMonitor.getTenantId() != null) {
139             answer.setTenantID(healthMonitor.getTenantId());
140         }
141         if (healthMonitor.getTimeout() != null) {
142             answer.setLoadBalancerHealthMonitorTimeout(healthMonitor.getTimeout().intValue());
143         }
144         if (healthMonitor.getType() != null) {
145             answer.setLoadBalancerHealthMonitorType(PROBE_MAP.get(healthMonitor.getType()));
146         }
147         if (healthMonitor.getUrlPath() != null) {
148             answer.setLoadBalancerHealthMonitorUrlPath(healthMonitor.getUrlPath());
149         }
150         if (healthMonitor.getUuid() != null) {
151             answer.setID(healthMonitor.getUuid().getValue());
152         } else {
153             LOGGER.warn("Attempting to write neutron laod balancer health monitor without UUID");
154         }
155         return answer;
156     }
157 }