BUG 2090 : Clustering : Bring akka-raft unit test coverage upto 80%
[controller.git] / opendaylight / md-sal / samples / l2switch / implementation / src / main / java / org / opendaylight / controller / sample / l2switch / md / addresstracker / AddressTracker.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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 package org.opendaylight.controller.sample.l2switch.md.addresstracker;
9
10 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
11 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
12 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.address.tracker.rev140402.L2Addresses;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.address.tracker.rev140402.l2.addresses.L2Address;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.address.tracker.rev140402.l2.addresses.L2AddressBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.address.tracker.rev140402.l2.addresses.L2AddressKey;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.opendaylight.yangtools.yang.common.RpcResult;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import java.util.concurrent.Future;
25
26 /**
27  * AddressTracker manages the MD-SAL data tree for L2Address (mac, node connector pairings) information.
28  */
29 public class AddressTracker {
30
31   private final static Logger _logger = LoggerFactory.getLogger(AddressTracker.class);
32   private DataBrokerService dataService;
33
34   /**
35    * Construct an AddressTracker with the specified inputs
36    * @param dataService  The DataBrokerService for the AddressTracker
37    */
38   public AddressTracker(DataBrokerService dataService) {
39     this.dataService = dataService;
40   }
41
42   /**
43    * Get all the L2 Addresses in the MD-SAL data tree
44    * @return    All the L2 Addresses in the MD-SAL data tree
45    */
46   public L2Addresses getAddresses() {
47     return (L2Addresses)dataService.readOperationalData(InstanceIdentifier.<L2Addresses>builder(L2Addresses.class).toInstance());
48   }
49
50   /**
51    * Get a specific L2 Address in the MD-SAL data tree
52    * @param macAddress  A MacAddress associated with an L2 Address object
53    * @return    The L2 Address corresponding to the specified macAddress
54    */
55   public L2Address getAddress(MacAddress macAddress) {
56     return (L2Address) dataService.readOperationalData(createPath(macAddress));
57   }
58
59   /**
60    * Add L2 Address into the MD-SAL data tree
61    * @param macAddress  The MacAddress of the new L2Address object
62    * @param nodeConnectorRef  The NodeConnectorRef of the new L2Address object
63    * @return  Future containing the result of the add operation
64    */
65   public Future<RpcResult<TransactionStatus>> addAddress(MacAddress macAddress, NodeConnectorRef nodeConnectorRef) {
66     if(macAddress == null || nodeConnectorRef == null) {
67       return null;
68     }
69
70     // Create L2Address
71     final L2AddressBuilder builder = new L2AddressBuilder();
72     builder.setKey(new L2AddressKey(macAddress))
73             .setMac(macAddress)
74             .setNodeConnectorRef(nodeConnectorRef);
75
76     // Add L2Address to MD-SAL data tree
77     final DataModificationTransaction it = dataService.beginTransaction();
78     it.putOperationalData(createPath(macAddress), builder.build());
79     return it.commit();
80   }
81
82   /**
83    * Remove L2Address from the MD-SAL data tree
84    * @param macAddress  The MacAddress of an L2Address object
85    * @return  Future containing the result of the remove operation
86    */
87   public Future<RpcResult<TransactionStatus>> removeHost(MacAddress macAddress) {
88     final DataModificationTransaction it = dataService.beginTransaction();
89     it.removeOperationalData(createPath(macAddress));
90     return it.commit();
91   }
92
93   /**
94    * Create InstanceIdentifier path for an L2Address in the MD-SAL data tree
95    * @param macAddress  The MacAddress of an L2Address object
96    * @return  InstanceIdentifier of the L2Address corresponding to the specified macAddress
97    */
98   private InstanceIdentifier<L2Address> createPath(MacAddress macAddress) {
99     return InstanceIdentifier.<L2Addresses>builder(L2Addresses.class)
100             .<L2Address, L2AddressKey>child(L2Address.class, new L2AddressKey(macAddress)).toInstance();
101   }
102 }