/* * Copyright (c) 2015 Inocybe 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.transactions.md; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.NoSuchElementException; import java.util.Set; 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.ovsdb.lib.error.SchemaVersionMismatchException; import org.opendaylight.ovsdb.lib.message.TableUpdates; import org.opendaylight.ovsdb.lib.notation.UUID; import org.opendaylight.ovsdb.lib.schema.DatabaseSchema; import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils; import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch; import org.opendaylight.ovsdb.southbound.SouthboundMapper; 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.OvsdbNodeAugmentationBuilder; 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.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.DatapathTypeEntry; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.DatapathTypeEntryBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntry; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntryBuilder; 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.OpenvswitchExternalIdsBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigsBuilder; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Optional; public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand { private static final Logger LOG = LoggerFactory .getLogger(OpenVSwitchUpdateCommand.class); public OpenVSwitchUpdateCommand(ConnectionInfo key, TableUpdates updates, DatabaseSchema dbSchema) { super(key, updates, dbSchema); } @Override public void execute(ReadWriteTransaction transaction) { Map updatedOpenVSwitchRows = TyperUtils .extractRowsUpdated(OpenVSwitch.class, getUpdates(), getDbSchema()); for (Entry entry : updatedOpenVSwitchRows.entrySet()) { OpenVSwitch openVSwitch = entry.getValue(); final InstanceIdentifier nodePath = SouthboundMapper.createInstanceIdentifier(getConnectionInfo()); Optional node = Optional.absent(); try { node = transaction.read(LogicalDatastoreType.OPERATIONAL, nodePath).checkedGet(); } catch (final ReadFailedException e) { LOG.debug("Read Operational/DS for Node fail! {}", nodePath, e); } if (node.isPresent()) { LOG.debug("Node {} is present", node); OvsdbNodeAugmentation ovsdbNode = SouthboundMapper .createOvsdbAugmentation(getConnectionInfo()); OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder(); try { ovsdbNodeBuilder.setOvsVersion(openVSwitch.getOvsVersionColumn().getData().iterator().next()); } catch (NoSuchElementException e) { LOG.debug("ovs_version is not set for this switch",e); } try { Set dptypes = openVSwitch.getDatapathTypesColumn() .getData(); List dpEntryList = new ArrayList(); for (String dpType : dptypes) { DatapathTypeEntry dpEntry = new DatapathTypeEntryBuilder() .setDatapathType( SouthboundMapper.createDatapathType(dpType)) .build(); dpEntryList.add(dpEntry); } ovsdbNodeBuilder.setDatapathTypeEntry(dpEntryList); } catch (SchemaVersionMismatchException e) { LOG.debug("Datapath types not supported by this version of ovsdb",e); } try { Set iftypes = openVSwitch.getIfaceTypesColumn().getData(); List ifEntryList = new ArrayList(); for (String ifType : iftypes) { InterfaceTypeEntry ifEntry = new InterfaceTypeEntryBuilder() .setInterfaceType( SouthboundMapper.createInterfaceType(ifType)) .build(); ifEntryList.add(ifEntry); } ovsdbNodeBuilder.setInterfaceTypeEntry(ifEntryList); } catch (SchemaVersionMismatchException e) { LOG.debug("Iface types not supported by this version of ovsdb",e);; } Map externalIds = openVSwitch.getExternalIdsColumn().getData(); if (externalIds != null && !externalIds.isEmpty()) { Set externalIdKeys = externalIds.keySet(); List externalIdsList = new ArrayList(); String externalIdValue; for (String externalIdKey : externalIdKeys) { externalIdValue = externalIds.get(externalIdKey); if (externalIdKey != null && externalIdValue != null) { externalIdsList.add(new OpenvswitchExternalIdsBuilder() .setExternalIdKey(externalIdKey) .setExternalIdValue(externalIdValue) .build()); } } ovsdbNodeBuilder.setOpenvswitchExternalIds(externalIdsList); } Map otherConfigs = openVSwitch.getOtherConfigColumn().getData(); if (otherConfigs != null && !otherConfigs.isEmpty()) { Set otherConfigKeys = otherConfigs.keySet(); List otherConfigsList = new ArrayList(); String otherConfigValue; for (String otherConfigKey : otherConfigKeys) { otherConfigValue = otherConfigs.get(otherConfigKey); if (otherConfigKey != null && otherConfigValue != null) { otherConfigsList.add(new OpenvswitchOtherConfigsBuilder() .setOtherConfigKey(otherConfigKey) .setOtherConfigValue(otherConfigValue) .build()); } } ovsdbNodeBuilder.setOpenvswitchOtherConfigs(otherConfigsList); } NodeBuilder nodeBuilder = new NodeBuilder(); ConnectionInfo connectionInfo = ovsdbNode.getConnectionInfo(); nodeBuilder.setNodeId(SouthboundMapper.createNodeId( connectionInfo.getRemoteIp(), connectionInfo.getRemotePort())); nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, ovsdbNodeBuilder.build()); transaction.merge(LogicalDatastoreType.OPERATIONAL, nodePath, nodeBuilder.build()); } } } }