2 * Copyright (c) 2015, 2016 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.ovsdb.southbound.ovsdb.transact;
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
12 import java.util.Collections;
13 import java.util.HashMap;
15 import java.util.Map.Entry;
17 import javax.annotation.Nonnull;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
20 import org.opendaylight.ovsdb.lib.notation.UUID;
21 import org.opendaylight.ovsdb.lib.operations.Insert;
22 import org.opendaylight.ovsdb.lib.operations.Mutate;
23 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
24 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
25 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
26 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
27 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
28 import org.opendaylight.ovsdb.schema.openvswitch.Port;
29 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
30 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
31 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
32 import org.opendaylight.ovsdb.utils.yang.YangUtils;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeInternal;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIds;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.google.common.base.Optional;
44 import com.google.common.collect.Sets;
46 public class BridgeUpdateCommand extends AbstractTransactCommand {
48 private static final Logger LOG = LoggerFactory.getLogger(BridgeUpdateCommand.class);
50 public BridgeUpdateCommand(BridgeOperationalState state,
51 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
52 super(state, changes);
58 public void execute(TransactionBuilder transaction) {
59 Map<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> created =
60 TransactUtils.extractCreated(getChanges(),OvsdbBridgeAugmentation.class);
61 for (Entry<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> ovsdbManagedNodeEntry:
63 updateBridge(transaction, ovsdbManagedNodeEntry.getKey(), ovsdbManagedNodeEntry.getValue());
65 Map<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> updated =
66 TransactUtils.extractUpdated(getChanges(),OvsdbBridgeAugmentation.class);
67 for (Entry<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> ovsdbManagedNodeEntry:
69 updateBridge(transaction, ovsdbManagedNodeEntry.getKey(), ovsdbManagedNodeEntry.getValue());
75 private void updateBridge(
76 TransactionBuilder transaction,
77 InstanceIdentifier<OvsdbBridgeAugmentation> iid, OvsdbBridgeAugmentation ovsdbManagedNode) {
78 LOG.debug("Received request to create ovsdb bridge name: {} uuid: {}",
79 ovsdbManagedNode.getBridgeName(),
80 ovsdbManagedNode.getBridgeUuid());
81 Optional<OvsdbBridgeAugmentation> operationalBridgeOptional =
82 getOperationalState().getOvsdbBridgeAugmentation(iid);
83 Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
84 setFailMode(bridge, ovsdbManagedNode);
85 setDataPathType(bridge, ovsdbManagedNode);
86 setOpenDaylightExternalIds(bridge, iid, ovsdbManagedNode);
87 setOpenDaylightOtherConfig(bridge, ovsdbManagedNode);
88 if (!operationalBridgeOptional.isPresent()) {
89 setName(bridge, ovsdbManagedNode,operationalBridgeOptional);
90 setPort(transaction, bridge, ovsdbManagedNode);
91 transaction.add(op.insert(bridge));
93 String existingBridgeName = operationalBridgeOptional.get().getBridgeName().getValue();
94 // Name is immutable, and so we *can't* update it. So we use extraBridge for the schema stuff
95 Bridge extraBridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
96 extraBridge.setName("");
97 transaction.add(op.update(bridge)
98 .where(extraBridge.getNameColumn().getSchema().opEqual(existingBridgeName))
100 stampInstanceIdentifier(transaction, iid.firstIdentifierOf(Node.class),existingBridgeName);
106 private void setDataPathType(Bridge bridge,OvsdbBridgeAugmentation ovsdbManagedNode) {
107 if (ovsdbManagedNode.getDatapathType() != null) {
108 bridge.setDatapathType(SouthboundMapper.createDatapathType(ovsdbManagedNode));
114 private void setName(Bridge bridge, OvsdbBridgeAugmentation ovsdbManagedNode,
115 Optional<OvsdbBridgeAugmentation> operationalBridgeOptional) {
116 if (ovsdbManagedNode.getBridgeName() != null) {
117 bridge.setName(ovsdbManagedNode.getBridgeName().getValue());
118 } else if (operationalBridgeOptional.isPresent() && operationalBridgeOptional.get().getBridgeName() != null) {
119 bridge.setName(operationalBridgeOptional.get().getBridgeName().getValue());
125 private void setOpenDaylightExternalIds(Bridge bridge, InstanceIdentifier<OvsdbBridgeAugmentation> iid,
126 OvsdbBridgeAugmentation ovsdbManagedNode) {
127 // Set the iid external_id
128 Map<String, String> externalIdMap = new HashMap<>();
129 externalIdMap.put(SouthboundConstants.IID_EXTERNAL_ID_KEY, SouthboundUtil.serializeInstanceIdentifier(iid));
130 // Set user provided external ids
132 YangUtils.copyYangKeyValueListToMap(externalIdMap, ovsdbManagedNode.getBridgeExternalIds(),
133 BridgeExternalIds::getBridgeExternalIdKey, BridgeExternalIds::getBridgeExternalIdValue);
134 } catch (NullPointerException e) {
135 LOG.warn("Incomplete bridge external Id", e);
137 bridge.setExternalIds(externalIdMap);
142 private void setOpenDaylightOtherConfig(@Nonnull Bridge bridge, @Nonnull OvsdbBridgeAugmentation ovsdbManagedNode) {
144 bridge.setOtherConfig(YangUtils.convertYangKeyValueListToMap(ovsdbManagedNode.getBridgeOtherConfigs(),
145 BridgeOtherConfigs::getBridgeOtherConfigKey, BridgeOtherConfigs::getBridgeOtherConfigValue));
146 } catch (NullPointerException e) {
147 LOG.warn("Incomplete bridge other config", e);
153 private void setPort(TransactionBuilder transaction, Bridge bridge,
154 OvsdbBridgeAugmentation ovsdbManagedNode) {
156 Insert<GenericTableSchema> interfaceInsert = setInterface(transaction,ovsdbManagedNode);
158 String portNamedUuid = "Port_" + SouthboundMapper.getRandomUUID();
159 Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
160 port.setName(ovsdbManagedNode.getBridgeName().getValue());
161 port.setInterfaces(Sets.newHashSet(TransactUtils.extractNamedUuid(interfaceInsert)));
162 transaction.add(op.insert(port).withId(portNamedUuid));
163 bridge.setPorts(Sets.newHashSet(new UUID(portNamedUuid)));
166 private Insert<GenericTableSchema> setInterface(TransactionBuilder transaction,
167 OvsdbBridgeAugmentation ovsdbManagedNode) {
169 String interfaceNamedUuid = "Interface_" + SouthboundMapper.getRandomUUID();
170 Interface interfaceOvs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
171 interfaceOvs.setName(ovsdbManagedNode.getBridgeName().getValue());
172 interfaceOvs.setType(SouthboundMapper.createOvsdbInterfaceType(InterfaceTypeInternal.class));
173 Insert<GenericTableSchema> result = op.insert(interfaceOvs).withId(interfaceNamedUuid);
174 transaction.add(result);
180 private void setFailMode(Bridge bridge,
181 OvsdbBridgeAugmentation ovsdbManagedNode) {
182 if (ovsdbManagedNode.getFailMode() != null
183 && SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode()) != null ) {
184 bridge.setFailMode(Sets.newHashSet(
185 SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode())));
189 private void stampInstanceIdentifier(TransactionBuilder transaction,InstanceIdentifier<Node> iid,
191 Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
192 bridge.setName(bridgeName);
193 bridge.setExternalIds(Collections.<String,String>emptyMap());
194 Mutate mutate = TransactUtils.stampInstanceIdentifierMutation(transaction,
197 bridge.getExternalIdsColumn().getSchema());
198 transaction.add(mutate
199 .where(bridge.getNameColumn().getSchema().opEqual(bridgeName))