User provided bridge external-ids not making to operational/OVSDB.
[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     }
62
63
64
65     private void updateBridge(
66             TransactionBuilder transaction,
67             InstanceIdentifier<OvsdbBridgeAugmentation> iid, OvsdbBridgeAugmentation ovsdbManagedNode) {
68         LOG.debug("Received request to create ovsdb bridge name: {} uuid: {}",
69                     ovsdbManagedNode.getBridgeName(),
70                     ovsdbManagedNode.getBridgeUuid());
71         Optional<OvsdbBridgeAugmentation> operationalBridgeOptional =
72                 getOperationalState().getOvsdbBridgeAugmentation(iid);
73         Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
74         setFailMode(bridge, ovsdbManagedNode);
75         setDataPathType(bridge, ovsdbManagedNode);
76         setOpenDaylightExternalIds(bridge, iid, ovsdbManagedNode);
77         setOpenDaylightOtherConfig(bridge, ovsdbManagedNode);
78         if (!operationalBridgeOptional.isPresent()) {
79             setName(bridge, ovsdbManagedNode,operationalBridgeOptional);
80             setPort(transaction, bridge, ovsdbManagedNode);
81             transaction.add(op.insert(bridge));
82         } else if (bridge.getName() != null) {
83             // Name is immutable, and so we *can't* update it.  So we use extraBridge for the schema stuff
84             Bridge extraBridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
85             extraBridge.setName("");
86             transaction.add(op.update(bridge)
87                     .where(extraBridge.getNameColumn().getSchema().opEqual(bridge.getName()))
88                     .build());
89         }
90     }
91
92
93
94     private void setDataPathType(Bridge bridge,OvsdbBridgeAugmentation ovsdbManagedNode) {
95         if (ovsdbManagedNode.getDatapathType() != null) {
96             bridge.setDatapathType(SouthboundMapper.createDatapathType(ovsdbManagedNode));
97         }
98     }
99
100
101
102     private void setName(Bridge bridge, OvsdbBridgeAugmentation ovsdbManagedNode,
103             Optional<OvsdbBridgeAugmentation> operationalBridgeOptional) {
104         if (ovsdbManagedNode.getBridgeName() != null) {
105             bridge.setName(ovsdbManagedNode.getBridgeName().getValue());
106         } else if (operationalBridgeOptional.isPresent() && operationalBridgeOptional.get().getBridgeName() != null) {
107             bridge.setName(operationalBridgeOptional.get().getBridgeName().getValue());
108         }
109     }
110
111
112
113     private void setOpenDaylightExternalIds(Bridge bridge, InstanceIdentifier<OvsdbBridgeAugmentation> iid,
114             OvsdbBridgeAugmentation ovsdbManagedNode) {
115         // Set the iid external_id
116         Map<String, String> externalIdMap = new HashMap<String, String>();
117         externalIdMap.put(SouthboundConstants.IID_EXTERNAL_ID_KEY, SouthboundUtil.serializeInstanceIdentifier(iid));
118         // Set user provided external ids
119         List<BridgeExternalIds> bridgeExternalId = ovsdbManagedNode.getBridgeExternalIds();
120         if (bridgeExternalId != null) {
121             for (BridgeExternalIds externalId : bridgeExternalId) {
122                 externalIdMap.put(externalId.getBridgeExternalIdKey(), externalId.getBridgeExternalIdValue());
123             }
124         }
125         try {
126             bridge.setExternalIds(ImmutableMap.copyOf(externalIdMap));
127         } catch (NullPointerException e) {
128             LOG.warn("Incomplete bridge external Id", e);
129         }
130     }
131
132
133
134     private void setOpenDaylightOtherConfig(Bridge bridge, OvsdbBridgeAugmentation ovsdbManagedNode) {
135         List<BridgeOtherConfigs> bridgeOtherConfig = ovsdbManagedNode.getBridgeOtherConfigs();
136         if (bridgeOtherConfig != null) {
137             HashMap<String, String> otherConfigMap = new HashMap<String, String>();
138             for (BridgeOtherConfigs otherConf : bridgeOtherConfig) {
139                 otherConfigMap.put(otherConf.getBridgeOtherConfigKey(), otherConf.getBridgeOtherConfigValue());
140             }
141             try {
142                 bridge.setOtherConfig(ImmutableMap.copyOf(otherConfigMap));
143             } catch (NullPointerException e) {
144                 LOG.warn("Incomplete bridge other config");
145             }
146         }
147     }
148
149
150
151     private void setPort(TransactionBuilder transaction, Bridge bridge,
152             OvsdbBridgeAugmentation ovsdbManagedNode) {
153
154         Insert<GenericTableSchema> interfaceInsert = setInterface(transaction,ovsdbManagedNode);
155         // Port part
156         String portNamedUuid = "Port_" + SouthboundMapper.getRandomUUID();
157         Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
158         port.setName(ovsdbManagedNode.getBridgeName().getValue());
159         port.setInterfaces(Sets.newHashSet(TransactUtils.extractNamedUuid(interfaceInsert)));
160         transaction.add(op.insert(port).withId(portNamedUuid));
161         bridge.setPorts(Sets.newHashSet(new UUID(portNamedUuid)));
162     }
163
164     private Insert<GenericTableSchema> setInterface(TransactionBuilder transaction,
165             OvsdbBridgeAugmentation ovsdbManagedNode) {
166         // Interface part
167         String interfaceNamedUuid = "Interface_" + SouthboundMapper.getRandomUUID();
168         Interface interfaceOvs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
169         interfaceOvs.setName(ovsdbManagedNode.getBridgeName().getValue());
170         interfaceOvs.setType(SouthboundMapper.createOvsdbInterfaceType(InterfaceTypeInternal.class));
171         Insert<GenericTableSchema> result = op.insert(interfaceOvs).withId(interfaceNamedUuid);
172         transaction.add(result);
173         return result;
174     }
175
176
177
178     private void setFailMode(Bridge bridge,
179             OvsdbBridgeAugmentation ovsdbManagedNode) {
180         if (ovsdbManagedNode.getFailMode() != null
181                 && SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode()) != null ) {
182             bridge.setFailMode(Sets.newHashSet(
183                     SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode())));
184         }
185     }
186
187 }