Allow vLB to check multiple vDNSs
[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.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vlb.business.vnf.onap.plugin.rev160918.vlb.business.vnf.onap.plugin.params.vdns.instances.VdnsInstance;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * 
32  * Class that maintains a map of all the active vDNS instances.
33  * A vDNS instance is represented by its IP address and a flag
34  * that represents its status, i.e. enabled or disabled.
35  * 
36  * Clients can update the vDNS map via NETCONF or RESTconf.
37  * Operations are translated by the Honeycomb agent into a JSON
38  * object that is persisted on disk (see Honeycomb distribution
39  * configuration for more information). The ElementCustomizer
40  * class then updates this map. Each operation results in a vLB
41  * setup change, e.g. GRE tunnels that connect the vLB to the
42  * vDNS instances are created or deleted, depending on the kind
43  * of operation that is invoked. Only vDNS instances for which
44  * the isEnabled flag is true are currently served by the vLB.
45  *
46  * The vLB setup and configuration is executed via shell scripts.
47  *
48  */
49
50 public class DnsInstanceManager {
51
52         private static DnsInstanceManager thisInstance = null;
53         private static final Logger LOG = LoggerFactory.getLogger(DnsInstanceManager.class);
54         private Map<String, VdnsInstance> dnsInstances = new HashMap<String, VdnsInstance>();
55
56         /*
57          * Add a new DNS instance to the map and create a GRE tunnel 
58          * towards that instance if isEnabled is set to true.
59          */
60
61         public static DnsInstanceManager getInstance() {
62                 if(thisInstance == null) {
63                         thisInstance = new DnsInstanceManager();
64                 }
65
66                 return thisInstance;
67         }
68
69         private DnsInstanceManager() {
70
71         }
72
73         void addDnsInstance(String ipAddr, VdnsInstance data) {
74                 // Call updateDnsInstance in case the DNS instance already exists.
75                 // This is somewhat redundant because Honeycomb already runs this check.
76                 if(dnsInstances.containsKey(ipAddr)) {
77                         updateDnsInstance(ipAddr, data);
78                         return;
79                 }
80
81                 dnsInstances.put(ipAddr, data);
82                 LOG.debug("DNS instance " + ipAddr + " with status isEnabled=" + data.isEnabled() + " has been added.");
83
84                 // Create a GRE tunnel towards the new DNS instance if isEnabled is true.
85                 if(data.isEnabled()) {
86                         addGreTunnel(ipAddr);
87                 }
88         }
89
90         /*
91          * Update an existing DNS instance and create or remove a GRE
92          * tunnel based on the current value of the isEnabled flag.
93          */
94         void updateDnsInstance(String ipAddr, VdnsInstance data) {
95                 // This is somewhat redundant because Honeycomb already runs this check.
96                 boolean isEnabled = data.isEnabled();
97                 if(dnsInstances.get(ipAddr).isEnabled() == isEnabled) {
98                         LOG.debug("DNS instance " + ipAddr + " with status isEnabled=" + isEnabled + " already exists. No update is necessary.");
99                         return;
100                 }
101
102                 dnsInstances.put(ipAddr, data);
103                 LOG.debug("DNS instance " + ipAddr + " has been updated with status isEnabled=" + isEnabled + ".");
104
105                 if(isEnabled) {
106                         addGreTunnel(ipAddr);
107                 }
108                 else {
109                         deleteGreTunnel(ipAddr);
110                 }
111         }
112
113         /*
114          * Delete an existing DNS instance from the map and remove the 
115          * GRE tunnel if isEnabled was set to true.
116          */
117         void deleteDnsInstance(String ipAddr) {
118                 // This is somewhat redundant because Honeycomb already runs this check.
119                 if(!dnsInstances.containsKey(ipAddr)) {
120                         LOG.debug("DNS instance " + ipAddr + " doesn't exist.");
121                         return;
122                 }
123
124                 // Remove a GRE tunnel towards this DNS instance if it exists.
125                 if(dnsInstances.get(ipAddr).isEnabled()) {
126                         deleteGreTunnel(ipAddr);
127                 }
128
129                 dnsInstances.remove(ipAddr);
130         }
131
132         /*
133          * Auxiliary function that calls a shell script to create a GRE tunnel towards a new vDNS instance.
134          */
135         private void addGreTunnel(String ipAddr) {
136                 String script = new String("bash /opt/add_dns.sh " + ipAddr);
137                 try {
138                         Runtime.getRuntime().exec(script);
139                         LOG.debug("New GRE tunnel towards DNS instance " + ipAddr + " created.");
140                 } catch (IOException e) {
141                         LOG.error("add_dns.sh returned an error.");
142                         e.printStackTrace();
143                 }
144         }
145
146         /*
147          * Auxiliary function that calls a shell script to delete a GRE tunnel towards a vDNS instance.
148          */
149         private void deleteGreTunnel(String ipAddr) {
150                 String script = new String("bash /opt/remove_dns.sh " + ipAddr);
151                 try {
152                         Runtime.getRuntime().exec(script);
153                         LOG.debug("GRE tunnel towards DNS instance " + ipAddr + " removed.");
154                 } catch (IOException e) {
155                         LOG.error("remove_dns.sh returned an error.");
156                         e.printStackTrace();
157                 }
158         }
159
160         /*
161          * Auxiliary function that returns vDNS instances as map.
162          */
163         public Map<String, VdnsInstance> getDnsInstancesAsMap() {
164                 return dnsInstances;
165         }
166 }