2 * Copyright IBM Corporation, 2015. 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.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map.Entry;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
20 import org.opendaylight.neutron.spi.INeutronMeteringLabelRuleCRUD;
21 import org.opendaylight.neutron.spi.NeutronMeteringLabelRule;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.metering.rev141002.MeteringRuleAttrs;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.metering.rev141002.metering.rules.attributes.MeteringRules;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.metering.rev141002.metering.rules.attributes.metering.rules.MeteringRule;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.metering.rev141002.metering.rules.attributes.metering.rules.MeteringRuleBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150325.Neutron;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.ServiceRegistration;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class NeutronMeteringLabelRuleInterface extends AbstractNeutronInterface<MeteringRule, NeutronMeteringLabelRule>
35 implements INeutronMeteringLabelRuleCRUD {
36 private static final Logger LOGGER = LoggerFactory.getLogger(NeutronMeteringLabelRuleInterface.class);
37 private ConcurrentMap<String, NeutronMeteringLabelRule> meteringLabelRuleDB = new ConcurrentHashMap<String, NeutronMeteringLabelRule>();
39 NeutronMeteringLabelRuleInterface(ProviderContext providerContext) {
40 super(providerContext);
43 // IfNBMeteringLabelRuleCRUD methods
46 public boolean neutronMeteringLabelRuleExists(String uuid) {
47 return meteringLabelRuleDB.containsKey(uuid);
51 public NeutronMeteringLabelRule getNeutronMeteringLabelRule(String uuid) {
52 if (!neutronMeteringLabelRuleExists(uuid)) {
55 return meteringLabelRuleDB.get(uuid);
59 public List<NeutronMeteringLabelRule> getAllNeutronMeteringLabelRules() {
60 Set<NeutronMeteringLabelRule> allMeteringLabelRules = new HashSet<NeutronMeteringLabelRule>();
61 for (Entry<String, NeutronMeteringLabelRule> entry : meteringLabelRuleDB.entrySet()) {
62 NeutronMeteringLabelRule meteringLabelRule = entry.getValue();
63 allMeteringLabelRules.add(meteringLabelRule);
65 LOGGER.debug("Exiting getAllMeteringLabelRules, Found {} OpenStackMeteringLabelRules",
66 allMeteringLabelRules.size());
67 List<NeutronMeteringLabelRule> ans = new ArrayList<NeutronMeteringLabelRule>();
68 ans.addAll(allMeteringLabelRules);
73 public boolean addNeutronMeteringLabelRule(NeutronMeteringLabelRule input) {
74 if (neutronMeteringLabelRuleExists(input.getMeteringLabelRuleUUID())) {
77 meteringLabelRuleDB.putIfAbsent(input.getMeteringLabelRuleUUID(), input);
78 // TODO: add code to find INeutronMeteringLabelRuleAware services and
79 // call newtorkCreated on them
84 public boolean removeNeutronMeteringLabelRule(String uuid) {
85 if (!neutronMeteringLabelRuleExists(uuid)) {
88 meteringLabelRuleDB.remove(uuid);
89 // TODO: add code to find INeutronMeteringLabelRuleAware services and
90 // call newtorkDeleted on them
95 public boolean updateNeutronMeteringLabelRule(String uuid, NeutronMeteringLabelRule delta) {
96 if (!neutronMeteringLabelRuleExists(uuid)) {
99 NeutronMeteringLabelRule target = meteringLabelRuleDB.get(uuid);
100 return overwrite(target, delta);
104 public boolean neutronMeteringLabelRuleInUse(String netUUID) {
105 if (!neutronMeteringLabelRuleExists(netUUID)) {
112 protected InstanceIdentifier<MeteringRule> createInstanceIdentifier(MeteringRule item) {
113 return InstanceIdentifier.create(Neutron.class).child(MeteringRules.class).child(MeteringRule.class);
118 protected MeteringRule toMd(NeutronMeteringLabelRule meteringLableRule) {
119 MeteringRuleBuilder meteringRuleBuilder = new MeteringRuleBuilder();
120 if (meteringLableRule.getMeteringLabelRuleLabelID() != null) {
121 meteringRuleBuilder.setId((toUuid(meteringLableRule.getMeteringLabelRuleLabelID())));
123 if (meteringLableRule.getMeteringLabelRuleUUID() != null) {
124 meteringRuleBuilder.setMeteringLabelId(toUuid(meteringLableRule.getMeteringLabelRuleUUID()));
126 if (meteringLableRule.getMeteringLabelRuleDirection() != null) {
127 meteringRuleBuilder.setDirection((MeteringRuleAttrs.Direction.valueOf(meteringLableRule
128 .getMeteringLabelRuleDirection())));
130 if (meteringLableRule.getMeteringLabelRuleRemoteIPPrefix() != null) {
131 IpAddress ipAddress = new IpAddress(meteringLableRule.getMeteringLabelRuleRemoteIPPrefix().toCharArray());
132 meteringRuleBuilder.setRemoteIpPrefix(ipAddress);
134 meteringRuleBuilder.setExcluded(meteringLableRule.getMeteringLabelRuleExcluded());
135 return meteringRuleBuilder.build();
139 protected MeteringRule toMd(String uuid) {
140 MeteringRuleBuilder meteringRuleBuilder = new MeteringRuleBuilder();
141 meteringRuleBuilder.setId((toUuid(uuid)));
142 return meteringRuleBuilder.build();
145 public static void registerNewInterface(BundleContext context,
146 ProviderContext providerContext,
147 List<ServiceRegistration<?>> registrations) {
148 NeutronMeteringLabelRuleInterface neutronMeteringLabelRuleInterface = new NeutronMeteringLabelRuleInterface(providerContext);
149 ServiceRegistration<INeutronMeteringLabelRuleCRUD> neutronMeteringLabelRuleInterfaceRegistration = context.registerService(INeutronMeteringLabelRuleCRUD.class, neutronMeteringLabelRuleInterface, null);
150 if(neutronMeteringLabelRuleInterfaceRegistration != null) {
151 registrations.add(neutronMeteringLabelRuleInterfaceRegistration);