68b0ae0e99e6fcf17e28c706cbe416ec632d1dc3
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / BridgeCreateCommand.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.ovsdb.southbound.ovsdb.transact;
9
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
11
12 import java.util.Map;
13
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
15 import org.opendaylight.ovsdb.lib.notation.Mutator;
16 import org.opendaylight.ovsdb.lib.notation.UUID;
17 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
18 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
19 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
20 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
21 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.google.common.collect.Sets;
29
30 public class BridgeCreateCommand implements TransactCommand {
31     private AsyncDataChangeEvent<InstanceIdentifier<?>, OvsdbBridgeAugmentation> changes;
32     private static final Logger LOG = LoggerFactory.getLogger(BridgeCreateCommand.class);
33
34
35     public BridgeCreateCommand(AsyncDataChangeEvent<InstanceIdentifier<?>, OvsdbBridgeAugmentation> changes) {
36         this.changes = changes;
37     }
38
39     @Override
40     public void execute(TransactionBuilder transaction) {
41         Map<InstanceIdentifier<Node>, OvsdbBridgeAugmentation> created = TransactUtils.extractOvsdbManagedNodeCreate(changes);
42         for(OvsdbBridgeAugmentation ovsdbManagedNode: created.values()) {
43             LOG.debug("Received request to create ovsdb bridge name: {} uuid: {}",
44                         ovsdbManagedNode.getBridgeName(),
45                         ovsdbManagedNode.getBridgeUuid());
46             // Bridge part
47             Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
48             bridge.setName(ovsdbManagedNode.getBridgeName().getValue());
49             String namedUuid = "Bridge_" + ovsdbManagedNode.getBridgeName().getValue();
50             bridge.setName(ovsdbManagedNode.getBridgeName().getValue());
51             if(SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode) != null
52                     && SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode).size() > 0){
53                 bridge.setProtocols(SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode));
54             }
55             transaction.add(op.insert(bridge).withId(namedUuid));
56
57             // OpenVSwitchPart
58             OpenVSwitch ovs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), OpenVSwitch.class);
59             ovs.setBridges(Sets.newHashSet(new UUID(namedUuid)));
60             transaction.add(op.mutate(ovs).addMutation(ovs.getBridgesColumn().getSchema(),
61                     Mutator.INSERT,
62                     ovs.getBridgesColumn().getData())
63                     );
64         }
65     }
66
67 }