2 /*************************************************************************//**
4 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
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
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.
17 ****************************************************************************/
19 package org.onap.vnf.vlb.write;
21 import java.io.IOException;
22 import java.util.HashMap;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
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.
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.
44 * The vLB setup and configuration is executed via shell scripts.
48 public class DnsInstanceManager {
50 private static final Logger LOG = LoggerFactory.getLogger(DnsInstanceManager.class);
51 private Map<String, Boolean> dnsInstances = new HashMap<String, Boolean>();
54 * Add a new DNS instance to the map and create a GRE tunnel
55 * towards that instance if isEnabled is set to true.
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);
65 dnsInstances.put(ipAddr, isEnabled);
66 LOG.debug("DNS instance " + ipAddr + " with status isEnabled=" + isEnabled + " has been added.");
68 // Create a GRE tunnel towards the new DNS instance if isEnabled is true.
75 * Update an existing DNS instance and create or remove a GRE
76 * tunnel based on the current value of the isEnabled flag.
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.");
85 dnsInstances.put(ipAddr, isEnabled);
86 LOG.debug("DNS instance " + ipAddr + " has been updated with status isEnabled=" + isEnabled + ".");
92 deleteGreTunnel(ipAddr);
97 * Delete an existing DNS instance from the map and remove the
98 * GRE tunnel if isEnabled was set to true.
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.");
107 // Remove a GRE tunnel towards this DNS instance if it exists.
108 if(dnsInstances.get(ipAddr)) {
109 deleteGreTunnel(ipAddr);
112 dnsInstances.remove(ipAddr);
116 * Auxiliary function that calls a shell script to create a GRE tunnel towards a new vDNS instance.
118 private void addGreTunnel(String ipAddr) {
119 String script = new String("bash /opt/add_dns.sh " + ipAddr);
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.");
130 * Auxiliary function that calls a shell script to delete a GRE tunnel towards a vDNS instance.
132 private void deleteGreTunnel(String ipAddr) {
133 String script = new String("bash /opt/remove_dns.sh " + ipAddr);
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.");