StateConfig.java 16.4 KB
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
package th.co.ais.ssbsrfc.config;

import th.co.ais.ssbsrfc.instance.CheckRequest;
import th.co.ais.ssbsrfc.instance.EC02Instance;
import th.co.ais.ssbsrfc.utils.Log;
import ec02.af.abstracts.AbstractAF;
import ec02.af.data.EquinoxRawData;
import ec02.utils.AppLog;

public class StateConfig {
	
	//define project name
	public static final String PROJECT_NAME = "LBCF";
	public static String PROJECT_STAT_NAME = PROJECT_NAME;
	public static final boolean isMultiFlow = false;
	
	//define main state
	public static final String STATE_IDLE = "IDLE";
	public static final String STATE_ACTIVE = "ACTIVE";
	public static final String STATE_E11_TIMEOUT = "E11_TIMEOUT";
	
	//define sub state
	public static final String SSTATE_UNKNOWN = "UNKNOWN";
	public static final String SSTATE_REJECT = "REJECT";
	public static final String SSTATE_ABORT = "ABORT";
	public static final String SSTATE_BEGIN = "BEGIN";
	public static final String SSTATE_END = "END";
	public static final String SSTATE_W_A = "W_A";
	public static final String SSTATE_W_CHARGEREQUEST = "W_CHARGEREQUEST";
	public static final String SSTATE_W_CHARGEREPORT = "W_CHARGEREPORT";
	public static final String SSTATE_W_ADJUSTMENT_CHARGEREQUEST = "W_ADJUSTMENTRESPONSE_CHARGEREQUEST";
	public static final String SSTATE_W_ADJUSTMENT_CHARGEREPORT = "W_ADJUSTMENTRESPONSE_CHARGEREPORT";
	//define event
	public static final String Incoming_Unknown ="Incoming_Unknown";
	public static final String Incoming_REJECT ="Incoming_REJECT";
	public static final String Incoming_ABORT ="Incoming_ABORT";
	public static final String Incoming_A_Request = "Incoming_A_Request";
	public static final String Incoming_ChargeRequest_Request = "Incoming_ChargeRequest_Request";
	public static final String Incoming_ChargeReport_Request = "Incoming_ChargeReport_Request";
	public static final String Incoming_Adjustment_ChargeRequest_Response = "Incoming_Adjustment_ChargeRequest_Response";
	public static final String Incoming_Adjustment_ChargeReport_Response = "Incoming_Adjustment_ChargeReport_Response";
	
	//define LASE (Log, Alarm, Stat, Error)
	public static final String  LASE_Unknown = "Unknown Command";
	public static final String  LASE_Reject = "Unknown Reject";
	public static final String  LASE_Abort = "Unknown Abort";
	
	// define frist state of each flow
	public static final String[] arrFirstStateFlow = {
			SSTATE_W_CHARGEREQUEST,SSTATE_W_CHARGEREPORT,
		
	}; // SSTATE_UNKNOWN
	
	//define sub state list and messages that relate to
	
	public static State subStateList[] = {
		new State(SSTATE_REJECT, Incoming_REJECT, LASE_Reject),
		new State(SSTATE_ABORT, Incoming_ABORT, LASE_Abort),
		new State(SSTATE_END, Incoming_Unknown, LASE_Unknown),

		new State(SSTATE_W_CHARGEREQUEST, Incoming_ChargeRequest_Request, "Test A", "Test A", Constant.TYPE_REQUEST, Constant.FLOW_TEST_A), 
		new State(SSTATE_W_CHARGEREPORT, Incoming_ChargeReport_Request, "Test A", "Test A", Constant.TYPE_REQUEST, Constant.FLOW_TEST_A), 
		new State(SSTATE_W_ADJUSTMENT_CHARGEREQUEST, Incoming_Adjustment_ChargeRequest_Response, "Test A", "Test A", Constant.TYPE_REQUEST, Constant.FLOW_TEST_A), 
		new State(SSTATE_W_ADJUSTMENT_CHARGEREPORT, Incoming_Adjustment_ChargeReport_Response, "Test A", "Test A", Constant.TYPE_REQUEST, Constant.FLOW_TEST_A), 
//		new State(SSTATE_W_A, Incoming_A_Request, "Test A", "Test A", Constant.TYPE_RESPONSE), 
		new State(SSTATE_END, Incoming_Unknown,"Unknown_Command"),
	};
	
	//define event type message filter
	public static final EventFilter[] eventFilter = {
		// Flow 0
			new EventFilter(Incoming_ChargeRequest_Request, "request", "text/plain", null, null),
			new EventFilter(Incoming_ChargeReport_Request, "request", "text/plain", null, null),
			new EventFilter(Incoming_Adjustment_ChargeRequest_Response, "request", "text/plain", null, null),
			new EventFilter(Incoming_Adjustment_ChargeReport_Response, "request", "text/plain", null, null),
	};
	
	public static String getSubStateFromEventType(String eventType)
	{
		String subState="";
		try {
			// find sub state to handler event type
			for(int i=0; i < subStateList.length; i++)
			{
				State state = subStateList[i];
				String ssName = state.name;
				String ssEventType = state.eventType;
				
				// if we found matching relation
				if(eventType.equals(ssEventType))
				{
					subState = ssName;
					break;
				}
			}
		} 
		catch (Exception e) {
			AppLog.e("ERROR: getSubStateFromEventType!! eventType:" + eventType + "desc: " + e);
		}
		return subState;
	}
	
	public static String getEventTypeFromSubState(String subState)
	{
		String eventType = "";
		try {
			// find sub state to handler event type
			for(int i=0; i < subStateList.length; i++)
			{
				State state = subStateList[i];
				String sstate = state.name;
				String ssEventType = state.eventType;
				
				// if we found matching relation
				if(sstate.equals(subState))
				{
					eventType = ssEventType;
					break;
				}
			}
		} 
		catch (Exception e) {
			AppLog.e("ERROR: getEventTypeFromSubState!! subState:" + subState + "desc: " + e);
		}
		return eventType;
	}
	
	public static String getCommandFromEventType(String eventType)
	{
		String command="";
		try {
			// find sub state to handler event type
			for(int i=0; i < subStateList.length; i++)
			{
				State state = subStateList[i];
				String ssCommand = state.nameReq;
				String ssEventType = state.eventType;
				
				// if we found matching relation
				if(eventType.equals(ssEventType))
				{
					command = ssCommand;
					break;
				}
			}
		} 
		catch (Exception e) {
			AppLog.e("ERROR: getSubStateFromEventType!! eventType:" + eventType + "desc: " + e);
		}
		return command;
	}
	
	public static void changeCommandFromEventType(String eventType, String command)
	{
		try {
			// find sub state to handler event type
			for(int i=0; i < subStateList.length; i++)
			{
				State state = subStateList[i];
				String ssEventType = state.eventType;
				
				// if we found matching relation
				if(eventType.equals(ssEventType))
				{
					subStateList[i].setLASEName(command);
					break;
				}
			}
		} 
		catch (Exception e) {
			AppLog.e("ERROR: changeCommangFromEventType!! eventType:" + eventType + " new command:" + command + " desc: " + e);
		}
	}
	
	public static int getFlowFromEventType(String eventType)
	{
		int flow = -1;
		try {
			// find sub state to handler event type
			for(int i=0; i < subStateList.length; i++)
			{
				State state = subStateList[i];
				String ssEventType = state.eventType;
				int ssFlow = state.flow;
				
				// if we found matching relation
				if(eventType.equals(ssEventType))
				{
					flow = ssFlow;
					break;
				}
			}
		} 
		catch (Exception e) {
			AppLog.e("ERROR: getFlowFromEventType!! eventType:" + eventType + "desc: " + e);
		}
		return flow;
	}
	
	public static String getCommandFromState(String subState)
	{
		String command="";
		try {
			// find sub state to handler event type
			for(int i=0; i < subStateList.length; i++)
			{
				State state = subStateList[i];
				String ssCommand = state.nameReq;
				String ssState = state.name;
				
				// if we found matching relation
				if(subState.equals(ssState))
				{
					command = ssCommand;
					break;
				}
			}
		} 
		catch (Exception e) {
			AppLog.e("ERROR: getSubStateFromEventType!! eventType:" + subState + "desc: " + e);
		}
		return command;
	}
	
	public static String getCommandFromStateAndType(String subState, int type)
	{
		String command="";
		try {
			// find sub state to handler event type
			for(int i=0; i < subStateList.length; i++)
			{
				State state = subStateList[i];
				String ssCmdReq= state.nameReq;
				String ssCmdRes= state.nameRes;
				String ssState = state.name;
				
				// if we found matching relation
				if(subState.equals(ssState))
				{
					if (type == Constant.TYPE_REQUEST) {
						command = ssCmdReq;
					} else if (type == Constant.TYPE_RESPONSE) {
						command = ssCmdRes;
					}
					break;
				}
			}
		} 
		catch (Exception e) {
			AppLog.e("ERROR: getSubStateFromEventType!! eventType:" + subState + "desc: " + e);
		}
		return command;
	}
	
	public static int getSubStateIndex(String currentState)
	{
		int idx=-1;
		// find sub state to handler event type
		for(int i=0; i < subStateList.length; i++)
		{
			State state = subStateList[i];
			String ssName = state.name;
			
			// if we found matching relation
			if(currentState.equals(ssName))
			{
				idx = i;
			}
		}
		return idx;
	}
	
	public static boolean isFirstState(String state) 
	{
		boolean first = false;
		for (String _state : arrFirstStateFlow) {
			if (_state.contains(state)) {
				first = true;
				break;
			}
		}
		return first;
	}
	
	public static void setNextSubState(EC02Instance ec02Instance)
	{
		// get current state
		String currentState = ec02Instance.getAFInstance().getCurrentState();
		String nextState = StateConfig.getNextSubState(currentState);
		ec02Instance.getAFInstance().setCurrentState(nextState);
		// end
	}
	
	public static String getNextSubState(String currentState)
	{
		if(currentState.equals(SSTATE_END))
		{
			return SSTATE_END;
		}
		
		int idx = getSubStateIndex(currentState);
		if(idx != -1)
		{
			idx++;
			State state = subStateList[idx];
			String ssName = state.name;
			return ssName;
		} 
		else	//if not found 
		{
			return SSTATE_UNKNOWN;
		}
	}
	
	public static String getEventTypeFromInvoke(String invokeMsg)
	{
		String eventType = null;
		Invoke invoke = new Invoke(invokeMsg);
		eventType = invoke.getEventType();
		return eventType;
	}
	
	public static boolean isValidateEventType(String eventType)
	{
		boolean isValidate = false;

		// find sub state to handler event type
		for(int i=0; i < subStateList.length; i++)
		{
			State state = subStateList[i];
			String ssEventType = state.eventType;
			
			// if we found matching relation
			if(ssEventType.equals(eventType))
			{
				isValidate = true;
			}
		}
		return isValidate;
	}
	
	public static String getEventTypeFromEquinoxRawData(EC02Instance ec02Instance, EquinoxRawData rawData)
	{	
		String eventType = null;
		// 1.1.1. Extract type, ctype, method, invoke, ret attributes.
		String type = rawData.getRawDataAttribute("type");
		String ctype = rawData.getRawDataAttribute("ctype");
		String method = rawData.getRawDataAttribute("method");
		String invoke = rawData.getInvoke();
		String cmd = "";
		/*String ret = rawData.getRawDataAttribute("ret");
		// 1.1.2. If ret in (1, 2, 3, 4) Then
		if (ret.equals("2")) {
			return StateConfig.Incoming_REJECT;
		} else if (ret.equals("3")) {
			return StateConfig.Incoming_ABORT;
		}*/
//		if (type.equals("response")) {
//			ArrayList<Timeout> listTimeout = ec02Instance.getAFInstance().getListTimeout();
//			boolean isBool = false;
//			for (Timeout timeout : listTimeout) {
//				if (timeout.getName().equals(invoke)) {
//					isBool = true;
//				}
//			}
//			if (!isBool) {
//				return StateConfig.Incoming_Unknown;
//			}
//		}
		
		// 1.1.3. Try to get event type from invoke
		if(invoke != null && invoke.length() >0)
		{
			// 1.1.4. If valid event type Then
			String event = getEventTypeFromInvoke(invoke);
			if(event != null && isValidateEventType(event))
			{
				// 1.1.4.1. Set event type = <valid event type>
				return event;
			}
		}
		
		// 1.1.6. Get event type from type, ctype, method, cmd
		int maxCount = 0;
		for(int i=0; i < eventFilter.length; i++)
		{
			EventFilter ef = eventFilter[i];
			boolean bMatch = false;
			int count = 0;	//use for count matched in each parameter
			
			// try matching 1 by 1
			// check type
			if(ef.type != null)
			{
				if(type.equals(ef.type))
				{
					bMatch = true;
					count++;
				}
				else
					bMatch = false;
			}
			
			// check ctype
			if(ef.ctype != null)
			{
				if(ctype.equals(ef.ctype))
				{
					bMatch = true;
					count++;
				}
				else
					bMatch = false;
			}
			
			// check method
			if(ef.method != null)
			{
				if(method.equals(ef.method))
				{
					bMatch = true;
					count++;
				}
				else
					bMatch = false;
			}
			
			// check cmd
			if(ef.cmd != null)
			{
				if(cmd.equals(ef.cmd))
				{
					bMatch = true;
					count++;
				}
				else
					bMatch = false;
			}
			//check and set maxCount for use to select event type
			if(bMatch && count > maxCount)
			{
				maxCount = count;
				
				//set return event type to this matched
				eventType = ef.eventType;
			}
			if (eventType.equals(StateConfig.Incoming_ChargeRequest_Request)) {
				//String page = rawData.getRawDataMessage();
				String page = rawData.getRawDataAttribute("val");
				CheckRequest checkRequest = new CheckRequest();
				checkRequest.setDn(page);
				String [] dnList = checkRequest.getDn().split("&", -1);
				page = dnList[0].substring("page=".length());
				if (page.equals("chargeRequest")) {
					eventType = StateConfig.Incoming_ChargeRequest_Request;
				} else if (page.equals("chargeReport")) {
					eventType = StateConfig.Incoming_ChargeReport_Request;
				}
			}
		}
		return eventType;
	}
	
	public static String getLASEName(String currentSubState, int sendOrReceive, int responseOrRequest, int missingOrInvalidOrTARE, String errMsg)
	{
		String name = StateConfig.getLASEName(currentSubState, sendOrReceive, responseOrRequest, missingOrInvalidOrTARE, errMsg, null);
		return name;
	}
	
	public static String getLASEName(String currentSubState, int sendOrReceive, int responseOrRequest, int missingOrInvalidOrTARE, String errMsg, String command, int flow)
	{
		switch (flow) {
			default:
				StateConfig.PROJECT_STAT_NAME = StateConfig.PROJECT_NAME;
				break;
		}
		
		String name = StateConfig.getLASEName(currentSubState, sendOrReceive, responseOrRequest, missingOrInvalidOrTARE, errMsg, command);
		return name;
	}
	
	public static String getLASEName(String currentSubState, int sendOrReceive, int responseOrRequest, int missingOrInvalidOrTARE, String errMsg, String command)
	{
//		AppLog.d("## [mSSB-SRFC] [Send|Received] [Bad] <CommandName> [Response|Request] [Timeout|Abort|Reject|Error] [errMsg]");
//		AppLog.d("## getLASEName :" + currentSubState + "/" + sendOrReceive + "/" + responseOrRequest + "/" + missingOrInvalidOrTARE + "/" + errMsg);
		String stateName = null;
//		int idx = StateConfig.getSubStateIndex(currentSubState);
//		State state = StateConfig.subStateList[idx];
		String LASEName = StateConfig.getCommandFromStateAndType(currentSubState, responseOrRequest);
		
		/*
		 * [mSSB-SRFC] [Send|Received] [Bad] <CommandName> [Response|Request] [Timeout|Abort|Reject|Error]
		 */
		
		String sr = "";
		
		switch (sendOrReceive)
		{
			case Constant.TYPE_SEND:
				sr = Constant.SEND;
				break;
			case Constant.TYPE_RECEIVE:
				sr = Constant.RECEIVE;
				break;
		}
		
		String rr = "";
		switch (responseOrRequest)
		{
			case Constant.TYPE_RESPONSE:
				rr = Constant.RESPONSE + " ";
				break;
			case Constant.TYPE_REQUEST:
				rr = Constant.REQUEST + " ";
				break;
		}
		
		//Timeout, Abort, Reject, Error
		String tare = "";
		String bad = "";
		String cmd = "";
		switch (missingOrInvalidOrTARE)
		{
			case Constant.TYPE_MISSING:
				bad = Constant.BAD + " ";
				break;
			case Constant.TYPE_INVALID:
				bad = Constant.BAD + " ";
				break;
			case Constant.TYPE_DUPLICATE:
				bad = Constant.DUPLICATE + " ";
				break;
			case Constant.TYPE_TIMEOUT:
				tare = Constant.TIMEOUT;
				break;
			case Constant.TYPE_ABORT:
				tare = Constant.ABORT;
				break;
			case Constant.TYPE_REJECT:
				tare = Constant.REJECT;
				break;
			case Constant.TYPE_ERROR:
			case Constant.TYPE_UNKNOWN_ERROR:
				tare = Constant.ERROR;
				break;
		}
		
		if (command != null) {
			cmd = command;
		} else {
			if (missingOrInvalidOrTARE == Constant.TYPE_REJECT_APPLICATION) {
				cmd = "Unknown";
				rr = Constant.REQUEST + " ";
			} else {
				cmd = LASEName;
			}
		}
		
		stateName = StateConfig.PROJECT_STAT_NAME + " " + sr + " " + bad + cmd + " " + rr + tare;
		stateName = stateName.trim();
		
		return stateName;
	}
	
	public static String getTimeout(AbstractAF abstractAF, String event) {
		String timeout = StateConfig.getTimeout(abstractAF, event, null);
		return timeout;
	}
	
	public static String getTimeout(AbstractAF abstractAF, String event, String command) {
		String time = "10";
		String configName = "Default-timeout";
		try {
			if (event.equals(StateConfig.Incoming_Unknown)) { 
				configName = "Wait-Get-File-Timeout";
			} else {
				configName = "Default-timeout";
			}

			time = abstractAF.getUtils().getHmWarmConfig().get(configName).get(0);
			AppLog.d("## SET TIMEOUT: " + configName + " " + time + " sec");
		} catch (Exception e) {
			if (Log.debug) {
				AppLog.d("## CHECKCONFIG EC02 READDING...");
				AppLog.d("## EXCEPTION : " + e.getMessage());
				AppLog.d("## DATA EVENTTYPE OR INVOKE:" + event + " COMMAND:" + command);
			}
		}
		return time;
	}
}