Merge topic 'unification'
[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.HashMap;
13 import java.util.Map;
14 import java.util.Map.Entry;
15
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;
35
36 import com.google.common.collect.Sets;
37
38 public class BridgeCreateCommand implements TransactCommand {
39     private AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes;
40     private static final Logger LOG = LoggerFactory.getLogger(BridgeCreateCommand.class);
41
42
43     public BridgeCreateCommand(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
44         this.changes = changes;
45     }
46
47     @Override
48     public void execute(TransactionBuilder transaction) {
49         Map<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> created =
50                 TransactUtils.extractCreated(changes,OvsdbBridgeAugmentation.class);
51         for (Entry<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> ovsdbManagedNodeEntry:
52             created.entrySet()) {
53             OvsdbBridgeAugmentation ovsdbManagedNode = ovsdbManagedNodeEntry.getValue();
54             LOG.debug("Received request to create ovsdb bridge name: {} uuid: {}",
55                         ovsdbManagedNode.getBridgeName(),
56                         ovsdbManagedNode.getBridgeUuid());
57
58             // Named UUIDs
59             String bridgeNamedUuid = "Bridge_" + SouthboundMapper.getRandomUUID();
60             String interfaceNamedUuid = "Interface_" + SouthboundMapper.getRandomUUID();
61             String portNamedUuid = "Port_" + SouthboundMapper.getRandomUUID();
62
63             // Interface part
64             Interface interfaceOvs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
65             interfaceOvs.setName(ovsdbManagedNode.getBridgeName().getValue());
66             interfaceOvs.setType(SouthboundMapper.createOvsdbInterfaceType(InterfaceTypeInternal.class));
67             transaction.add(op.insert(interfaceOvs).withId(interfaceNamedUuid));
68
69             // Port part
70             Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
71             port.setName(ovsdbManagedNode.getBridgeName().getValue());
72             port.setInterfaces(Sets.newHashSet(new UUID(interfaceNamedUuid)));
73             transaction.add(op.insert(port).withId(portNamedUuid));
74
75             // Bridge part
76             Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
77             bridge.setName(ovsdbManagedNode.getBridgeName().getValue());
78             if (ovsdbManagedNode.getFailMode() != null
79                     && SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode()) != null ) {
80                 bridge.setFailMode(Sets.newHashSet(
81                         SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode())));
82             }
83             bridge.setDatapathType(SouthboundMapper.createDatapathType(ovsdbManagedNode));
84             if (SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode) != null
85                     && SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode).size() > 0) {
86                 bridge.setProtocols(SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode));
87             }
88             Map<UUID,Controller> controllerMap = SouthboundMapper.createOvsdbController(
89                     ovsdbManagedNode, transaction.getDatabaseSchema());
90             for (Entry<UUID,Controller> entry: controllerMap.entrySet()) {
91                 transaction.add(op.insert(entry.getValue()).withId(entry.getKey().toString()));
92             }
93             if (!controllerMap.isEmpty()) {
94                 bridge.setController(controllerMap.keySet());
95             }
96             bridge.setPorts(Sets.newHashSet(new UUID(portNamedUuid)));
97
98             // Set the iid external_id
99             Map<String,String> externalIds = new HashMap<String,String>();
100             externalIds.put(SouthboundConstants.IID_EXTERNAL_ID_KEY,
101                     SouthboundUtil.serializeInstanceIdentifier(ovsdbManagedNodeEntry.getKey()));
102             bridge.setExternalIds(externalIds);
103
104             transaction.add(op.insert(bridge).withId(bridgeNamedUuid));
105
106             // OpenVSwitchPart
107             OpenVSwitch ovs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), OpenVSwitch.class);
108             ovs.setBridges(Sets.newHashSet(new UUID(bridgeNamedUuid)));
109             transaction.add(op.mutate(ovs).addMutation(ovs.getBridgesColumn().getSchema(),
110                     Mutator.INSERT,
111                     ovs.getBridgesColumn().getData()));
112         }
113     }
114
115 }