AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / impl / AbsTrans.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.misc.env.impl;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Stack;
27
28 import org.onap.aaf.misc.env.Env;
29 import org.onap.aaf.misc.env.LogTarget;
30 import org.onap.aaf.misc.env.Slot;
31 import org.onap.aaf.misc.env.StoreImpl;
32 import org.onap.aaf.misc.env.TimeTaken;
33 import org.onap.aaf.misc.env.TransStore;
34
35 public abstract class AbsTrans<ENV extends Env> implements TransStore {
36         private static final float[] EMPTYF = new float[0];
37         private static final Object[] EMPTYO = new Object[0];
38         
39         protected ENV delegate;
40         protected List<TimeTaken> trail = new ArrayList<TimeTaken>(30);
41         private Object[] state;
42         
43         
44     public AbsTrans(ENV delegate) {
45                 this.delegate = delegate;
46                 state = delegate instanceof StoreImpl?((StoreImpl) delegate).newTransState():EMPTYO;
47         }
48
49         //      @Override
50         public LogTarget fatal() {
51                 return delegate.fatal();
52         }
53
54 //      @Override
55         public LogTarget error() {
56                 return delegate.error();
57         }
58
59 //      @Override
60         public LogTarget audit() {
61                 return delegate.audit();
62         }
63
64 //      @Override
65         public LogTarget init() {
66                 return delegate.init();
67         }
68
69 //      @Override
70         public LogTarget warn() {
71                 return delegate.warn();
72         }
73
74 //      @Override
75         public LogTarget info() {
76                 return delegate.info();
77         }
78
79 //      @Override
80         public LogTarget debug() {
81                 return delegate.debug();
82         }
83
84 //      @Override
85         public LogTarget trace() {
86                 return delegate.trace();
87         }
88
89         /**
90          * Let the final Trans Implementation choose the exact kind of TimeTaken to use
91          * @param name
92          * @param flag
93          * @return
94          */
95         protected abstract TimeTaken newTimeTaken(String name, int flag);
96         
97 //      @Override
98         public final TimeTaken start(String name, int flag) {
99                 TimeTaken tt = newTimeTaken(name,flag);
100                 trail.add(tt);
101                 return tt;
102         }
103         
104 //      @Override
105         public final void checkpoint(String name) {
106                 TimeTaken tt = newTimeTaken(name,CHECKPOINT);
107                 tt.done();
108                 trail.add(tt);
109         }
110
111         public final void checkpoint(String name, int additionalFlag) {
112                 TimeTaken tt = newTimeTaken(name,CHECKPOINT|additionalFlag);
113                 trail.add(tt);
114                 tt.done();
115         }
116
117         @Override
118         public Metric auditTrail(int indent, StringBuilder sb, int ... flags) {
119                 return auditTrail(info(),indent,sb,flags);
120         }
121         
122         @Override
123         public Metric auditTrail(LogTarget lt, int indent, StringBuilder sb, int ... flags) {
124                 Metric metric = new Metric();
125                 int last = (metric.entries = trail.size()) -1;
126                 metric.buckets = flags.length==0?EMPTYF:new float[flags.length];
127                 if(last>=0) {
128                         TimeTaken first = trail.get(0);
129                         // If first entry is sub, then it's actually the last "end" as well
130                         // otherwise, check end
131                         //long end = (first.flag&SUB)==SUB?first.end():trail.get(last).end();
132                         long end = trail.get(last).end();
133                         metric.total = (end - first.start) / 1000000f;
134                 }
135                 
136                 if(sb==null) {
137                         for(TimeTaken tt : trail) {
138                                 float ms = tt.millis();
139                                 for(int i=0;i<flags.length;++i) {
140                                         if(tt.flag == flags[i]) metric.buckets[i]+=ms;
141                                 }
142                         }
143                 } else if(!lt.isLoggable()) {
144                         boolean first = true;
145                         for(TimeTaken tt : trail) {
146                                 float ms = tt.millis();
147                                 for(int i=0;i<flags.length;++i) {
148                                         if(tt.flag == flags[i]) metric.buckets[i]+=ms;
149                                 }
150                                 if((tt.flag&ALWAYS)==ALWAYS) {
151                                         if(first) first = false;
152                                         else sb.append('/');
153                                         sb.append(tt.name);
154                                 }
155                         }                       
156                 } else {
157                         Stack<Long> stack = new Stack<Long>();
158                         for(TimeTaken tt : trail) {
159                                 // Create Indentation based on SUB
160                                 while(!stack.isEmpty() && tt.end()>stack.peek()) {
161                                         --indent;
162                                         stack.pop();
163                                 }
164                                 for(int i=0;i<indent;++i) {
165                                         sb.append("  ");
166                                 }
167                                 tt.output(sb);
168                                 sb.append('\n');
169                                 if((tt.flag&SUB)==SUB) {
170                                         stack.push(tt.end());
171                                         ++indent;
172                                 }
173                                 
174                                 // Add time values to Metric
175                                 float ms = tt.millis();
176                                 for(int i=0;i<flags.length;++i) {
177                                         if(tt.flag == flags[i]) metric.buckets[i]+=ms;
178                                 }
179                         }
180                 }
181                 return metric;
182         }
183
184         /**
185          * Put data into the Trans State at the right slot 
186          */
187 //      @Override
188         public void put(Slot slot, Object value) {
189                 slot.put(state, value);
190         }
191
192         /**
193          *  Get data from the Trans State from the right slot
194          *  
195          *  This will do a cast to the expected type derived from Default
196          */
197 //      @Override
198         @SuppressWarnings("unchecked")
199         public<T> T get(Slot slot, T deflt) {
200                 Object o;
201                 try {
202                         o = slot.get(state);
203                 } catch(ArrayIndexOutOfBoundsException e) {
204                         // Env State Size has changed because of dynamic Object creation... Rare event, but needs to be covered
205                         Object[] temp = ((StoreImpl) delegate).newTransState();
206                         System.arraycopy(state, 0, temp, 0, state.length);
207                         state = temp;
208                         o=null;
209                 }
210                 return o==null?deflt:(T)o;
211         }
212
213
214 }