/*
 * Copyright (c) 2014 Cisco Systems, Inc. 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.ovsdb.southbound;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.CheckedFuture;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAttributes;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SouthboundUtil {

    private static final Logger LOG = LoggerFactory.getLogger(SouthboundUtil.class);

    private static InstanceIdentifierCodec instanceIdentifierCodec;

    private SouthboundUtil() {
        // Prevent instantiating a utility class
    }

    public static void setInstanceIdentifierCodec(InstanceIdentifierCodec iidc) {
        instanceIdentifierCodec = iidc;
    }

    public static InstanceIdentifierCodec getInstanceIdentifierCodec() {
        return instanceIdentifierCodec;
    }

    public static String serializeInstanceIdentifier(InstanceIdentifier<?> iid) {
        return instanceIdentifierCodec.serialize(iid);
    }

    public static InstanceIdentifier<?> deserializeInstanceIdentifier(String iidString) {
        InstanceIdentifier<?> result = null;
        try {
            result = instanceIdentifierCodec.bindingDeserializer(iidString);
        } catch (DeserializationException e) {
            LOG.warn("Unable to deserialize iidString", e);
        }
        return result;
    }


    public static Optional<OvsdbNodeAugmentation> getManagingNode(DataBroker db, OvsdbBridgeAttributes mn) {
        Preconditions.checkNotNull(mn);
        try {
            OvsdbNodeRef ref = mn.getManagedBy();
            if (ref != null && ref.getValue() != null) {
                ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
                @SuppressWarnings("unchecked") // Note: erasure makes this safe in combination with the typecheck below
                InstanceIdentifier<Node> path = (InstanceIdentifier<Node>) ref.getValue();

                CheckedFuture<Optional<Node>, ReadFailedException> nf = transaction.read(
                        LogicalDatastoreType.OPERATIONAL, path);
                transaction.close();
                Optional<Node> optional = nf.get();
                if (optional != null && optional.isPresent()) {
                    OvsdbNodeAugmentation ovsdbNode = null;
                    Node node = optional.get();
                    if (node instanceof OvsdbNodeAugmentation) {
                        ovsdbNode = (OvsdbNodeAugmentation) node;
                    } else if (node != null) {
                        ovsdbNode = node.getAugmentation(OvsdbNodeAugmentation.class);
                    }
                    if (ovsdbNode != null) {
                        return Optional.of(ovsdbNode);
                    } else {
                        LOG.warn("OvsdbManagedNode {} claims to be managed by {} but "
                                + "that OvsdbNode does not exist", mn, ref.getValue());
                        return Optional.absent();
                    }
                } else {
                    LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}", optional);
                    return Optional.absent();
                }
            } else {
                LOG.warn("Cannot find client for OvsdbManagedNode without a specified ManagedBy {}", mn);
                return Optional.absent();
            }
        } catch (Exception e) {
            LOG.warn("Failed to get OvsdbNode that manages OvsdbManagedNode {}", mn, e);
            return Optional.absent();
        }
    }

    public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> Optional<D> readNode(
            ReadWriteTransaction transaction, final InstanceIdentifier<D> connectionIid) {
        Optional<D> node = Optional.absent();
        try {
            node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet();
        } catch (final ReadFailedException e) {
            LOG.warn("Read Operational/DS for Node failed! {}", connectionIid, e);
        }
        return node;
    }

    public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean deleteNode(
            ReadWriteTransaction transaction, final InstanceIdentifier<D> connectionIid) {
        boolean result = false;
        transaction.delete(LogicalDatastoreType.OPERATIONAL, connectionIid);
        CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
        try {
            future.checkedGet();
            result = true;
        } catch (TransactionCommitFailedException e) {
            LOG.warn("Failed to delete {} ", connectionIid, e);
        }
        return result;
    }

    private static String getLocalControllerHostIpAddress() {
        String ipaddress = null;
        try {
            for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
                 ifaces.hasMoreElements();) {
                NetworkInterface iface = ifaces.nextElement();

                for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
                    InetAddress inetAddr = inetAddrs.nextElement();
                    if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
                        ipaddress = inetAddr.getHostAddress();
                        break;
                    }
                }
            }
        } catch (Exception e) {
            LOG.warn("Exception while fetching local host ip address ",e);
        }
        return ipaddress;
    }

    public static String getControllerTarget(Node ovsdbNode) {
        String target = null;
        String ipAddr = null;
        OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
        ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
        LOG.info("connectionInfo: {}", connectionInfo);
        if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
            ipAddr = String.valueOf(connectionInfo.getLocalIp().getValue());
        }
        if (ipAddr == null) {
            ipAddr = getLocalControllerHostIpAddress();
        }

        if (ipAddr != null) {
            target = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
                    + ipAddr + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;
        }

        return target;
    }

    public static String connectionInfoToString(final ConnectionInfo connectionInfo) {
        return String.valueOf(
                connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
    }
}

@@ -961,100 +961,8 @@
 on;%0A
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;%0A
 impo
@@ -5497,635 +5497,8 @@
  %7D%0A%0A
-    public static %3CD extends org.opendaylight.yangtools.yang.binding.DataObject%3E boolean deleteNode(%0A            ReadWriteTransaction transaction, final InstanceIdentifier%3CD%3E connectionIid) %7B%0A        boolean result = false;%0A        transaction.delete(LogicalDatastoreType.OPERATIONAL, connectionIid);%0A        CheckedFuture%3CVoid, TransactionCommitFailedException%3E future = transaction.submit();%0A        try %7B%0A            future.checkedGet();%0A            result = true;%0A        %7D catch (TransactionCommitFailedException e) %7B%0A            LOG.warn(%22Failed to delete %7B%7D %22, connectionIid, e);%0A        %7D%0A        return result;%0A    %7D%0A%0A
     

Remove SouthboundUtil::deleteNode()