|
| 1 | +// |
| 2 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +// or more contributor license agreements. See the NOTICE file |
| 4 | +// distributed with this work for additional information |
| 5 | +// regarding copyright ownership. The ASF licenses this file |
| 6 | +// to you under the Apache License, Version 2.0 (the |
| 7 | +// "License"); you may not use this file except in compliance |
| 8 | +// with the License. You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, |
| 13 | +// software distributed under the License is distributed on an |
| 14 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +// KIND, either express or implied. See the License for the |
| 16 | +// specific language governing permissions and limitations |
| 17 | +// under the License. |
| 18 | +// |
| 19 | + |
| 20 | +package org.apache.cloudstack.dns; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import javax.inject.Inject; |
| 26 | + |
| 27 | +import org.apache.cloudstack.api.ApiConstants; |
| 28 | +import org.apache.cloudstack.framework.events.Event; |
| 29 | +import org.apache.cloudstack.framework.events.EventBus; |
| 30 | +import org.apache.cloudstack.framework.events.EventBusException; |
| 31 | +import org.apache.cloudstack.framework.events.EventSubscriber; |
| 32 | +import org.apache.cloudstack.framework.events.EventTopic; |
| 33 | +import org.springframework.stereotype.Component; |
| 34 | + |
| 35 | +import com.cloud.event.EventTypes; |
| 36 | +import com.cloud.network.Network; |
| 37 | +import com.cloud.network.dao.NetworkDao; |
| 38 | +import com.cloud.utils.StringUtils; |
| 39 | +import com.cloud.utils.component.ManagerBase; |
| 40 | +import com.cloud.vm.Nic; |
| 41 | +import com.cloud.vm.NicVO; |
| 42 | +import com.cloud.vm.VMInstanceVO; |
| 43 | +import com.cloud.vm.dao.NicDao; |
| 44 | +import com.cloud.vm.dao.VMInstanceDao; |
| 45 | +import com.fasterxml.jackson.databind.JsonNode; |
| 46 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 47 | + |
| 48 | +@Component |
| 49 | +public class DnsVmLifecycleListener extends ManagerBase implements EventSubscriber { |
| 50 | + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
| 51 | + |
| 52 | + @Inject |
| 53 | + private EventBus eventBus = null; |
| 54 | + |
| 55 | + @Inject |
| 56 | + VMInstanceDao vmInstanceDao; |
| 57 | + @Inject |
| 58 | + NetworkDao networkDao; |
| 59 | + @Inject |
| 60 | + NicDao nicDao; |
| 61 | + @Inject |
| 62 | + DnsProviderManager providerManager; |
| 63 | + |
| 64 | + @Override |
| 65 | + public boolean configure(final String name, final Map<String, Object> params) { |
| 66 | + if (eventBus == null) { |
| 67 | + logger.info("EventBus is not available; DNS Instance lifecycle listener will not subscribe to events"); |
| 68 | + return true; |
| 69 | + } |
| 70 | + try { |
| 71 | + eventBus.subscribe(new EventTopic(null, EventTypes.EVENT_VM_CREATE, null, null, null), this); |
| 72 | + eventBus.subscribe(new EventTopic(null, EventTypes.EVENT_VM_STOP, null, null, null), this); |
| 73 | + eventBus.subscribe(new EventTopic(null, EventTypes.EVENT_VM_DESTROY, null, null, null), this); |
| 74 | + eventBus.subscribe(new EventTopic(null, EventTypes.EVENT_NIC_CREATE, null, null, null), this); |
| 75 | + eventBus.subscribe(new EventTopic(null, EventTypes.EVENT_NIC_DELETE, null, null, null), this); |
| 76 | + } catch (EventBusException ex) { |
| 77 | + logger.error("Failed to subscribe DnsVmLifecycleListener to EventBus", ex); |
| 78 | + } |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void onEvent(Event event) { |
| 84 | + logger.debug("Received EventBus event: {}", event); |
| 85 | + JsonNode descJson = parseEventDescription(event); |
| 86 | + if (!isEventCompleted(descJson)) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + String eventType = event.getEventType(); |
| 91 | + String resourceUuid = event.getResourceUUID(); |
| 92 | + logger.debug("Processing Event: {}", event); |
| 93 | + try { |
| 94 | + switch (eventType) { |
| 95 | + case EventTypes.EVENT_VM_CREATE: |
| 96 | + case EventTypes.EVENT_VM_START: |
| 97 | + handleVmEvent(resourceUuid, true); |
| 98 | + break; |
| 99 | + case EventTypes.EVENT_VM_STOP: |
| 100 | + case EventTypes.EVENT_VM_DESTROY: |
| 101 | + handleVmEvent(resourceUuid, false); |
| 102 | + break; |
| 103 | + case EventTypes.EVENT_NIC_CREATE: |
| 104 | + handleNicEvent(descJson, true); |
| 105 | + break; |
| 106 | + case EventTypes.EVENT_NIC_DELETE: |
| 107 | + handleNicEvent(descJson, false); |
| 108 | + break; |
| 109 | + default: |
| 110 | + break; |
| 111 | + } |
| 112 | + } catch (Exception ex) { |
| 113 | + logger.error("Failed to process DNS lifecycle event: type={}, resourceUuid={}", |
| 114 | + eventType, event.getResourceUUID(), ex); |
| 115 | + |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private void handleNicEvent(JsonNode eventDesc, boolean isAddDnsRecord) { |
| 120 | + JsonNode nicUuid = eventDesc.get("Nic"); |
| 121 | + JsonNode vmUuid = eventDesc.get("VirtualMachine"); |
| 122 | + JsonNode networkUuid = eventDesc.get("Network"); |
| 123 | + if (nicUuid == null || nicUuid.isNull() || vmUuid == null || vmUuid.isNull() || networkUuid == null || networkUuid.isNull()) { |
| 124 | + logger.warn("Event has missing data to work on: {}", eventDesc); |
| 125 | + return; |
| 126 | + } |
| 127 | + VMInstanceVO vmInstanceVO = vmInstanceDao.findByUuid(vmUuid.asText()); |
| 128 | + if (vmInstanceVO == null) { |
| 129 | + logger.error("Unable to find Instance with ID: {}", vmUuid); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + Network network = networkDao.findByUuid(networkUuid.asText()); |
| 134 | + if (network == null || !Network.GuestType.Shared.equals(network.getGuestType())) { |
| 135 | + logger.warn("Network is not eligible for DNS record registration"); |
| 136 | + return; |
| 137 | + } |
| 138 | + Nic nic = nicDao.findByUuid(nicUuid.asText()); |
| 139 | + if (nic == null) { |
| 140 | + logger.error("NIC is not found for the ID: {}", nicUuid); |
| 141 | + } |
| 142 | + |
| 143 | + boolean dnsRecordAdded = providerManager.processDnsRecordForInstance(vmInstanceVO, network, nic, isAddDnsRecord); |
| 144 | + if (!dnsRecordAdded) { |
| 145 | + logger.error("Failure {} DNS record for Instance: {} for Network with ID: {}", |
| 146 | + isAddDnsRecord ? "adding" : "removing", vmUuid, networkUuid); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private void handleVmEvent(String vmUuid, boolean isAddDnsRecord) { |
| 151 | + VMInstanceVO vmInstanceVO = vmInstanceDao.findByUuid(vmUuid); |
| 152 | + if (vmInstanceVO == null) { |
| 153 | + logger.error("Unable to find Instance with ID: {}", vmUuid); |
| 154 | + return; |
| 155 | + } |
| 156 | + List<NicVO> vmNics = nicDao.listByVmId(vmInstanceVO.getId()); |
| 157 | + for (NicVO nic : vmNics) { |
| 158 | + Network network = networkDao.findById(nic.getNetworkId()); |
| 159 | + if (Network.GuestType.Shared.equals(network.getGuestType())) { |
| 160 | + boolean dnsRecordAdded = providerManager.processDnsRecordForInstance(vmInstanceVO, network, nic, isAddDnsRecord); |
| 161 | + if (!dnsRecordAdded) { |
| 162 | + logger.error("Failure {} DNS record for Instance: {} for Network with ID: {}", |
| 163 | + isAddDnsRecord ? "adding" : "removing", vmUuid, network.getUuid()); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private JsonNode parseEventDescription(Event event) { |
| 170 | + String rawDescription = event.getDescription(); |
| 171 | + if (StringUtils.isBlank(rawDescription)) { |
| 172 | + return null; |
| 173 | + } |
| 174 | + try { |
| 175 | + return OBJECT_MAPPER.readTree(rawDescription); |
| 176 | + } catch (Exception ex) { |
| 177 | + logger.warn("parseEventDescription: failed to parse description for event [{}]: {}", |
| 178 | + event.getEventType(), ex.getMessage()); |
| 179 | + return null; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private boolean isEventCompleted(JsonNode descJson) { |
| 184 | + if (descJson == null) { |
| 185 | + return false; |
| 186 | + } |
| 187 | + JsonNode statusNode = descJson.get(ApiConstants.STATUS); |
| 188 | + if (statusNode == null || statusNode.isNull()) { |
| 189 | + return false; |
| 190 | + } |
| 191 | + return ApiConstants.COMPLETED.equalsIgnoreCase(statusNode.asText()); |
| 192 | + } |
| 193 | +} |
0 commit comments