cc6bf1881fe97b92e6e22f91104fabf52cc3565e
[integration/csit.git] / tests / sdnc / sdnc_netconf_tls_post_deploy / libraries / config.sh
1 #!/bin/bash
2
3 #
4 # ============LICENSE_START=======================================================
5 #   Copyright (C) 2020 Nordix Foundation.
6 # ================================================================================
7 #  Licensed under the Apache License, Version 2.0 (the "License");
8 #  you may not use this file except in compliance with the License.
9 #  You may obtain a copy of the License at
10 #
11 #       http://www.apache.org/licenses/LICENSE-2.0
12 #
13 #  Unless required by applicable law or agreed to in writing, software
14 #  distributed under the License is distributed on an "AS IS" BASIS,
15 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 #  See the License for the specific language governing permissions and
17 #  limitations under the License.
18 #  SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
20
21 # @author Ajay Deep Singh (ajay.deep.singh@est.tech)
22
23 CONTAINER_NAME="$1"
24 LOGFILE="${WORKSPACE}"/archives/config.log
25 CONTAINER_ID=$(docker inspect --format="{{.Id}}" "$CONTAINER_NAME")
26
27 OWNER="odl"
28 DEST_DIR="/tmp"
29
30 CERT_DIR="${WORKSPACE}"/tests/sdnc/sdnc_netconf_tls_post_deploy/cert-data/*
31
32 function now_ms() {
33   date +"%Y-%m-%d %H:%M:%S.%3N"
34 }
35
36 function log() {
37   local level=$1
38   shift
39   local message="$*"
40   printf "%s %-5s %s\n" "$(now_ms)" "$level" "$message" >>"$LOGFILE"
41 }
42
43 # Copy [keystore.jks, truststore.jks, truststore.pass, keystore.pass] files into SDNC container.
44 function docker_cp() {
45   local file=$1
46   docker cp "$file" "$CONTAINER_ID":"$DEST_DIR"
47   docker exec -u 0 "$CONTAINER_ID" chown "$OWNER":"$OWNER" "$DEST_DIR"/"${file##*/}"
48 }
49
50 # Run installCerts.py script to push X509 Certificates to SDNC-ODL Keystore/Truststore.
51 function sdnc_conf() {
52   log INFO "Configuring SDNC-ODL Keystore..."
53   count=0
54   exit_code=false
55   for i in {1..4}; do
56     for file in $CERT_DIR; do
57       if [[ -f $file ]]; then
58         log INFO "Uploading file :" "$file"
59         docker_cp "$file"
60         count=$((count + 1))
61       fi
62     done
63     if [[ $count -eq 4 ]]; then
64       log INFO "SDNC JKS files upload successful"
65       exit_code=true
66       break
67     fi
68     log DEBUG "Waiting for JKS files to be uploaded to SDNC container.."
69     sleep 2m
70   done
71   if [[ "$exit_code" != "true" ]]; then
72     log DEBUG "JKS files Not found in $CERT_DIR"
73     exit 1 # Return error code
74   fi
75   sleep 2m
76   docker exec "$CONTAINER_ID" rm -rf /tmp/certs.properties
77   docker exec "$CONTAINER_ID" rm -rf /tmp/keys0.zip
78   if ! docker exec "$CONTAINER_ID" /usr/bin/python /opt/onap/sdnc/bin/installCerts.py; then
79     log DEBUG "Issue executing installCerts.py script"
80     docker cp "$CONTAINER_ID":/opt/opendaylight/data/log/installCerts.log "${WORKSPACE}"/archives
81     exit 1 # Return error code
82   fi
83   log INFO "Configuring SDNC-ODL Keystore successful"
84 }
85
86 # Copy [Server_key.pem, Server_cert.pem, Ca.pem] files into Netconf-Simulator container.
87 # Reconfigure TLS config by invoking reconfigure-tls.sh script.
88 function netconf-simulator_conf() {
89   log INFO "Configuring Netconf-Pnp-Simulator..."
90   count=0
91   exit_code=false
92   for i in {1..4}; do
93     for file in $CERT_DIR; do
94       if [[ -f $file && ${file: -4} == ".pem" ]]; then
95         log INFO "Uploading file :" "$file"
96         docker cp "$file" "$CONTAINER_ID":/config/tls
97         count=$((count + 1))
98       fi
99     done
100     if [[ $count -eq 3 ]]; then
101       log INFO "PEM files upload successful"
102       exit_code=true
103       break
104     fi
105     log DEBUG "Waiting for PEM files to be uploaded to Netconf-Pnp-Simulator.."
106     sleep 2m
107   done
108   if [[ "$exit_code" != "true" ]]; then
109     log DEBUG "PEM files Not found in $CERT_DIR"
110     exit 1 # Return error code
111   fi
112   sleep 2m
113   if ! docker exec "$CONTAINER_ID" /opt/bin/reconfigure-tls.sh; then
114     log DEBUG "Issue executing reconfigure-tls.sh script"
115     docker logs "$CONTAINER_ID" > "${WORKSPACE}"/archives/simulator.log
116     exit 1 # Return error code
117   fi
118   log INFO "Configuring Netconf-Pnp-Simulator successful"
119 }
120
121 # Push Config on SDNC, Netconf-Simulator.
122 if [[ -n $CONTAINER_ID ]]; then
123   log INFO "Container Name: $CONTAINER_NAME, Container Id: $CONTAINER_ID"
124   if [[ "$CONTAINER_NAME" == "sdnc" ]]; then
125     sdnc_conf
126   elif [[ "$CONTAINER_NAME" == "netconf-simulator" ]]; then
127     netconf-simulator_conf
128   fi
129 fi