Merge "Bug 4641 - openstack.net-virt-sfc-features fail to find artifacts"
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / ProtocolUpdateCommand.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
9 package org.opendaylight.ovsdb.southbound.ovsdb.transact;
10
11 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
12
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.error.SchemaVersionMismatchException;
18 import org.opendaylight.ovsdb.lib.notation.Mutator;
19 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
20 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
21 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
22 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.google.common.base.Optional;
31 import com.google.common.collect.Sets;
32
33 public class ProtocolUpdateCommand extends AbstractTransactCommand {
34
35     private static final Logger LOG = LoggerFactory.getLogger(ProtocolUpdateCommand.class);
36     private Map<InstanceIdentifier<ProtocolEntry>, ProtocolEntry> protocols;
37     private Map<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> bridges;
38
39     public ProtocolUpdateCommand(BridgeOperationalState state,
40             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
41         super(state, changes);
42         protocols = TransactUtils.extractCreatedOrUpdated(getChanges(), ProtocolEntry.class);
43         bridges = TransactUtils.extractCreatedOrUpdated(getChanges(), OvsdbBridgeAugmentation.class);
44     }
45
46     @Override
47     public void execute(TransactionBuilder transaction) {
48         for (Entry<InstanceIdentifier<ProtocolEntry>, ProtocolEntry> entry: protocols.entrySet()) {
49             Optional<ProtocolEntry> operationalProtocolEntryOptional =
50                     getOperationalState().getProtocolEntry(entry.getKey());
51             if (!operationalProtocolEntryOptional.isPresent()) {
52                 InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid =
53                         entry.getKey().firstIdentifierOf(OvsdbBridgeAugmentation.class);
54                 Optional<OvsdbBridgeAugmentation> bridgeOptional =
55                         getOperationalState().getOvsdbBridgeAugmentation(bridgeIid);
56                 OvsdbBridgeAugmentation ovsdbBridge;
57                 if (bridgeOptional.isPresent()) {
58                     ovsdbBridge = bridgeOptional.get();
59                 } else {
60                     ovsdbBridge = bridges.get(bridgeIid);
61                 }
62                 if (ovsdbBridge != null
63                         && ovsdbBridge.getBridgeName() != null
64                         && entry.getValue() != null
65                         && entry.getValue().getProtocol() != null) {
66                     String protocolString = SouthboundConstants.OVSDB_PROTOCOL_MAP.get(entry.getValue().getProtocol());
67                     if (protocolString != null) {
68                         Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
69                         bridge.setName(ovsdbBridge.getBridgeName().getValue());
70                         try {
71                             bridge.setProtocols(Sets.newHashSet(protocolString));
72                             transaction.add(op.mutate(bridge).addMutation(bridge.getProtocolsColumn().getSchema(),
73                                         Mutator.INSERT,bridge.getProtocolsColumn().getData())
74                                 .where(bridge.getNameColumn().getSchema().opEqual(bridge.getNameColumn().getData()))
75                                 .build());
76                         } catch (SchemaVersionMismatchException e) {
77                             // We don't care about the exception stack trace here
78                             LOG.warn("protocol not supported by this version of ovsdb: {}", e.getMessage());
79                         }
80                     }
81                 }
82             }
83         }
84     }
85
86 }