2 * Copyright (c) 2014 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;
14 import java.util.Map.Entry;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
17 import org.opendaylight.ovsdb.lib.notation.Mutator;
18 import org.opendaylight.ovsdb.lib.notation.UUID;
19 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
20 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
21 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
22 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
23 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
24 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
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.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.google.common.collect.Sets;
38 public class BridgeCreateCommand implements TransactCommand {
39 private AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes;
40 private BridgeOperationalState operationalState;
41 private static final Logger LOG = LoggerFactory.getLogger(BridgeCreateCommand.class);
44 public BridgeCreateCommand(BridgeOperationalState state, AsyncDataChangeEvent<InstanceIdentifier<?>,
45 DataObject> changes) {
46 this.operationalState = state;
47 this.changes = changes;
51 public void execute(TransactionBuilder transaction) {
52 Map<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> created =
53 TransactUtils.extractCreated(changes,OvsdbBridgeAugmentation.class);
54 for (Entry<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> ovsdbManagedNodeEntry:
56 OvsdbBridgeAugmentation ovsdbManagedNode = ovsdbManagedNodeEntry.getValue();
57 LOG.debug("Received request to create ovsdb bridge name: {} uuid: {}",
58 ovsdbManagedNode.getBridgeName(),
59 ovsdbManagedNode.getBridgeUuid());
62 String bridgeNamedUuid = "Bridge_" + SouthboundMapper.getRandomUUID();
63 String interfaceNamedUuid = "Interface_" + SouthboundMapper.getRandomUUID();
64 String portNamedUuid = "Port_" + SouthboundMapper.getRandomUUID();
67 Interface interfaceOvs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
68 interfaceOvs.setName(ovsdbManagedNode.getBridgeName().getValue());
69 interfaceOvs.setType(SouthboundMapper.createOvsdbInterfaceType(InterfaceTypeInternal.class));
70 transaction.add(op.insert(interfaceOvs).withId(interfaceNamedUuid));
73 Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
74 port.setName(ovsdbManagedNode.getBridgeName().getValue());
75 port.setInterfaces(Sets.newHashSet(new UUID(interfaceNamedUuid)));
76 transaction.add(op.insert(port).withId(portNamedUuid));
79 Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
80 bridge.setName(ovsdbManagedNode.getBridgeName().getValue());
81 if (ovsdbManagedNode.getFailMode() != null
82 && SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode()) != null ) {
83 bridge.setFailMode(Sets.newHashSet(
84 SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode())));
86 bridge.setDatapathType(SouthboundMapper.createDatapathType(ovsdbManagedNode));
87 if (SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode) != null
88 && SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode).size() > 0) {
89 bridge.setProtocols(SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode));
91 Map<UUID,Controller> controllerMap = SouthboundMapper.createOvsdbController(
92 ovsdbManagedNode, transaction.getDatabaseSchema());
93 for (Entry<UUID,Controller> entry: controllerMap.entrySet()) {
94 transaction.add(op.insert(entry.getValue()).withId(entry.getKey().toString()));
96 if (!controllerMap.isEmpty()) {
97 bridge.setController(controllerMap.keySet());
99 bridge.setPorts(Sets.newHashSet(new UUID(portNamedUuid)));
101 // Set the iid external_id
102 Map<String,String> externalIds = new HashMap<String,String>();
103 externalIds.put(SouthboundConstants.IID_EXTERNAL_ID_KEY,
104 SouthboundUtil.serializeInstanceIdentifier(ovsdbManagedNodeEntry.getKey()));
105 bridge.setExternalIds(externalIds);
107 transaction.add(op.insert(bridge).withId(bridgeNamedUuid));
110 OpenVSwitch ovs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), OpenVSwitch.class);
111 ovs.setBridges(Sets.newHashSet(new UUID(bridgeNamedUuid)));
112 transaction.add(op.mutate(ovs).addMutation(ovs.getBridgesColumn().getSchema(),
114 ovs.getBridgesColumn().getData()));