Merge "AAI services registration in MSB/HEAT"
[demo.git] / vnfs / vLBMS / apis / vlb-business-vnf-onap-plugin / vlb-business-vnf-onap-plugin-impl / src / main / java / org / onap / vnf / vlb / write / DnsInstanceManager.java
1
2 /*************************************************************************//**
3  *
4  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  ****************************************************************************/
18
19 package org.onap.vnf.vlb.write;
20
21 import java.io.IOException;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * 
30  * Class that maintains a map of all the active vDNS instances.
31  * A vDNS instance is represented by its IP address and a flag
32  * that represents its status, i.e. enabled or disabled.
33  * 
34  * Clients can update the vDNS map via NETCONF or RESTconf.
35  * Operations are translated by the Honeycomb agent into a JSON
36  * object that is persisted on disk (see Honeycomb distribution
37  * configuration for more information). The ElementCustomizer
38  * class then updates this map. Each operation results in a vLB
39  * setup change, e.g. GRE tunnels that connect the vLB to the
40  * vDNS instances are created or deleted, depending on the kind
41  * of operation that is invoked. Only vDNS instances for which
42  * the isEnabled flag is true are currently served by the vLB.
43  *
44  * The vLB setup and configuration is executed via shell scripts.
45  *
46  */
47
48 public class DnsInstanceManager {
49
50         private static final Logger LOG = LoggerFactory.getLogger(DnsInstanceManager.class);
51         private Map<String, Boolean> dnsInstances = new HashMap<String, Boolean>();
52
53         /*
54          * Add a new DNS instance to the map and create a GRE tunnel 
55          * towards that instance if isEnabled is set to true.
56          */
57         void addDnsInstance(String ipAddr, Boolean isEnabled) {
58                 // Call updateDnsInstance in case the DNS instance already exists.
59                 // This is somewhat redundant because Honeycomb already runs this check.
60                 if(dnsInstances.containsKey(ipAddr)) {
61                         updateDnsInstance(ipAddr, isEnabled);
62                         return;
63                 }
64
65                 dnsInstances.put(ipAddr, isEnabled);
66                 LOG.debug("DNS instance " + ipAddr + " with status isEnabled=" + isEnabled + " has been added.");
67
68                 // Create a GRE tunnel towards the new DNS instance if isEnabled is true.
69                 if(isEnabled) {
70                         addGreTunnel(ipAddr);
71                 }
72         }
73
74         /*
75          * Update an existing DNS instance and create or remove a GRE
76          * tunnel based on the current value of the isEnabled flag.
77          */
78         void updateDnsInstance(String ipAddr, Boolean isEnabled) {
79                 // This is somewhat redundant because Honeycomb already runs this check.
80                 if(dnsInstances.get(ipAddr) == isEnabled) {
81                         LOG.debug("DNS instance " + ipAddr + " with status isEnabled=" + isEnabled + " already exists. No update is necessary.");
82                         return;
83                 }
84
85                 dnsInstances.put(ipAddr, isEnabled);
86                 LOG.debug("DNS instance " + ipAddr + " has been updated with status isEnabled=" + isEnabled + ".");
87
88                 if(isEnabled) {
89                         addGreTunnel(ipAddr);
90                 }
91                 else {
92                         deleteGreTunnel(ipAddr);
93                 }
94         }
95
96         /*
97          * Delete an existing DNS instance from the map and remove the 
98          * GRE tunnel if isEnabled was set to true.
99          */
100         void deleteDnsInstance(String ipAddr) {
101                 // This is somewhat redundant because Honeycomb already runs this check.
102                 if(!dnsInstances.containsKey(ipAddr)) {
103                         LOG.debug("DNS instance " + ipAddr + " doesn't exist.");
104                         return;
105                 }
106
107                 // Remove a GRE tunnel towards this DNS instance if it exists.
108                 if(dnsInstances.get(ipAddr)) {
109                         deleteGreTunnel(ipAddr);
110                 }
111
112                 dnsInstances.remove(ipAddr);
113         }
114
115         /*
116          * Auxiliary function that calls a shell script to create a GRE tunnel towards a new vDNS instance.
117          */
118         private void addGreTunnel(String ipAddr) {
119                 String script = new String("bash /opt/add_dns.sh " + ipAddr);
120                 try {
121                         Runtime.getRuntime().exec(script);
122                         LOG.debug("New GRE tunnel towards DNS instance " + ipAddr + " created.");
123                 } catch (IOException e) {
124                         LOG.error("add_dns.sh returned an error.");
125                         e.printStackTrace();
126                 }
127         }
128
129         /*
130          * Auxiliary function that calls a shell script to delete a GRE tunnel towards a vDNS instance.
131          */
132         private void deleteGreTunnel(String ipAddr) {
133                 String script = new String("bash /opt/remove_dns.sh " + ipAddr);
134                 try {
135                         Runtime.getRuntime().exec(script);
136                         LOG.debug("GRE tunnel towards DNS instance " + ipAddr + " removed.");
137                 } catch (IOException e) {
138                         LOG.error("remove_dns.sh returned an error.");
139                         e.printStackTrace();
140                 }
141         }
142 }