Merge "transcriber: consolidate crud logic"
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronMeteringLabelInterface.java
1 /*
2  * Copyright (c) 2015 IBM Corporation 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.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map.Entry;
15 import java.util.Set;
16
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
18 import org.opendaylight.neutron.spi.INeutronMeteringLabelCRUD;
19 import org.opendaylight.neutron.spi.NeutronMeteringLabel;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.metering.rev141002.metering.labels.attributes.MeteringLabels;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.metering.rev141002.metering.labels.attributes.metering.labels.MeteringLabel;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.metering.rev141002.metering.labels.attributes.metering.labels.MeteringLabelBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150325.Neutron;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.osgi.framework.BundleContext;
26 import org.osgi.framework.ServiceRegistration;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class NeutronMeteringLabelInterface extends AbstractNeutronInterface<MeteringLabel, NeutronMeteringLabel>  implements INeutronMeteringLabelCRUD {
31     private static final Logger LOGGER = LoggerFactory.getLogger(NeutronMeteringLabelInterface.class);
32
33     NeutronMeteringLabelInterface(ProviderContext providerContext) {
34         super(providerContext);
35     }
36
37     // IfNBMeteringLabelCRUD methods
38
39     @Override
40     public boolean neutronMeteringLabelExists(String uuid) {
41         return exists(uuid);
42     }
43
44     @Override
45     public NeutronMeteringLabel getNeutronMeteringLabel(String uuid) {
46         return get(uuid);
47     }
48
49     @Override
50     public List<NeutronMeteringLabel> getAll() {
51         Set<NeutronMeteringLabel> allMeteringLabels = new HashSet<NeutronMeteringLabel>();
52         MeteringLabels labels = readMd(createInstanceIdentifier());
53         if (labels != null) {
54             for (MeteringLabel label: labels.getMeteringLabel()) {
55                 allMeteringLabels.add(fromMd(label));
56             }
57         }
58         LOGGER.debug("Exiting getAllMeteringLabels, Found {} OpenStackMeteringLabels", allMeteringLabels.size());
59         List<NeutronMeteringLabel> ans = new ArrayList<NeutronMeteringLabel>();
60         ans.addAll(allMeteringLabels);
61         return ans;
62     }
63
64     @Override
65     public List<NeutronMeteringLabel> getAllNeutronMeteringLabels() {
66         return getAll();
67     }
68
69     @Override
70     public boolean addNeutronMeteringLabel(NeutronMeteringLabel input) {
71         return add(input);
72     }
73
74     @Override
75     public boolean removeNeutronMeteringLabel(String uuid) {
76         return remove(uuid);
77     }
78
79     @Override
80     public boolean updateNeutronMeteringLabel(String uuid, NeutronMeteringLabel delta) {
81         return update(uuid, delta);
82     }
83
84     @Override
85     public boolean neutronMeteringLabelInUse(String netUUID) {
86         return !exists(netUUID);
87     }
88
89     @Override
90     protected InstanceIdentifier<MeteringLabel> createInstanceIdentifier(
91             MeteringLabel item) {
92         return InstanceIdentifier.create(Neutron.class)
93             .child(MeteringLabels.class)
94             .child(MeteringLabel.class,item.getKey());
95     }
96
97     protected InstanceIdentifier<MeteringLabels> createInstanceIdentifier() {
98         return InstanceIdentifier.create(Neutron.class)
99             .child(MeteringLabels.class);
100     }
101
102     @Override
103     protected MeteringLabel toMd(NeutronMeteringLabel meteringLabel) {
104         MeteringLabelBuilder meteringLabelBuilder = new MeteringLabelBuilder();
105         if (meteringLabel.getMeteringLabelName()!=null) {
106             meteringLabelBuilder.setName(meteringLabel.getMeteringLabelName());
107         }
108         if (meteringLabel.getMeteringLabelDescription()!=null) {
109             meteringLabelBuilder.setDescription(meteringLabel.getMeteringLabelDescription());
110         }
111         if (meteringLabel.getMeteringLabelTenantID()!=null) {
112             meteringLabelBuilder.setTenantId(toUuid(meteringLabel.getMeteringLabelTenantID()));
113         }
114         if (meteringLabel.getID()!=null) {
115             meteringLabelBuilder.setUuid(toUuid(meteringLabel.getID()));
116         }
117         return meteringLabelBuilder.build();
118     }
119
120     protected NeutronMeteringLabel fromMd(MeteringLabel label) {
121         NeutronMeteringLabel answer = new NeutronMeteringLabel();
122         if (label.getName() != null) {
123             answer.setMeteringLabelName(label.getName());
124         }
125         if (label.getDescription() != null) {
126             answer.setMeteringLabelDescription(label.getName());
127         }
128 //todo: remove '-' chars as tenant id doesn't use them
129         if (label.getTenantId() != null) {
130             answer.setMeteringLabelTenantID(label.getTenantId().getValue());
131         }
132         if (label.getUuid() != null) {
133             answer.setID(label.getUuid().getValue());
134         }
135         return answer;
136     }
137
138     @Override
139     protected MeteringLabel toMd(String uuid) {
140         MeteringLabelBuilder meteringLabelBuilder = new MeteringLabelBuilder();
141         meteringLabelBuilder.setUuid(toUuid(uuid));
142         return meteringLabelBuilder.build();
143     }
144
145     public static void registerNewInterface(BundleContext context,
146                                             ProviderContext providerContext,
147                                             List<ServiceRegistration<?>> registrations) {
148         NeutronMeteringLabelInterface neutronMeteringLabelInterface = new NeutronMeteringLabelInterface(providerContext);
149         ServiceRegistration<INeutronMeteringLabelCRUD> neutronMeteringLabelInterfaceRegistration = context.registerService(INeutronMeteringLabelCRUD.class, neutronMeteringLabelInterface, null);
150         if(neutronMeteringLabelInterfaceRegistration != null) {
151             registrations.add(neutronMeteringLabelInterfaceRegistration);
152         }
153     }
154 }