2 * Copyright © 2024 Smartoptics and others. All rights reserved.
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
9 package org.opendaylight.transportpce.renderer.provisiondevice.transaction.history;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Collections;
14 import java.util.HashSet;
15 import java.util.LinkedHashSet;
16 import java.util.List;
18 import org.opendaylight.transportpce.renderer.provisiondevice.transaction.DeviceInterface;
19 import org.opendaylight.transportpce.renderer.provisiondevice.transaction.Transaction;
20 import org.opendaylight.transportpce.renderer.provisiondevice.transaction.delete.Delete;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * A class keeping track of transaction history.
28 * A transaction can be something like an interface or a roadm connection, that may need to be
29 * rolled back in the future.
31 public class TransactionHistory implements History {
33 private static final Logger LOG = LoggerFactory.getLogger(TransactionHistory.class);
34 Set<Transaction> transactionHistory = Collections.synchronizedSet(new LinkedHashSet<>());
37 public boolean add(Transaction transaction) {
39 boolean result = transactionHistory.add(transaction);
42 LOG.info("Adding {}", transaction.description());
44 LOG.warn("Transaction {} not added.", transaction.description());
51 public boolean add(List<Transaction> transactions) {
52 Set<Boolean> results = new HashSet<>(transactions.size());
54 for (Transaction transaction : transactions) {
55 results.add(add(transaction));
58 return results.stream().allMatch(i -> (i.equals(Boolean.TRUE)));
62 public boolean addInterfaces(String nodeId, String interfaceId) {
63 return addInterfaces(nodeId, Collections.singletonList(interfaceId));
67 public boolean addInterfaces(String nodeId, String[] interfaceIds) {
69 return addInterfaces(nodeId, Arrays.asList(interfaceIds));
74 public boolean addInterfaces(String nodeId, List<String> interfaceIds) {
76 Set<Boolean> results = new HashSet<>();
77 Set<String> unique = new LinkedHashSet<>();
79 for (String interfaceId : interfaceIds) {
80 if (interfaceId != null && !interfaceId.trim().isEmpty()) {
81 unique.add(interfaceId.trim());
85 for (String interfaceId : unique) {
86 results.add(this.add(new DeviceInterface(nodeId, interfaceId)));
89 return results.stream().allMatch(i -> (i.equals(Boolean.TRUE)));
94 public boolean rollback(Delete delete) {
96 LOG.info("History contains {} items. Rolling them back in reverse order.",
97 transactionHistory.size());
99 List<Transaction> reverse = new ArrayList<>(transactionHistory);
101 Collections.reverse(reverse);
103 boolean success = true;
105 for (Transaction transaction : reverse) {
106 LOG.info("Rolling back {}", transaction.description());
107 if (!transaction.rollback(delete)) {