Do not send bridge name in update
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / BridgeUpdateCommand.java
1 /*
2  * Copyright (c) 2015 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.Insert;
19 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
20 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
21 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
22 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
23 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
24 import org.opendaylight.ovsdb.schema.openvswitch.Port;
25 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
26 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
27 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeInternal;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.google.common.base.Optional;
36 import com.google.common.collect.Sets;
37
38 public class BridgeUpdateCommand extends AbstractTransactCommand {
39
40     private static final Logger LOG = LoggerFactory.getLogger(BridgeUpdateCommand.class);
41
42     public BridgeUpdateCommand(BridgeOperationalState state,
43             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
44         super(state, changes);
45     }
46
47
48
49     @Override
50     public void execute(TransactionBuilder transaction) {
51         Map<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> created =
52                 TransactUtils.extractCreated(getChanges(),OvsdbBridgeAugmentation.class);
53         for (Entry<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> ovsdbManagedNodeEntry:
54             created.entrySet()) {
55             updateBridge(transaction,  ovsdbManagedNodeEntry.getKey(), ovsdbManagedNodeEntry.getValue());
56         }
57     }
58
59
60
61     private void updateBridge(
62             TransactionBuilder transaction,
63             InstanceIdentifier<OvsdbBridgeAugmentation> iid, OvsdbBridgeAugmentation ovsdbManagedNode) {
64         LOG.debug("Received request to create ovsdb bridge name: {} uuid: {}",
65                     ovsdbManagedNode.getBridgeName(),
66                     ovsdbManagedNode.getBridgeUuid());
67         Optional<OvsdbBridgeAugmentation> operationalBridgeOptional =
68                 getOperationalState().getOvsdbBridgeAugmentation(iid);
69         Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
70         setFailMode(bridge, ovsdbManagedNode);
71         setDataPathType(bridge, ovsdbManagedNode);
72         setOpenDaylightIidExternalId(bridge, iid);
73         if (!operationalBridgeOptional.isPresent()) {
74             setName(bridge, ovsdbManagedNode,operationalBridgeOptional);
75             setPort(transaction, bridge, ovsdbManagedNode);
76             transaction.add(op.insert(bridge));
77         } else if (bridge.getName() != null) {
78             // Name is immutable, and so we *can't* update it.  So we use extraBridge for the schema stuff
79             Bridge extraBridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
80             extraBridge.setName("");
81             transaction.add(op.update(bridge)
82                     .where(extraBridge.getNameColumn().getSchema().opEqual(bridge.getName()))
83                     .build());
84         }
85     }
86
87
88
89     private void setDataPathType(Bridge bridge,OvsdbBridgeAugmentation ovsdbManagedNode) {
90         if (ovsdbManagedNode.getDatapathType() != null) {
91             bridge.setDatapathType(SouthboundMapper.createDatapathType(ovsdbManagedNode));
92         }
93     }
94
95
96
97     private void setName(Bridge bridge, OvsdbBridgeAugmentation ovsdbManagedNode,
98             Optional<OvsdbBridgeAugmentation> operationalBridgeOptional) {
99         if (ovsdbManagedNode.getBridgeName() != null) {
100             bridge.setName(ovsdbManagedNode.getBridgeName().getValue());
101         } else if (operationalBridgeOptional.isPresent() && operationalBridgeOptional.get().getBridgeName() != null) {
102             bridge.setName(operationalBridgeOptional.get().getBridgeName().getValue());
103         }
104     }
105
106
107
108     private void setOpenDaylightIidExternalId(Bridge bridge,
109             InstanceIdentifier<OvsdbBridgeAugmentation> iid) {
110         // Set the iid external_id
111         Map<String,String> externalIds = new HashMap<String,String>();
112         externalIds.put(SouthboundConstants.IID_EXTERNAL_ID_KEY,
113                 SouthboundUtil.serializeInstanceIdentifier(iid));
114         bridge.setExternalIds(externalIds);
115     }
116
117
118
119     private void setPort(TransactionBuilder transaction, Bridge bridge,
120             OvsdbBridgeAugmentation ovsdbManagedNode) {
121
122         Insert<GenericTableSchema> interfaceInsert = setInterface(transaction,ovsdbManagedNode);
123         // Port part
124         String portNamedUuid = "Port_" + SouthboundMapper.getRandomUUID();
125         Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
126         port.setName(ovsdbManagedNode.getBridgeName().getValue());
127         port.setInterfaces(Sets.newHashSet(TransactUtils.extractNamedUuid(interfaceInsert)));
128         transaction.add(op.insert(port).withId(portNamedUuid));
129         bridge.setPorts(Sets.newHashSet(new UUID(portNamedUuid)));
130     }
131
132     private Insert<GenericTableSchema> setInterface(TransactionBuilder transaction,
133             OvsdbBridgeAugmentation ovsdbManagedNode) {
134         // Interface part
135         String interfaceNamedUuid = "Interface_" + SouthboundMapper.getRandomUUID();
136         Interface interfaceOvs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
137         interfaceOvs.setName(ovsdbManagedNode.getBridgeName().getValue());
138         interfaceOvs.setType(SouthboundMapper.createOvsdbInterfaceType(InterfaceTypeInternal.class));
139         Insert<GenericTableSchema> result = op.insert(interfaceOvs).withId(interfaceNamedUuid);
140         transaction.add(result);
141         return result;
142     }
143
144
145
146     private void setFailMode(Bridge bridge,
147             OvsdbBridgeAugmentation ovsdbManagedNode) {
148         if (ovsdbManagedNode.getFailMode() != null
149                 && SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode()) != null ) {
150             bridge.setFailMode(Sets.newHashSet(
151                     SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode())));
152         }
153     }
154
155 }