97d8834ba05fa6b606f12b997bcbd14558528ae6
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / lisp / mappers / HostVrfRoutingInformationMapper.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.mappers;
10
11
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.flat.overlay.RoutingInfo;
14
15 import java.util.HashMap;
16
17 /**
18  * Created by Shakib Ahmed on 5/4/17.
19  */
20 public class HostVrfRoutingInformationMapper {
21     HashMap<String, HashMap<Long, RoutingInfo> > mapper;
22
23     private static final HostVrfRoutingInformationMapper INSTANCE = new HostVrfRoutingInformationMapper();
24
25     private HostVrfRoutingInformationMapper() {
26         mapper = new HashMap<>();
27     }
28
29     public static HostVrfRoutingInformationMapper getInstance() {
30         return INSTANCE;
31     }
32
33     public void addRoutingVrfToHost(String hostId, long vrf, RoutingInfo routingInfo) {
34         HashMap<Long, RoutingInfo> routingInfoMapper = mapper.get(hostId);
35
36         if (routingInfoMapper == null) {
37             routingInfoMapper = new HashMap<>();
38             mapper.put(hostId, routingInfoMapper);
39         }
40
41         routingInfoMapper.put(vrf, routingInfo);
42     }
43
44     public boolean vrfExists(String hostId, long vrf) {
45         return mapper.containsKey(hostId) && mapper.get(hostId).containsKey(vrf);
46     }
47
48     public String getProtocolName(String hostId, long vrf) {
49         Preconditions.checkArgument(vrfExists(hostId, vrf));
50
51         return mapper.get(hostId).get(vrf).getProtocolName();
52     }
53
54     public void addStaticRoute(String hostId, long vrf) {
55         HashMap<Long, RoutingInfo> routingInfoMapper = mapper.get(hostId);
56
57         Preconditions.checkNotNull(routingInfoMapper, "Routing protocol not created, can't add route entry");
58
59         RoutingInfo routingInfo = routingInfoMapper.get(vrf);
60
61         Preconditions.checkNotNull(routingInfoMapper, "VRF was not created for this host");
62
63         routingInfo.incrementCount();
64     }
65
66     public Long getEndPointCountInVrf(String hostId, long vrf) {
67         int count = 0;
68
69         if (vrfExists(hostId, vrf)) {
70             count = mapper.get(hostId).get(vrf).getCount();
71         }
72         return (long) (count + 1);
73     }
74 }