Cleanup of Lisp in VPP renderer
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / lisp / info / container / HostRelatedInfoContainer.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 package org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.info.container;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.HashBasedTable;
12 import com.google.common.collect.Sets;
13 import com.google.common.collect.Table;
14
15 import java.util.HashMap;
16 import java.util.Set;
17
18 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.info.container.states.PhysicalInterfaces;
19 import org.opendaylight.groupbasedpolicy.renderer.vpp.listener.VppEndpointListener;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class HostRelatedInfoContainer {
24     private HashMap<String, PhysicalInterfaces> hostNameToPhysicalInterfacesMapper;
25
26     //route IDs on an interface on a host
27     private Table<String, String, Set<Long>> routeIdsByHostByVrf = HashBasedTable.create();
28
29     private static final HostRelatedInfoContainer INSTANCE = new HostRelatedInfoContainer();
30
31     private HostRelatedInfoContainer() {
32         this.hostNameToPhysicalInterfacesMapper = new HashMap<>();
33     }
34
35     public void addRouteToIntfc(String hostname, String intfName, Long routeId) {
36         Preconditions.checkNotNull(hostname);
37         Preconditions.checkNotNull(intfName);
38         Preconditions.checkNotNull(routeId);
39         if (routeIdsByHostByVrf.get(hostname, intfName) != null) {
40             routeIdsByHostByVrf.get(hostname, intfName).add(routeId);
41             return;
42         }
43         routeIdsByHostByVrf.put(hostname, intfName, Sets.newHashSet(routeId));
44     }
45
46     public void deleteRouteFromIntfc(String hostname, String intfName, Long routeId) {
47         Preconditions.checkNotNull(hostname);
48         Preconditions.checkNotNull(intfName);
49         Preconditions.checkNotNull(routeId);
50         if (routeIdsByHostByVrf.get(hostname, intfName) != null) {
51             routeIdsByHostByVrf.get(hostname, intfName).remove(routeId);
52         }
53     }
54
55     private static final Logger LOG = LoggerFactory.getLogger(VppEndpointListener.class);
56
57     public boolean intfcIsBusy(String hostname, String intfName) {
58         Preconditions.checkNotNull(hostname);
59         Preconditions.checkNotNull(intfName);
60         if (routeIdsByHostByVrf.get(hostname, intfName) != null) {
61             int size = routeIdsByHostByVrf.get(hostname, intfName).size();
62             LOG.trace("ISPORTBUSY -> hostname: {}, inftName: {}, entries: {}", hostname, intfName,
63                 routeIdsByHostByVrf.get(hostname, intfName));
64             return size != 0;
65         }
66         LOG.trace("ISPORTBUSY -> not busy interface on hostname: {}, inftName: {}", hostname, intfName);
67         return false;
68     }
69
70     public static HostRelatedInfoContainer getInstance() {
71         return INSTANCE;
72     }
73
74     public PhysicalInterfaces getPhysicalInterfaceState(String hostName) {
75         return hostNameToPhysicalInterfacesMapper.get(hostName);
76     }
77
78     public void setPhysicalInterfaceStateOfHost(String hostName, PhysicalInterfaces physicalInterfaces) {
79         hostNameToPhysicalInterfacesMapper.put(hostName, physicalInterfaces);
80     }
81
82     public void removePhysicalInterfaceStateOfHost(String hostName) {
83         //TODO should be called when host is removed
84         hostNameToPhysicalInterfacesMapper.remove(hostName);
85     }
86 }