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