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.HashMap;
13 import java.util.List;
15 import java.util.Map.Entry;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
18 import org.opendaylight.ovsdb.lib.notation.UUID;
19 import org.opendaylight.ovsdb.lib.operations.Insert;
20 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
21 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
22 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
23 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
24 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
25 import org.opendaylight.ovsdb.schema.openvswitch.Port;
26 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
27 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
28 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeInternal;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIds;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.common.base.Optional;
39 import com.google.common.collect.ImmutableMap;
40 import com.google.common.collect.Sets;
42 public class BridgeUpdateCommand extends AbstractTransactCommand {
44 private static final Logger LOG = LoggerFactory.getLogger(BridgeUpdateCommand.class);
46 public BridgeUpdateCommand(BridgeOperationalState state,
47 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
48 super(state, changes);
54 public void execute(TransactionBuilder transaction) {
55 Map<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> created =
56 TransactUtils.extractCreated(getChanges(),OvsdbBridgeAugmentation.class);
57 for (Entry<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> ovsdbManagedNodeEntry:
59 updateBridge(transaction, ovsdbManagedNodeEntry.getKey(), ovsdbManagedNodeEntry.getValue());
65 private void updateBridge(
66 TransactionBuilder transaction,
67 InstanceIdentifier<OvsdbBridgeAugmentation> iid, OvsdbBridgeAugmentation ovsdbManagedNode) {
68 LOG.debug("Received request to create ovsdb bridge name: {} uuid: {}",
69 ovsdbManagedNode.getBridgeName(),
70 ovsdbManagedNode.getBridgeUuid());
71 Optional<OvsdbBridgeAugmentation> operationalBridgeOptional =
72 getOperationalState().getOvsdbBridgeAugmentation(iid);
73 Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
74 setFailMode(bridge, ovsdbManagedNode);
75 setDataPathType(bridge, ovsdbManagedNode);
76 setOpenDaylightExternalIds(bridge, iid, ovsdbManagedNode);
77 setOpenDaylightOtherConfig(bridge, ovsdbManagedNode);
78 if (!operationalBridgeOptional.isPresent()) {
79 setName(bridge, ovsdbManagedNode,operationalBridgeOptional);
80 setPort(transaction, bridge, ovsdbManagedNode);
81 transaction.add(op.insert(bridge));
82 } else if (bridge.getName() != null) {
83 // Name is immutable, and so we *can't* update it. So we use extraBridge for the schema stuff
84 Bridge extraBridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
85 extraBridge.setName("");
86 transaction.add(op.update(bridge)
87 .where(extraBridge.getNameColumn().getSchema().opEqual(bridge.getName()))
94 private void setDataPathType(Bridge bridge,OvsdbBridgeAugmentation ovsdbManagedNode) {
95 if (ovsdbManagedNode.getDatapathType() != null) {
96 bridge.setDatapathType(SouthboundMapper.createDatapathType(ovsdbManagedNode));
102 private void setName(Bridge bridge, OvsdbBridgeAugmentation ovsdbManagedNode,
103 Optional<OvsdbBridgeAugmentation> operationalBridgeOptional) {
104 if (ovsdbManagedNode.getBridgeName() != null) {
105 bridge.setName(ovsdbManagedNode.getBridgeName().getValue());
106 } else if (operationalBridgeOptional.isPresent() && operationalBridgeOptional.get().getBridgeName() != null) {
107 bridge.setName(operationalBridgeOptional.get().getBridgeName().getValue());
113 private void setOpenDaylightExternalIds(Bridge bridge, InstanceIdentifier<OvsdbBridgeAugmentation> iid,
114 OvsdbBridgeAugmentation ovsdbManagedNode) {
115 // Set the iid external_id
116 Map<String, String> externalIdMap = new HashMap<String, String>();
117 externalIdMap.put(SouthboundConstants.IID_EXTERNAL_ID_KEY, SouthboundUtil.serializeInstanceIdentifier(iid));
118 // Set user provided external ids
119 List<BridgeExternalIds> bridgeExternalId = ovsdbManagedNode.getBridgeExternalIds();
120 if (bridgeExternalId != null) {
121 for (BridgeExternalIds externalId : bridgeExternalId) {
122 externalIdMap.put(externalId.getBridgeExternalIdKey(), externalId.getBridgeExternalIdValue());
126 bridge.setExternalIds(ImmutableMap.copyOf(externalIdMap));
127 } catch (NullPointerException e) {
128 LOG.warn("Incomplete bridge external Id", e);
134 private void setOpenDaylightOtherConfig(Bridge bridge, OvsdbBridgeAugmentation ovsdbManagedNode) {
135 List<BridgeOtherConfigs> bridgeOtherConfig = ovsdbManagedNode.getBridgeOtherConfigs();
136 if (bridgeOtherConfig != null) {
137 HashMap<String, String> otherConfigMap = new HashMap<String, String>();
138 for (BridgeOtherConfigs otherConf : bridgeOtherConfig) {
139 otherConfigMap.put(otherConf.getBridgeOtherConfigKey(), otherConf.getBridgeOtherConfigValue());
142 bridge.setOtherConfig(ImmutableMap.copyOf(otherConfigMap));
143 } catch (NullPointerException e) {
144 LOG.warn("Incomplete bridge other config");
151 private void setPort(TransactionBuilder transaction, Bridge bridge,
152 OvsdbBridgeAugmentation ovsdbManagedNode) {
154 Insert<GenericTableSchema> interfaceInsert = setInterface(transaction,ovsdbManagedNode);
156 String portNamedUuid = "Port_" + SouthboundMapper.getRandomUUID();
157 Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
158 port.setName(ovsdbManagedNode.getBridgeName().getValue());
159 port.setInterfaces(Sets.newHashSet(TransactUtils.extractNamedUuid(interfaceInsert)));
160 transaction.add(op.insert(port).withId(portNamedUuid));
161 bridge.setPorts(Sets.newHashSet(new UUID(portNamedUuid)));
164 private Insert<GenericTableSchema> setInterface(TransactionBuilder transaction,
165 OvsdbBridgeAugmentation ovsdbManagedNode) {
167 String interfaceNamedUuid = "Interface_" + SouthboundMapper.getRandomUUID();
168 Interface interfaceOvs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
169 interfaceOvs.setName(ovsdbManagedNode.getBridgeName().getValue());
170 interfaceOvs.setType(SouthboundMapper.createOvsdbInterfaceType(InterfaceTypeInternal.class));
171 Insert<GenericTableSchema> result = op.insert(interfaceOvs).withId(interfaceNamedUuid);
172 transaction.add(result);
178 private void setFailMode(Bridge bridge,
179 OvsdbBridgeAugmentation ovsdbManagedNode) {
180 if (ovsdbManagedNode.getFailMode() != null
181 && SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode()) != null ) {
182 bridge.setFailMode(Sets.newHashSet(
183 SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode())));