MRI version bumpup for Aluminium
[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 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;
35
36 @Singleton
37 public final class NeutronSecurityGroupUtils {
38     private static final Logger LOG = LoggerFactory.getLogger(NeutronSecurityGroupUtils.class);
39
40     private final IdManagerService idManager;
41
42     @Inject
43     public NeutronSecurityGroupUtils(final IdManagerService idManager) {
44         this.idManager = idManager;
45     }
46
47     /**
48      * Creates remote acl id pools.
49      */
50     public void createAclIdPool() {
51         createIdPoolForAclTag(NeutronSecurityGroupConstants.ACL_TAG_POOL_NAME);
52     }
53
54     /**
55      * Creates id pool for ACL tag.
56      *
57      * @param poolName the pool name
58      */
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();
63         try {
64             Future<RpcResult<CreateIdPoolOutput>> result = idManager.createIdPool(createPool);
65             if (result != null && result.get().isSuccessful()) {
66                 LOG.debug("Created IdPool for {}", poolName);
67             }
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);
71         }
72     }
73
74     /**
75      * Deletes remote acl id pools.
76      */
77     public void deleteAclIdPool() {
78         deleteIdPool(NeutronSecurityGroupConstants.ACL_TAG_POOL_NAME);
79     }
80
81     /**
82      * Deletes id pool.
83      *
84      * @param poolName the pool name
85      */
86     public void deleteIdPool(String poolName) {
87         DeleteIdPoolInput deletePool = new DeleteIdPoolInputBuilder().setPoolName(poolName).build();
88         try {
89             Future<RpcResult<DeleteIdPoolOutput>> result = idManager.deleteIdPool(deletePool);
90             if (result != null && result.get().isSuccessful()) {
91                 LOG.debug("Deleted IdPool for {}", poolName);
92             }
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);
96         }
97     }
98
99     /**
100      * Allocates ACL tag.
101      *
102      * @param aclName the ACL name
103      * @return the integer
104      */
105     public Integer allocateAclTag(String aclName) {
106         Integer aclTag = allocateId(NeutronSecurityGroupConstants.ACL_TAG_POOL_NAME, aclName,
107                 NeutronSecurityGroupConstants.INVALID_ACL_TAG);
108         return aclTag;
109     }
110
111     /**
112      * Releases ACL tag.
113      *
114      * @param aclName the ACL name
115      */
116     public void releaseAclTag(String aclName) {
117         releaseId(NeutronSecurityGroupConstants.ACL_TAG_POOL_NAME, aclName);
118     }
119
120     public Integer allocateId(String poolName, String idKey, Integer defaultId) {
121         AllocateIdInput getIdInput = new AllocateIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
122         try {
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);
128                 return allocatedId;
129             } else {
130                 LOG.error("RPC Call to Get Unique Id for key {} from pool {} returned with Errors {}",
131                         idKey, poolName, rpcResult.getErrors());
132             }
133         } catch (InterruptedException | ExecutionException e) {
134             LOG.error("Exception when getting Unique Id for key {} from pool {} ", idKey, poolName, e);
135         }
136         return defaultId;
137     }
138
139     public void releaseId(String poolName, String idKey) {
140         ReleaseIdInput idInput = new ReleaseIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
141         try {
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());
147             } else {
148                 LOG.debug("Released ACL ID with key: {} from pool: {}", idKey, poolName);
149             }
150         } catch (InterruptedException | ExecutionException e) {
151             LOG.error("Exception when releasing Id for key {} from pool {} ", idKey, poolName, e);
152         }
153     }
154
155     public Acl getAcl(DataBroker broker, InstanceIdentifier<Acl> aclInstanceIdentifier) {
156         return MDSALUtil.read(LogicalDatastoreType.CONFIGURATION, aclInstanceIdentifier, broker).orElse(null);
157     }
158 }