Fix metadata for route, arp and overlay
[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 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
15
16 import java.util.HashMap;
17
18 /**
19  * Created by Shakib Ahmed on 5/4/17.
20  */
21 public class HostVrfRoutingInformationMapper {
22     HashMap<String, HashMap<Long, RoutingInfo> > mapper;
23
24     private static final HostVrfRoutingInformationMapper INSTANCE = new HostVrfRoutingInformationMapper();
25
26     private HostVrfRoutingInformationMapper() {
27         mapper = new HashMap<>();
28     }
29
30     public static HostVrfRoutingInformationMapper getInstance() {
31         return INSTANCE;
32     }
33
34     public void addRoutingVrfToHost(String hostId, long vrf, RoutingInfo routingInfo) {
35         HashMap<Long, RoutingInfo> routingInfoMapper = mapper.get(hostId);
36
37         if (routingInfoMapper == null) {
38             routingInfoMapper = new HashMap<>();
39             mapper.put(hostId, routingInfoMapper);
40         }
41
42         routingInfoMapper.put(vrf, routingInfo);
43     }
44
45     public boolean vrfExists(String hostId, long vrf) {
46         return mapper.containsKey(hostId) && mapper.get(hostId).containsKey(vrf);
47     }
48
49     public String getProtocolName(String hostId, long vrf) {
50         Preconditions.checkArgument(vrfExists(hostId, vrf));
51
52         return mapper.get(hostId).get(vrf).getProtocolName();
53     }
54
55     public void addStaticRoute(String hostId, long vrf, Ipv4Address ip) {
56         HashMap<Long, RoutingInfo> routingInfoMapper = mapper.get(hostId);
57
58         Preconditions.checkNotNull(routingInfoMapper, "Routing protocol not created, can't add route entry");
59
60         RoutingInfo routingInfo = routingInfoMapper.get(vrf);
61
62         Preconditions.checkNotNull(routingInfoMapper, "VRF was not created for this host");
63
64         routingInfo.addIpInVrf(ip);
65     }
66
67     public Long getEndPointCountInVrf(String hostId, long vrf) {
68         int count = 0;
69
70         if (vrfExists(hostId, vrf)) {
71             count = mapper.get(hostId).get(vrf).getCount();
72         }
73         return (long) (count + 1);
74     }
75
76     public boolean ipAlreadyExistsInHostVrf(String hostId, long vrf, Ipv4Address ip) {
77         HashMap<Long, RoutingInfo> routingInfoMapper = mapper.get(hostId);
78
79         Preconditions.checkNotNull(routingInfoMapper, "Routing protocol not created, can't add route entry");
80
81         RoutingInfo routingInfo = routingInfoMapper.get(vrf);
82
83         return routingInfo.ipAlreadyExistsinVrf(ip);
84     }
85 }