summaryrefslogtreecommitdiffstats
path: root/dcaedt_catalog/commons/src/main/java/org/onap/sdc/dcae/catalog/commons/Actions.java
blob: 132b0c00f981d20a824597e8d671341e08090c00 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package org.onap.sdc.dcae.catalog.commons;

import java.util.List;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;

import org.onap.sdc.common.onaplog.OnapLoggerDebug;
import org.onap.sdc.common.onaplog.OnapLoggerError;
import org.onap.sdc.common.onaplog.Enums.LogLevel;
import org.onap.sdc.dcae.catalog.commons.Action;
import org.onap.sdc.dcae.catalog.commons.Future;
import org.onap.sdc.dcae.catalog.commons.FutureHandler;
import org.onap.sdc.dcae.catalog.commons.Futures;

/**
 */
public interface Actions {

	/** */
	public static interface CompoundAction<T> extends Action<List<T>> {

		public CompoundAction<T> addAction(Action<T> theAction);

		public List<Action<T>> actions();

		public Future<List<T>> execute();
	} 


	public static class BasicCompoundAction<T> implements CompoundAction<T> {

		private LinkedList<Action<T>> actions = new LinkedList<Action<T>>();



		public CompoundAction<T> addAction(Action<T> theAction) {
			this.actions.add(theAction);
			return this;
		}

		public List<Action<T>> actions() {
			return this.actions;
		}

		public Future<List<T>> execute() {
			CompoundFuture<T> cf = new CompoundFuture<T>(this.actions.size());
			for (Action a: this.actions)
				cf.addFuture(a.execute());
			return cf;
		}
	}


	public static class CompoundFuture<T> extends Futures.BasicFuture<List<T>> {

		private static OnapLoggerError errLogger = OnapLoggerError.getInstance();
		private static OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();

		private LinkedList<Future<T>> futures = new LinkedList<Future<T>>();
		private FutureHandler<T>			hnd; 

		CompoundFuture(int theActionCount) {

			hnd = new Futures.BasicHandler<T>(new CountDownLatch(theActionCount)) {

							private List<T>	results = new ArrayList<T>(Collections.nCopies(theActionCount, null));

							protected void process(Future<T> theResult) {
								synchronized(CompoundFuture.this) {
									if (theResult.failed()) {
										CompoundFuture.this.cause(theResult.cause());
										//and stop processing of other results
										this.results = null;
										//??
									}
									else {
										if (this.results != null)
											this.results.set(futures.indexOf(theResult), theResult.result());
										debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Got result for action {}. Count at {}", futures.indexOf(theResult), this.latch.getCount());
									}
									if (this.latch.getCount() == 1) {//this was the last result
										debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Got all results: {}", this.results);
										CompoundFuture.this.result(this.results);
									}
								}
							}
						};
		}

		CompoundFuture<T> addFuture(Future<T> theFuture) {
			synchronized(this) {
				futures.add(theFuture);
				theFuture.setHandler(this.hnd);
			}
			return this;
		}

	}

/*
	public static class CompoundFutureHandler<T> implements FutureHandler<T> {

		protected List<T> 				result = null;
		protected List<Throwable>	error = null;
		protected CountDownLatch 	latch = null;

		CompoundFutureHandler(int theResultCount) {
			this(new CountDownLatch(theResultCount));
		}

		public void handle(Future<T> theResult) {
			if (this.latch != null) {
				this.latch.countDown();
			}
		}

		public T result()
															throws InterruptedException, RuntimeException {
			return result(true);
		}

		public BasicHandler<T> waitForCompletion() throws InterruptedException {
			this.latch.await();
			return this;
		}

	}
*/

	public static class Sequence<T> implements Action<List<T>> {

		private static OnapLoggerError errLogger = OnapLoggerError.getInstance();
		private static OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();

		private List<Action<T>> 	actions = new LinkedList<Action<T>>();
		private int								current = 0;
		private SequenceFuture<T> future = new SequenceFuture<T>();

		public Sequence<T> add(Action<T> theAction) {
			if (this.current > 0)
				throw new IllegalStateException("In execution");
			this.actions.add(theAction);
			return this;
		}

		/* we allow 'early' access to the future so that a client can pass its reference while
		 * it still builds the sequence, for example. 
		 */
		public Future<List<T>> future() {
			return this.future;
		}

		//need to add protection when for the 'no action' case
		public Future<List<T>> execute() {
			debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Starting serialized execution of {}", actions);
			if (hasNext())
				next().execute().setHandler(future.hnd);
			return this.future;
		}

		protected boolean hasNext() {
			return this.current < actions.size();
		}

		protected Action next() {
			return actions.get(this.current++);
		} 
	
		private class SequenceFuture<T> extends Futures.BasicFuture<List<T>> {

			private List<T>					 results = new LinkedList<T>();
			private FutureHandler<T> hnd = new Futures.BasicHandler<T>() {

				protected void process(Future<T> theResult) {
				
					if (theResult.failed()) {
						SequenceFuture.this.cause(theResult.cause());
						//and stop processing of other results
					}
					else {
						SequenceFuture.this.results.add(theResult.result());
						if (Sequence.this.hasNext()) {
							Sequence.this.next().execute().setHandler(this);
						}
						else {
							SequenceFuture.this.result(SequenceFuture.this.results);
						}
					}
				}
			};


		}	



	}

}