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