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;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
16 import org.opendaylight.genius.mdsalutil.MDSALUtil;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.DeleteIdPoolInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.DeleteIdPoolInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.DeleteIdPoolOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdOutput;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 public final class NeutronSecurityGroupUtils {
40 private static final Logger LOG = LoggerFactory.getLogger(NeutronSecurityGroupUtils.class);
42 private final IdManagerService idManager;
45 public NeutronSecurityGroupUtils(final IdManagerService idManager) {
46 this.idManager = idManager;
50 * Creates remote acl id pools.
52 public void createAclIdPool() {
53 createIdPoolForAclTag(NeutronSecurityGroupConstants.ACL_TAG_POOL_NAME);
57 * Creates id pool for ACL tag.
59 * @param poolName the pool name
61 public void createIdPoolForAclTag(String poolName) {
62 CreateIdPoolInput createPool = new CreateIdPoolInputBuilder()
63 .setPoolName(poolName).setLow(NeutronSecurityGroupConstants.ACL_TAG_POOL_START)
64 .setHigh(NeutronSecurityGroupConstants.ACL_TAG_POOL_END).build();
66 Future<RpcResult<CreateIdPoolOutput>> result = idManager.createIdPool(createPool);
67 if (result != null && result.get().isSuccessful()) {
68 LOG.debug("Created IdPool for {}", poolName);
70 } catch (InterruptedException | ExecutionException e) {
71 LOG.error("Failed to create ID pool [{}] for remote ACL ids", poolName, e);
72 throw new RuntimeException("Failed to create ID pool [{}] for remote ACL ids", e);
77 * Deletes remote acl id pools.
79 public void deleteAclIdPool() {
80 deleteIdPool(NeutronSecurityGroupConstants.ACL_TAG_POOL_NAME);
86 * @param poolName the pool name
88 public void deleteIdPool(String poolName) {
89 DeleteIdPoolInput deletePool = new DeleteIdPoolInputBuilder().setPoolName(poolName).build();
91 Future<RpcResult<DeleteIdPoolOutput>> result = idManager.deleteIdPool(deletePool);
92 if (result != null && result.get().isSuccessful()) {
93 LOG.debug("Deleted IdPool for {}", poolName);
95 } catch (InterruptedException | ExecutionException e) {
96 LOG.error("Failed to delete ID pool [{}]", poolName, e);
97 throw new RuntimeException("Failed to delete ID pool [" + poolName + "]", e);
104 * @param aclName the ACL name
105 * @return the integer
107 public Integer allocateAclTag(String aclName) {
108 Integer aclTag = allocateId(NeutronSecurityGroupConstants.ACL_TAG_POOL_NAME, aclName,
109 NeutronSecurityGroupConstants.INVALID_ACL_TAG);
116 * @param aclName the ACL name
118 public void releaseAclTag(String aclName) {
119 releaseId(NeutronSecurityGroupConstants.ACL_TAG_POOL_NAME, aclName);
122 public Integer allocateId(String poolName, String idKey, Integer defaultId) {
123 AllocateIdInput getIdInput = new AllocateIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
125 Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
126 RpcResult<AllocateIdOutput> rpcResult = result.get();
127 if (rpcResult.isSuccessful()) {
128 Integer allocatedId = rpcResult.getResult().getIdValue().intValue();
129 LOG.debug("Allocated ACL ID: {} with key: {} into pool: {}", allocatedId, idKey, poolName);
132 LOG.error("RPC Call to Get Unique Id for key {} from pool {} returned with Errors {}",
133 idKey, poolName, rpcResult.getErrors());
135 } catch (InterruptedException | ExecutionException e) {
136 LOG.error("Exception when getting Unique Id for key {} from pool {} ", idKey, poolName, e);
141 public void releaseId(String poolName, String idKey) {
142 ReleaseIdInput idInput = new ReleaseIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
144 Future<RpcResult<ReleaseIdOutput>> result = idManager.releaseId(idInput);
145 RpcResult<ReleaseIdOutput> rpcResult = result.get();
146 if (!rpcResult.isSuccessful()) {
147 LOG.error("RPC Call to release Id with Key {} from pool {} returned with Errors {}",
148 idKey, poolName, rpcResult.getErrors());
150 LOG.debug("Released ACL ID with key: {} from pool: {}", idKey, poolName);
152 } catch (InterruptedException | ExecutionException e) {
153 LOG.error("Exception when releasing Id for key {} from pool {} ", idKey, poolName, e);
157 public Acl getAcl(DataBroker broker, InstanceIdentifier<Acl> aclInstanceIdentifier) {
158 return MDSALUtil.read(LogicalDatastoreType.CONFIGURATION, aclInstanceIdentifier, broker).orElse(null);