summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-plugin/src/main/java/org/openecomp/core/zusammen/plugin/collaboration/RevertService.java
blob: 0d2ea2c0d889e2e765b06ff1c06098db84091bbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package org.openecomp.core.zusammen.plugin.collaboration;

import com.amdocs.zusammen.datatypes.Id;
import com.amdocs.zusammen.datatypes.SessionContext;
import com.amdocs.zusammen.datatypes.item.ElementContext;
import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity;
import org.openecomp.core.zusammen.plugin.dao.types.SynchronizationStateEntity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class RevertService {

  private ElementPublicStore elementPublicStore;
  private ElementPrivateStore elementPrivateStore;

  public RevertService(ElementPublicStore elementPublicStore,
                       ElementPrivateStore elementPrivateStore) {
    this.elementPublicStore = elementPublicStore;
    this.elementPrivateStore = elementPrivateStore;
  }

  public void revert(SessionContext context, Id itemId, Id versionId, Id revisionId) {
    ElementContext targetContext = new ElementContext(itemId, versionId);
    ElementContext sourceContext = new ElementContext(itemId, versionId, revisionId);
    copyElementsFromPublic(context, sourceContext, targetContext);
  }

  private void copyElementsFromPublic(SessionContext context, ElementContext sourceContext,
                                      ElementContext targetContext) {
    Collection<RevertElementAction> revertElementActions =
        evaluateRevertElementActions(context, sourceContext, targetContext);

    revertElementActions.forEach(revertElementAction -> revertElementAction.run(context));
  }

  private Collection<RevertElementAction> evaluateRevertElementActions(SessionContext context,
                                                                       ElementContext sourceContext,
                                                                       ElementContext targetContext) {

    Map<Id, Id> sourceElements = elementPublicStore.listIds(context, sourceContext);
    Map<Id, Id> targetPublicElements = elementPublicStore.listIds(context, targetContext);
    Collection<SynchronizationStateEntity> synchronizationStateEntities =
        elementPrivateStore.listSynchronizationStates(context, targetContext);

    Map<Id, Id> targetElements =
        evaluateTargetElements(targetPublicElements, synchronizationStateEntities);


    Collection<RevertElementAction> revertElementActions = new ArrayList<>();

    sourceElements.entrySet().forEach(entry -> {
      Id sourceElementId = entry.getKey();
      Id sourceElementRevisionId = entry.getValue();

      if (!targetElements.containsKey(sourceElementId)) {
        revertElementActions
            .add(new RevertElementAction(sourceContext, sourceElementId, commands[CREATE]));
      } else if (!targetElements.get(sourceElementId).equals(sourceElementRevisionId)) {
        revertElementActions
            .add(new RevertElementAction(sourceContext, sourceElementId, commands[UPDATE]));
      }
    });

    targetElements.entrySet().forEach(entry -> {
      Id targetElementId = entry.getKey();
      if (!sourceElements.containsKey(targetElementId)) {
        revertElementActions
            .add(new RevertElementAction(targetContext, targetElementId, commands[DELETE]));
      }
    });

    return revertElementActions;
  }

  private Map<Id, Id> evaluateTargetElements(Map<Id, Id> targetPublicElements,
                                             Collection<SynchronizationStateEntity> syncStates) {
    Map<Id, Id> targetElements = new HashMap<>(targetPublicElements);
    syncStates.stream()
        .filter(SynchronizationStateEntity::isDirty)
        .forEach(syncState -> targetElements.put(syncState.getId(), Id.ZERO));
    return targetElements;
  }

  private static class RevertElementAction {
    private ElementContext elementContext;
    private Id elementId;
    private ActionCommand command;

    private RevertElementAction(ElementContext elementContext, Id elementId,
                                ActionCommand command) {
      this.elementContext = elementContext;
      this.elementId = elementId;
      this.command = command;
    }

    public ElementContext getElementContext() {
      return elementContext;
    }

    public Id getElementId() {
      return elementId;
    }

    public void run(SessionContext context) {
      command.run(context, elementContext, elementId);
    }
  }

  private interface ActionCommand {
    void run(SessionContext context, ElementContext elementContext, Id elementId);
  }

  private static int CREATE = 0;
  private static int UPDATE = 1;
  private static int DELETE = 2;

  private ActionCommand[] commands = {new ActionCommand() {
    @Override
    public void run(SessionContext context, ElementContext elementContext, Id elementId) {
      //create
      Optional<ElementEntity> element = elementPublicStore.get(context, elementContext, elementId);
      if (!element.isPresent()) {
        throw getMissingElementException(elementContext, elementId);
      }
      elementPrivateStore.create(context, elementContext, element.get());
    }
  }, new ActionCommand() {
    @Override
    public void run(SessionContext context, ElementContext elementContext, Id elementId) {
      //update
      Optional<ElementEntity> element = elementPublicStore.get(context, elementContext, elementId);
      if (!element.isPresent()) {
        throw getMissingElementException(elementContext, elementId);
      }
      elementPrivateStore.update(context, elementContext, element.get());
    }
  }, new ActionCommand() {
    @Override
    public void run(SessionContext context, ElementContext elementContext, Id elementId) {
      //delete
      Optional<ElementEntity> element = elementPrivateStore.get(context, elementContext, elementId);
      if (!element.isPresent()) {
        return; // deleted by parent when hierarchy was deleted
      }
      elementPrivateStore.delete(context, elementContext, element.get());
    }
  }};

  private RuntimeException getMissingElementException(ElementContext elementContext,
                                                      Id elementId) {
    return new IllegalStateException(
        String.format("Item Id %s, version Id %s, revision Id %s: Missing element with Id %s",
            elementContext.getItemId().getValue(), elementContext.getVersionId().getValue(),
            elementContext.getRevisionId().getValue(), elementId.getValue())
    );
  }
}