2 * Copyright © 2015, 2017 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.Collection;
13 import java.util.Collections;
14 import java.util.HashMap;
16 import java.util.Map.Entry;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.mdsal.binding.api.DataTreeModification;
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.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.InstanceIdentifierCodec;
29 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
30 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
31 import org.opendaylight.ovsdb.utils.yang.YangUtils;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeInternal;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIds;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class BridgeUpdateCommand implements TransactCommand {
43 private static final Logger LOG = LoggerFactory.getLogger(BridgeUpdateCommand.class);
46 public void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
47 final DataChangeEvent events, final InstanceIdentifierCodec instanceIdentifierCodec) {
48 execute(transaction, state, TransactUtils.extractCreatedOrUpdated(events, OvsdbBridgeAugmentation.class),
49 instanceIdentifierCodec);
53 public void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
54 final Collection<DataTreeModification<Node>> modifications,
55 final InstanceIdentifierCodec instanceIdentifierCodec) {
56 execute(transaction, state,
57 TransactUtils.extractCreatedOrUpdated(modifications, OvsdbBridgeAugmentation.class),
58 instanceIdentifierCodec);
61 private static void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
62 final Map<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> createdOrUpdated,
63 final InstanceIdentifierCodec instanceIdentifierCodec) {
64 for (Entry<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> ovsdbManagedNodeEntry :
65 createdOrUpdated.entrySet()) {
66 updateBridge(transaction, state, ovsdbManagedNodeEntry.getKey(), ovsdbManagedNodeEntry.getValue(),
67 instanceIdentifierCodec);
71 private static void updateBridge(final TransactionBuilder transaction, final BridgeOperationalState state,
72 final InstanceIdentifier<OvsdbBridgeAugmentation> iid, final OvsdbBridgeAugmentation ovsdbManagedNode,
73 final InstanceIdentifierCodec instanceIdentifierCodec) {
74 LOG.debug("Received request to create ovsdb bridge name: {} uuid: {}",
75 ovsdbManagedNode.getBridgeName(),
76 ovsdbManagedNode.getBridgeUuid());
77 Bridge bridge = transaction.getTypedRowWrapper(Bridge.class);
78 setFailMode(bridge, ovsdbManagedNode);
79 setDataPathType(bridge, ovsdbManagedNode);
80 setStpEnalbe(bridge, ovsdbManagedNode);
81 setOpenDaylightExternalIds(bridge, iid, ovsdbManagedNode, instanceIdentifierCodec);
82 setOpenDaylightOtherConfig(bridge, ovsdbManagedNode);
83 Optional<OvsdbBridgeAugmentation> operationalBridgeOptional =
84 state.getOvsdbBridgeAugmentation(iid);
85 if (!operationalBridgeOptional.isPresent()) {
86 setName(bridge, ovsdbManagedNode,operationalBridgeOptional);
87 setPort(transaction, bridge, ovsdbManagedNode);
88 transaction.add(op.insert(bridge));
89 LOG.info("Added ovsdb Bridge name: {} uuid: {}",
90 ovsdbManagedNode.getBridgeName(),
91 ovsdbManagedNode.getBridgeUuid());
93 String existingBridgeName = operationalBridgeOptional.get().getBridgeName().getValue();
94 LOG.debug("Bridge {} already exists in device updating {}", existingBridgeName, iid);
95 // Name is immutable, and so we *can't* update it. So we use extraBridge for the schema stuff
96 Bridge extraBridge = transaction.getTypedRowWrapper(Bridge.class);
97 extraBridge.setName("");
98 transaction.add(op.update(bridge)
99 .where(extraBridge.getNameColumn().getSchema().opEqual(existingBridgeName))
101 stampInstanceIdentifier(transaction, iid.firstIdentifierOf(Node.class), existingBridgeName,
102 instanceIdentifierCodec);
106 private static void setDataPathType(final Bridge bridge,final OvsdbBridgeAugmentation ovsdbManagedNode) {
107 if (ovsdbManagedNode.getDatapathType() != null) {
108 bridge.setDatapathType(SouthboundMapper.createDatapathType(ovsdbManagedNode));
112 private static void setStpEnalbe(final Bridge bridge, final OvsdbBridgeAugmentation ovsdbManageNode) {
113 if (ovsdbManageNode.isStpEnable() != null) {
114 bridge.setStpEnable(ovsdbManageNode.isStpEnable());
118 private static void setName(final Bridge bridge, final OvsdbBridgeAugmentation ovsdbManagedNode,
119 final Optional<OvsdbBridgeAugmentation> operationalBridgeOptional) {
120 if (ovsdbManagedNode.getBridgeName() != null) {
121 bridge.setName(ovsdbManagedNode.getBridgeName().getValue());
122 } else if (operationalBridgeOptional.isPresent() && operationalBridgeOptional.get().getBridgeName() != null) {
123 bridge.setName(operationalBridgeOptional.get().getBridgeName().getValue());
127 private static void setOpenDaylightExternalIds(final Bridge bridge,
128 final InstanceIdentifier<OvsdbBridgeAugmentation> iid, final OvsdbBridgeAugmentation ovsdbManagedNode,
129 final InstanceIdentifierCodec instanceIdentifierCodec) {
130 // Set the iid external_id
131 Map<String, String> externalIdMap = new HashMap<>();
132 externalIdMap.put(SouthboundConstants.IID_EXTERNAL_ID_KEY, instanceIdentifierCodec.serialize(iid));
133 // Set user provided external ids
135 YangUtils.copyYangKeyValueListToMap(externalIdMap, ovsdbManagedNode.getBridgeExternalIds(),
136 BridgeExternalIds::getBridgeExternalIdKey, BridgeExternalIds::getBridgeExternalIdValue);
137 } catch (NullPointerException e) {
138 LOG.warn("Incomplete bridge external Id", e);
140 bridge.setExternalIds(externalIdMap);
143 private static void setOpenDaylightOtherConfig(final @NonNull Bridge bridge,
144 final @NonNull OvsdbBridgeAugmentation ovsdbManagedNode) {
146 bridge.setOtherConfig(YangUtils.convertYangKeyValueListToMap(ovsdbManagedNode.getBridgeOtherConfigs(),
147 BridgeOtherConfigs::getBridgeOtherConfigKey, BridgeOtherConfigs::getBridgeOtherConfigValue));
148 } catch (NullPointerException e) {
149 LOG.warn("Incomplete bridge other config", e);
153 private static void setPort(final TransactionBuilder transaction, final Bridge bridge,
154 final OvsdbBridgeAugmentation ovsdbManagedNode) {
156 Insert<GenericTableSchema> interfaceInsert = setInterface(transaction,ovsdbManagedNode);
158 String portNamedUuid = "Port_" + SouthboundMapper.getRandomUuid();
159 Port port = transaction.getTypedRowWrapper(Port.class);
160 port.setName(ovsdbManagedNode.getBridgeName().getValue());
161 port.setInterfaces(Collections.singleton(TransactUtils.extractNamedUuid(interfaceInsert)));
162 transaction.add(op.insert(port).withId(portNamedUuid));
163 bridge.setPorts(Collections.singleton(new UUID(portNamedUuid)));
166 private static Insert<GenericTableSchema> setInterface(final TransactionBuilder transaction,
167 final OvsdbBridgeAugmentation ovsdbManagedNode) {
169 String interfaceNamedUuid = "Interface_" + SouthboundMapper.getRandomUuid();
170 Interface interfaceOvs = transaction.getTypedRowWrapper(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);
178 private static void setFailMode(final Bridge bridge,
179 final OvsdbBridgeAugmentation ovsdbManagedNode) {
180 if (ovsdbManagedNode.getFailMode() != null
181 && SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode()) != null) {
182 bridge.setFailMode(Collections.singleton(
183 SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode())));
187 private static void stampInstanceIdentifier(final TransactionBuilder transaction,
188 final InstanceIdentifier<Node> iid, final String bridgeName,
189 final InstanceIdentifierCodec instanceIdentifierCodec) {
190 Bridge bridge = transaction.getTypedRowWrapper(Bridge.class);
191 bridge.setName(bridgeName);
192 bridge.setExternalIds(Collections.emptyMap());
193 Mutate mutate = TransactUtils.stampInstanceIdentifierMutation(transaction, iid, bridge.getSchema(),
194 bridge.getExternalIdsColumn().getSchema(), instanceIdentifierCodec);
195 transaction.add(mutate
196 .where(bridge.getNameColumn().getSchema().opEqual(bridgeName))