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.UUID;
18 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
19 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
20 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
21 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
22 import org.opendaylight.ovsdb.schema.openvswitch.Port;
23 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
24 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
25 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeInternal;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.google.common.collect.Sets;
35 public class BridgeCreateCommand extends AbstractTransactCommand {
37 private static final Logger LOG = LoggerFactory.getLogger(BridgeCreateCommand.class);
39 public BridgeCreateCommand(BridgeOperationalState state,
40 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
41 super(state, changes);
47 public void execute(TransactionBuilder transaction) {
48 Map<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> created =
49 TransactUtils.extractCreated(getChanges(),OvsdbBridgeAugmentation.class);
50 for (Entry<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> ovsdbManagedNodeEntry:
52 OvsdbBridgeAugmentation ovsdbManagedNode = ovsdbManagedNodeEntry.getValue();
53 LOG.debug("Received request to create ovsdb bridge name: {} uuid: {}",
54 ovsdbManagedNode.getBridgeName(),
55 ovsdbManagedNode.getBridgeUuid());
58 String bridgeNamedUuid = "Bridge_" + SouthboundMapper.getRandomUUID();
59 String interfaceNamedUuid = "Interface_" + SouthboundMapper.getRandomUUID();
60 String portNamedUuid = "Port_" + SouthboundMapper.getRandomUUID();
63 Interface interfaceOvs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
64 interfaceOvs.setName(ovsdbManagedNode.getBridgeName().getValue());
65 interfaceOvs.setType(SouthboundMapper.createOvsdbInterfaceType(InterfaceTypeInternal.class));
66 transaction.add(op.insert(interfaceOvs).withId(interfaceNamedUuid));
69 Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
70 port.setName(ovsdbManagedNode.getBridgeName().getValue());
71 port.setInterfaces(Sets.newHashSet(new UUID(interfaceNamedUuid)));
72 transaction.add(op.insert(port).withId(portNamedUuid));
75 Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
76 bridge.setName(ovsdbManagedNode.getBridgeName().getValue());
77 if (ovsdbManagedNode.getFailMode() != null
78 && SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode()) != null ) {
79 bridge.setFailMode(Sets.newHashSet(
80 SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode())));
82 bridge.setDatapathType(SouthboundMapper.createDatapathType(ovsdbManagedNode));
83 bridge.setPorts(Sets.newHashSet(new UUID(portNamedUuid)));
85 // Set the iid external_id
86 Map<String,String> externalIds = new HashMap<String,String>();
87 externalIds.put(SouthboundConstants.IID_EXTERNAL_ID_KEY,
88 SouthboundUtil.serializeInstanceIdentifier(ovsdbManagedNodeEntry.getKey()));
89 bridge.setExternalIds(externalIds);
91 transaction.add(op.insert(bridge).withId(bridgeNamedUuid));