2 * Copyright (c) 2019 Ericsson India Global Services Pvt Ltd. 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
8 package org.opendaylight.netvirt.neutronvpn;
10 import java.util.concurrent.ExecutionException;
11 import java.util.concurrent.Future;
12 import javax.inject.Inject;
13 import javax.inject.Singleton;
14 import org.opendaylight.genius.mdsalutil.MDSALUtil;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.DeleteIdPoolInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.DeleteIdPoolInputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.DeleteIdPoolOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdOutput;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 public final class NeutronSecurityGroupUtils {
38 private static final Logger LOG = LoggerFactory.getLogger(NeutronSecurityGroupUtils.class);
40 private final IdManagerService idManager;
43 public NeutronSecurityGroupUtils(final IdManagerService idManager) {
44 this.idManager = idManager;
48 * Creates remote acl id pools.
50 public void createAclIdPool() {
51 createIdPoolForAclTag(NeutronSecurityGroupConstants.ACL_TAG_POOL_NAME);
55 * Creates id pool for ACL tag.
57 * @param poolName the pool name
59 public void createIdPoolForAclTag(String poolName) {
60 CreateIdPoolInput createPool = new CreateIdPoolInputBuilder()
61 .setPoolName(poolName).setLow(NeutronSecurityGroupConstants.ACL_TAG_POOL_START)
62 .setHigh(NeutronSecurityGroupConstants.ACL_TAG_POOL_END).build();
64 Future<RpcResult<CreateIdPoolOutput>> result = idManager.createIdPool(createPool);
65 if (result != null && result.get().isSuccessful()) {
66 LOG.debug("Created IdPool for {}", poolName);
68 } catch (InterruptedException | ExecutionException e) {
69 LOG.error("Failed to create ID pool [{}] for remote ACL ids", poolName, e);
70 throw new RuntimeException("Failed to create ID pool [{}] for remote ACL ids", e);
75 * Deletes remote acl id pools.
77 public void deleteAclIdPool() {
78 deleteIdPool(NeutronSecurityGroupConstants.ACL_TAG_POOL_NAME);
84 * @param poolName the pool name
86 public void deleteIdPool(String poolName) {
87 DeleteIdPoolInput deletePool = new DeleteIdPoolInputBuilder().setPoolName(poolName).build();
89 Future<RpcResult<DeleteIdPoolOutput>> result = idManager.deleteIdPool(deletePool);
90 if (result != null && result.get().isSuccessful()) {
91 LOG.debug("Deleted IdPool for {}", poolName);
93 } catch (InterruptedException | ExecutionException e) {
94 LOG.error("Failed to delete ID pool [{}]", poolName, e);
95 throw new RuntimeException("Failed to delete ID pool [" + poolName + "]", e);
102 * @param aclName the ACL name
103 * @return the integer
105 public Integer allocateAclTag(String aclName) {
106 Integer aclTag = allocateId(NeutronSecurityGroupConstants.ACL_TAG_POOL_NAME, aclName,
107 NeutronSecurityGroupConstants.INVALID_ACL_TAG);
114 * @param aclName the ACL name
116 public void releaseAclTag(String aclName) {
117 releaseId(NeutronSecurityGroupConstants.ACL_TAG_POOL_NAME, aclName);
120 public Integer allocateId(String poolName, String idKey, Integer defaultId) {
121 AllocateIdInput getIdInput = new AllocateIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
123 Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
124 RpcResult<AllocateIdOutput> rpcResult = result.get();
125 if (rpcResult.isSuccessful()) {
126 Integer allocatedId = rpcResult.getResult().getIdValue().intValue();
127 LOG.debug("Allocated ACL ID: {} with key: {} into pool: {}", allocatedId, idKey, poolName);
130 LOG.error("RPC Call to Get Unique Id for key {} from pool {} returned with Errors {}",
131 idKey, poolName, rpcResult.getErrors());
133 } catch (InterruptedException | ExecutionException e) {
134 LOG.error("Exception when getting Unique Id for key {} from pool {} ", idKey, poolName, e);
139 public void releaseId(String poolName, String idKey) {
140 ReleaseIdInput idInput = new ReleaseIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
142 Future<RpcResult<ReleaseIdOutput>> result = idManager.releaseId(idInput);
143 RpcResult<ReleaseIdOutput> rpcResult = result.get();
144 if (!rpcResult.isSuccessful()) {
145 LOG.error("RPC Call to release Id with Key {} from pool {} returned with Errors {}",
146 idKey, poolName, rpcResult.getErrors());
148 LOG.debug("Released ACL ID with key: {} from pool: {}", idKey, poolName);
150 } catch (InterruptedException | ExecutionException e) {
151 LOG.error("Exception when releasing Id for key {} from pool {} ", idKey, poolName, e);
155 public Acl getAcl(DataBroker broker, InstanceIdentifier<Acl> aclInstanceIdentifier) {
156 return MDSALUtil.read(LogicalDatastoreType.CONFIGURATION, aclInstanceIdentifier, broker).orElse(null);