spi: consolidate common member
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronLoadBalancerInterface.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 java.util.List;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.neutron.spi.INeutronLoadBalancerCRUD;
14 import org.opendaylight.neutron.spi.NeutronLoadBalancer;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev150712.lbaas.attributes.Loadbalancers;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev150712.lbaas.attributes.loadbalancers.Loadbalancer;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev150712.lbaas.attributes.loadbalancers.LoadbalancerBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public final class NeutronLoadBalancerInterface extends
25         AbstractNeutronInterface<Loadbalancer, Loadbalancers, NeutronLoadBalancer> implements INeutronLoadBalancerCRUD {
26     private static final Logger LOGGER = LoggerFactory.getLogger(NeutronLoadBalancerInterface.class);
27
28     NeutronLoadBalancerInterface(DataBroker db) {
29         super(db);
30     }
31
32     @Override
33     protected List<Loadbalancer> getDataObjectList(Loadbalancers lbs) {
34         return lbs.getLoadbalancer();
35     }
36
37     @Override
38     protected InstanceIdentifier<Loadbalancer> createInstanceIdentifier(Loadbalancer loadBalancer) {
39         return InstanceIdentifier.create(Neutron.class).child(Loadbalancers.class).child(Loadbalancer.class,
40                 loadBalancer.getKey());
41     }
42
43     @Override
44     protected InstanceIdentifier<Loadbalancers> createInstanceIdentifier() {
45         return InstanceIdentifier.create(Neutron.class).child(Loadbalancers.class);
46     }
47
48     protected NeutronLoadBalancer fromMd(Loadbalancer loadBalancer) {
49         final NeutronLoadBalancer answer = new NeutronLoadBalancer();
50         if (loadBalancer.isAdminStateUp() != null) {
51             answer.setAdminStateUp(loadBalancer.isAdminStateUp());
52         }
53         if (loadBalancer.getName() != null) {
54             answer.setName(loadBalancer.getName());
55         }
56         if (loadBalancer.getStatus() != null) {
57             answer.setStatus(loadBalancer.getStatus());
58         }
59         if (loadBalancer.getTenantId() != null) {
60             answer.setTenantID(loadBalancer.getTenantId());
61         }
62         if (loadBalancer.getVipAddress() != null) {
63             answer.setLoadBalancerVipAddress(String.valueOf(loadBalancer.getVipAddress().getValue()));
64         }
65         if (loadBalancer.getVipSubnetId() != null) {
66             answer.setLoadBalancerVipSubnetID(loadBalancer.getVipSubnetId().getValue());
67         }
68         if (loadBalancer.getUuid() != null) {
69             answer.setID(loadBalancer.getUuid().getValue());
70         }
71         return answer;
72     }
73
74     @Override
75     protected Loadbalancer toMd(NeutronLoadBalancer loadBalancer) {
76         final LoadbalancerBuilder loadBalancerBuilder = new LoadbalancerBuilder();
77         loadBalancerBuilder.setAdminStateUp(loadBalancer.getAdminStateUp());
78         if (loadBalancer.getName() != null) {
79             loadBalancerBuilder.setName(loadBalancer.getName());
80         }
81         if (loadBalancer.getStatus() != null) {
82             loadBalancerBuilder.setStatus(loadBalancer.getStatus());
83         }
84         if (loadBalancer.getTenantID() != null) {
85             loadBalancerBuilder.setTenantId(toUuid(loadBalancer.getTenantID()));
86         }
87         if (loadBalancer.getLoadBalancerVipAddress() != null) {
88             loadBalancerBuilder.setVipAddress(new IpAddress(loadBalancer.getLoadBalancerVipAddress().toCharArray()));
89         }
90         if (loadBalancer.getLoadBalancerVipSubnetID() != null) {
91             loadBalancerBuilder.setVipSubnetId(toUuid(loadBalancer.getLoadBalancerVipSubnetID()));
92         }
93         if (loadBalancer.getID() != null) {
94             loadBalancerBuilder.setUuid(toUuid(loadBalancer.getID()));
95         } else {
96             LOGGER.warn("Attempting to write neutron load balancer without UUID");
97         }
98         return loadBalancerBuilder.build();
99     }
100
101     @Override
102     protected Loadbalancer toMd(String uuid) {
103         final LoadbalancerBuilder loadBalancerBuilder = new LoadbalancerBuilder();
104         loadBalancerBuilder.setUuid(toUuid(uuid));
105         return loadBalancerBuilder.build();
106     }
107 }