Add Neutron host-id to RLOC mapping
[lispflowmapping.git] / mappingservice / neutron / src / main / java / org / opendaylight / lispflowmapping / neutron / mappingmanager / HostIdToRlocMapper.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc.  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.lispflowmapping.neutron.mappingmanager;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.concurrent.ConcurrentHashMap;
13
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * Created by Shakib Ahmed on 1/12/17.
20  */
21 public class HostIdToRlocMapper {
22     private static final Logger LOG = LoggerFactory.getLogger(HostIdToRlocMapper.class);
23
24     private ConcurrentHashMap<String, List<Rloc>> mapper;
25     private static HostIdToRlocMapper instance;
26
27     private HostIdToRlocMapper() {
28         mapper = new ConcurrentHashMap();
29     }
30
31     public static synchronized HostIdToRlocMapper getInstance() {
32         if (instance == null) {
33             instance = new HostIdToRlocMapper();
34         }
35         return instance;
36     }
37
38     public synchronized void addMapping(String hostId, Rloc hostRloc) {
39         List<Rloc> hostIdSpeceficRlocs = mapper.get(hostId);
40
41         if (hostIdSpeceficRlocs == null) {
42             hostIdSpeceficRlocs = new ArrayList<>();
43         }
44
45         hostIdSpeceficRlocs.add(hostRloc);
46         mapper.put(hostId, hostIdSpeceficRlocs);
47         LOG.debug("Adding " + hostRloc.getAddress() + " as Rloc of " + hostId);
48     }
49
50     public List<Rloc> getRlocs(String hostId) {
51         return mapper.get(hostId);
52     }
53
54     public synchronized void deleteMapping(String hostId) {
55         //for now, delete all rlocs
56         mapper.remove(hostId);
57     }
58 }