dfbad83ebd3f7b584a456387e1ffcf73a1f0d045
[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.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;
32
33 import com.google.common.collect.Sets;
34
35 public class BridgeCreateCommand extends AbstractTransactCommand {
36
37     private static final Logger LOG = LoggerFactory.getLogger(BridgeCreateCommand.class);
38
39     public BridgeCreateCommand(BridgeOperationalState state,
40             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
41         super(state, changes);
42     }
43
44
45
46     @Override
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:
51             created.entrySet()) {
52             OvsdbBridgeAugmentation ovsdbManagedNode = ovsdbManagedNodeEntry.getValue();
53             LOG.debug("Received request to create ovsdb bridge name: {} uuid: {}",
54                         ovsdbManagedNode.getBridgeName(),
55                         ovsdbManagedNode.getBridgeUuid());
56
57             // Named UUIDs
58             String bridgeNamedUuid = "Bridge_" + SouthboundMapper.getRandomUUID();
59             String interfaceNamedUuid = "Interface_" + SouthboundMapper.getRandomUUID();
60             String portNamedUuid = "Port_" + SouthboundMapper.getRandomUUID();
61
62             // Interface part
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));
67
68             // Port part
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));
73
74             // Bridge part
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())));
81             }
82             bridge.setDatapathType(SouthboundMapper.createDatapathType(ovsdbManagedNode));
83             bridge.setPorts(Sets.newHashSet(new UUID(portNamedUuid)));
84
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);
90
91             transaction.add(op.insert(bridge).withId(bridgeNamedUuid));
92         }
93     }
94
95 }