From 0448a09f74513107f895c237878c577a62c66978 Mon Sep 17 00:00:00 2001 From: "Keith Burns (alagalah)" Date: Sun, 24 May 2015 07:29:20 -0700 Subject: [PATCH] Centralising DataStore and IidFactory. - Needed this for L3PrefixEp. - Can move other sub-project general helpers here - Potentially can be structured as -- General (all GBP) -- Feature specific packages Change-Id: I627b7ec6dcf82eda8ecb9b251e76f4ea77582ac3 Signed-off-by: Keith Burns (alagalah) --- .../util/DataStoreHelper.java | 76 +++++++ .../groupbasedpolicy/util/IidFactory.java | 213 ++++++++++++++++++ 2 files changed, 289 insertions(+) create mode 100644 groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/util/DataStoreHelper.java create mode 100644 groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/util/IidFactory.java diff --git a/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/util/DataStoreHelper.java b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/util/DataStoreHelper.java new file mode 100644 index 000000000..cb47a9a92 --- /dev/null +++ b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/util/DataStoreHelper.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. 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.groupbasedpolicy.util; + +import org.opendaylight.controller.md.sal.binding.api.ReadTransaction; +import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction; +import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Optional; +import com.google.common.util.concurrent.CheckedFuture; + +/** + * @author Martin Sunal + */ +public class DataStoreHelper { + + private static final Logger LOG = LoggerFactory.getLogger(DataStoreHelper.class); + + /** + * Reads data from datastore as synchrone call. + * @return {@link Optional#isPresent()} is {@code true} if reading was successful and data exists in datastore; {@link Optional#isPresent()} is {@code false} otherwise + */ + public static Optional readFromDs(LogicalDatastoreType store, InstanceIdentifier path, ReadTransaction rTx) { + CheckedFuture, ReadFailedException> resultFuture = rTx.read(store, path); + try { + return resultFuture.checkedGet(); + } catch (ReadFailedException e) { + LOG.warn("Read failed from DS.", e); + return Optional.absent(); + } + } + + /** + * Calls {@link WriteTransaction#submit()} on write transaction. + * @param wTx write transaction + * @return {@code true} if transaction commit was successful; {@code false} otherwise + */ + public static boolean submitToDs(WriteTransaction wTx) { + CheckedFuture submitFuture = wTx.submit(); + try { + submitFuture.checkedGet(); + return true; + } catch (TransactionCommitFailedException e) { + LOG.warn("Transaction commit failed to DS.", e); + return false; + } + } + + /** + * If an element on the path exists in datastore the element is removed and returned as a result. + * {@link Optional#isPresent()} is {@code false} in case that element on path does not exist. + * @return removed element in {@link Optional#get()}; otherwise {@link Optional#absent()} + */ + public static Optional removeIfExists(LogicalDatastoreType store, InstanceIdentifier path, + ReadWriteTransaction rwTx) { + Optional potentialResult = readFromDs(store, path, rwTx); + if (potentialResult.isPresent()) { + rwTx.delete(store, path); + } + return potentialResult; + } + +} diff --git a/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/util/IidFactory.java b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/util/IidFactory.java new file mode 100644 index 000000000..f98d191d3 --- /dev/null +++ b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/util/IidFactory.java @@ -0,0 +1,213 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. 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.groupbasedpolicy.util; + +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2FloodDomainId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RuleName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SelectorName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.Endpoints; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Key; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3PrefixKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Tenants; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRef; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRefKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Contract; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.ContractKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroup; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroupKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2BridgeDomain; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2BridgeDomainKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2FloodDomain; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2FloodDomainKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L3Context; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L3ContextKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.SubjectFeatureInstances; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Subnet; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.SubnetKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.Clause; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.ClauseKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.Subject; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.SubjectKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.Rule; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.RuleKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ConsumerNamedSelector; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ConsumerNamedSelectorKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderNamedSelector; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderNamedSelectorKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.subject.feature.instances.ActionInstance; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.subject.feature.instances.ActionInstanceKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.subject.feature.instances.ClassifierInstance; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.subject.feature.instances.ClassifierInstanceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class IidFactory { + + private IidFactory() { + throw new UnsupportedOperationException(); + } + + public static InstanceIdentifier tenantIid(TenantId id) { + return InstanceIdentifier.builder(Tenants.class).child(Tenant.class, new TenantKey(id)).build(); + } + + public static InstanceIdentifier endpointGroupIid(TenantId tenantId, EndpointGroupId epgId) { + return InstanceIdentifier.builder(Tenants.class) + .child(Tenant.class, new TenantKey(tenantId)) + .child(EndpointGroup.class, new EndpointGroupKey(epgId)) + .build(); + } + + public static InstanceIdentifier contractIid(TenantId tenantId, ContractId contractId) { + return InstanceIdentifier.builder(Tenants.class) + .child(Tenant.class, new TenantKey(tenantId)) + .child(Contract.class, new ContractKey(contractId)) + .build(); + } + + public static InstanceIdentifier subjectIid(TenantId tenantId, ContractId contractId, + SubjectName subjectName) { + return InstanceIdentifier.builder(Tenants.class) + .child(Tenant.class, new TenantKey(tenantId)) + .child(Contract.class, new ContractKey(contractId)) + .child(Subject.class, new SubjectKey(subjectName)) + .build(); + } + + public static InstanceIdentifier providerNamedSelectorIid(TenantId tenantId, + EndpointGroupId epgId, SelectorName providerSelectorName) { + return InstanceIdentifier.builder(Tenants.class) + .child(Tenant.class, new TenantKey(tenantId)) + .child(EndpointGroup.class, new EndpointGroupKey(epgId)) + .child(ProviderNamedSelector.class, new ProviderNamedSelectorKey(providerSelectorName)) + .build(); + } + + public static InstanceIdentifier consumerNamedSelectorIid(TenantId tenantId, + EndpointGroupId epgId, SelectorName consumerSelectorName) { + return InstanceIdentifier.builder(Tenants.class) + .child(Tenant.class, new TenantKey(tenantId)) + .child(EndpointGroup.class, new EndpointGroupKey(epgId)) + .child(ConsumerNamedSelector.class, new ConsumerNamedSelectorKey(consumerSelectorName)) + .build(); + } + + public static InstanceIdentifier clauseIid(TenantId tenantId, ContractId contractId, ClauseName clauseName) { + return InstanceIdentifier.builder(Tenants.class) + .child(Tenant.class, new TenantKey(tenantId)) + .child(Contract.class, new ContractKey(contractId)) + .child(Clause.class, new ClauseKey(clauseName)) + .build(); + } + + public static InstanceIdentifier ruleIid(TenantId tenantId, ContractId contractId, SubjectName subjectName, + RuleName ruleName) { + return InstanceIdentifier.builder(Tenants.class) + .child(Tenant.class, new TenantKey(tenantId)) + .child(Contract.class, new ContractKey(contractId)) + .child(Subject.class, new SubjectKey(subjectName)) + .child(Rule.class, new RuleKey(ruleName)) + .build(); + } + + public static InstanceIdentifier actionInstanceIid(TenantId tenantId, ActionName actionName) { + return InstanceIdentifier.builder(Tenants.class) + .child(Tenant.class, new TenantKey(tenantId)) + .child(SubjectFeatureInstances.class) + .child(ActionInstance.class, new ActionInstanceKey(actionName)) + .build(); + } + + public static InstanceIdentifier classifierInstanceIid(TenantId tenantId, + ClassifierName classifierName) { + return InstanceIdentifier.builder(Tenants.class) + .child(Tenant.class, new TenantKey(tenantId)) + .child(SubjectFeatureInstances.class) + .child(ClassifierInstance.class, new ClassifierInstanceKey(classifierName)) + .build(); + } + + public static InstanceIdentifier classifierRefIid(TenantId tenantId, ContractId contractId, + SubjectName subjectName, RuleName ruleName, ClassifierName classifierRefName) { + return InstanceIdentifier.builder(Tenants.class) + .child(Tenant.class, new TenantKey(tenantId)) + .child(Contract.class, new ContractKey(contractId)) + .child(Subject.class, new SubjectKey(subjectName)) + .child(Rule.class, new RuleKey(ruleName)) + .child(ClassifierRef.class, new ClassifierRefKey(classifierRefName)) + .build(); + } + + public static InstanceIdentifier l2FloodDomainIid(TenantId tenantId, L2FloodDomainId l2FloodDomainId) { + return InstanceIdentifier.builder(Tenants.class) + .child(Tenant.class, new TenantKey(tenantId)) + .child(L2FloodDomain.class, new L2FloodDomainKey(l2FloodDomainId)) + .build(); + } + + public static InstanceIdentifier l2BridgeDomainIid(TenantId tenantId, + L2BridgeDomainId l2BridgeDomainId) { + return InstanceIdentifier.builder(Tenants.class) + .child(Tenant.class, new TenantKey(tenantId)) + .child(L2BridgeDomain.class, new L2BridgeDomainKey(l2BridgeDomainId)) + .build(); + } + + public static InstanceIdentifier l3ContextIid(TenantId tenantId, L3ContextId l3ContextId) { + return InstanceIdentifier.builder(Tenants.class) + .child(Tenant.class, new TenantKey(tenantId)) + .child(L3Context.class, new L3ContextKey(l3ContextId)) + .build(); + } + + public static InstanceIdentifier endpointIid(L2BridgeDomainId l2Context, MacAddress macAddress) { + return InstanceIdentifier.builder(Endpoints.class) + .child(Endpoint.class, new EndpointKey(l2Context, macAddress)) + .build(); + } + + public static InstanceIdentifier endpointL3Iid(L3ContextId l3Context, IpAddress ipAddress) { + return InstanceIdentifier.builder(Endpoints.class) + .child(EndpointL3.class, new EndpointL3Key(ipAddress, l3Context)) + .build(); + } + + public static InstanceIdentifier endpointL3PrefixIid(L3ContextId l3Context, IpPrefix ipPrefix) { + return InstanceIdentifier.builder(Endpoints.class) + .child(EndpointL3Prefix.class, new EndpointL3PrefixKey(ipPrefix, l3Context)) + .build(); + } + + public static InstanceIdentifier subnetIid(TenantId tenantId, SubnetId subnetId) { + return InstanceIdentifier.builder(Tenants.class) + .child(Tenant.class, new TenantKey(tenantId)) + .child(Subnet.class, new SubnetKey(subnetId)) + .build(); + } + + +} -- 2.36.6