aboutsummaryrefslogtreecommitdiffstats
path: root/policy-management/src/main/server-gen/bin/features
blob: 8aa863c42959ab58ad66d084548e9d5e951d8697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
#!/usr/bin/env bash

###
# ============LICENSE_START=======================================================
# ONAP POLICY
# ================================================================================
# Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
# ================================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#      http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============LICENSE_END=========================================================
##

# #############################################################
# Features Directory Layout:
#
# POLICY_HOME/
#   L─ features/
#        L─ <feature-name>*/
#            L─ [config]/
#            |   L─ <config-file>+
#            L─ [bin]/
#            |   L─ <bin-file>+
#            L─ lib/
#            |   L─ [dependencies]/
#            |   |   L─ <dependent-jar>+
#            │   L─ feature/
#            │       L─ <feature-jar>
#            L─ [db]/
#            │   L─ <db-name>/+
#            │       L─ sql/
#            │           L─ <sql-scripts>*
#            L─ [artifacts]/
#                L─ <artifact>+
#            L─ [install]
#                L─ [enable]
#                L─ [disable]
#                L─ [other-directories-or-files]
#
# notes:  [] = optional , * = 0 or more , + = 1 or more
#   <feature-name> directory without "feature-" prefix.
#   [config]       feature configuration directory that contains all configuration
#                  needed for this features
#   [config]/<config-file>  preferable named with "feature-<feature-name>" prefix to
#                  precisely match it against the exact features, source code, and 
#                  associated wiki page for configuration details.
#   [bin]       feature bin directory that contains helper scripts for this feature
#   [bin]/<executable-file>  preferable named with "feature-<feature-name>" prefix.
#   lib            jar libraries needed by this features
#   lib/[dependencies]  3rd party jar dependencies not provided by base installation
#                  of pdp-d that are necessary for <feature-name> to operate
#                  correctly.
#   lib/feature    the single feature jar that implements the feature.
#   [db]           database directory, if the feature contains sql.
#   [db]/<db-name> database to which underlying sql scripts should be applied against.
#                  ideally, <db-name> = <feature-name> so it is easily to associate
#                  the db data with a feature itself.   Ideally, since a feature is
#                  a somewhat independent isolated unit of functionality,the <db-name>
#                  database ideally isolates all its data.
#   [db]/<db-name>/sql  directory with all the sql scripts.
#   [db]/<db-name>/sql/<sql-scripts>  for this feature sql scripts
#                  upgrade scripts should be suffixed with ".upgrade.sql"
#                  downgrade scripts should be suffixed with ".downgrade.sql"
#   [artifacts]    maven artifacts to be deployed in a maven repository.
#   [artifacts]/<artifact>  maven artifact with identifiable maven coordinates embedded
#                  in the artifact.
#   [install]      custom installation directory where custom enable or disable scripts
#                  and other free form data is included to be used for the enable and 
#                  and disable scripts.
#   [install]/[enable] enable script executed when the enable operation is invoked in
#                  the feature.
#   [install]/[disable] disable script executed when the disable operation is invoked in
#                  the feature.
#   [install]/[other-directories-or-files] other executables, or data that can be used
#                  by the feature for any of its operations.   The content is determined
#                  by the feature designer.
#  
# Operations:
#   install: installs a feature
#   uninstall: uninstalls a feature
#   enable : enables 1) dependencies, 2) configuration, 3) binaries 4) database, 5) artifacts,
#                    6) feature, 7) customization.
#   disable: disables 1) dependencies, 2) configuration, 3) binaries, 4) database, 5) feature,
#                     6) customization
#   status : status of a feature
#
# 'enable' operation details:
#  0. Validates current state before the operation is committed
#  1. sets the symbolic link to the actual feature jar in pdp-d classpath ($POLICY_HOME/lib)
#  2. sets symbolic links to feature dependencies in pdp-d classpath ($POLICY_HOME/lib)
#  3. sets symbolic links to feature configuration in pdp-d configuration directory ($POLICY_HOME/config)
#  4. sets symbolic links to feature executables in pdp-d bin directory ($POLICY_HOME/bin)
#  5. sets symbolic links to feature upgrade scripts and removes links to downgrade scripts (if any)
#     in the pdp-d migration directory ($POLICY_HOME/etc/db/migration).
#  6. deploys any maven artifacts in the maven repositories in use (if any)
#  7. cd to the feature 'install' directory an executes (if exists) the 'enable' script to allow for specific
#     customizations for this feature.
#
# 'disable' operation details:
#  0. Validates current state before the operation is committed
#  1. removes the symbolic link to the actual feature jar in pdp-d classpath ($POLICY_HOME/lib)
#  2. removes symbolic links to feature dependencies in pdp-d classpath ($POLICY_HOME/lib)
#  3. removes symbolic links to feature configuration in pdp-d configuration directory ($POLICY_HOME/config)
#  4. removes symbolic links to feature executables in pdp-d bin directory ($POLICY_HOME/bin)
#  5. removes symbolic links to feature upgrade scripts and sets links to downgrade scripts (if any)
#     in the pdp-d migration directory ($POLICY_HOME/etc/db/migration).
#  6. cd to the feature 'install' directory an executes (if exists) the 'disable' script to allow for specific
#     customizations for this feature.
# 
# Notes for DB enabled features:
# 	A. Upgrade/Downgrade SQL File Name Format:  
# 			<VERSION>-<pdp|feature-name>[-description](.upgrade|.downgrade).sql
#   B. See related tooling: db-migrator, deploy-artifact, and policy
#
# Example:
#
# POLICY_HOME/
#   L─ features/
#        L── eelf/
#        │   L── config/
#        │   │   L── logback-eelf.xml
#        │   L─ lib/
#        │   │   L─ dependencies/
#        │   │   │   L─ ONAP-Logging-1.1.0-SNAPSHOT.jar
#        │   │   │   L─ eelf-core-1.0.0.jar
#        │   │   L─ feature/
#        │   │       L─ feature-eelf-1.1.0-SNAPSHOT.jar
#        │   L─ install/
#        │       L─ enable
#        │       L─ disable
#        L─ healthcheck/
#            L── config/
#            │   L─ feature-healthcheck.properties
#            L─ lib/
#                L─ feature/
#                    L─ feature-healthcheck-1.1.0-SNAPSHOT.jar
# #############################################################

if [[ ${DEBUG} == y ]]; then
	echo "-- MAIN --"
	set -x
fi
	
# The directories at play

LIB=${POLICY_HOME}/lib
CONFIG=${POLICY_HOME}/config
BIN=${POLICY_HOME}/bin
DB=${POLICY_HOME}/etc/db/migration
FEATURES=${POLICY_HOME}/features

if [[ ! ( -d "${LIB}" && -x "${LIB}" ) ]]; then
	echo "error: no ${LIB} directory"
	exit 1
fi

if [[ ! ( -d "${CONFIG}" && -x "${CONFIG}" ) ]]; then
	echo "error: no ${CONFIG} directory"
	exit 2
fi

# ensure that the directory exists
mkdir -p "${FEATURES}" 2> /dev/null

if [[ ! -d "${DB}" ]]; then
	mkdir -p "${DB}"
fi

# relative per Feature Directory Paths

FEATURE_DEPS="lib/dependencies"
FEATURE_LIB="lib/feature"
FEATURE_CONFIG="config"
FEATURE_BIN="bin"
FEATURE_INSTALL="install"
FEATURE_ARTIFACTS="artifacts"
FEATURE_DB="db"
FEATURE_SQL="sql"

UPGRADE_SQL_SUFFIX=".upgrade.sql"
DOWNGRADE_SQL_SUFFIX=".downgrade.sql"

featureJars=$(find "${FEATURES}" -name "feature-*.jar" -type f -exec basename {} \; 2> /dev/null)

# default field lengths
nameLength=20
versionLength=15

# update field lengths, if needed
for jar in ${featureJars} ; do
	# get file name without 'jar' suffix
	tmp="${jar%\.jar}"

	# remove feature prefix
	tmp="${tmp#feature-}"
		
	# get feature name by removing the version portion
	name="${tmp%%-[0-9]*}"

	# extract version portion of name
	version="${tmp#${name}-}"

	# grow the size of the name/version field, if needed
	if (( "${#name}" > nameLength )) ; then
		nameLength="${#name}"
	fi
	if (( "${#version}" > versionLength )) ; then
		versionLength="${#version}"
	fi
done

# ##########################################################
# usage: usage information
# ##########################################################
function usage
{
		# print out usage information
		cat >&2 <<-'EOF'
		Usage:  features status
		            Get enabled/disabled status on all features
		        features enable <feature> ...
		            Enable the specified feature
		        features disable <feature> ...
		            Disable the specified feature
		        features install [ <feature> | <file-name> ] ...
		            Install the specified feature
		        features uninstall <feature> ...
		            Uninstall the specified feature
		EOF
}

# ##########################################################
# status: dump out status information
# ##########################################################
function status
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} --"
		set -x
	fi
	
	local tmp name version status
	local format="%-${nameLength}s %-${versionLength}s %s\n"
	
	printf "${format}" "name" "version" "status"
	printf "${format}" "----" "-------" "------"
	
	for jar in ${featureJars} ; do
		# get file name without 'jar' suffix
		tmp="${jar%\.jar}"
		
		# remove feature prefix
		tmp="${tmp#feature-}"

		# get feature name by removing the version portion
		name="${tmp%%-[0-9]*}"

		# extract version portion of name
		version="${tmp#${name}-}"

		# determine status
		status=disabled
		if [[ -e "${LIB}/${jar}" ]] ; then
			status=enabled
		fi
		printf "${format}" "${name}" "${version}" "${status}"
	done
	echo
}

# ##########################################################
# enableDepAnalysis (featureName):  
#                   reports on potential dependency conflicts
#   featureName: name of the feature
# ##########################################################
function enableDepAnalysis ()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi
	
	local featureName="$1"
	local featureDepJars featureDepJarPath depJarName multiVersionJars
	
	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi
	
	featureDepJars=$(ls "${FEATURES}"/"${featureName}"/"${FEATURE_DEPS}"/*.jar 2> /dev/null)
	for featureDepJarPath in ${featureDepJars}; do
		depJarName=$(basename "${featureDepJarPath}")
		
		# it could be a base jar

		if [[ -f "${LIB}"/"${depJarName}" ]]; then
			echo "warning: dependency ${depJarName} already in use"
			continue
		fi
		
		# it could be a link from another feature

		if [[ -L "${LIB}"/"${depJarName}" ]]; then
			continue
		fi
		
		# unadvisable if multiple versions exist but the base or
		# installed one wins.

		multiVersionJars=$(ls "${LIB}"/"${depJarName%%-[0-9]*.jar}"-*.jar 2> /dev/null)
		if [[ -n "${multiVersionJars}" ]]; then
			echo "warning: other version of library ${depJarName} present: ${multiVersionJars}"
			continue
		fi
	done
}

# ##########################################################
# enableConfigAnalysis (featureName):  
#                   reports on potential dependency conflicts
#   featureName: name of the feature
# ##########################################################
function enableConfigAnalysis ()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi
	
	local featureName="$1"
	local featureConfigs configPath configFileName
	
	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi
	
	featureConfigs=$(ls "${FEATURES}"/"${featureName}"/"${FEATURE_CONFIG}"/ 2> /dev/null)
	for configPath in ${featureConfigs}; do
		configFileName=$(basename "${configPath}")
		if [[ -e "${CONFIG}"/"${configFileName}" ]]; then
			echo "error: a config file of the same name is already in the base installation: ${configFileName}"
			return 2
		fi
	done
}

# ##########################################################
# enableBinAnalysis (featureName):
#                   reports on potential dependency conflicts
#   featureName: name of the feature
# ##########################################################
function enableBinAnalysis ()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi

	local featureName="$1"
	local featureBins binPath binFileName

	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi

	featureBins=$(ls "${FEATURES}"/"${featureName}"/"${FEATURE_BIN}"/ 2> /dev/null)
	for binPath in ${featureBins}; do
		binFileName=$(basename "${binPath}")
		if [[ -e "${CONFIG}"/"${binFileName}" ]]; then
			echo "error: a bin file of the same name is already in the base installation: ${binFileName}"
			return 2
		fi
	done
}

# ##########################################################
# enableDbAnalysis (featureName):  
#                   reports on potential db access problems
#   featureName: name of the feature
# ##########################################################
function enableDbAnalysis()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi
	
	local featureName="$1"
	local featureSqls
	
	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi
	
	featureSqls=$(ls "${FEATURES}"/"${featureName}"/"${FEATURE_DB}"/*/${FEATURE_SQL}/*${UPGRADE_SQL_SUFFIX} 2> /dev/null)
	if [[ -z ${featureSqls} ]]; then
		return 0
	fi
	
	source "${POLICY_HOME}"/etc/profile.d/env.sh
	if [[ -z ${SQL_HOST} ]] || [[ -z ${SQL_USER} ]] || [[ -z ${SQL_PASSWORD} ]]; then
		echo "warning: DB server is not configured"
	fi

	return 0
}

# ##########################################################
# enableFeatureDeps(featureName):  
#                               enables feature dependencies
#   featureName: name of the feature
# ##########################################################
function enableFeatureDeps()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi
	
	local featureName="$1"
	local featureDeps featureDepPath depJarName
	
	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi
	
	featureDeps=$(ls "${FEATURES}"/"${featureName}"/"${FEATURE_DEPS}"/*.jar 2> /dev/null)
	for featureDepPath in ${featureDeps}; do
		depJarName=$(basename "${featureDepPath}")
		if [[ ! -f "${LIB}"/"${depJarName}" ]]; then
			ln -s -f "${featureDepPath}" "${LIB}/"
		fi
	done
}

# ##########################################################
# enableFeatureConfig(featureName):  
#                               enables feature configuration
#   featureName: name of the feature
# ##########################################################
function enableFeatureConfig()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi
	
	local featureName="$1"
	local featureConfigs featureConfigPath
	
	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi
	
	featureConfigs=$(find "${FEATURES}"/"${featureName}"/"${FEATURE_CONFIG}"/ -type f -maxdepth 1 2> /dev/null)
	for featureConfigPath in ${featureConfigs}; do
		ln -s -f "${featureConfigPath}" "${CONFIG}/"
	done
}

# ##########################################################
# enableFeatureBin(featureName):
#                               enables feature binaries
#   featureName: name of the feature
# ##########################################################
function enableFeatureBin()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi

	local featureName="$1"
	local featureBins featureBinPath

	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi

	featureBins=$(find "${FEATURES}"/"${featureName}"/"${FEATURE_BIN}"/ -type f -maxdepth 1 2> /dev/null)
	for featureBinPath in ${featureBins}; do
		chmod u+x "${featureBinPath}"
		ln -s -f "${featureBinPath}" "${BIN}/"
	done
}

# ##########################################################
# enableFeatureDbSchema(featureName):  
#        enables feature DB Schema configuration
#   featureName: name of the feature
# ##########################################################
function enableFeatureDbSchema()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi
	
	local featureName="$1"
	local featureDbPath="$2"
	local schemaName="$3"
	
	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi
	
	if [[ -z ${featureDbPath} ]]; then
		echo "warning: ${featureName} contains no DB path"
		return 2
	fi
	
	if [[ -z ${schemaName} ]]; then
		echo "warning: feature ${featureName} contains no schema name"
		return 3
	fi
	
	rc=0
	sqlUpgradeScripts=$(ls "${featureDbPath%/}"/${FEATURE_SQL}/*${UPGRADE_SQL_SUFFIX} 2> /dev/null)
	for sqlUpgradeScript in ${sqlUpgradeScripts}; do
		if [[ ! -d "${DB}"/"${schemaName}"/${FEATURE_SQL} ]]; then
			mkdir -p "${DB}"/"${schemaName}"/${FEATURE_SQL} 2> /dev/null
		fi		
		ln -s -f "${sqlUpgradeScript}" "${DB}"/"${schemaName}"/${FEATURE_SQL}/
	done
	
	sqlDowngradeScripts=$(ls "${featureDbPath%/}"/${FEATURE_SQL}/*${DOWNGRADE_SQL_SUFFIX} 2> /dev/null)
	for sqlDowngradeScript in ${sqlDowngradeScripts}; do
		if [[ -d "${DB}"/"${schemaName}"/${FEATURE_SQL} ]]; then
			sqlName=$(basename "${sqlDowngradeScript}")
			rm -f "${DB}"/"${schemaName}"/"${FEATURE_SQL}"/"${sqlName}" 2> /dev/null
		else
			echo "warning: feature ${featureName} only contains downgrade scripts"
			rc=4
			break
		fi
	done
	
	if [[ -n ${sqlUpgradeScripts} || -n ${sqlDowngradeScripts} ]]; then
		DEBUG=${DEBUG} db-migrator -s "${schemaName}" -o ok
	fi
	
	return ${rc}
}

# ##########################################################
# enableFeatureDb(featureName):  
#       enables DB feature configuration
#   featureName: name of the feature
# ##########################################################
function enableFeatureDb()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi
	
	local featureName="$1"
	local featureDbs featureDbPath schemaName sqls
	
	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi
	
	featureDbs=$(ls -d "${FEATURES}"/"${featureName}"/"${FEATURE_DB}"/*/ 2> /dev/null)
	for featureDbPath in ${featureDbs}; do
		sqls=$(ls "${featureDbPath%/}"/"${FEATURE_SQL}"/*.sql 2> /dev/null)
		if [[ -z ${sqls} ]]; then
			continue
		fi
		schemaName=$(basename "${featureDbPath%/}")
		enableFeatureDbSchema "${featureName}" "${featureDbPath%/}" "${schemaName}"
	done
}

# ##########################################################
# enableFeatureArtifacts(featureName):
#   deploys maven artifacts
#       featureName: name of the feature
# ##########################################################
function enableFeatureArtifacts()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi

	local featureName="$1"
	local artifacts

	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi

	artifacts=$(ls "${FEATURES}"/"${featureName}"/"${FEATURE_ARTIFACTS}"/* 2> /dev/null)
	for artifactPath in ${artifacts}; do
	    deploy-artifact -f -a "${artifactPath}"
	done
}

# ##########################################################
# customize(featureName): 
#	executes customized script for an operation.
#
# featureName - feature name
# operation - operation, ie. 
#  'enable', 'disable', 'install', or 'uninstall'
# ##########################################################
function customOpScript()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi
	
	local featureName="$1"
	local operation="$2"
	
	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi
	
	if [[ -z ${operation} ]]; then
		echo "warning: ${featureName} : a custom operation script must be provided"
		return 1
	fi
	
	local customScript="${FEATURES}"/"${featureName}"/"${FEATURE_INSTALL}"/"${operation}"
	if [[ -f ${customScript} ]]; then
		(
			cd "${FEATURES}"/"${featureName}"/"${FEATURE_INSTALL}"
			chmod u+x "${customScript}"
			./"$(basename "${customScript}")"
		)
	fi
}

# ##########################################################
# enableFeature(featureName, featureJar):  enables a feature
#   featureName: name of the feature
#   featureJar:  path to feature jar implementation
# ##########################################################
function enableFeature()
{
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi
	
	local featureName="$1"
	local featureJar="$2"
	
	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi
	
	if [[ -z ${featureJar} ]]; then
		echo "warning: no feature jar"
		return 2
	fi
	
	if ! enableDepAnalysis  "${featureName}"; then
		return "$?"
	fi
	
	if ! enableConfigAnalysis  "${featureName}"; then
		return "$?"
	fi
	
	if ! enableBinAnalysis  "${featureName}"; then
		return "$?"
	fi

	if ! enableDbAnalysis "${featureName}"; then
		return "$?"
	fi
	
	# enable feature itself

	ln -s -f "${featureJar}" "${LIB}/"
		
	# enable dependent libraries if any
	
	enableFeatureDeps "${featureName}"
	
	# enable configuration

	enableFeatureConfig "${featureName}" 
	
	# enable binaries

	enableFeatureBin "${featureName}"

	# enable db

	enableFeatureDb "${featureName}"

	# enable feature artifacts

	enableFeatureArtifacts "${featureName}"

	# run custom enable if any

	customOpScript "${featureName}" "enable"
}

# ##########################################################
# disableFeatureDeps(featureName):  
#			disables feature dependencies
# ##########################################################
function disableFeatureDeps()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi
	
	local featureName="$1"
	local xDepsEnabledMap featureBaseDirs aFeatureDir aFeatureName
	local featureDeps aFeatureDep 
	local depJarPath depJarName depJarRealPath
	
	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi
	
	declare -A xDepsEnabledMap
	
	featureBaseDirs=$(ls -d "${FEATURES}"/*/ 2> /dev/null)
	for aFeatureDir in ${featureBaseDirs}; do 
		aFeatureName=$(basename "${aFeatureDir}")
		if [[ "${aFeatureName}" == "${featureName}" ]]; then
			continue
		fi
		
		depJarPaths=$(ls "${aFeatureDir}"/"${FEATURE_DEPS}"/*.jar 2> /dev/null)
		for depJarPath in ${depJarPaths}; do
			if [[ "$?" == 0 ]] ; then
				depJarName=$(basename "${depJarPath}")
				xDepsEnabledMap[${depJarName}]="${depJarPath}"
			fi
		done
	done
	
	if [[ ${DEBUG} == y ]]; then
		echo "${!xDepsEnabledMap[@]}"
		echo "${xDepsEnabledMap[@]}"
	fi
	
	featureDeps=$(ls "${FEATURES}"/"${featureName}"/"${FEATURE_DEPS}"/*.jar 2> /dev/null)
	for aFeatureDep in ${featureDeps}; do
		depJarName=$(basename "${aFeatureDep}")
		if [[ -L "${LIB}"/"${depJarName}" ]]; then
			depJarRealPath=$(readlink -f "${LIB}"/"${depJarName}")
			if [[ "${depJarRealPath}" == "${aFeatureDep}" ]]; then
				rm -f "${LIB}"/"${depJarName}"
				
				# case there were multiple features using this library
				# re-enable link fron an enabled feature
		
				if [[ -n ${xDepsEnabledMap[${depJarName}]} ]]; then
					ln -s -f "${xDepsEnabledMap[${depJarName}]}" "${LIB}/"
				fi
			fi
		fi		
	done
}

# ##########################################################
# disableFeatureConfig(featureName):  
#                               disables feature configuration
#   featureName: name of the feature
# ##########################################################
function disableFeatureConfig()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi
	
	local featureName="$1"
	local featureConfigs featureConfigPath
	
	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi
	
	featureConfigs=$(find "${FEATURES}"/"${featureName}"/"${FEATURE_CONFIG}"/ -type f -maxdepth 1 2> /dev/null)
	for featureConfigPath in ${featureConfigs}; do
		configFileName=$(basename "${featureConfigPath}")
		rm -f "${CONFIG}"/"${configFileName}" 2> /dev/null
	done
}

# ##########################################################
# disableFeatureBin(featureName):
#                               disables feature binaries
#   featureName: name of the feature
# ##########################################################
function disableFeatureBin()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi

	local featureName="$1"
	local featureBins featureBinPath

	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi

	featureBins=$(find "${FEATURES}"/"${featureName}"/"${FEATURE_BIN}"/ -type f -maxdepth 1 2> /dev/null)
	for featureBinPath in ${featureBins}; do
		binFileName=$(basename "${featureBinPath}")
		rm -f "${BIN}"/"${binFileName}" 2> /dev/null
	done
}

# ##########################################################
# disableFeatureDbSchema(featureName, featureDbPath, schemaName):  
# 				disables feature db configuration for a schema
#   featureName: name of the feature
# ##########################################################
function disableFeatureDbSchema()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi
	
	local featureName="$1" featureDbPath="$2" schemaName="$3"
	local upgradeFeatureSqls downgradeFeatureSqls featureSql sqlDir sqlName schemaDir schemaName
	
	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi
	
	if [[ -z ${featureDbPath} ]]; then
		echo "warning: ${featureName} contains no DB path"
		return 2
	fi
	
	if [[ -z ${schemaName} ]]; then
		echo "warning: feature ${featureName} contains no schema name"
		return 3
	fi
	
	
	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi
	
	upgradeFeatureSqls=$(find "${FEATURES}"/"${featureName}"/"${FEATURE_DB}"/"${schemaName}"/"${FEATURE_SQL}"/*"${UPGRADE_SQL_SUFFIX}" -type f -maxdepth 1 2> /dev/null)
	for featureSql in ${upgradeFeatureSqls}; do
		sqlName=$(basename "${featureSql}")
		sqlDir=$(dirname "${featureSql}")
		schemaDir=$(dirname "${sqlDir}")
		schemaName=$(basename "${schemaDir}")
		rm -f "${DB}"/"${schemaName}"/"${FEATURE_SQL}"/"${sqlName}" 2> /dev/null
	done
	
	downgradeFeatureSqls=$(find "${FEATURES}"/"${featureName}"/"${FEATURE_DB}"/"${schemaName}"/"${FEATURE_SQL}"/*"${DOWNGRADE_SQL_SUFFIX}" -type f -maxdepth 1 2> /dev/null)
	for featureSql in ${downgradeFeatureSqls}; do
		sqlName=$(basename "${featureSql}")
		sqlDir=$(dirname "${featureSql}")
		schemaDir=$(dirname "${sqlDir}")
		schemaName=$(basename "${schemaDir}")
		if [[ ! -d "${DB}"/"${schemaName}"/${FEATURE_SQL} ]]; then
			mkdir -p "${DB}"/"${schemaName}"/${FEATURE_SQL} 2> /dev/null
		fi		
		ln -s -f "${featureSql}" "${DB}"/"${schemaName}"/${FEATURE_SQL}/
	done
	
	if [[ -n ${sqlUpgradeScripts} || -n ${sqlDowngradeScripts} ]]; then
		DEBUG=${DEBUG} db-migrator -s "${schemaName}" -o ok
	fi
}

# ##########################################################
# disableFeatureDb(featureName):  
# 				disables feature db configuration
#   featureName: name of the feature
# ##########################################################
function disableFeatureDb()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi

	local featureName="$1"
	local featureDbPath featureDbs schemaName
	
	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi
	
	featureDbs=$(ls -d "${FEATURES}"/"${featureName}"/"${FEATURE_DB}"/*/ 2> /dev/null)
	for featureDbPath in ${featureDbs}; do
		if [[ -z "$(ls "${featureDbPath%/}"/"${FEATURE_SQL}"/*${UPGRADE_SQL_SUFFIX} 2> /dev/null)" ]]; then
			continue
		fi
		schemaName=$(basename "${featureDbPath%/}")
		disableFeatureDbSchema "${featureName}" "${featureDbPath%/}" "${schemaName}"
	done
}

# ##########################################################
# disableFeature(featureName):  disables a feature
#   featureName: name of the feature
# ##########################################################
function disableFeature()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi
	
	local featureName="$1"
	
	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return
	fi
	
	# disable feature itself

	(
	cd "${LIB}"
	rm -f feature-"${featureName}"-[0-9]*.jar 2> /dev/null
	)
		
	# disable dependencies if any

	disableFeatureDeps "${featureName}"
	
	# disable configuration if any

	disableFeatureConfig "${featureName}"
	
	# disable binaries if any

	disableFeatureBin "${featureName}"

	# disable DB SQL scripts if any

	disableFeatureDb "${featureName}"

	# run custom disable if any

	customOpScript "${featureName}" "disable"
}

############################################################
# configureFeature <config-file> <features-root-directory>
#
# This was copied from 'policy-drools/docker-install.sh'
# in the 'docker' repository, and modified where needed.
############################################################
function configureFeature() 
{
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi

	local envConfig=$1 featureRoot=$2 
	local sedLine="sed -i" 
	local sedFiles="" nonBinaryFiles sedFile name value
		
	while read line || [ -n "${line}" ]; do
		if [[ -n ${line} ]] && [[ ${line:0:1} != \# ]]; then
			name="${line%%=*}"
			value="${line#*=}"
			value=$(echo "${value}" | sed -e 's/[\/&]/\\&/g')
			if [[ -z ${name} ]] || [[ -z ${value} ]]; then
				echo "warning: ${line} missing name or value"
			fi
			sedLine+=" -e 's/\${{${name}}}/${value}/g' "
		fi
	done < "${envConfig}"
	
	nonBinaryFiles=$(find "${featureRoot}" -type f -exec grep -Iq . {} \; -print 2> /dev/null)
	for sedFile in ${nonBinaryFiles}; do
		if fgrep -l '${{' ${sedFile} > /dev/null 2>&1; then
			sedFiles+="${sedFile} "
		fi
	done

	if [[ -n ${sedFiles} ]]; then
		sedLine+=${sedFiles}
		eval "${sedLine}"
	fi
}

############################################################
# installFeatures <feature-name-or-zip-file> ...
#
# This was copied from 'policy-drools/docker-install.sh'
# in the 'docker' repository, and modified where needed.
############################################################
function installFeatures
{
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi

	local name featureConf feature dir conf
	
	if [[ -d "${FEATURES}" && -x "${FEATURES}" ]]; then
		SOURCE_DIR=$PWD
		for feature in "$@" ; do
			dir=
			if [[ "${feature}" == feature-*.zip ||
						"${feature}" == */feature-*.zip ]] ; then
				# the ZIP file is specified -- find the name
				name="${feature##*/}"
				name="${name#feature-}"
				name="${name%%-[0-9]*\.zip}"

				# if the ZIP file has a directory name component,
				# set 'dir' accordingly
				if [[ "${feature}" =~ / ]] ; then
					dir="${feature%/*}"
				fi
			else
				# Doesn't match the ZIP file name convention -- interpret
				# this as a feature name, and try to locate a matching ZIP
				# file. If there is more than one, choose the one with the
				# highest version number.
				name="${feature}"
				feature=$(ls -v feature-"${name}"-[0-9]*.zip 2>/dev/null|tail -1)
			fi
			if [[ ! -f "${feature}" ]] ; then
				# include the file name in the error message, unless we don't
				# have one -- in this case, use the feature name
				echo "error: feature file ${feature:-for ${name}} not found"
				continue
			fi
			if [[ -d "${FEATURES}/${name}" ]] ; then
				echo "error: feature ${name} has already been installed"
				continue
			fi

			# extract contents of ZIP file in to feature directory
			mkdir -p "${FEATURES}/${name}" > /dev/null 2>&1
			(cd "${FEATURES}/${name}"; jar xf "${SOURCE_DIR}"/"${feature}")

			# if there is a configuration file available,
			# use it to configure the feature
			featureConf="${dir:+$dir/}feature-${name}.conf"
			if [[ -r "${featureConf}" ]]; then
				configureFeature "${featureConf}" "${FEATURES}"/"${name}"
				cp "${featureConf}" "${POLICY_HOME}"/etc/profile.d
				echo "feature ${name} has been installed (configuration present)"
			else
				echo "feature ${name} has been installed (no configuration present)"
			fi
					
			customOpScript "${name}" "install"
		done
		
		# check the current directory and the 'config' directory for a
		# 'base.conf' file -- use the first one that is found
		for conf in base.conf ${POLICY_HOME}/config/base.conf ${POLICY_HOME}/etc/profile.d/base.conf; do
			if [[ -f "${conf}" ]] ; then
				echo "applying base configuration '${conf}' to features"
				configureFeature "${conf}" "${FEATURES}"
				break
			fi
		done
	else
		echo "error: aborting -- ${FEATURES} is not accessible"
		exit 1
	fi
}

# ##########################################################
# uninstallFeatureDb(featureName):  
# 				uninstalls the feature db configuration
#   featureName: name of the feature
# ##########################################################
function uninstallFeatureDb()
{
	if [[ ${DEBUG} == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi

	local featureName="$1"
	local featureSqls sqlDir sqlName schemaDir schemaName schemaNames leftSqls
	
	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return 1
	fi
	
	featureSqls=$(find "${FEATURES}"/"${featureName}"/"${FEATURE_DB}"/*/${FEATURE_SQL}/*.sql -type f -maxdepth 1 2> /dev/null)
	for featureSql in ${featureSqls}; do
		sqlName=$(basename "${featureSql}")
		sqlDir=$(dirname "${featureSql}")
		schemaDir=$(dirname "${sqlDir}")
		schemaName=$(basename "${schemaDir}")
		schemaNames+=${schemaName} 
		rm -f "${DB}"/"${schemaName}"/"${FEATURE_SQL}"/"${sqlName}" 2> /dev/null
	done
	
	for schemaName in ${schemaNames};
	do
		leftSqls=$(ls "${DB}"/"${schemaName}"/"${FEATURE_SQL}"/*.sql 2> /dev/null)
		if [[ -z ${leftSqls} ]]; then
			if ! DEBUG=${DEBUG} db-migrator -s "${schemaName}" -o ok; then
				echo -n "warning: ${featureName}: ${schemaName}: database data is leftover. "
				echo -n "Consider cleaning left over data with 'db-migrator'."
			fi
		fi		
	done
}

############################################################
# uninstallFeature <feature-name> ...
############################################################
function uninstallFeature
{
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $* --"
		set -x
	fi
	
	local featureName="$1"
	
	if [[ -z ${featureName} ]]; then
		echo "warning: no feature name"
		return
	fi
	
	disableFeature "${featureName}"
	uninstallFeatureDb "${featureName}"
	customOpScript "${featureName}" "uninstall"
	
	if [[ -n ${FEATURES} && -n ${featureName} ]]; then
		rm -rf "${FEATURES:-???}/${featureName}"
	fi
}

############################################################
# uninstallFeatures <feature-name> ...
############################################################
function uninstallFeatures
{
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} --"
		set -x
	fi
	
	local name
	local allFeatures=$'\n'$(cd ${FEATURES};ls)$'\n'
	for name in "$@" ; do
		# the following check takes care of potentially troublesome names
		# like '.', '..', and names containing '/'
		if [[ "${allFeatures}" =~ $'\n'${name}$'\n' ]] ; then
			uninstallFeature "${name}"
		else
			echo "feature ${name} not found"
		fi
	done
}

case "$1" in
	status)
	{
		# dump out status information
		status
	};;

	enable)
	{
		if [[ -f "${POLICY_HOME}"/PID ]]; then
			echo "error: enable: not allowed when policy is running .."
			echo
			status
			exit 10
		fi
		
		# enable the specified options
		shift
		match=
		for name in "$@" ; do
			# look for matches - 'file' has the full path name
			file=$(ls "${FEATURES}"/"${name}"/"${FEATURE_LIB}"/feature-"${name}"-[0-9]*.jar 2> /dev/null)
			if [[ "$?" != 0 ]] ; then
				# no matching file
				echo "${name}:  no such option"
			else
				# make sure there is only one feature jar
				countFeatureJars=$(echo "${file}" | wc -w)
				if [[ ${countFeatureJars} != 1 ]]; then
					echo "warning: skipping ${name},  ${countFeatureJars} feature libraries found"
					continue
				fi			
				
				# found a match (handle multiple matches, just in case)
				match=true
				
				enableFeature "${name}" "${file}"
			fi
		done
		if [[ "${match}" ]] ; then
			echo
			status
		fi
	};;

	disable)
	{
		if [[ -f "${POLICY_HOME}"/PID ]]; then
			echo "error: disable: not allowed when policy is running .."
			echo
			status
			exit 11
		fi
		
		# disable the specified options
		shift
		match=
		for name in "$@" ; do
			# look for matches -- 'file' has the last segment of the path name
			file=$(ls "${FEATURES}"/"${name}"/"${FEATURE_LIB}"/feature-"${name}"-[0-9]*.jar 2> /dev/null)
			if [[ "$?" != 0 ]] ; then
				echo "${name}:  no such option"
			else
				# found a match (handle multiple matches, just in case)
				match=true
				
				disableFeature "${name}"
			fi
		done
		if [[ "${match}" ]] ; then
			echo
			status
		fi
	};;

	install)
	{
		shift
		installFeatures "$@"
	};;

	uninstall)
	{
		if [[ -f "${POLICY_HOME}"/PID ]]; then
			echo "error: uninstall: not allowed when policy is running .."
			echo
			status
			exit 12
		fi
		shift
		uninstallFeatures "$@"
	};;

	*)
	{
		usage
	};;
esac
exit