# ============LICENSE_START=======================================================
 # org.onap.dcae
 # ================================================================================
-# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
+# Copyright (c) 2018-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.
 
 # Store type files locally
 RUN mkdir scripts
-COPY get-type-files.sh dcae-cleanup.sh start-persistent.sh setup-secret.sh scripts/
+COPY scripts/* scripts/
 # Load our type files and the Cloudify 3.4 type files
 # Setup rc.local to set up k8s credentials for CM
 RUN scripts/get-type-files.sh ${TYPE_REPO} ${CCSDK_REPO}\
     && chown -R cfyuser:cfyuser /opt/manager/resources/spec/cloudify/3.4\
     && chmod +x scripts/*.sh\
     && echo "/scripts/setup-secret.sh" >> /etc/rc.d/rc.local\
+    && echo "/scripts/set-resolver-rules.sh" >> /etc/rc.d/rc.local\
     && chmod +x /etc/rc.d/rc.local
 # Create mount point for CM config file
 RUN mkdir -p /opt/onap && chown cfyuser:cfyuser /opt/onap
 
--- /dev/null
+#!/bin/bash
+# ============LICENSE_START=======================================================
+# org.onap.dcae
+# ================================================================================
+# Copyright (c) 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=========================================================
+#
+# Checking Cloudify Manager readiness by looking
+# for non-running services
+# Relying on the output format of the "cfy status" command.
+# A successful execution of the command outputs:
+#
+# cfy status
+# Retrieving manager services status... [ip=localhost]
+#
+# Services:
+# +--------------------------------+---------+
+# |            service             |  status |
+# +--------------------------------+---------+
+# | InfluxDB                       | running |
+# | Logstash                       | running |
+# | AMQP InfluxDB                  | running |
+# | RabbitMQ                       | running |
+# | Webserver                      | running |
+# | Management Worker              | running |
+# | PostgreSQL                     | running |
+# | Cloudify Console               | running |
+# | Manager Rest-Service           | running |
+# | Riemann                        | running |
+# +--------------------------------+---------+
+#
+# When an individual service is not running, it will have a status other than "running".
+# If the Cloudify API cannot be reached, the "Services:" line will not appear.
+
+STAT=$(cfy status)
+if (echo "${STAT}" | grep "^Services:$")
+then
+   echo "Got a status response"
+   if !(echo "${STAT}" | egrep '^\| [[:alnum:]]+'| grep -iv '| running ')
+   then
+      echo "All services running"
+      exit 0
+   else
+      echo "Some service(s) not running"
+   fi
+else
+  echo "Did not get a status response"
+fi
+echo "${STAT}"
+exit 1
 
 # ============LICENSE_START=======================================================
 # org.onap.dcae
 # ================================================================================
-# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
+# Copyright (c) 2018-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.
 #
 set -x
 DEST=/opt/manager/resources/onapspec
+EXTRA_RULES=/opt/manager/extra-resolver-rules
 
 DCAETYPEFILES=\
 "\
 chown cfyuser:cfyuser ${DEST}
 
 # Add our local type file store to CM import resolver configuration
-TYPE_RULE0="{$1: file://${DEST}}"
-TYPE_RULE1="{$2: file://${DEST}}"
+TYPE_RULE0="{\"$1\": \"file://${DEST}\"}"
+TYPE_RULE1="{\"$2\": \"file://${DEST}\"}"
 # This sed re is 'brittle' but we can be sure the config.yaml file
 # won't change as long as we do not change the source Docker image for CM
 sed -i -e "s#      rules:#      rules:\n      - ${TYPE_RULE0}#" /etc/cloudify/config.yaml
 sed -i -e "s#      rules:#      rules:\n      - ${TYPE_RULE1}#" /etc/cloudify/config.yaml
 
 chown cfyuser:cfyuser /etc/cloudify/config.yaml
+
+# Changing /etc/cloudify/config.yaml is no longer sufficient
+# Need to provide the additional rules in a file that can be
+# used at deployment time to update the resolver rules
+echo "- ${TYPE_RULE0}" > ${EXTRA_RULES}
+echo "- ${TYPE_RULE1}" >> ${EXTRA_RULES}
\ No newline at end of file
 
--- /dev/null
+#!/bin/bash
+# ============LICENSE_START=======================================================
+# org.onap.dcae
+# ================================================================================
+# Copyright (c) 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=========================================================
+#
+# Check whether Cloudify Manager is ready to take traffic
+# Two conditions must be met:
+#    -- The import resolver rules must have been updated.
+#       This is indicated by the presence of the file named
+#       /opt/manager/extra-resolver-rules-loaded.
+#    -- All Cloudify Manager services must be running, as
+#       indicated by the output of the cfy status command.
+
+RULES_LOADED=/opt/manager/extra-resolver-rules-loaded
+
+set -x
+
+if [[ -f  $RULES_LOADED ]]
+then
+  # Check for all services running
+  if /scripts/cloudify-ready.sh
+  then
+    exit 0
+  fi
+fi
+exit 1
\ No newline at end of file
 
--- /dev/null
+#!/bin/bash
+# ============LICENSE_START=======================================================
+# org.onap.dcae
+# ================================================================================
+# Copyright (c) 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=========================================================
+
+set -ex
+EXTRA_RULES=/opt/manager/extra-resolver-rules
+PY=/opt/manager/env/bin/python
+# Wait for Cloudify Manager to come up
+while ! /scripts/cloudify-ready.sh
+do
+    echo "Waiting for CM to come up"
+    sleep 15
+done
+
+if [[ -s ${EXTRA_RULES} && -r ${EXTRA_RULES} ]]
+then
+    # Capture current resolver rules and append to new rules
+    ${PY} /scripts/update_resolver.py --dry-run | egrep "^-" >> ${EXTRA_RULES}
+
+    # Update the resolver rules
+    ${PY} /scripts/update_resolver.py ${EXTRA_RULES}
+    systemctl restart cloudify-restservice.service
+    mv ${EXTRA_RULES} ${EXTRA_RULES}-loaded
+fi
\ No newline at end of file
 
--- /dev/null
+#!/usr/bin/env python
+#============LICENSE_START==========================================================
+# org.onap.dcae
+# ==================================================================================
+# Copyright (c) 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===========================================================
+#
+import sys
+import yaml
+from sqlalchemy.orm.attributes import flag_modified
+from manager_rest.flask_utils import setup_flask_app
+from manager_rest.constants import PROVIDER_CONTEXT_ID
+from manager_rest.storage import get_storage_manager, models
+
+
+def main(dry_run, rules_file):
+
+    with setup_flask_app().app_context():
+        sm = get_storage_manager()
+        ctx = sm.get(models.ProviderContext, PROVIDER_CONTEXT_ID)
+        print 'Resolver rules before update:'
+        print yaml.safe_dump(ctx.context['cloudify']['import_resolver']['parameters']['rules'])
+
+        if dry_run:
+            return
+
+        with open(rules_file, 'r') as rules:
+            new_rules = yaml.load(rules)
+        ctx.context['cloudify']['import_resolver']['parameters']['rules'] = new_rules
+        print '\nResolver rules to update:'
+        print yaml.safe_dump(new_rules)
+        flag_modified(ctx, 'context')
+        sm.update(ctx)
+        print '\nProvide Context Saved'
+        print '\nResolver rules after update:'
+        print yaml.safe_dump(ctx.context['cloudify']['import_resolver']['parameters']['rules'])
+
+
+if __name__ == '__main__':
+    if len(sys.argv) < 2:
+        print 'Must provide path to yaml file containing new rules or --dry-run'
+        exit(1)
+
+    main(sys.argv[1]=='--dry-run', sys.argv[1])
\ No newline at end of file