Fixing MySQL query parsing bug for IoTRelation; adjusting MySQL queries for new inter...
[iot2.git] / iotjava / iotinstaller / IoTInstaller.java
1 package iotinstaller;
2
3 import iotinstaller.MySQLInterface;
4 import iotinstaller.TableProperty;
5 import iotinstaller.Table;
6
7 import java.io.*;
8 import java.sql.*;
9 import java.util.Scanner;
10 import java.util.Properties;
11
12 /** A class that creates an object for IoT device/entity installation into database
13  *
14  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
15  * @version     1.0
16  * @since       2015-12-01
17  */
18 public class IoTInstaller {
19
20         /**
21          * IoTInstaller class properties
22          */
23         private Table tbl;
24
25         /**
26          * IoTInstaller class constants
27          */
28         private static final String STR_MAIN_TABLE_NAME = "IoTMain";
29         private static final String STR_COMM_TABLE_NAME = "IoTComm";
30         private static final String STR_HOST_TABLE_NAME = "IoTComputeNode";
31         private static final String STR_ADDRESS_TABLE_NAME = "IoTAddress";
32         private static final String STR_DEV_ADD_TABLE_NAME = "IoTDeviceAddress";
33         private static final String STR_ZB_ADD_TABLE_NAME = "IoTZigbeeAddress";
34         private static final int INT_NUM_COMM_FIELDS = 5;
35         private static final int INT_NUM_HOST_FIELDS = 3;
36
37         private static final String STR_INSTALL_ENTITY_CMD = "-install_ent";
38         private static final String STR_INSTALL_COMMUNICATION_CMD = "-install_comm";
39         private static final String STR_INSTALL_COMPLETE_CMD = "-install_comp";
40         private static final String STR_INSTALL_ADDRESS_CMD = "-install_add";
41         private static final String STR_INSTALL_DEV_ADDRESS_CMD = "-install_dev_add";
42         private static final String STR_INSTALL_ZB_ADDRESS_CMD = "-install_zb_add";
43         private static final String STR_INSTALL_HOST_CMD = "-install_host";
44         private static final String STR_DELETE_ENTITY_CMD = "-delete_ent";
45         private static final String STR_DELETE_ADDRESS_CMD = "-delete_add";
46         private static final String STR_DELETE_DEV_ADD_CMD = "-delete_dev_add";
47         private static final String STR_DELETE_ZB_ADD_CMD = "-delete_zb_add";
48         private static final String STR_DELETE_HOST_CMD = "-delete_host";
49         private static final String STR_HELP_CMD = "-help";
50
51         /**
52          * Class constructor
53          */
54         public IoTInstaller() {
55
56                 // Make this not verbose by default
57                 tbl = new Table(false);
58                 System.out.println("IoTInstaller: Initializing installation..");
59         }
60
61         /**
62          * A method to insert a new entry to the main table (IoTMain)
63          * <p>
64          * This entry can be a new device or a new entity that we should keep track about
65          * A new entry will need a unique ID and a type name from the driver
66          *
67          * @param  strID    string ID to insert device into the main table
68          * @param  strType  string type to insert device into the main table
69          * @return          void
70          */
71         private void insertMainDBEntry(String strID, String strType) {
72
73                 // Creating String array
74                 String[] strFlds = new String[2];
75                 for(int i=0; i<2; i++) {
76                         strFlds[i] = new String();
77                 }
78                 strFlds[0] = strID;
79                 strFlds[1] = strType;
80
81                 // Insert entry through Table object
82                 tbl.setTableName(STR_MAIN_TABLE_NAME);
83                 tbl.insertEntry(strFlds);
84                 System.out.println("IoTInstaller: Inserting a new entry into main table");
85         }
86
87         /**
88          * A method to extract device/entity information from the user
89          * <p>
90          * Users are supposed to supply the information needed for installation
91          *
92          * @param  strCfgFileName String config file name for device/entity
93          * @return                void
94          */
95         public void extractTableAndInstall(String strCfgFileName) {
96                 // TO DO: WE PROBABLY NEED TO IMPROVE THE FILE PARSING BUT FOR NOW KEEP IT MINIMUM
97
98                 try {
99
100                         // Parse configuration file
101                         // Assumption here is that .config file is written with the correct syntax (need typechecking)
102                         File file = new File(strCfgFileName);
103                         Scanner scanFile = new Scanner(new FileReader(file));
104                         System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
105
106                         // Initialize String for ID and TYPE
107                         String strID = "";
108                         String strType = "";
109                         String strTypeSpecific = "";
110
111                         // Initialize TableProperty for devices and specific devices
112                         // We have 2 tables,
113                         // e.g. ProximitySensor - table of many ProximitySensor devices
114                         //      ProximitySensorBrandA - table that contains the constructor
115                         //                              information for a specific device
116                         TableProperty[] tpDevice = new TableProperty[1];
117                         TableProperty[] tpDeviceSpecific = new TableProperty[1];
118
119                         // Initialize array of string
120                         String[] strFields = new String[1];
121                         String[] strFieldsSpecific = new String[1];
122
123                         // String for scanning the file
124                         String strScan = "";
125
126                         // Store number of fields here
127                         int iFields = 0;
128                         while (scanFile.hasNext()) {
129
130                                 strScan = scanFile.next();
131                                 if (strScan.equals("IoTMain")) {
132
133                                         while (scanFile.hasNext()) {
134                                                 strScan = scanFile.next();
135
136                                                 // Get ID
137                                                 if (strScan.equals("ID")) {
138                                                         strID = scanFile.next();
139                                                 }
140                                                 // Get TYPE
141                                                 else if (strScan.equals("TYPE")) {
142                                                         strType = scanFile.next();
143                                                 }
144                                                 // Get TYPE
145                                                 else if (strScan.equals("TYPESPECIFIC")) {
146                                                         strTypeSpecific = scanFile.next();
147                                                 } else if (strScan.equals("END")) {
148                                                         // Break out of loop
149                                                         break;
150                                                 }
151                                         }
152                                 } else if (strScan.equals("Table")) {
153
154                                         // Get number of fields, e.g. Table 3
155                                         iFields = scanFile.nextInt();
156
157                                         // We have device ID and device specific names
158                                         // e.g. ID = PS1; TYPE
159                                         tpDevice = new TableProperty[2];
160                                         tpDevice[0] = new TableProperty();
161                                         tpDevice[0].setField("ID");
162                                         tpDevice[0].setType("VARCHAR");
163                                         tpDevice[0].setLength("5");
164                                         tpDevice[1] = new TableProperty();
165                                         tpDevice[1].setField("TYPE");
166                                         tpDevice[1].setType("VARCHAR");
167                                         tpDevice[1].setLength("30");
168
169                                         // Prepare properties for a specific device
170                                         tpDeviceSpecific = new TableProperty[iFields];
171                                         for (int i=0; i<iFields; i++) {
172                                                 tpDeviceSpecific[i] = new TableProperty();
173
174                                                 // Looping over the fields
175                                                 strScan = scanFile.next();
176                                                 tpDeviceSpecific[i].setField(strScan);
177                                                 strScan = scanFile.next();
178                                                 tpDeviceSpecific[i].setType(strScan);
179                                                 strScan = scanFile.next();
180                                                 tpDeviceSpecific[i].setLength(strScan);
181                                         }
182                                 } else if (strScan.equals("Data")) {
183
184                                         // Get the device information
185                                         strFields = new String[2];
186                                         strFields[0] = strID;
187                                         strFields[1] = strTypeSpecific;
188
189                                         if ((tpDeviceSpecific.length == 1) &&
190                                                         (tpDeviceSpecific[0].getField().equals("EMPTY"))) {
191
192                                                 // Get the fields for specific device
193                                                 strFieldsSpecific = null;
194                                                 System.out.println("IoTInstaller: Empty constructor for: " + strTypeSpecific);
195
196                                         } else {
197
198                                                 // Get the fields for specific device
199                                                 strFieldsSpecific = new String[iFields];
200                                                 for (int i=0; i<iFields; i++) {
201                                                         strScan = scanFile.next();
202                                                         strFieldsSpecific[i] = strScan;
203                                                 }
204                                         }
205                                 }
206                         }
207
208                         installNewEntity(strType, strTypeSpecific, strID, tpDevice,
209                                                                                          tpDeviceSpecific, strFields, strFieldsSpecific);
210                         System.out.println("IoTInstaller: Installing a new entity/device into the system");
211
212                 } catch (FileNotFoundException ex) {
213
214                         System.out.println("IoTInstaller: Exception: ");
215                         ex.printStackTrace();
216
217                 }
218         }
219
220         /**
221          * A method to install a new entity/device into the database
222          * <p>
223          * 1) Insert this device/entity into the main table IoTMain
224          * 2) Create a new device/entity table if it doesn't exist yet
225          * 3) Insert this entry into the specific device/entity table
226          *
227          * @param  strType           String device type
228          * @param  strTypeSpecific   String device specific type
229          * @param  strID             String unique device/entity ID
230          * @param  tpDevice          array of TableProperty to construct the new table
231          * @param  tpDeviceSpecific  array of TableProperty to construct the new table
232          * @param  strFields         field values of device table
233          * @param  strFieldsSpecific field values of device specific table
234          */
235         private void installNewEntity(String strType, String strTypeSpecific, String strID,
236                 TableProperty[] tpDevice, TableProperty[] tpDeviceSpecific, String[] strFields, String[] strFieldsSpecific) {
237
238                 // Create a new IoTInstaller object
239                 System.out.println("IoTInstaller: Installing device " + strType + " with specific type " + strTypeSpecific);
240                 tbl.setTableName(strType);
241
242                 // 1) Insert this device/entity into the main table IoTMain
243                 insertMainDBEntry(strID, strType);
244
245                 // Device table
246                 // 2) Create a new device/entity table if it doesn't exist yet
247                 tbl.setTableName(strType);
248                 if (tbl.isTableExisting()) {
249                         // table does exist
250                         System.out.println("IoTInstaller: Table " + strType + " exists.. just insert new entry!");
251                 } else {
252                         // table does not exist yet
253                         tbl.createTable(tpDevice, "ID");
254                 }
255
256                 // 3) Insert this entry into the device/entity table
257                 tbl.insertEntry(strFields);
258
259                 // Device specific table
260                 // 2) Create a new device/entity table if it doesn't exist yet
261                 // P.S. We should assume that table doesn't exist yet, and we throw error otherwise!
262                 tbl.setTableName(strTypeSpecific + strID);
263                 tbl.createTable(tpDeviceSpecific, null);
264
265                 // 3) Insert this entry into the device/entity table
266                 if (strFieldsSpecific != null) {
267                         tbl.insertEntry(strFieldsSpecific);
268                 }
269         }
270
271         /**
272          * A method to extract device/entity communication configuration from the user
273          * <p>
274          * Users are supposed to supply the information needed for installation
275          *
276          * @param  strCfgFileName String config file name for device/entity
277          * @return                void
278          */
279         public void extractCommAndInstall(String strCfgFileName) {
280                 // TODO: WE PROBABLY NEED TO IMPROVE THE FILE PARSING BUT FOR NOW KEEP IT MINIMUM
281
282                 try {
283
284                         // Parse configuration file
285                         // Assumption here is that .config file is written with the correct syntax (need typechecking)
286                         File file = new File(strCfgFileName);
287                         Scanner scanFile = new Scanner(new FileReader(file));
288
289                         System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
290
291                         // Field counter
292                         int iFieldCnt = 0;
293
294                         // Initialize array of string
295                         String[] strFields = new String[INT_NUM_COMM_FIELDS];
296                         for(int i=0; i<INT_NUM_COMM_FIELDS; i++) {
297                                 strFields[i] = new String();
298                         }
299                         while (scanFile.hasNext() && (iFieldCnt < INT_NUM_COMM_FIELDS)) {
300
301                                 strFields[iFieldCnt++] = scanFile.next();
302                         }
303
304                         // Create a new installer object
305                         tbl.setTableName(STR_COMM_TABLE_NAME);
306                         tbl.insertEntry(strFields);
307
308                         System.out.println("IoTInstaller: Installing a new communication pattern into the system");
309
310                 } catch (FileNotFoundException ex) {
311
312                         System.out.println("IoTInstaller: Exception: ");
313                         ex.printStackTrace();
314
315                 }
316         }
317
318         /**
319          * A method to extract device/entity addresses information
320          * <p>
321          * Users are supposed to supply the information needed for installation
322          *
323          * @param  strCfgFileName String config file name for device/entity
324          * @return                void
325          */
326         public void installDeviceAddress(String strCfgFileName) {
327
328                 try {
329
330                         // Parse configuration file
331                         Properties prop = new Properties();
332                         File file = new File(strCfgFileName);
333                         FileInputStream fis = new FileInputStream(file);
334                         try {
335                                 prop.load(fis);
336                         } catch (IOException ex) {
337                                 ex.printStackTrace();
338                         }
339                         System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
340                         // Initialize string
341                         // We can only install one device address per one time with the following sequence
342                         String[] strFields = new String[2];
343                         String[] strFieldsAddress = null;
344                         // Check for wildcard feature
345                         if ((prop.getProperty("SOURCEWILDCARD", null) != null) &&
346                                 (prop.getProperty("DESTWILDCARD", null) != null)) {
347                                 strFieldsAddress = new String[5];
348                                 strFieldsAddress[3] = prop.getProperty("SOURCEWILDCARD");
349                                 strFieldsAddress[4] = prop.getProperty("DESTWILDCARD");
350                         } else {
351                                 strFieldsAddress = new String[3];
352                         }
353                         strFields[0] = prop.getProperty("ID");
354                         strFields[1] = prop.getProperty("ADDRESSFOR");
355                         strFieldsAddress[0] = prop.getProperty("DEVICEADDRESS");
356                         strFieldsAddress[1] = prop.getProperty("PORTNUMBER");
357                         strFieldsAddress[2] = prop.getProperty("PROTOCOL");
358
359                         // Insert this entry into the main device address table
360                         tbl.setTableName(STR_DEV_ADD_TABLE_NAME);
361                         tbl.insertEntry(strFields);
362
363                         // Create a new table for a specific device address
364                         // e.g. AmcrestCameraAdd + CM1 = AmcrestCameraAddCM1
365                         tbl.setTableName(strFields[1] + strFields[0]);
366
367                         // Table does not exist yet
368                         // Set TableProperty for device address (MAC address)
369                         TableProperty[] tp = null;
370                         // Check for wildcard feature
371                         if (strFieldsAddress.length == 5) {
372                                 tp = new TableProperty[5];
373                                 tp[3] = new TableProperty();
374                                 tp[3].setField("SOURCEWILDCARD");
375                                 tp[3].setType("VARCHAR");
376                                 tp[3].setLength("5");
377                                 tp[4] = new TableProperty();
378                                 tp[4].setField("DESTWILDCARD");
379                                 tp[4].setType("VARCHAR");
380                                 tp[4].setLength("5");
381                         } else {
382                                 tp = new TableProperty[3];
383                         }
384                         tp[0] = new TableProperty();
385                         tp[0].setField("DEVICEADDRESS");
386                         tp[0].setType("VARCHAR");
387                         tp[0].setLength("20");
388                         tp[1] = new TableProperty();
389                         tp[1].setField("PORTNUMBER");
390                         tp[1].setType("INT");
391                         tp[1].setLength("11");
392                         tp[2] = new TableProperty();
393                         tp[2].setField("PROTOCOL");
394                         tp[2].setType("VARCHAR");
395                         tp[2].setLength("5");
396                         tbl.createTable(tp, "DEVICEADDRESS");
397
398                         // Insert new address entry
399                         tbl.insertEntry(strFieldsAddress);
400
401                         System.out.println("IoTInstaller: Installing a new device/entity address into the system");
402
403                 } catch (FileNotFoundException ex) {
404
405                         System.out.println("IoTInstaller: Exception: ");
406                         ex.printStackTrace();
407
408                 }
409         }
410
411         /**
412          * A method to extract Zigbee device addresses information
413          * <p>
414          * Users are supposed to supply the information needed for installation
415          *
416          * @param  strCfgFileName String config file name for device/entity
417          * @return                void
418          */
419         public void installZigbeeAddress(String strCfgFileName) {
420
421                 try {
422
423                         // Parse configuration file
424                         Properties prop = new Properties();
425                         File file = new File(strCfgFileName);
426                         FileInputStream fis = new FileInputStream(file);
427                         try {
428                                 prop.load(fis);
429                         } catch (IOException ex) {
430                                 ex.printStackTrace();
431                         }
432                         System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
433                         // Initialize string
434                         // We can only install one device address per one time with the following sequence
435                         String[] strFields = new String[2];
436                         String[] strFieldsAddress = new String[1];
437                         strFields[0] = prop.getProperty("ID");
438                         strFields[1] = prop.getProperty("ADDRESSFOR");
439                         strFieldsAddress[0] = prop.getProperty("DEVICEADDRESS");
440
441                         // Insert this entry into the main device address table
442                         tbl.setTableName(STR_ZB_ADD_TABLE_NAME);
443                         tbl.insertEntry(strFields);
444
445                         // Create a new table for a specific device address
446                         // e.g. AmcrestCameraZBAdd + CM1 = AmcrestCameraZBAddCM1
447                         tbl.setTableName(strFields[1] + strFields[0]);
448
449                         // Table does not exist yet
450                         // Set TableProperty for device address (MAC address)
451                         TableProperty[] tp = new TableProperty[1];
452                         tp[0] = new TableProperty();
453                         tp[0].setField("DEVICEADDRESS");
454                         tp[0].setType("VARCHAR");
455                         tp[0].setLength("25");
456                         tbl.createTable(tp, "DEVICEADDRESS");
457
458                         // Insert new address entry
459                         tbl.insertEntry(strFieldsAddress);
460
461                         System.out.println("IoTInstaller: Installing a new device/entity address into the system");
462
463                 } catch (FileNotFoundException ex) {
464
465                         System.out.println("IoTInstaller: Exception: ");
466                         ex.printStackTrace();
467
468                 }
469         }
470
471
472         /**
473          * A method to extract simple addresses information, e.g. www.google.com
474          * <p>
475          * Users are supposed to supply the information needed for installation
476          *
477          * @param  strCfgFileName String config file name for device/entity
478          * @return                void
479          */
480         public void installAddress(String strCfgFileName) {
481
482                 try {
483
484                         // Parse configuration file
485                         Properties prop = new Properties();
486                         File file = new File(strCfgFileName);
487                         FileInputStream fis = new FileInputStream(file);
488                         try {
489                                 prop.load(fis);
490                         } catch (IOException ex) {
491                                 ex.printStackTrace();
492                         }
493                         System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
494                         // Initialize string
495                         // We can only install one device address per one time with the following sequence
496                         String[] strFields = new String[2];
497                         String[] strFieldsAddress = new String[1];
498                         strFields[0] = prop.getProperty("ID");
499                         strFields[1] = prop.getProperty("ADDRESSFOR");
500                         strFieldsAddress[0] = prop.getProperty("ADDRESS");
501
502                         // Insert this entry into the main device address table
503                         tbl.setTableName(STR_ADDRESS_TABLE_NAME);
504                         tbl.insertEntry(strFields);
505
506                         // Create a new table for a specific device address
507                         // e.g. WeatherForecastAdd + WF1 = WeatherForecastAddCM1
508                         tbl.setTableName(strFields[1] + strFields[0]);
509
510                         // Table does not exist yet
511                         // Set TableProperty for device address (MAC address)
512                         TableProperty[] tp = new TableProperty[1];
513                         tp[0] = new TableProperty();
514                         tp[0].setField("ADDRESS");
515                         tp[0].setType("VARCHAR");
516                         tp[0].setLength("50");
517                         tbl.createTable(tp, "ADDRESS");
518
519                         // Insert new address entry
520                         tbl.insertEntry(strFieldsAddress);
521
522                         System.out.println("IoTInstaller: Installing a new device/entity address into the system");
523
524                 } catch (FileNotFoundException ex) {
525
526                         System.out.println("IoTInstaller: Exception: ");
527                         ex.printStackTrace();
528
529                 }
530         }
531
532         /**
533          * A method to extract host information for host installation
534          * <p>
535          * Users are supposed to supply the information needed for installation
536          *
537          * @param  strCfgFileName String config file name for device/entity
538          * @return                void
539          */
540         public void installHost(String strCfgFileName) {
541                 try {
542                         // Parse configuration file
543                         Properties prop = new Properties();
544                         File file = new File(strCfgFileName);
545                         FileInputStream fis = new FileInputStream(file);
546                         try {
547                                 prop.load(fis);
548                         } catch (IOException ex) {
549                                 ex.printStackTrace();
550                         }
551                         System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
552                         // Initialize array of string
553                         String[] strFields = new String[3];
554                         strFields[0] = prop.getProperty("HOSTADDRESS");
555                         strFields[1] = prop.getProperty("PROCESSOR");
556                         strFields[2] = prop.getProperty("MEMORY");
557                         // Create a new installer object
558                         tbl.setTableName(STR_HOST_TABLE_NAME);
559                         tbl.insertEntry(strFields);
560
561                         System.out.println("IoTInstaller: Installing a new host into the system");
562
563                 } catch (FileNotFoundException ex) {
564
565                         System.out.println("IoTInstaller: Exception: ");
566                         ex.printStackTrace();
567
568                 }
569         }
570
571         /**
572          * A method to delete host information from database by putting in host address
573          *
574          * @param  strHostAddress String for host address
575          * @return                void
576          */
577         public void deleteHost(String strHostAddress) {
578
579                 tbl.setTableName(STR_HOST_TABLE_NAME);
580                 String strWhere = "HOSTADDRESS='" + strHostAddress + "';";
581                 tbl.deleteEntry(strWhere);
582
583                 System.out.println("IoTInstaller: Deleting a host from the system");
584         }
585
586         /**
587          * A method to delete entity information from database
588          *
589          * @param  strEntID             String for entity ID
590          * @param  strEntType   String for entity type
591          * @param  strEntName   String for entity name
592          * @return              void
593          */
594         public void deleteEntity(String strEntID, String strEntType, String strEntName) {
595
596                 // Delete from table IoTMain
597                 tbl.setTableName(STR_MAIN_TABLE_NAME);
598                 String strWhere = "ID='" + strEntID + "';";
599                 tbl.deleteEntry(strWhere);
600                 System.out.println("IoTInstaller: Removing entity from table " + STR_MAIN_TABLE_NAME);
601
602                 // Delete from table with type name, e.g. Camera
603                 tbl.setTableName(strEntType);
604                 strWhere = "ID='" + strEntID + "';";
605                 tbl.deleteEntry(strWhere);
606                 System.out.println("IoTInstaller: Removing entity from table type: " + strEntType);
607                 // Drop table if this was the last entry
608                 if (tbl.isTableEmpty()) {
609                         tbl.dropTable();
610                         System.out.println("IoTInstaller: Dropping the table.. It was the last entry!");
611                 }
612
613                 // Drop the table that contains constructor information
614                 tbl.setTableName(strEntName + strEntID);
615                 tbl.dropTable();
616                 System.out.println("IoTInstaller: Dropping class constructor table...");
617
618                 System.out.println("IoTInstaller: Deleting an entry from the system...");
619         }
620
621         /**
622          * A method to delete address information from database
623          *
624          * @param  strTableName         String for main table, i.e. IoTAddress, IoTDeviceAddress, or IoTZigbeeAddress
625          * @param  strEntID                     String for entity ID, e.g. CM1
626          * @param  strEntAddType        String for entity address type, e.g. AmcrestCameraAdd
627          * @return                                      void
628          */
629         public void deleteAddress(String strTableName, String strEntID, String strEntAddType) {
630
631                 // Delete from main table, e.g. IoTAddress, IoTDeviceAddress, or IoTZigbeeAddress
632                 tbl.setTableName(strTableName);
633                 String strWhere = "ID='" + strEntID + "';";
634                 tbl.deleteEntry(strWhere);
635                 System.out.println("IoTInstaller: Removing entity from table " + strTableName);
636
637                 // Drop the table that contains constructor information
638                 tbl.setTableName(strEntAddType + strEntID);
639                 tbl.dropTable();
640                 System.out.println("IoTInstaller: Dropping class constructor table...");
641
642                 System.out.println("IoTInstaller: Deleting an entry from the system...");
643         }
644
645
646         /**
647          * A method to install a pair of new devices with their communication pattern
648          *
649          * @param  strFirstDeviceFile  String that contains the file name of the fist device
650          * @param  strSecondDeviceFile String that contains the file name of the second device
651          * @param  strCommFile         String that contains the file name of the communication file
652          * @return                     void
653          */
654         public void installPairOfEntities(String strFirstEntityFile,
655                 String strSecondEntityFile, String strCommFile) {
656                 // TODO: NEED TO DO THE INPUT FAILURE CHECKING HERE
657                 // NOW JUST ASSUME THAT THE INPUT FILES ARE GOOD
658
659                 extractTableAndInstall(strFirstEntityFile);
660                 extractTableAndInstall(strSecondEntityFile);
661                 extractCommAndInstall(strCommFile);
662         }
663
664         /**
665          * A method to output help messages
666          *
667          * @return void
668          */
669         private void helpMessages() {
670                 System.out.println();
671                 System.out.println("IoTInstaller: Command line options:");
672                 System.out.println("IoTInstaller: 1) Install one device, e.g. java iotinstaller.IoTInstaller -install_ent <filename>");
673                 System.out.println("IoTInstaller: 2) Install comm pattern, e.g. java iotinstaller.IoTInstaller -install_comm <filename>");
674                 System.out.print("IoTInstaller: 3) Install two devices and comm pattern, e.g. java iotinstaller.IoTInstaller ");
675                 System.out.println("-install_comp <first_entity_filename> <second_entity_filename> <communication_filename>");
676                 System.out.println("IoTInstaller: 4) Install address, e.g. java iotinstaller.IoTInstaller -install_add <filename>");
677                 System.out.println("IoTInstaller: 5) Install device address, e.g. java iotinstaller.IoTInstaller -install_dev_add <filename>");
678                 System.out.println("IoTInstaller: 6) Install zigbee device address, e.g. java iotinstaller.IoTInstaller -install_zb_add <filename>");
679                 System.out.println("IoTInstaller: 7) Install host, e.g. java iotinstaller.IoTInstaller -install_host <filename>");
680                 System.out.println("IoTInstaller: 8) Delete entity, e.g. java iotinstaller.IoTInstaller -delete_ent <ent_id> <ent_type> <ent_name>");
681                 System.out.println("IoTInstaller: 9) Delete address, e.g. java iotinstaller.IoTInstaller -delete_add <ent_id>");
682                 System.out.println("IoTInstaller: 10) Delete device address, e.g. java iotinstaller.IoTInstaller -delete_dev_add <ent_id>");
683                 System.out.println("IoTInstaller: 11) Delete zigbee device address, e.g. java iotinstaller.IoTInstaller -delete_zb_add <ent_id>");
684                 System.out.println("IoTInstaller: 12) Delete host, e.g. java iotinstaller.IoTInstaller -delete_host <host_address>");
685                 System.out.println("IoTInstaller: Type 'java iotinstaller.IoTInstaller -help' to display this help.");
686                 System.out.println();
687         }
688
689         /**
690          * Main method that accepts inputs for installation
691          *
692          * @param  args[0]  String that contains the command line parameter
693          * @param  args[1]  String that contains the first file name / entity ID / host address
694          * @param  args[2]  String that contains the second file name
695          * @param  args[3]  String that contains the third file name
696          * @see    helpMessages
697          */
698         public static void main(String[] args) {
699
700                 // Testing IoTInstaller object
701                 IoTInstaller iotinst = new IoTInstaller();
702
703                 // TODO: PROBABLY NEED A BETTER ERROR HANDLING FOR INPUTS HERE
704                 // NOW ASSUME MINIMAL ERROR FOR INPUTS
705                 if (args.length > 0) {
706                         // Check for input parameters
707                         if (args[0].equals(STR_INSTALL_ENTITY_CMD)) {
708                                 iotinst.extractTableAndInstall(args[1]);
709
710                         } else if (args[0].equals(STR_INSTALL_COMMUNICATION_CMD)) {
711                                 iotinst.extractCommAndInstall(args[1]);
712
713                         } else if (args[0].equals(STR_INSTALL_COMPLETE_CMD)) {
714                                 iotinst.installPairOfEntities(args[1], args[2], args[3]);
715
716                         } else if (args[0].equals(STR_INSTALL_ADDRESS_CMD)) {
717                                 iotinst.installAddress(args[1]);
718
719                         } else if (args[0].equals(STR_INSTALL_DEV_ADDRESS_CMD)) {
720                                 iotinst.installDeviceAddress(args[1]);
721
722                         } else if (args[0].equals(STR_INSTALL_ZB_ADDRESS_CMD)) {
723                                 iotinst.installZigbeeAddress(args[1]);
724
725                         } else if (args[0].equals(STR_INSTALL_HOST_CMD)) {
726                                 iotinst.installHost(args[1]);
727
728                         } else if (args[0].equals(STR_DELETE_ENTITY_CMD)) {
729                                 iotinst.deleteEntity(args[1], args[2], args[3]);
730
731                         } else if (args[0].equals(STR_DELETE_ADDRESS_CMD)) {
732                                 iotinst.deleteAddress(STR_ADDRESS_TABLE_NAME, args[1], args[2]);
733
734                         } else if (args[0].equals(STR_DELETE_DEV_ADD_CMD)) {
735                                 iotinst.deleteAddress(STR_DEV_ADD_TABLE_NAME, args[1], args[2]);
736
737                         } else if (args[0].equals(STR_DELETE_ZB_ADD_CMD)) {
738                                 iotinst.deleteAddress(STR_ZB_ADD_TABLE_NAME, args[1], args[2]);
739
740                         } else if (args[0].equals(STR_DELETE_HOST_CMD)) {
741                                 iotinst.deleteHost(args[1]);
742
743                         } else if (args[0].equals(STR_HELP_CMD)) {
744                                 iotinst.helpMessages();
745
746                         } else {
747                                 System.out.println("IoTInstaller: ERROR: Wrong input parameters!");
748                                 iotinst.helpMessages();
749                         }
750                 } else {
751                         System.out.println("IoTInstaller: ERROR: No input parameters detected!");
752                         iotinst.helpMessages();
753                 }
754         }
755
756
757 }