1 package de.siemens.icn.hipath.dls.apiv100;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.net.URL;
9 import java.util.Arrays;
10import java.util.Stack;
11
12import de.siemens.icn.hipath.dls.apiv100.device.Attribute;
13import de.siemens.icn.hipath.dls.apiv100.device.AttributeDescription;
14import de.siemens.icn.hipath.dls.apiv100.device.Device;
15import de.siemens.icn.hipath.dls.apiv100.device.DeviceDescription;
16import de.siemens.icn.hipath.dls.apiv100.device.FunctionDescription;
17import de.siemens.icn.hipath.dls.apiv100.device.Key;
18import de.siemens.icn.hipath.dls.apiv100.device.ModuleDescription;
19
20
56public final class DlsAPIv100TestClient
57{
58 private static final String UI_STATE_START = "START";
59 private static final String UI_STATE_LOGIN_PERFORMED = "LOGIN_PERFOMRED";
60 private static final String UI_STATE_DEVICE_SELECTED = "DEVICE_SELECTED";
61 private static final String UI_STATE_MODULE_SELECTED = "MODULE_SELECTED";
62 private static final String UI_STATE_LEVEL_SELECTED = "LEVEL_SELECTED";
63 private static final String UI_STATE_KEY_SELECTED = "KEY_SELECTED";
64 private static final String UI_STATE_SW_TYPE_SELECTED = "SOFTWARE_TYPE_SELECTED";
65 private static final String UI_STATE_SW_VERSION_SELECTED = "SOFTWARE_VERSION_SELECTED";
66 private static final String UI_STATE_FUNCTION_SELECTED = "FUNCTION_SELECTED";
67 private static final String UI_STATE_ATTRIBUTES_SUBMITTED = "ATTRIBUTES_SUBMITTED";
68 private static final String UI_STATE_E164_SUBMITTED = "KEYS_SUBMITTED";
69 private static final String UI_STATE_STOP = "STOP";
70
71 private static final String INPUT_STOP = "exit";
72 private static final String INPUT_BACK = "<<";
73
74 private static final String DLS_API_URL = "http://localhost:18080/DeploymentService/services/DlsAPIv100";
77 private static final DeviceFactoryIf DEFAULT_DEVICE_FACTORY = new DeviceFactory();
79
80 private final BufferedReader reader;
81 private final DlsAPIv100 dlsApi;
82
83 private String input;
84
85 private String sessionId;
86 private DeviceDescription[] deviceDescriptions;
87 private FunctionDescription[] functionDescriptions;
88 private int selectedDeviceIndex = -1;
89 private int selectedModuleIndex = -1;
90 private int selectedLevelIndex = -1;
91 private int selectedKeyIndex = -1;
92 private String selectedSoftwareType;
93 private String selectedSoftwareVersion;
94 private int selectedFunctionIndex = -1;
95
96 private final Key configuredKey;
97 private final Device configuredDevice;
98
99 private Stack uiStateStack = new Stack();
00
01 public static void main(String[] args) throws Exception
02 {
03 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
04 DeviceFactoryIf deviceFactory = DEFAULT_DEVICE_FACTORY;
05 for (int i = 0; i < args.length; i++)
06 {
07 String[] fragments = args[i].split("=");
09 if (fragments.length == 2 && "input".equals(fragments[0]))
10 {
11 File inputFile = new File(fragments[1]);
12 if (inputFile.exists() && inputFile.isFile())
13 {
14 reader = new BufferedReader(new FileReader(inputFile));
15 }
16 else
17 {
18 System.out.println("File '" + inputFile.getAbsolutePath()
19 + "' is not a file! Using console input.");
20 }
21 }
22 else if (fragments.length == 2 && "factory".equals(fragments[0]))
25 {
26 String deviceFactoryClassname = fragments[1];
27 try
28 {
29 Class deviceFactoryClass = Class.forName(deviceFactoryClassname);
30 deviceFactory = (DeviceFactoryIf) deviceFactoryClass.newInstance();
31 }
32 catch (Exception e)
33 {
34 System.out.println("'" + deviceFactoryClassname + "' could not be loaded!");
35 }
36 }
37 }
38 DlsAPIv100TestClient testClient = new DlsAPIv100TestClient(deviceFactory, reader);
39 testClient.start();
40 }
41
42 private DlsAPIv100TestClient(DeviceFactoryIf deviceFactory, BufferedReader reader)
43 throws Exception
44 {
45 DlsAPIv100ServiceLocator serviceLocator = new DlsAPIv100ServiceLocator();
47
48 URL url = new URL(DLS_API_URL);
49
50 dlsApi = serviceLocator.getDlsAPIv100(url);
51 this.reader = reader;
52 this.configuredDevice = deviceFactory.createDevice();
53 this.configuredKey = deviceFactory.createKey();
54 }
55
56 private void start()
57 {
58 System.out.println(" ********************************************");
59 System.out.println(" **** Welcome to DLS API Testprogramm! ****");
60 System.out.println(" **** Please, follow the instructions or ****");
61 System.out.println(" **** - type 'exit' to exit the program, ****");
62 System.out.println(" **** - type '<<' to go one step back. ****");
63 System.out.println(" ********************************************");
64 System.out.println();
65
66 String uiState = UI_STATE_START;
67 nextUiState(uiState);
68
69 while (!UI_STATE_STOP.equals(uiState))
70 {
71 try
72 {
73 if (UI_STATE_START.equals(uiState))
74 {
75 handleLogin();
76 }
77 else if (UI_STATE_LOGIN_PERFORMED.equals(uiState))
78 {
79 handleChooseDevice();
80 }
81 else if (UI_STATE_DEVICE_SELECTED.equals(uiState))
82 {
83 handleChooseModule();
84 }
85 else if (UI_STATE_MODULE_SELECTED.equals(uiState))
86 {
87 handleChooseLevel();
88 }
89 else if (UI_STATE_LEVEL_SELECTED.equals(uiState))
90 {
91 handleChooseKey();
92 }
93 else if (UI_STATE_KEY_SELECTED.equals(uiState))
94 {
95 handleChooseSoftwareType();
96 }
97 else if (UI_STATE_SW_TYPE_SELECTED.equals(uiState))
98 {
99 handleChooseSoftwareVersion();
00 }
01 else if (UI_STATE_SW_VERSION_SELECTED.equals(uiState))
02 {
03 handleChooseFunction();
04 }
05 else if (UI_STATE_FUNCTION_SELECTED.equals(uiState))
06 {
07 handleSubmitAttributes();
08 }
09 else if (UI_STATE_ATTRIBUTES_SUBMITTED.equals(uiState))
10 {
11 handleSubmitE164();
12 }
13 else if (UI_STATE_E164_SUBMITTED.equals(uiState))
14 {
15 nextUiState(UI_STATE_START);
16 }
17 }
18 catch (Exception e)
19 {
20 System.out.println("The following error ocurred: ");
21 System.out.println(e.toString());
22
23 e.printStackTrace();
24 }
25 checkStopCondition();
26 checkBackCondition();
27
28 uiState = (String) uiStateStack.peek();
29 }
30 System.out.println();
31 System.out.println(" ****************************************");
32 System.out.println(" **** DLS API Testprogramm finished! ****");
33 System.out.println(" ****************************************");
34 }
35
36 private void checkStopCondition()
37 {
38 if (INPUT_STOP.equals(input))
39 {
40 nextUiState(UI_STATE_STOP);
41 }
42 }
43
44 private void checkBackCondition()
45 {
46 if (INPUT_BACK.equals(input))
47 {
48 previousUiState();
49 }
50 }
51
52 private void handleLogin() throws Exception
53 {
54 printSelection();
55
56 printMessage("Please login [username,password]: ");
57 String input = readString("");
58
59 String[] fragments = input.split(",");
60 if (fragments.length == 2)
61 {
62 String user = fragments[0].trim();
65 String password = fragments[1].trim();
66
67 DlsAPIout out = dlsApi.openSession(user, password);
70 this.sessionId = out.getResult();
71
72 if (this.sessionId != null && this.sessionId.length() > 0)
73 {
74 nextUiState(UI_STATE_LOGIN_PERFORMED);
75 }
76 else
77 {
78 throw new RuntimeException("Wrong user or password for [user, password]=[" + user
79 + "," + password + "]!");
80 }
81 }
82 }
83
84 private void handleChooseDevice() throws Exception
85 {
86 printSelection();
87
88 this.deviceDescriptions = dlsApi.getDeviceDescriptions(sessionId).getDeviceDescriptions();
90
91 printMessageAndDefault("Please choose a device description [0 - " + (deviceDescriptions.length - 1) + "]",
93 configuredDevice.getType() );
94 for (int i = 0; i < deviceDescriptions.length; i++)
95 {
96 System.out.println(i + ": " + deviceDescriptions[i].getDeviceType());
97 }
98
99 int index = readIndex(configuredDevice.getType());
01 if (0 <= index && index < deviceDescriptions.length)
02 {
03 this.selectedDeviceIndex = index;
04 nextUiState(UI_STATE_DEVICE_SELECTED);
05 }
06 else
07 {
08 this.selectedDeviceIndex = -1;
09 }
10 }
11
12 private void handleChooseModule() throws Exception
13 {
14 printSelection();
15
16 DeviceDescription selectedDevice = deviceDescriptions[selectedDeviceIndex];
17 ModuleDescription[] moduleDescriptions = selectedDevice.getModules();
19
20 printMessageAndDefault("Please choose a module description [0 - " + (moduleDescriptions.length - 1)
22 + "]", configuredKey.getModule());
23 for (int i = 0; i < moduleDescriptions.length; i++)
24 {
25 System.out.println(i + ": " + moduleDescriptions[i].getType());
26 }
27
28 int index = readIndex(configuredKey.getModule());
30 if (0 <= index && index < moduleDescriptions.length)
31 {
32 this.selectedModuleIndex = index;
33 nextUiState(UI_STATE_MODULE_SELECTED);
34 }
35 else
36 {
37 this.selectedModuleIndex = -1;
38 }
39 }
40
41 private void handleChooseLevel() throws Exception
42 {
43 printSelection();
44
45 DeviceDescription selectedDevice = deviceDescriptions[selectedDeviceIndex];
46 ModuleDescription moduleDescription = selectedDevice.getModules()[selectedModuleIndex];
47 int numberOfLevels = moduleDescription.getNumberOfLevels();
49
50 printMessageAndDefault("Please choose a level [0 - " + (numberOfLevels - 1) + "]", configuredKey.getLevel() );
52 int index = readIndex(configuredKey.getLevel());
53 if (0 <= index && index < numberOfLevels)
54 {
55 this.selectedLevelIndex = index;
56 nextUiState(UI_STATE_LEVEL_SELECTED);
57 }
58 else
59 {
60 this.selectedLevelIndex = -1;
61 }
62 }
63
64 private void handleChooseKey() throws Exception
65 {
66 printSelection();
67
68 DeviceDescription selectedDevice = deviceDescriptions[selectedDeviceIndex];
69 ModuleDescription moduleDescription = selectedDevice.getModules()[selectedModuleIndex];
70 int numberOfKeys = moduleDescription.getNumberOfKeys();
72
73 printMessageAndDefault("Please choose a key [0 - " + (numberOfKeys - 1) + "]", configuredKey.getNumber() );
75 int index = readIndex(configuredKey.getNumber());
76 if (0 <= index && index < numberOfKeys)
77 {
78 this.selectedKeyIndex = index;
79 nextUiState(UI_STATE_KEY_SELECTED);
80 }
81 else
82 {
83 this.selectedKeyIndex = -1;
84 }
85 }
86
87 private void handleChooseSoftwareType() throws Exception
88 {
89 printSelection();
90
91 DeviceDescription selectedDevice = deviceDescriptions[selectedDeviceIndex];
92 String[] softwareTypes = selectedDevice.getSoftwareTypes();
94
95 String message = "Please choose a softwareType [ ";
97 for (int i = 0; i < softwareTypes.length; i++)
98 {
99 message = i == 0 ? message : message + ", ";
00 message = message + softwareTypes[i];
01 }
02 message = message + "]";
03
04 printMessageAndDefault(message, configuredDevice.getSoftwareType());
05
06 String swType = readString(configuredDevice.getSoftwareType());
07 if (Arrays.asList(softwareTypes).contains(swType))
08 {
09 this.selectedSoftwareType = swType;
10 nextUiState(UI_STATE_SW_TYPE_SELECTED);
11 }
12 else
13 {
14 this.selectedSoftwareType = null;
15 }
16 }
17
18 private void handleChooseSoftwareVersion() throws Exception
19 {
20 printSelection();
21
22 printMessageAndDefault("Please choose a software version [x.y.z where x,y,z are non-negative numbers]",
24 configuredDevice.getSoftwareVersion());
25
26 String swVersion = readString(configuredDevice.getSoftwareVersion());
27 String[] fragments = swVersion.split("\\.");
28 this.selectedSoftwareVersion = null;
29 if (fragments.length == 3)
30 {
31 if (Integer.parseInt(fragments[0]) >= 0 && Integer.parseInt(fragments[1]) >= 0
32 && Integer.parseInt(fragments[2]) >= 0)
33
34 this.selectedSoftwareVersion = swVersion;
35
36 nextUiState(UI_STATE_SW_VERSION_SELECTED);
37 }
38 }
39
40 private void handleChooseFunction() throws Exception
41 {
42 printSelection();
43
44 String deviceType = deviceDescriptions[selectedDeviceIndex].getDeviceType();
45
46 functionDescriptions = dlsApi.getFunctionDescriptions(sessionId, deviceType,
50 selectedLevelIndex, selectedSoftwareType, selectedSoftwareVersion)
51 .getFunctionDescriptions();
52
53 printMessageAndDefault("Please choose a function [0 - " + functionDescriptions.length + "]", configuredKey.getKeyFunct());
55 for (int i = 0; i < functionDescriptions.length; i++)
56 {
57 System.out.println(i + ": " + functionDescriptions[i].getName());
58 }
59
60 int index = readIndex(configuredKey.getKeyFunct());
62 if (0 <= index && index < functionDescriptions.length)
63 {
64 this.selectedFunctionIndex = index;
65 nextUiState(UI_STATE_FUNCTION_SELECTED);
66 }
67 else
68 {
69 this.selectedFunctionIndex = -1;
70 }
71 }
72
73 private void handleSubmitAttributes() throws Exception
74 {
75 printSelection();
76
77 AttributeDescription[] attributeDescriptions = functionDescriptions[selectedFunctionIndex]
79 .getAttributes();
80
81 printMessage("Please submit attributes: ");
82 Attribute[] attributes = new Attribute[attributeDescriptions.length];
83 for (int i = 0; i < attributeDescriptions.length; i++)
84 {
85 String attributeName = attributeDescriptions[i].getName();
86 printMessage(attributeName + "::" + attributeDescriptions[i].getType());
88 String value = readString("");
90 attributes[i] = new Attribute();
91 attributes[i].setName(attributeName);
92 attributes[i].setValue(value);
93 }
94
95 configuredKey.setAttributes(attributes);
96 configuredKey.setKeyFunct(getSelectedFunction().getName());
97 configuredKey.setLevel(getSelectedLevel());
98 configuredKey.setModule(getSelectedModule().getType());
99 configuredKey.setNumber(getSelectedKey());
00
01 nextUiState(UI_STATE_ATTRIBUTES_SUBMITTED);
02 }
03
04 private void handleSubmitE164() throws Exception
05 {
06 printSelection();
07
08 printMessage("Please submit e164 number: ");
09
10 String e164 = readString("");
12 String orderId = "";
13
14 configuredDevice.setE164(e164);
16 configuredDevice.setKeys(new Key[] { configuredKey });
17 configuredDevice.setSoftwareType(selectedSoftwareType);
18 configuredDevice.setSoftwareVersion(selectedSoftwareVersion);
19 configuredDevice.setType(getSelectedDevice().getDeviceType());
20
21 DeviceAPIout apiOut = dlsApi.e164ModifyKeyLayout(sessionId, orderId, configuredDevice);
23 printApiOut(apiOut);
25
26 nextUiState(UI_STATE_E164_SUBMITTED);
27 }
28
29 private DeviceDescription getSelectedDevice()
30 {
31 return deviceDescriptions[selectedDeviceIndex];
32 }
33
34 private ModuleDescription getSelectedModule()
35 {
36 return getSelectedDevice().getModules()[selectedModuleIndex];
37 }
38
39 private String getSelectedLevel()
40 {
41 return "" + selectedLevelIndex;
42 }
43
44 private FunctionDescription getSelectedFunction()
45 {
46 return functionDescriptions[selectedFunctionIndex];
47 }
48
49 private String getSelectedKey()
50 {
51 return "" + selectedKeyIndex;
52 }
53
54 private Attribute[] getConfiguredAttributes()
55 {
56 return configuredKey.getAttributes();
57 }
58
59 private void previousUiState()
60 {
61 uiStateStack.pop();
62 }
63
64 private void nextUiState(String newState)
65 {
66 uiStateStack.push(newState);
67 }
68
69 private String readString(String defaultInput) throws IOException
70 {
71 this.input = reader.readLine();
72 if("".equals(input.trim()))
73 {
74 this.input = defaultInput;
75 }
76
77 return input;
78 }
79
80 private int readIndex(String defaultInput) throws IOException
81 {
82 int index = -1;
83 this.input = reader.readLine();
84 if("".equals(input.trim()))
85 {
86 this.input = defaultInput;
87 }
88
89 try
90 {
91 index = Integer.parseInt(input);
92 }
93 catch (NumberFormatException e)
94 {
95 System.out.println("'" + input + "' is not a number. Please retry!");
96 }
97
98 return index;
99 }
00
01 private void printApiOut(DeviceAPIout apiOut)
02 {
03 System.out.println("*** API e164SetSipKeys was called ...");
04 System.out.println("*** API returned: [status, message]=[" + apiOut.getStatus() + ","
05 + apiOut.getStatusText() + "]");
06 if ("-2".equals(apiOut.getStatus())) {
08 System.out.println("*** Parameter info:");
09 Key[] keysOut = apiOut.getDevice().getKeys();
10 for (int j = 0; j < keysOut.length; j++)
11 {
12 Attribute[] attributesOut = keysOut[j].getAttributes();
13 printAttribute("module", keysOut[j].getModule(), keysOut[j].getModuleVerifyState());
14 printAttribute("level", keysOut[j].getLevel(), keysOut[j].getLevelVerifyState());
15 printAttribute("keyFunct", keysOut[j].getKeyFunct(), keysOut[j]
16 .getKeyFunctVerifyState());
17 printAttribute("number", keysOut[j].getNumber(), keysOut[j].getNumberVerifyState());
18 for (int i = 0; i < attributesOut.length; i++)
19 {
20 printAttribute(attributesOut[i].getName(), attributesOut[i].getValue(),
21 attributesOut[i].getValidationState());
22 }
23 }
24 }
25 }
26
27 private void printAttribute(String name, String value, String validationStatus)
28 {
29 if (!"OK".equals(validationStatus))
30 {
31 System.out.println(" [attributeName, attributeValue, attribuetState]=[" + name + ", "
32 + value + ", " + validationStatus + "]");
33 }
34 }
35
36 private void printMessage(String message)
37 {
38 System.out.println(message);
39 }
40
41 private void printMessageAndDefault(String message, String defaultValue)
42 {
43 if(defaultValue == null)
44 {
45 printMessage(message);
46 }
47 else
48 {
49 System.out.println(message + " [default=" + defaultValue + "]");
50 }
51 }
52
53 private void printSelection()
54 {
55 System.out.println("Currently selected: ");
56 if (selectedDeviceIndex > -1)
57 {
58 System.out.print("[device = " + getSelectedDevice().getDeviceType() + "]");
59 }
60 if (selectedModuleIndex > -1)
61 {
62 System.out.print("[module = " + getSelectedModule().getType() + "]");
63 }
64 if (selectedLevelIndex > -1)
65 {
66 System.out.print("[level = " + getSelectedLevel() + "]");
67 }
68 if (selectedKeyIndex > -1)
69 {
70 System.out.print("[key = " + getSelectedKey() + "]");
71 }
72 if (selectedSoftwareType != null)
73 {
74 System.out.print("[software type = " + selectedSoftwareType + "]");
75 }
76 if (selectedSoftwareVersion != null)
77 {
78 System.out.print("[software version = " + selectedSoftwareVersion + "]");
79 }
80 if (selectedFunctionIndex > -1)
81 {
82 System.out.println();
83 System.out.print("[function = " + getSelectedFunction().getName());
84 if (configuredKey != null && getConfiguredAttributes() != null)
85 {
86 System.out.print("(");
87 Attribute[] attributes = getConfiguredAttributes();
88 for (int i = 0; i < attributes.length; i++)
89 {
90 if (i > 0)
91 {
92 System.out.print(", ");
93 }
94 System.out.print(attributes[i].getName() + "=" + attributes[i].getValue());
95 }
96 System.out.print(")");
97 }
98 System.out.print("]");
99 }
00 System.out.println();
01 }
02}
03