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