Fix build faliures due to OFPlugin checktyle fixes
[netvirt.git] / vpnservice / policyservice / impl / src / main / java / org / opendaylight / netvirt / policyservice / PolicyIdManager.java
1 /*
2  * Copyright (c) 2017 Hewlett Packard Enterprise, Co. 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
9 package org.opendaylight.netvirt.policyservice;
10
11 import com.google.common.collect.ImmutableMap;
12
13 import java.math.BigInteger;
14 import java.util.Map;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Future;
18
19 import javax.annotation.PostConstruct;
20 import javax.inject.Inject;
21 import javax.inject.Singleton;
22
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInputBuilder;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Manage allocation of policy ids and policy group ids.
37  *
38  */
39 @Singleton
40 public class PolicyIdManager {
41     private static final Logger LOG = LoggerFactory.getLogger(PolicyIdManager.class);
42
43     private final IdManagerService idManager;
44     private final Map<String,
45             Map<String, Long>> idCache = ImmutableMap.of(//
46                     PolicyServiceConstants.POLICY_CLASSIFIER_POOL_NAME, new ConcurrentHashMap<String, Long>(),
47                     PolicyServiceConstants.POLICY_GROUP_POOL_NAME, new ConcurrentHashMap<String, Long>());
48
49     @Inject
50     public PolicyIdManager(final IdManagerService idManager) {
51         this.idManager = idManager;
52     }
53
54     @PostConstruct
55     public void init() {
56         LOG.info("init");
57         createIdPools();
58     }
59
60     public long getPolicyClassifierId(String policyClassifierName) {
61         return allocateId(policyClassifierName, PolicyServiceConstants.POLICY_CLASSIFIER_POOL_NAME);
62     }
63
64     public void releasePolicyClassifierId(String policyClassifierName) {
65         releaseId(policyClassifierName, PolicyServiceConstants.POLICY_CLASSIFIER_POOL_NAME);
66     }
67
68     public long getPolicyClassifierGroupId(String policyClassifierName, BigInteger remoteDpId) {
69         return allocateId(getPolicyClassifierGroupKey(policyClassifierName, remoteDpId),
70                 PolicyServiceConstants.POLICY_GROUP_POOL_NAME);
71     }
72
73     public void releasePolicyClassifierGroupId(String policyClassifierName, BigInteger remoteDpId) {
74         releaseId(getPolicyClassifierGroupKey(policyClassifierName, remoteDpId),
75                 PolicyServiceConstants.POLICY_GROUP_POOL_NAME);
76     }
77
78     public static String getPolicyClassifierGroupKey(String policyClassifier, BigInteger remoteDpId) {
79         return policyClassifier + '-' + remoteDpId;
80     }
81
82     private void createIdPools() {
83         createIdPool(PolicyServiceConstants.POLICY_CLASSIFIER_POOL_NAME,
84                 PolicyServiceConstants.POLICY_CLASSIFIER_LOW_ID, PolicyServiceConstants.POLICY_CLASSIFIER_HIGH_ID);
85         createIdPool(PolicyServiceConstants.POLICY_GROUP_POOL_NAME, PolicyServiceConstants.POLICY_GROUP_LOW_ID,
86                 PolicyServiceConstants.POLICY_GROUP_HIGH_ID);
87     }
88
89     private void createIdPool(String poolName, long lowId, long highId) {
90         CreateIdPoolInput createPoolInput = new CreateIdPoolInputBuilder().setPoolName(poolName).setLow(lowId)
91                 .setHigh(highId).build();
92
93         try {
94             Future<RpcResult<Void>> result = idManager.createIdPool(createPoolInput);
95             if ((result != null) && (result.get().isSuccessful())) {
96                 LOG.info("Created IdPool for {}", poolName);
97             }
98         } catch (InterruptedException | ExecutionException e) {
99             LOG.error("Failed to create idPool for {}", poolName, e);
100         }
101     }
102
103     private long allocateId(String key, String poolName) {
104         Long id = idCache.get(poolName).get(key);
105         if (id != null) {
106             return id;
107         }
108
109         AllocateIdInput getIdInput = new AllocateIdInputBuilder().setPoolName(poolName).setIdKey(key).build();
110         try {
111             Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
112             RpcResult<AllocateIdOutput> rpcResult = result.get();
113             Long idValue = rpcResult.getResult().getIdValue();
114             if (idValue != null) {
115                 idCache.get(poolName).putIfAbsent(key, idValue);
116                 return idValue;
117             }
118         } catch (InterruptedException | ExecutionException e) {
119             LOG.warn("Exception thrown while allocating id for key {}", key);
120         }
121
122         return PolicyServiceConstants.INVALID_ID;
123     }
124
125     private void releaseId(String key, String poolName) {
126         idCache.get(poolName).remove(key);
127         ReleaseIdInput idInput = new ReleaseIdInputBuilder().setPoolName(poolName).setIdKey(key).build();
128         try {
129             Future<RpcResult<Void>> result = idManager.releaseId(idInput);
130             RpcResult<Void> rpcResult = result.get();
131             if (!rpcResult.isSuccessful()) {
132                 LOG.warn("RPC Call to release {} returned with Errors {}", key, rpcResult.getErrors());
133             }
134         } catch (InterruptedException | ExecutionException e) {
135             LOG.warn("Exception thrown while releasing id for key {}", key);
136         }
137     }
138 }