|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.cloudstack.storage.listener; |
| 19 | + |
| 20 | +import javax.inject.Inject; |
| 21 | + |
| 22 | +import com.cloud.agent.api.ModifyStoragePoolCommand; |
| 23 | +import com.cloud.alert.AlertManager; |
| 24 | +import com.cloud.storage.StoragePoolHostVO; |
| 25 | +import com.cloud.storage.dao.StoragePoolHostDao; |
| 26 | +import org.apache.logging.log4j.Logger; |
| 27 | +import org.apache.logging.log4j.LogManager; |
| 28 | +import com.cloud.agent.AgentManager; |
| 29 | +import com.cloud.agent.api.Answer; |
| 30 | +import com.cloud.agent.api.DeleteStoragePoolCommand; |
| 31 | +import com.cloud.host.Host; |
| 32 | +import com.cloud.storage.StoragePool; |
| 33 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 34 | +import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; |
| 35 | +import org.apache.cloudstack.engine.subsystem.api.storage.HypervisorHostListener; |
| 36 | +import com.cloud.host.dao.HostDao; |
| 37 | + |
| 38 | +/** |
| 39 | + * HypervisorHostListener implementation for ONTAP storage. |
| 40 | + * Handles connecting/disconnecting hosts to/from ONTAP-backed storage pools. |
| 41 | + */ |
| 42 | +public class OntapHostListener implements HypervisorHostListener { |
| 43 | + protected Logger logger = LogManager.getLogger(getClass()); |
| 44 | + |
| 45 | + @Inject |
| 46 | + private AgentManager _agentMgr; |
| 47 | + @Inject |
| 48 | + private AlertManager _alertMgr; |
| 49 | + @Inject |
| 50 | + private PrimaryDataStoreDao _storagePoolDao; |
| 51 | + @Inject |
| 52 | + private HostDao _hostDao; |
| 53 | + @Inject private StoragePoolHostDao storagePoolHostDao; |
| 54 | + |
| 55 | + |
| 56 | + @Override |
| 57 | + public boolean hostConnect(long hostId, long poolId) { |
| 58 | + logger.info("Connect to host " + hostId + " from pool " + poolId); |
| 59 | + Host host = _hostDao.findById(hostId); |
| 60 | + if (host == null) { |
| 61 | + logger.error("Failed to add host by HostListener as host was not found with id : {}", hostId); |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + // TODO add host type check also since we support only KVM for now, host.getHypervisorType().equals(HypervisorType.KVM) |
| 66 | + StoragePool pool = _storagePoolDao.findById(poolId); |
| 67 | + logger.info("Connecting host {} to ONTAP storage pool {}", host.getName(), pool.getName()); |
| 68 | + |
| 69 | + |
| 70 | + // incase host was not added by cloudstack , we will add it |
| 71 | + StoragePoolHostVO storagePoolHost = storagePoolHostDao.findByPoolHost(poolId, hostId); |
| 72 | + |
| 73 | + if (storagePoolHost == null) { |
| 74 | + storagePoolHost = new StoragePoolHostVO(poolId, hostId, ""); |
| 75 | + |
| 76 | + storagePoolHostDao.persist(storagePoolHost); |
| 77 | + } |
| 78 | + |
| 79 | + // Validate pool type - ONTAP supports NFS and iSCSI |
| 80 | +// StoragePoolType poolType = pool.getPoolType(); |
| 81 | +// // TODO add iscsi also here |
| 82 | +// if (poolType != StoragePoolType.NetworkFilesystem) { |
| 83 | +// logger.error("Unsupported pool type {} for ONTAP storage", poolType); |
| 84 | +// return false; |
| 85 | +// } |
| 86 | + |
| 87 | + try { |
| 88 | + // Create the CreateStoragePoolCommand to send to the agent |
| 89 | + ModifyStoragePoolCommand cmd = new ModifyStoragePoolCommand(true, pool); |
| 90 | + |
| 91 | + Answer answer = _agentMgr.easySend(hostId, cmd); |
| 92 | + |
| 93 | + if (answer == null) { |
| 94 | + throw new CloudRuntimeException(String.format("Unable to get an answer to the modify storage pool command (%s)", pool)); |
| 95 | + } |
| 96 | + |
| 97 | + if (!answer.getResult()) { |
| 98 | + String msg = String.format("Unable to attach storage pool %s to host %d", pool, hostId); |
| 99 | + |
| 100 | + _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_HOST, pool.getDataCenterId(), pool.getPodId(), msg, msg); |
| 101 | + |
| 102 | + throw new CloudRuntimeException(String.format( |
| 103 | + "Unable to establish a connection from agent to storage pool %s due to %s", pool, answer.getDetails())); |
| 104 | + } |
| 105 | + } catch (Exception e) { |
| 106 | + logger.error("Exception while connecting host {} to storage pool {}", host.getName(), pool.getName(), e); |
| 107 | + throw new CloudRuntimeException("Failed to connect host to storage pool: " + e.getMessage(), e); |
| 108 | + } |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public boolean hostDisconnected(Host host, StoragePool pool) { |
| 114 | + logger.info("Disconnect from host " + host.getId() + " from pool " + pool.getName()); |
| 115 | + |
| 116 | + Host hostToremove = _hostDao.findById(host.getId()); |
| 117 | + if (hostToremove == null) { |
| 118 | + logger.error("Failed to add host by HostListener as host was not found with id : {}", host.getId()); |
| 119 | + return false; |
| 120 | + } |
| 121 | + // TODO add storage pool get validation |
| 122 | + logger.info("Disconnecting host {} from ONTAP storage pool {}", host.getName(), pool.getName()); |
| 123 | + |
| 124 | + try { |
| 125 | + DeleteStoragePoolCommand cmd = new DeleteStoragePoolCommand(pool); |
| 126 | + long hostId = host.getId(); |
| 127 | + Answer answer = _agentMgr.easySend(hostId, cmd); |
| 128 | + |
| 129 | + if (answer != null && answer.getResult()) { |
| 130 | + logger.info("Successfully disconnected host {} from ONTAP storage pool {}", host.getName(), pool.getName()); |
| 131 | + return true; |
| 132 | + } else { |
| 133 | + String errMsg = (answer != null) ? answer.getDetails() : "Unknown error"; |
| 134 | + logger.warn("Failed to disconnect host {} from storage pool {}. Error: {}", host.getName(), pool.getName(), errMsg); |
| 135 | + return false; |
| 136 | + } |
| 137 | + } catch (Exception e) { |
| 138 | + logger.error("Exception while disconnecting host {} from storage pool {}", host.getName(), pool.getName(), e); |
| 139 | + return false; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public boolean hostDisconnected(long hostId, long poolId) { |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public boolean hostAboutToBeRemoved(long hostId) { |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public boolean hostRemoved(long hostId, long clusterId) { |
| 155 | + return false; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public boolean hostEnabled(long hostId) { |
| 160 | + return false; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public boolean hostAdded(long hostId) { |
| 165 | + return false; |
| 166 | + } |
| 167 | + |
| 168 | +} |
0 commit comments