2 * Copyright (c) 2015 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;
14 import java.util.List;
16 import java.util.Map.Entry;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
19 import org.opendaylight.ovsdb.lib.notation.UUID;
20 import org.opendaylight.ovsdb.lib.operations.Insert;
21 import org.opendaylight.ovsdb.lib.operations.Mutate;
22 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
23 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
24 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
25 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
26 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
27 import org.opendaylight.ovsdb.schema.openvswitch.Port;
28 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
29 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
30 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeInternal;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIds;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import com.google.common.base.Optional;
42 import com.google.common.collect.ImmutableMap;
43 import com.google.common.collect.Sets;
45 public class BridgeUpdateCommand extends AbstractTransactCommand {
47 private static final Logger LOG = LoggerFactory.getLogger(BridgeUpdateCommand.class);
49 public BridgeUpdateCommand(BridgeOperationalState state,
50 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
51 super(state, changes);
57 public void execute(TransactionBuilder transaction) {
58 Map<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> created =
59 TransactUtils.extractCreated(getChanges(),OvsdbBridgeAugmentation.class);
60 for (Entry<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> ovsdbManagedNodeEntry:
62 updateBridge(transaction, ovsdbManagedNodeEntry.getKey(), ovsdbManagedNodeEntry.getValue());
64 Map<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> updated =
65 TransactUtils.extractUpdated(getChanges(),OvsdbBridgeAugmentation.class);
66 for (Entry<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> ovsdbManagedNodeEntry:
68 updateBridge(transaction, ovsdbManagedNodeEntry.getKey(), ovsdbManagedNodeEntry.getValue());
74 private void updateBridge(
75 TransactionBuilder transaction,
76 InstanceIdentifier<OvsdbBridgeAugmentation> iid, OvsdbBridgeAugmentation ovsdbManagedNode) {
77 LOG.debug("Received request to create ovsdb bridge name: {} uuid: {}",
78 ovsdbManagedNode.getBridgeName(),
79 ovsdbManagedNode.getBridgeUuid());
80 Optional<OvsdbBridgeAugmentation> operationalBridgeOptional =
81 getOperationalState().getOvsdbBridgeAugmentation(iid);
82 Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
83 setFailMode(bridge, ovsdbManagedNode);
84 setDataPathType(bridge, ovsdbManagedNode);
85 setOpenDaylightExternalIds(bridge, iid, ovsdbManagedNode);
86 setOpenDaylightOtherConfig(bridge, ovsdbManagedNode);
87 if (!operationalBridgeOptional.isPresent()) {
88 setName(bridge, ovsdbManagedNode,operationalBridgeOptional);
89 setPort(transaction, bridge, ovsdbManagedNode);
90 transaction.add(op.insert(bridge));
92 String existingBridgeName = operationalBridgeOptional.get().getBridgeName().getValue();
93 // Name is immutable, and so we *can't* update it. So we use extraBridge for the schema stuff
94 Bridge extraBridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
95 extraBridge.setName("");
96 transaction.add(op.update(bridge)
97 .where(extraBridge.getNameColumn().getSchema().opEqual(existingBridgeName))
99 stampInstanceIdentifier(transaction, iid.firstIdentifierOf(Node.class),existingBridgeName);
105 private void setDataPathType(Bridge bridge,OvsdbBridgeAugmentation ovsdbManagedNode) {
106 if (ovsdbManagedNode.getDatapathType() != null) {
107 bridge.setDatapathType(SouthboundMapper.createDatapathType(ovsdbManagedNode));
113 private void setName(Bridge bridge, OvsdbBridgeAugmentation ovsdbManagedNode,
114 Optional<OvsdbBridgeAugmentation> operationalBridgeOptional) {
115 if (ovsdbManagedNode.getBridgeName() != null) {
116 bridge.setName(ovsdbManagedNode.getBridgeName().getValue());
117 } else if (operationalBridgeOptional.isPresent() && operationalBridgeOptional.get().getBridgeName() != null) {
118 bridge.setName(operationalBridgeOptional.get().getBridgeName().getValue());
124 private void setOpenDaylightExternalIds(Bridge bridge, InstanceIdentifier<OvsdbBridgeAugmentation> iid,
125 OvsdbBridgeAugmentation ovsdbManagedNode) {
126 // Set the iid external_id
127 Map<String, String> externalIdMap = new HashMap<>();
128 externalIdMap.put(SouthboundConstants.IID_EXTERNAL_ID_KEY, SouthboundUtil.serializeInstanceIdentifier(iid));
129 // Set user provided external ids
130 List<BridgeExternalIds> bridgeExternalId = ovsdbManagedNode.getBridgeExternalIds();
131 if (bridgeExternalId != null) {
132 for (BridgeExternalIds externalId : bridgeExternalId) {
133 externalIdMap.put(externalId.getBridgeExternalIdKey(), externalId.getBridgeExternalIdValue());
137 bridge.setExternalIds(ImmutableMap.copyOf(externalIdMap));
138 } catch (NullPointerException e) {
139 LOG.warn("Incomplete bridge external Id", e);
145 private void setOpenDaylightOtherConfig(Bridge bridge, OvsdbBridgeAugmentation ovsdbManagedNode) {
146 List<BridgeOtherConfigs> bridgeOtherConfig = ovsdbManagedNode.getBridgeOtherConfigs();
147 if (bridgeOtherConfig != null) {
148 Map<String, String> otherConfigMap = new HashMap<>();
149 for (BridgeOtherConfigs otherConf : bridgeOtherConfig) {
150 otherConfigMap.put(otherConf.getBridgeOtherConfigKey(), otherConf.getBridgeOtherConfigValue());
153 bridge.setOtherConfig(ImmutableMap.copyOf(otherConfigMap));
154 } catch (NullPointerException e) {
155 LOG.warn("Incomplete bridge other config", e);
162 private void setPort(TransactionBuilder transaction, Bridge bridge,
163 OvsdbBridgeAugmentation ovsdbManagedNode) {
165 Insert<GenericTableSchema> interfaceInsert = setInterface(transaction,ovsdbManagedNode);
167 String portNamedUuid = "Port_" + SouthboundMapper.getRandomUUID();
168 Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
169 port.setName(ovsdbManagedNode.getBridgeName().getValue());
170 port.setInterfaces(Sets.newHashSet(TransactUtils.extractNamedUuid(interfaceInsert)));
171 transaction.add(op.insert(port).withId(portNamedUuid));
172 bridge.setPorts(Sets.newHashSet(new UUID(portNamedUuid)));
175 private Insert<GenericTableSchema> setInterface(TransactionBuilder transaction,
176 OvsdbBridgeAugmentation ovsdbManagedNode) {
178 String interfaceNamedUuid = "Interface_" + SouthboundMapper.getRandomUUID();
179 Interface interfaceOvs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
180 interfaceOvs.setName(ovsdbManagedNode.getBridgeName().getValue());
181 interfaceOvs.setType(SouthboundMapper.createOvsdbInterfaceType(InterfaceTypeInternal.class));
182 Insert<GenericTableSchema> result = op.insert(interfaceOvs).withId(interfaceNamedUuid);
183 transaction.add(result);
189 private void setFailMode(Bridge bridge,
190 OvsdbBridgeAugmentation ovsdbManagedNode) {
191 if (ovsdbManagedNode.getFailMode() != null
192 && SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode()) != null ) {
193 bridge.setFailMode(Sets.newHashSet(
194 SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode())));
198 private void stampInstanceIdentifier(TransactionBuilder transaction,InstanceIdentifier<Node> iid,
200 Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
201 bridge.setName(bridgeName);
202 bridge.setExternalIds(Collections.<String,String>emptyMap());
203 Mutate mutate = TransactUtils.stampInstanceIdentifierMutation(transaction,
206 bridge.getExternalIdsColumn().getSchema());
207 transaction.add(mutate
208 .where(bridge.getNameColumn().getSchema().opEqual(bridgeName))