2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sample.l2switch.md.addresstracker;
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;
24 import java.util.concurrent.Future;
27 * AddressTracker manages the MD-SAL data tree for L2Address (mac, node connector pairings) information.
29 public class AddressTracker {
31 private final static Logger _logger = LoggerFactory.getLogger(AddressTracker.class);
32 private DataBrokerService dataService;
35 * Construct an AddressTracker with the specified inputs
36 * @param dataService The DataBrokerService for the AddressTracker
38 public AddressTracker(DataBrokerService dataService) {
39 this.dataService = dataService;
43 * Get all the L2 Addresses in the MD-SAL data tree
44 * @return All the L2 Addresses in the MD-SAL data tree
46 public L2Addresses getAddresses() {
47 return (L2Addresses)dataService.readOperationalData(InstanceIdentifier.<L2Addresses>builder(L2Addresses.class).toInstance());
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
55 public L2Address getAddress(MacAddress macAddress) {
56 return (L2Address) dataService.readOperationalData(createPath(macAddress));
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
65 public Future<RpcResult<TransactionStatus>> addAddress(MacAddress macAddress, NodeConnectorRef nodeConnectorRef) {
66 if(macAddress == null || nodeConnectorRef == null) {
71 final L2AddressBuilder builder = new L2AddressBuilder();
72 builder.setKey(new L2AddressKey(macAddress))
74 .setNodeConnectorRef(nodeConnectorRef);
76 // Add L2Address to MD-SAL data tree
77 final DataModificationTransaction it = dataService.beginTransaction();
78 it.putOperationalData(createPath(macAddress), builder.build());
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
87 public Future<RpcResult<TransactionStatus>> removeHost(MacAddress macAddress) {
88 final DataModificationTransaction it = dataService.beginTransaction();
89 it.removeOperationalData(createPath(macAddress));
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
98 private InstanceIdentifier<L2Address> createPath(MacAddress macAddress) {
99 return InstanceIdentifier.<L2Addresses>builder(L2Addresses.class)
100 .<L2Address, L2AddressKey>child(L2Address.class, new L2AddressKey(macAddress)).toInstance();