NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / neutronvpn / impl / src / main / java / org / opendaylight / netvirt / neutronvpn / NeutronSecurityGroupUtils.java
1 /*
2  * Copyright (c) 2019 Ericsson India Global Services Pvt Ltd. 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.netvirt.neutronvpn;
9
10 import java.util.concurrent.ExecutionException;
11 import java.util.concurrent.Future;
12
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15
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;
37
38 @Singleton
39 public final class NeutronSecurityGroupUtils {
40     private static final Logger LOG = LoggerFactory.getLogger(NeutronSecurityGroupUtils.class);
41
42     private final IdManagerService idManager;
43
44     @Inject
45     public NeutronSecurityGroupUtils(final IdManagerService idManager) {
46         this.idManager = idManager;
47     }
48
49     /**
50      * Creates remote acl id pools.
51      */
52     public void createAclIdPool() {
53         createIdPoolForAclTag(NeutronSecurityGroupConstants.ACL_TAG_POOL_NAME);
54     }
55
56     /**
57      * Creates id pool for ACL tag.
58      *
59      * @param poolName the pool name
60      */
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();
65         try {
66             Future<RpcResult<CreateIdPoolOutput>> result = idManager.createIdPool(createPool);
67             if (result != null && result.get().isSuccessful()) {
68                 LOG.debug("Created IdPool for {}", poolName);
69             }
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);
73         }
74     }
75
76     /**
77      * Deletes remote acl id pools.
78      */
79     public void deleteAclIdPool() {
80         deleteIdPool(NeutronSecurityGroupConstants.ACL_TAG_POOL_NAME);
81     }
82
83     /**
84      * Deletes id pool.
85      *
86      * @param poolName the pool name
87      */
88     public void deleteIdPool(String poolName) {
89         DeleteIdPoolInput deletePool = new DeleteIdPoolInputBuilder().setPoolName(poolName).build();
90         try {
91             Future<RpcResult<DeleteIdPoolOutput>> result = idManager.deleteIdPool(deletePool);
92             if (result != null && result.get().isSuccessful()) {
93                 LOG.debug("Deleted IdPool for {}", poolName);
94             }
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);
98         }
99     }
100
101     /**
102      * Allocates ACL tag.
103      *
104      * @param aclName the ACL name
105      * @return the integer
106      */
107     public Integer allocateAclTag(String aclName) {
108         Integer aclTag = allocateId(NeutronSecurityGroupConstants.ACL_TAG_POOL_NAME, aclName,
109                 NeutronSecurityGroupConstants.INVALID_ACL_TAG);
110         return aclTag;
111     }
112
113     /**
114      * Releases ACL tag.
115      *
116      * @param aclName the ACL name
117      */
118     public void releaseAclTag(String aclName) {
119         releaseId(NeutronSecurityGroupConstants.ACL_TAG_POOL_NAME, aclName);
120     }
121
122     public Integer allocateId(String poolName, String idKey, Integer defaultId) {
123         AllocateIdInput getIdInput = new AllocateIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
124         try {
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);
130                 return allocatedId;
131             } else {
132                 LOG.error("RPC Call to Get Unique Id for key {} from pool {} returned with Errors {}",
133                         idKey, poolName, rpcResult.getErrors());
134             }
135         } catch (InterruptedException | ExecutionException e) {
136             LOG.error("Exception when getting Unique Id for key {} from pool {} ", idKey, poolName, e);
137         }
138         return defaultId;
139     }
140
141     public void releaseId(String poolName, String idKey) {
142         ReleaseIdInput idInput = new ReleaseIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
143         try {
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());
149             } else {
150                 LOG.debug("Released ACL ID with key: {} from pool: {}", idKey, poolName);
151             }
152         } catch (InterruptedException | ExecutionException e) {
153             LOG.error("Exception when releasing Id for key {} from pool {} ", idKey, poolName, e);
154         }
155     }
156
157     public Acl getAcl(DataBroker broker, InstanceIdentifier<Acl> aclInstanceIdentifier) {
158         return MDSALUtil.read(LogicalDatastoreType.CONFIGURATION, aclInstanceIdentifier, broker).orElse(null);
159     }
160 }