// Path: hostservice/api/src/main/java/org/opendaylight/atrium/hostservice/api/ArpMessageAddress.java
// @Immutable
// public class ArpMessageAddress {
// 
//     private final MacAddress hwAddress;
//     private final Ipv4Address protocolAddress;
// 
//     public ArpMessageAddress(MacAddress hwAddress, Ipv4Address protocolAddress) {
//         this.hwAddress = checkNotNull(hwAddress);
//         this.protocolAddress = checkNotNull(protocolAddress);
//     }
// 
//     public MacAddress getHardwareAddress() {
//         return hwAddress;
//     }
// 
//     public Ipv4Address getProtocolAddress() {
//         return protocolAddress;
//     }
// 
// }
// 
// Path: hostservice/impl/src/main/java/org/opendaylight/atrium/hostservice/impl/ConfigWriter.java
// public class ConfigWriter {
// 
// 	public static void writeBgpConfigData(DataBroker dataBroker, Addresses arpAddresses) {
// 		WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
// 		transaction.put(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.builder(Addresses.class).build(),
// 				arpAddresses);
// 		transaction.submit();
// 	}
// }


import java.net.URL;
import java.util.List;
import java.io.File;
import java.util.concurrent.ExecutionException;
import org.opendaylight.atrium.hostservice.api.ArpMessageAddress;
import org.opendaylight.atrium.hostservice.api.HostService;
import org.opendaylight.atrium.hostservice.impl.ArpSender;
import org.opendaylight.atrium.hostservice.impl.ConfigReader;
import org.opendaylight.atrium.hostservice.impl.ConfigWriter;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.arp.rev140528.ArpPacketListener;
import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.arp.rev140528.ArpPacketReceived;
import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.arp.rev140528.arp.packet.received.packet.chain.packet.ArpPacket;
import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.PacketChain;
import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.packet.chain.packet.RawPacket;
import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.ethernet.packet.fields.Header8021q;
import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.ethernet.packet.received.packet.chain.packet.EthernetPacket;
import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hostservice.api.rev150725.Addresses;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hostservice.api.rev150725.addresses.Address;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
 * Copyright (c) 2015 Wipro Ltd. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.atrium.hostservice.arp;



public class ArpHandler implements ArpPacketListener {

	private DataBroker dataBroker;
	private ArpSender arpSender;
	private HostService hostService;

	private static final String DEFAULT_CONFIG_FILE = "./configuration/initial/addresses.json";

	private final Logger log = LoggerFactory.getLogger(getClass());

	public ArpHandler(DataBroker dataBroker, PacketProcessingService packetProcessingService, HostService hostService) {
		this.dataBroker = dataBroker;
		this.arpSender = new ArpSender(packetProcessingService);
		this.hostService = hostService;
	}

	public void readConfiguration() {
		log.info("reading configuration");
		
        URL configFileUrl=null;
        try {
                configFileUrl = new File(DEFAULT_CONFIG_FILE).toURI().toURL();
        } catch (Exception ex) {
                log.error("Error reading configuration file " + DEFAULT_CONFIG_FILE);
                return;
        }
        if(configFileUrl == null) {
                return;
        }

		boolean isSuccess = ConfigReader.initialize(configFileUrl);
		if (isSuccess) {
			Addresses arpAddresses = ConfigReader.getArpAddresses();
// Path: hostservice/api/src/main/java/org/opendaylight/atrium/hostservice/api/ArpMessageAddress.java
// @Immutable
// public class ArpMessageAddress {
// 
//     private final MacAddress hwAddress;
//     private final Ipv4Address protocolAddress;
// 
//     public ArpMessageAddress(MacAddress hwAddress, Ipv4Address protocolAddress) {
//         this.hwAddress = checkNotNull(hwAddress);
//         this.protocolAddress = checkNotNull(protocolAddress);
//     }
// 
//     public MacAddress getHardwareAddress() {
//         return hwAddress;
//     }
// 
//     public Ipv4Address getProtocolAddress() {
//         return protocolAddress;
//     }
// 
// }
// 
// Path: hostservice/impl/src/main/java/org/opendaylight/atrium/hostservice/impl/ConfigWriter.java
// public class ConfigWriter {
// 
// 	public static void writeBgpConfigData(DataBroker dataBroker, Addresses arpAddresses) {
// 		WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
// 		transaction.put(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.builder(Addresses.class).build(),
// 				arpAddresses);
// 		transaction.submit();
// 	}
// }


// Path: hostservice/impl/src/main/java/org/opendaylight/atrium/hostservice/arp/ArpHandler.java
import java.net.URL;
import java.util.List;
import java.io.File;
import java.util.concurrent.ExecutionException;
import org.opendaylight.atrium.hostservice.api.ArpMessageAddress;
import org.opendaylight.atrium.hostservice.api.HostService;
import org.opendaylight.atrium.hostservice.impl.ArpSender;
import org.opendaylight.atrium.hostservice.impl.ConfigReader;
import org.opendaylight.atrium.hostservice.impl.ConfigWriter;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.arp.rev140528.ArpPacketListener;
import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.arp.rev140528.ArpPacketReceived;
import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.arp.rev140528.arp.packet.received.packet.chain.packet.ArpPacket;
import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.PacketChain;
import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.packet.chain.packet.RawPacket;
import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.ethernet.packet.fields.Header8021q;
import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.ethernet.packet.received.packet.chain.packet.EthernetPacket;
import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hostservice.api.rev150725.Addresses;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hostservice.api.rev150725.addresses.Address;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * Copyright (c) 2015 Wipro Ltd. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.atrium.hostservice.arp;



public class ArpHandler implements ArpPacketListener {

	private DataBroker dataBroker;
	private ArpSender arpSender;
	private HostService hostService;

	private static final String DEFAULT_CONFIG_FILE = "./configuration/initial/addresses.json";

	private final Logger log = LoggerFactory.getLogger(getClass());

	public ArpHandler(DataBroker dataBroker, PacketProcessingService packetProcessingService, HostService hostService) {
		this.dataBroker = dataBroker;
		this.arpSender = new ArpSender(packetProcessingService);
		this.hostService = hostService;
	}

	public void readConfiguration() {
		log.info("reading configuration");
		
        URL configFileUrl=null;
        try {
                configFileUrl = new File(DEFAULT_CONFIG_FILE).toURI().toURL();
        } catch (Exception ex) {
                log.error("Error reading configuration file " + DEFAULT_CONFIG_FILE);
                return;
        }
        if(configFileUrl == null) {
                return;
        }

		boolean isSuccess = ConfigReader.initialize(configFileUrl);
		if (isSuccess) {
			Addresses arpAddresses = ConfigReader.getArpAddresses();
			ConfigWriter.writeBgpConfigData(dataBroker, arpAddresses);