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