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