Initial implementation for dynamic SNAT
[groupbasedpolicy.git] / neutron-mapper / src / test / java / org / opendaylight / groupbasedpolicy / neutron / mapper / test / ConfigDataStoreReader.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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 package org.opendaylight.groupbasedpolicy.neutron.mapper.test;
9
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
14 import org.opendaylight.groupbasedpolicy.util.IidFactory;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionName;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SelectorName;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.Contract;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Clause;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ConsumerNamedSelector;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ActionInstance;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ClassifierInstance;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30
31 import com.google.common.base.Optional;
32
33 public final class ConfigDataStoreReader {
34
35     private ConfigDataStoreReader() {
36         throw new UnsupportedOperationException("Cannot create an instance");
37     }
38
39     public static Optional<Tenant> readTenant(DataBroker dataBroker, String tenantId) throws Exception {
40         try (ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction()) {
41             return DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
42                     IidFactory.tenantIid(new TenantId(tenantId)), rTx);
43         }
44     }
45
46     public static Optional<Contract> readContract(DataBroker dataBroker, String tenantId, String contractId)
47             throws Exception {
48         try (ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction()) {
49             return DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
50                     IidFactory.contractIid(new TenantId(tenantId), new ContractId(contractId)), rTx);
51         }
52     }
53
54     public static Optional<EndpointGroup> readEndpointGroup(DataBroker dataBroker, String tenantId,
55             String endpointGroupId) throws Exception {
56         try (ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction()) {
57             return DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
58                     IidFactory.endpointGroupIid(new TenantId(tenantId), new EndpointGroupId(endpointGroupId)), rTx);
59         }
60     }
61
62     public static Optional<ClassifierInstance> readClassifierInstance(DataBroker dataBroker, String tenantId,
63             ClassifierName classifierName) throws Exception {
64         InstanceIdentifier<ClassifierInstance> clsfInstanceIid = IidFactory.classifierInstanceIid(
65                 new TenantId(tenantId), classifierName);
66         try (ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction()) {
67             return DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION, clsfInstanceIid, rTx);
68         }
69     }
70
71     public static Optional<ActionInstance> readActionInstance(DataBroker dataBroker, String tenantId,
72             ActionName actionName) throws Exception {
73         InstanceIdentifier<ActionInstance> actionIid = IidFactory.actionInstanceIid(new TenantId(tenantId), actionName);
74         try (ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction()) {
75             return DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION, actionIid, rTx);
76         }
77     }
78
79     public static Optional<ConsumerNamedSelector> readConsumerNamedSelector(DataBroker dataBroker, String tenantId, String egId,
80                                                                             String selectorName) {
81         InstanceIdentifier<ConsumerNamedSelector> cnsIid = IidFactory.consumerNamedSelectorIid(new TenantId(tenantId),
82                 new EndpointGroupId(egId), new SelectorName(selectorName));
83         try (ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction()) {
84             return DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION, cnsIid, rTx);
85         }
86     }
87
88     public static Optional<Clause> readClause(DataBroker dataBroker, String tenantId, String contractId, String clauseName) {
89         InstanceIdentifier<Clause> clauseIid = IidFactory.clauseIid(new TenantId(tenantId), new ContractId(contractId),
90                 new ClauseName(clauseName));
91         try (ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction()) {
92             return DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION, clauseIid, rTx);
93         }
94     }
95 }