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.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;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
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.
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.
46 * The vLB setup and configuration is executed via shell scripts.
50 public class DnsInstanceManager {
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>();
57 * Add a new DNS instance to the map and create a GRE tunnel
58 * towards that instance if isEnabled is set to true.
61 public static DnsInstanceManager getInstance() {
62 if(thisInstance == null) {
63 thisInstance = new DnsInstanceManager();
69 private DnsInstanceManager() {
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);
81 dnsInstances.put(ipAddr, data);
82 LOG.debug("DNS instance " + ipAddr + " with status isEnabled=" + data.isEnabled() + " has been added.");
84 // Create a GRE tunnel towards the new DNS instance if isEnabled is true.
85 if(data.isEnabled()) {
91 * Update an existing DNS instance and create or remove a GRE
92 * tunnel based on the current value of the isEnabled flag.
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.");
102 dnsInstances.put(ipAddr, data);
103 LOG.debug("DNS instance " + ipAddr + " has been updated with status isEnabled=" + isEnabled + ".");
106 addGreTunnel(ipAddr);
109 deleteGreTunnel(ipAddr);
114 * Delete an existing DNS instance from the map and remove the
115 * GRE tunnel if isEnabled was set to true.
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.");
124 // Remove a GRE tunnel towards this DNS instance if it exists.
125 if(dnsInstances.get(ipAddr).isEnabled()) {
126 deleteGreTunnel(ipAddr);
129 dnsInstances.remove(ipAddr);
133 * Auxiliary function that calls a shell script to create a GRE tunnel towards a new vDNS instance.
135 private void addGreTunnel(String ipAddr) {
136 String script = new String("bash /opt/add_dns.sh " + ipAddr);
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.");
147 * Auxiliary function that calls a shell script to delete a GRE tunnel towards a vDNS instance.
149 private void deleteGreTunnel(String ipAddr) {
150 String script = new String("bash /opt/remove_dns.sh " + ipAddr);
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.");
161 * Auxiliary function that returns vDNS instances as map.
163 public Map<String, VdnsInstance> getDnsInstancesAsMap() {