<commit_before>/*
 * Copyright (c) 2015 Intel Corporation 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.ovsdb.transact;

import static org.opendaylight.ovsdb.lib.operations.Operations.op;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
import org.opendaylight.ovsdb.lib.notation.Mutator;
import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
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.ovsdb.node.attributes.OpenvswitchExternalIds;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableMap;

public class OvsdbNodeUpdateCommand implements TransactCommand {
    private AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes;
    private static final Logger LOG = LoggerFactory.getLogger(OvsdbNodeUpdateCommand.class);


    public OvsdbNodeUpdateCommand(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
        this.changes = changes;
    }

    @Override
    public void execute(TransactionBuilder transaction) {
        Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> updated =
                TransactUtils.extractUpdated(changes, OvsdbNodeAugmentation.class);
        for (Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> ovsdbNodeEntry:
            updated.entrySet()) {
            OvsdbNodeAugmentation ovsdbNode = ovsdbNodeEntry.getValue();
            LOG.debug("Received request to update ovsdb node ip: {} port: {}",
                        ovsdbNode.getConnectionInfo().getRemoteIp(),
                        ovsdbNode.getConnectionInfo().getRemotePort());

            // OpenVSwitchPart
            OpenVSwitch ovs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), OpenVSwitch.class);

            List<OpenvswitchExternalIds> externalIds = ovsdbNode.getOpenvswitchExternalIds();
            if (externalIds != null) {
                HashMap<String, String> externalIdsMap = new HashMap<String, String>();
                for (OpenvswitchExternalIds externalId : externalIds) {
                    externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
                }
                try {
                    ovs.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
                    transaction.add(op.mutate(ovs).addMutation(ovs.getExternalIdsColumn().getSchema(),
                        Mutator.INSERT,
                        ovs.getExternalIdsColumn().getData()));
                } catch (NullPointerException e) {
                    LOG.warn("Incomplete OVSDB Node external IDs");
                }
            }

            List<OpenvswitchOtherConfigs> otherConfigs = ovsdbNode.getOpenvswitchOtherConfigs();
            if (otherConfigs != null) {
                HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
                for (OpenvswitchOtherConfigs otherConfig : otherConfigs) {
                    otherConfigsMap.put(otherConfig.getOtherConfigKey(), otherConfig.getOtherConfigValue());
                }
                try {
                    ovs.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
                    transaction.add(op.mutate(ovs).addMutation(ovs.getOtherConfigColumn().getSchema(),
                        Mutator.INSERT,
                        ovs.getOtherConfigColumn().getData()));
                } catch (NullPointerException e) {
                    LOG.warn("Incomplete OVSDB Node other_config", e);
                }
            }
        }
    }
}<commit_msg>Write iid of ovsdb-node to OpenVSwitch.externalIds<commit_after>/*
 * Copyright (c) 2015 Intel Corporation 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.ovsdb.transact;

import static org.opendaylight.ovsdb.lib.operations.Operations.op;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
import org.opendaylight.ovsdb.lib.notation.Mutation;
import org.opendaylight.ovsdb.lib.notation.Mutator;
import org.opendaylight.ovsdb.lib.notation.OvsdbSet;
import org.opendaylight.ovsdb.lib.operations.Mutate;
import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
import org.opendaylight.ovsdb.southbound.SouthboundConstants;
import org.opendaylight.ovsdb.southbound.SouthboundUtil;
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.ovsdb.node.attributes.OpenvswitchExternalIds;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

public class OvsdbNodeUpdateCommand implements TransactCommand {
    private AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes;
    private static final Logger LOG = LoggerFactory.getLogger(OvsdbNodeUpdateCommand.class);


    public OvsdbNodeUpdateCommand(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
        this.changes = changes;
    }

    @Override
    public void execute(TransactionBuilder transaction) {
        Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> updated =
                TransactUtils.extractCreatedOrUpdated(changes, OvsdbNodeAugmentation.class);
        for (Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> ovsdbNodeEntry:
            updated.entrySet()) {
            OvsdbNodeAugmentation ovsdbNode = ovsdbNodeEntry.getValue();
            LOG.debug("Received request to update ovsdb node ip: {} port: {}",
                        ovsdbNode.getConnectionInfo().getRemoteIp(),
                        ovsdbNode.getConnectionInfo().getRemotePort());

            // OpenVSwitchPart
            OpenVSwitch ovs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), OpenVSwitch.class);
            HashMap<String, String> externalIdsMap = new HashMap<String, String>();

            List<OpenvswitchExternalIds> externalIds = ovsdbNode.getOpenvswitchExternalIds();

            if (externalIds != null) {
                for (OpenvswitchExternalIds externalId : externalIds) {
                    externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
                }
            }

            externalIdsMap.put(SouthboundConstants.IID_EXTERNAL_ID_KEY,
                    SouthboundUtil.serializeInstanceIdentifier(ovsdbNodeEntry.getKey()));
            try {
                ovs.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
                Mutate<GenericTableSchema> mutate = op.mutate(ovs)
                            .addMutation(ovs.getExternalIdsColumn().getSchema(),
                                Mutator.INSERT,
                                ovs.getExternalIdsColumn().getData());
                Mutation deleteIidMutation = new Mutation(ovs.getExternalIdsColumn().getSchema().getName(),
                        Mutator.DELETE,
                        OvsdbSet.fromSet(Sets.newHashSet(SouthboundConstants.IID_EXTERNAL_ID_KEY)));
                List<Mutation> mutations = Lists.newArrayList(Sets.newHashSet(deleteIidMutation));
                mutations.addAll(mutate.getMutations());
                mutate.setMutations(mutations);
                transaction.add(mutate);
            } catch (NullPointerException e) {
                LOG.warn("Incomplete OVSDB Node external IDs");
            }

            List<OpenvswitchOtherConfigs> otherConfigs = ovsdbNode.getOpenvswitchOtherConfigs();
            if (otherConfigs != null) {
                HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
                for (OpenvswitchOtherConfigs otherConfig : otherConfigs) {
                    otherConfigsMap.put(otherConfig.getOtherConfigKey(), otherConfig.getOtherConfigValue());
                }
                try {
                    ovs.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
                    transaction.add(op.mutate(ovs).addMutation(ovs.getOtherConfigColumn().getSchema(),
                        Mutator.INSERT,
                        ovs.getOtherConfigColumn().getData()));
                } catch (NullPointerException e) {
                    LOG.warn("Incomplete OVSDB Node other_config", e);
                }
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.09498314495340075, 'fraction_numerical': 0.006147134642078128, 'mean_word_length': 2.974389282899921, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6446169', 'n_tokens_mistral': 2940, 'n_tokens_neox': 2811, 'n_words': 424}