2 * Copyright (c) 2015 IBM Corporation and others. All rights reserved.
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
9 package org.opendaylight.neutron.transcriber;
11 import java.util.List;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
14 import org.opendaylight.neutron.spi.INeutronMeteringLabelRuleCRUD;
15 import org.opendaylight.neutron.spi.NeutronMeteringLabelRule;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionBase;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionEgress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionIngress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.metering.rev150712.metering.rules.attributes.MeteringRules;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.metering.rev150712.metering.rules.attributes.metering.rules.MeteringRule;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.metering.rev150712.metering.rules.attributes.metering.rules.MeteringRuleBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.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;
30 import com.google.common.collect.ImmutableBiMap;
32 public class NeutronMeteringLabelRuleInterface extends AbstractNeutronInterface<MeteringRule, MeteringRules, NeutronMeteringLabelRule>
33 implements INeutronMeteringLabelRuleCRUD {
34 private static final Logger LOGGER = LoggerFactory.getLogger(NeutronMeteringLabelRuleInterface.class);
36 private static final ImmutableBiMap<Class<? extends DirectionBase>,String> DIRECTION_MAP
37 = new ImmutableBiMap.Builder<Class<? extends DirectionBase>,String>()
38 .put(DirectionEgress.class,"egress")
39 .put(DirectionIngress.class,"ingress")
43 NeutronMeteringLabelRuleInterface(ProviderContext providerContext) {
44 super(providerContext);
47 // IfNBMeteringLabelRuleCRUD methods
50 public boolean neutronMeteringLabelRuleExists(String uuid) {
55 public NeutronMeteringLabelRule getNeutronMeteringLabelRule(String uuid) {
60 protected List<MeteringRule> getDataObjectList(MeteringRules rules) {
61 return rules.getMeteringRule();
65 public List<NeutronMeteringLabelRule> getAllNeutronMeteringLabelRules() {
70 public boolean addNeutronMeteringLabelRule(NeutronMeteringLabelRule input) {
75 public boolean removeNeutronMeteringLabelRule(String uuid) {
80 public boolean updateNeutronMeteringLabelRule(String uuid, NeutronMeteringLabelRule delta) {
81 return update(uuid, delta);
85 public boolean neutronMeteringLabelRuleInUse(String netUUID) {
86 return !exists(netUUID);
90 protected InstanceIdentifier<MeteringRule> createInstanceIdentifier(MeteringRule item) {
91 return InstanceIdentifier.create(Neutron.class)
92 .child(MeteringRules.class)
93 .child(MeteringRule.class, item.getKey());
97 protected InstanceIdentifier<MeteringRules> createInstanceIdentifier() {
98 return InstanceIdentifier.create(Neutron.class)
99 .child(MeteringRules.class);
103 protected MeteringRule toMd(NeutronMeteringLabelRule meteringLabelRule) {
104 final MeteringRuleBuilder meteringRuleBuilder = new MeteringRuleBuilder();
105 if (meteringLabelRule.getID() != null) {
106 meteringRuleBuilder.setUuid(toUuid(meteringLabelRule.getID()));
108 if (meteringLabelRule.getTenantID() != null) {
109 meteringRuleBuilder.setTenantId(toUuid(meteringLabelRule.getTenantID()));
111 if (meteringLabelRule.getMeteringLabelRuleLabelID() != null) {
112 meteringRuleBuilder.setMeteringLabelId(toUuid(meteringLabelRule.getMeteringLabelRuleLabelID()));
114 if (meteringLabelRule.getMeteringLabelRuleDirection() != null) {
115 final ImmutableBiMap<String, Class<? extends DirectionBase>> mapper =
116 DIRECTION_MAP.inverse();
117 meteringRuleBuilder.setDirection((Class<? extends DirectionBase>) mapper.get(meteringLabelRule.getMeteringLabelRuleDirection()));
119 if (meteringLabelRule.getMeteringLabelRuleRemoteIPPrefix() != null) {
120 final IpPrefix ipPrefix = new IpPrefix(meteringLabelRule.getMeteringLabelRuleRemoteIPPrefix().toCharArray());
121 meteringRuleBuilder.setRemoteIpPrefix(ipPrefix);
123 meteringRuleBuilder.setExcluded(meteringLabelRule.getMeteringLabelRuleExcluded());
124 return meteringRuleBuilder.build();
127 protected NeutronMeteringLabelRule fromMd(MeteringRule rule) {
128 final NeutronMeteringLabelRule answer = new NeutronMeteringLabelRule();
129 if (rule.getUuid() != null) {
130 answer.setID(rule.getUuid().getValue());
132 if (rule.getTenantId() != null) {
133 answer.setTenantID(rule.getTenantId());
135 if (rule.getMeteringLabelId() != null) {
136 answer.setMeteringLabelRuleLabelID(rule.getMeteringLabelId().getValue());
138 if (rule.getDirection() != null) {
139 answer.setMeteringLabelRuleDirection(
140 DIRECTION_MAP.get(rule.getDirection()));
142 if (rule.getRemoteIpPrefix() != null) {
143 answer.setMeteringLabelRuleRemoteIPPrefix(new String(rule.getRemoteIpPrefix().getValue()));
145 answer.setMeteringLabelRuleExcluded(rule.isExcluded());
150 protected MeteringRule toMd(String uuid) {
151 final MeteringRuleBuilder meteringRuleBuilder = new MeteringRuleBuilder();
152 meteringRuleBuilder.setUuid(toUuid(uuid));
153 return meteringRuleBuilder.build();
156 public static void registerNewInterface(BundleContext context,
157 ProviderContext providerContext,
158 List<ServiceRegistration<?>> registrations) {
159 final NeutronMeteringLabelRuleInterface neutronMeteringLabelRuleInterface = new NeutronMeteringLabelRuleInterface(providerContext);
160 final ServiceRegistration<INeutronMeteringLabelRuleCRUD> neutronMeteringLabelRuleInterfaceRegistration = context.registerService(INeutronMeteringLabelRuleCRUD.class, neutronMeteringLabelRuleInterface, null);
161 if (neutronMeteringLabelRuleInterfaceRegistration != null) {
162 registrations.add(neutronMeteringLabelRuleInterfaceRegistration);