e025b91706e596a6315c01ccb659d12be2ef5547
[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
16 import com.google.common.base.Preconditions;
17
18 public class MappingServiceManager {
19
20     private IntentMappingService intentMappingService;
21     private String IP_PREFIX_PROPERTY = "ip_prefix";
22     private String SWITCH_PORT_ID_PROPERTY = "switch_port";
23     private String MPLS_LABEL_PROPERTY = "mpls_label";
24     private String NEXT_HOP_PROPERTY = "next_hop";
25
26     public MappingServiceManager(IntentMappingService intentMappingService) {
27         Preconditions.checkNotNull(intentMappingService);
28         this.intentMappingService = intentMappingService;
29     }
30
31     /**
32      * @param siteName
33      *            Name of the member
34      * @param ipPrefix
35      *            Ip prefix of the member
36      * @param switchPortId
37      *            Switch ID and port ID (i.e. openflow:1:2)
38      * @param mplsLabel
39      *            MPLS label, if needed
40      * @param nextHop
41      *            Next hop in the route
42      */
43     public void add(final String siteName, final String ipPrefix, final String switchPortId, final Long mplsLabel,
44             final String nextHop) {
45         Preconditions.checkNotNull(siteName);
46         Preconditions.checkNotNull(ipPrefix);
47         Preconditions.checkNotNull(switchPortId);
48
49         Map<String, String> objs = new HashMap<>();
50         objs.put(IP_PREFIX_PROPERTY, ipPrefix);
51         objs.put(SWITCH_PORT_ID_PROPERTY, switchPortId);
52
53         if (mplsLabel != null)
54             objs.put(MPLS_LABEL_PROPERTY, String.valueOf(mplsLabel));
55         if (nextHop != null)
56             objs.put(NEXT_HOP_PROPERTY, nextHop);
57
58         intentMappingService.add(siteName, objs);
59     }
60
61     /**
62      * @param siteName
63      *            Name of the member
64      * @return Map of parameters related to the member
65      */
66     public Map<String, String> get(String siteName) {
67         return intentMappingService.get(siteName);
68     }
69
70     /**
71      * @param siteName
72      *            Name of the member
73      * @return Return true if transaction succeed, otherwise false
74      */
75     public boolean delete(String siteName) {
76         try {
77             // TODO: Implement delete() in mapping service
78             // By now, it's going to overwrite data for this key
79             intentMappingService.add(siteName, null);
80             return true;
81         } catch (Exception e) {
82             throw e;
83         }
84     }
85 }