Cleanup exception logging
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / QueueUpdateCommand.java
1 /*
2  * Copyright (c) 2016 Intel Corporation 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.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Set;
18
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
20 import org.opendaylight.ovsdb.lib.notation.Mutator;
21 import org.opendaylight.ovsdb.lib.notation.UUID;
22 import org.opendaylight.ovsdb.lib.operations.Mutate;
23 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
24 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
25 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
26 import org.opendaylight.ovsdb.schema.openvswitch.Qos;
27 import org.opendaylight.ovsdb.schema.openvswitch.Queue;
28 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
29 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Queues;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesExternalIds;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfig;
37 import org.opendaylight.yangtools.yang.binding.DataObject;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import com.google.common.collect.ImmutableMap;
43
44 public class QueueUpdateCommand extends AbstractTransactCommand {
45     private static final Logger LOG = LoggerFactory.getLogger(QueueUpdateCommand.class);
46
47
48     public QueueUpdateCommand(BridgeOperationalState state, AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
49         super(state, changes);
50     }
51
52     @Override
53     public void execute(TransactionBuilder transaction) {
54         Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> created =
55                 TransactUtils.extractCreated(getChanges(),OvsdbNodeAugmentation.class);
56         for (Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> ovsdbNodeEntry:
57             created.entrySet()) {
58             updateQueue(transaction,  ovsdbNodeEntry.getKey(), ovsdbNodeEntry.getValue());
59         }
60         Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> updated =
61                 TransactUtils.extractUpdated(getChanges(),OvsdbNodeAugmentation.class);
62         for (Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> ovsdbNodeEntry:
63             updated.entrySet()) {
64             updateQueue(transaction,  ovsdbNodeEntry.getKey(), ovsdbNodeEntry.getValue());
65         }
66     }
67
68     private void updateQueue(
69             TransactionBuilder transaction,
70             InstanceIdentifier<OvsdbNodeAugmentation> iid, OvsdbNodeAugmentation ovsdbNode) {
71
72         List<Queues> queueList = ovsdbNode.getQueues();
73
74         if (!getOperationalState().getBridgeNode(iid).isPresent()) {
75             return;
76         }
77         OvsdbNodeAugmentation operNode = getOperationalState().getBridgeNode(iid).get().getAugmentation(OvsdbNodeAugmentation.class);
78         List<Queues> operQueues = operNode.getQueues();
79
80         if (queueList != null) {
81             for (Queues queueEntry : queueList) {
82                 Queue queue = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Queue.class);
83
84                 if (queueEntry.getDscp() != null) {
85                     try {
86                         Set<Long> dscpSet = new HashSet<>();
87                             if (dscpSet.add(new Long(queueEntry.getDscp().toString()))) {
88                             queue.setDscp(dscpSet);
89                         }
90                     } catch (NumberFormatException e) {
91                         LOG.warn("Invalid DSCP {} setting for Queue {}", queueEntry.getDscp(), queueEntry, e);
92                     }
93                 }
94
95                 Uuid queueUuid = getQueueEntryUuid(operQueues, queueEntry.getQueueId());
96                 UUID uuid = null;
97                 if (queueUuid != null) {
98                     uuid = new UUID(queueUuid.getValue());
99                 }
100
101                 List<QueuesExternalIds> externalIds = queueEntry.getQueuesExternalIds();
102                 Map<String, String> externalIdsMap = new HashMap<>();
103                 if (externalIds != null) {
104                     for (QueuesExternalIds externalId : externalIds) {
105                         externalIdsMap.put(externalId.getQueuesExternalIdKey(), externalId.getQueuesExternalIdValue());
106                     }
107                 }
108                 externalIdsMap.put(SouthboundConstants.QUEUE_ID_EXTERNAL_ID_KEY, queueEntry.getQueueId().getValue());
109                 try {
110                     queue.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
111                 } catch (NullPointerException e) {
112                     LOG.warn("Incomplete Queue external IDs", e);
113                 }
114
115                 List<QueuesOtherConfig> otherConfigs = queueEntry.getQueuesOtherConfig();
116                 if (otherConfigs != null) {
117                     Map<String, String> otherConfigsMap = new HashMap<>();
118                     for (QueuesOtherConfig otherConfig : otherConfigs) {
119                         otherConfigsMap.put(otherConfig.getQueueOtherConfigKey(), otherConfig.getQueueOtherConfigValue());
120                     }
121                     try {
122                         queue.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
123                     } catch (NullPointerException e) {
124                         LOG.warn("Incomplete Queue other_config", e);
125                     }
126                 }
127                 if (uuid == null) {
128                     transaction.add(op.insert(queue)).build();
129                 } else {
130                     transaction.add(op.update(queue)).build();
131                     Queue extraQueue = TyperUtils.getTypedRowWrapper(
132                             transaction.getDatabaseSchema(), Queue.class, null);
133                     extraQueue.getUuidColumn().setData(uuid);
134                     transaction.add(op.update(queue.getSchema())
135                             .where(extraQueue.getUuidColumn().getSchema().opEqual(uuid)).build());
136                 }
137                 transaction.build();
138             }
139         }
140     }
141
142     private Uuid getQueueEntryUuid(List<Queues> operQueues, Uri queueId) {
143         if (operQueues != null && !operQueues.isEmpty()) {
144             for (Queues queueEntry : operQueues) {
145                 if (queueEntry.getQueueId().equals(queueId)) {
146                     return queueEntry.getQueueUuid();
147                 }
148             }
149         }
150         return null;
151     }
152 }