/* * Copyright (c) 2015 IBM Corporation and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.neutron.transcriber; import java.util.List; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext; import org.opendaylight.neutron.spi.INeutronMeteringLabelRuleCRUD; import org.opendaylight.neutron.spi.NeutronMeteringLabelRule; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionBase; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionEgress; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionIngress; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.metering.rev150712.metering.rules.attributes.MeteringRules; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.metering.rev150712.metering.rules.attributes.metering.rules.MeteringRule; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.metering.rev150712.metering.rules.attributes.metering.rules.MeteringRuleBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.collect.ImmutableBiMap; public class NeutronMeteringLabelRuleInterface extends AbstractNeutronInterface implements INeutronMeteringLabelRuleCRUD { private static final Logger LOGGER = LoggerFactory.getLogger(NeutronMeteringLabelRuleInterface.class); private static final ImmutableBiMap,String> DIRECTION_MAP = new ImmutableBiMap.Builder,String>() .put(DirectionEgress.class,"egress") .put(DirectionIngress.class,"ingress") .build(); NeutronMeteringLabelRuleInterface(ProviderContext providerContext) { super(providerContext); } // IfNBMeteringLabelRuleCRUD methods @Override protected List getDataObjectList(MeteringRules rules) { return rules.getMeteringRule(); } @Override protected InstanceIdentifier createInstanceIdentifier(MeteringRule item) { return InstanceIdentifier.create(Neutron.class) .child(MeteringRules.class) .child(MeteringRule.class, item.getKey()); } @Override protected InstanceIdentifier createInstanceIdentifier() { return InstanceIdentifier.create(Neutron.class) .child(MeteringRules.class); } @Override protected MeteringRule toMd(NeutronMeteringLabelRule meteringLabelRule) { final MeteringRuleBuilder meteringRuleBuilder = new MeteringRuleBuilder(); if (meteringLabelRule.getID() != null) { meteringRuleBuilder.setUuid(toUuid(meteringLabelRule.getID())); } if (meteringLabelRule.getTenantID() != null) { meteringRuleBuilder.setTenantId(toUuid(meteringLabelRule.getTenantID())); } if (meteringLabelRule.getMeteringLabelRuleLabelID() != null) { meteringRuleBuilder.setMeteringLabelId(toUuid(meteringLabelRule.getMeteringLabelRuleLabelID())); } if (meteringLabelRule.getMeteringLabelRuleDirection() != null) { final ImmutableBiMap> mapper = DIRECTION_MAP.inverse(); meteringRuleBuilder.setDirection((Class) mapper.get(meteringLabelRule.getMeteringLabelRuleDirection())); } if (meteringLabelRule.getMeteringLabelRuleRemoteIPPrefix() != null) { final IpPrefix ipPrefix = new IpPrefix(meteringLabelRule.getMeteringLabelRuleRemoteIPPrefix().toCharArray()); meteringRuleBuilder.setRemoteIpPrefix(ipPrefix); } meteringRuleBuilder.setExcluded(meteringLabelRule.getMeteringLabelRuleExcluded()); return meteringRuleBuilder.build(); } protected NeutronMeteringLabelRule fromMd(MeteringRule rule) { final NeutronMeteringLabelRule answer = new NeutronMeteringLabelRule(); if (rule.getUuid() != null) { answer.setID(rule.getUuid().getValue()); } if (rule.getTenantId() != null) { answer.setTenantID(rule.getTenantId()); } if (rule.getMeteringLabelId() != null) { answer.setMeteringLabelRuleLabelID(rule.getMeteringLabelId().getValue()); } if (rule.getDirection() != null) { answer.setMeteringLabelRuleDirection( DIRECTION_MAP.get(rule.getDirection())); } if (rule.getRemoteIpPrefix() != null) { answer.setMeteringLabelRuleRemoteIPPrefix(new String(rule.getRemoteIpPrefix().getValue())); } answer.setMeteringLabelRuleExcluded(rule.isExcluded()); return answer; } @Override protected MeteringRule toMd(String uuid) { final MeteringRuleBuilder meteringRuleBuilder = new MeteringRuleBuilder(); meteringRuleBuilder.setUuid(toUuid(uuid)); return meteringRuleBuilder.build(); } public static void registerNewInterface(BundleContext context, ProviderContext providerContext, List> registrations) { final NeutronMeteringLabelRuleInterface neutronMeteringLabelRuleInterface = new NeutronMeteringLabelRuleInterface(providerContext); final ServiceRegistration neutronMeteringLabelRuleInterfaceRegistration = context.registerService(INeutronMeteringLabelRuleCRUD.class, neutronMeteringLabelRuleInterface, null); if (neutronMeteringLabelRuleInterfaceRegistration != null) { registrations.add(neutronMeteringLabelRuleInterfaceRegistration); } } }