Merge "Introducing Bridge Operational State"
[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 BridgeOperationalState operationalState;
41     private static final Logger LOG = LoggerFactory.getLogger(BridgeCreateCommand.class);
42
43
44     public BridgeCreateCommand(BridgeOperationalState state, AsyncDataChangeEvent<InstanceIdentifier<?>,
45             DataObject> changes) {
46         this.operationalState = state;
47         this.changes = changes;
48     }
49
50     @Override
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:
55             created.entrySet()) {
56             OvsdbBridgeAugmentation ovsdbManagedNode = ovsdbManagedNodeEntry.getValue();
57             LOG.debug("Received request to create ovsdb bridge name: {} uuid: {}",
58                         ovsdbManagedNode.getBridgeName(),
59                         ovsdbManagedNode.getBridgeUuid());
60
61             // Named UUIDs
62             String bridgeNamedUuid = "Bridge_" + SouthboundMapper.getRandomUUID();
63             String interfaceNamedUuid = "Interface_" + SouthboundMapper.getRandomUUID();
64             String portNamedUuid = "Port_" + SouthboundMapper.getRandomUUID();
65
66             // Interface part
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));
71
72             // Port part
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));
77
78             // Bridge part
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())));
85             }
86             bridge.setDatapathType(SouthboundMapper.createDatapathType(ovsdbManagedNode));
87             if (SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode) != null
88                     && SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode).size() > 0) {
89                 bridge.setProtocols(SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode));
90             }
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()));
95             }
96             if (!controllerMap.isEmpty()) {
97                 bridge.setController(controllerMap.keySet());
98             }
99             bridge.setPorts(Sets.newHashSet(new UUID(portNamedUuid)));
100
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);
106
107             transaction.add(op.insert(bridge).withId(bridgeNamedUuid));
108
109             // OpenVSwitchPart
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(),
113                     Mutator.INSERT,
114                     ovs.getBridgesColumn().getData()));
115         }
116     }
117
118 }