Code cleanup for vpnintent module as per comments
[vpnservice.git] / vpnintent / impl / src / main / java / org / opendaylight / vpnservice / impl / MappingServiceManager.java
1 /*
2  * Copyright (c) 2016 Inocybe Technologies and others.  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.vpnservice.impl;
10
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import org.opendaylight.nic.mapping.api.IntentMappingService;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 import com.google.common.base.Preconditions;
19
20 public class MappingServiceManager {
21
22     private static final Logger LOG = LoggerFactory.getLogger(MappingServiceManager.class);
23     private IntentMappingService intentMappingService;
24     private String IP_PREFIX_PROPERTY = "ip_prefix";
25     private String SWITCH_PORT_ID_PROPERTY = "switch_port";
26     private String MPLS_LABEL_PROPERTY = "mpls_label";
27     private String NEXT_HOP_PROPERTY = "next_hop";
28
29     public MappingServiceManager(IntentMappingService intentMappingService) {
30         Preconditions.checkNotNull(intentMappingService);
31         this.intentMappingService = intentMappingService;
32     }
33
34     /**
35      * @param siteName
36      *            Name of the member
37      * @param ipPrefix
38      *            Ip prefix of the member
39      * @param switchPortId
40      *            Switch ID and port ID (i.e. openflow:1:2)
41      * @param mplsLabel
42      *            MPLS label, if needed
43      * @param nextHop
44      *            Next hop in the route
45      */
46     public void add(final String siteName, final String ipPrefix, final String switchPortId, final Long mplsLabel,
47             final String nextHop) {
48         Preconditions.checkNotNull(siteName);
49         Preconditions.checkNotNull(ipPrefix);
50         Preconditions.checkNotNull(switchPortId);
51
52         Map<String, String> objs = new HashMap<>();
53         objs.put(IP_PREFIX_PROPERTY, ipPrefix);
54         objs.put(SWITCH_PORT_ID_PROPERTY, switchPortId);
55
56         if (mplsLabel != null)
57             objs.put(MPLS_LABEL_PROPERTY, String.valueOf(mplsLabel));
58         if (nextHop != null)
59             objs.put(NEXT_HOP_PROPERTY, nextHop);
60
61         intentMappingService.add(siteName, objs);
62     }
63
64     /**
65      * @param siteName
66      *            Name of the member
67      * @return Map of parameters related to the member
68      */
69     public Map<String, String> get(String siteName) {
70         return intentMappingService.get(siteName);
71     }
72
73     /**
74      * @param siteName
75      *            Name of the member
76      * @return Return true if transaction succeed, otherwise false
77      */
78     public boolean delete(String siteName) {
79         try {
80             // TODO: Implement delete() in mapping service
81             // By now, it's going to overwrite data for this key
82             intentMappingService.add(siteName, null);
83             return true;
84         } catch (Exception e) {
85             LOG.error("Error deleting from NIC's mapping service {}", e);
86             throw e;
87         }
88     }
89 }