Migrate to odlparent 1.9.0
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / lisp / bvi / BviHostSpecificInfo.java
1 /*
2  * Copyright (c) 2017 Cisco Systems. 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.groupbasedpolicy.renderer.vpp.lisp.bvi;
10
11 import com.google.common.collect.ArrayListMultimap;
12 import com.google.common.collect.Multimap;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.stream.Collectors;
18
19 /**
20  * Created by Shakib Ahmed on 4/26/17.
21  */
22 public class BviHostSpecificInfo {
23     private HashMap<String, HashMap<String, String>> hostIdToSubnetMapper;
24     private Multimap<String, String> subnetUuidToHostIdList;
25
26     public BviHostSpecificInfo() {
27         hostIdToSubnetMapper = new HashMap<>();
28         subnetUuidToHostIdList = ArrayListMultimap.create();
29     }
30
31     private HashMap<String, String> getSubnetsOfHost(String hostName) {
32         return hostIdToSubnetMapper.get(hostName);
33     }
34
35     private void putSubnetInfoOfAHost(String hostId, String subnetUuid, String interfaceName) {
36         HashMap<String, String> subnetsOfAHost = getSubnetsOfHost(hostId);
37
38         if (subnetsOfAHost == null) {
39             subnetsOfAHost = new HashMap<>();
40             hostIdToSubnetMapper.put(hostId, subnetsOfAHost);
41         }
42         subnetsOfAHost.put(subnetUuid, interfaceName);
43     }
44
45     public boolean bviAlreadyExists(String hostName, String subnetUuid) {
46         return hostIdToSubnetMapper.containsKey(hostName) &&
47                 hostIdToSubnetMapper.get(hostName).containsKey(subnetUuid);
48     }
49
50     public void addBviForHost(String hostName, String subnetUuid, String interfaceName) {
51         putSubnetInfoOfAHost(hostName, subnetUuid, interfaceName);
52         subnetUuidToHostIdList.put(subnetUuid, hostName);
53     }
54
55     public int getBviCount(String hostName) {
56         if (hostIdToSubnetMapper.get(hostName) == null) {
57             return 0;
58         }
59         return hostIdToSubnetMapper.get(hostName).size();
60     }
61
62     public void clearSubnet(String subnetUuid) {
63         subnetUuidToHostIdList.get(subnetUuid).forEach(hostId -> {
64             deleteParticularSubnetFromHost(hostId, subnetUuid);
65         });
66         subnetUuidToHostIdList.get(subnetUuid).clear();
67     }
68
69     private void deleteParticularSubnetFromHost(String hostId, String subnetUuid) {
70         hostIdToSubnetMapper.get(hostId).remove(subnetUuid);
71     }
72
73     public List<String> getHostsWithSubnet(String subnetUuid) {
74         return subnetUuidToHostIdList.get(subnetUuid).stream().collect(Collectors.toList());
75     }
76
77     public String getInterfaceNameForBviInHost(String hostId, String subnetUuid) {
78         if (hostIdToSubnetMapper.get(hostId) != null) {
79             return hostIdToSubnetMapper.get(hostId).get(subnetUuid);
80         } else {
81             return null;
82         }
83     }
84 }