* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
+ * Modifications Copyright (c) 2019 Samsung
+ * ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
import java.util.ArrayList;
import java.util.Calendar;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
+import java.util.Optional;
import org.onap.so.cloud.CloudConfig;
import org.onap.so.cloud.authentication.AuthenticationMethodFactory;
import org.onap.so.db.catalog.beans.CloudIdentity;
import org.onap.so.db.catalog.beans.CloudSite;
import org.onap.so.db.catalog.beans.ServerType;
+import org.onap.so.logger.ErrorCode;
import org.onap.so.logger.MessageEnum;
-import org.onap.so.logger.MsoAlarmLogger;
-import org.onap.so.logger.MsoLogger;
import org.onap.so.openstack.beans.NetworkInfo;
-import org.onap.so.openstack.beans.NeutronCacheEntry;
import org.onap.so.openstack.exceptions.MsoAdapterException;
import org.onap.so.openstack.exceptions.MsoCloudSiteNotFound;
import org.onap.so.openstack.exceptions.MsoException;
import org.onap.so.openstack.exceptions.MsoNetworkNotFound;
import org.onap.so.openstack.exceptions.MsoOpenstackException;
import org.onap.so.openstack.mappers.NetworkInfoMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.woorea.openstack.quantum.Quantum;
import com.woorea.openstack.quantum.model.Network;
import com.woorea.openstack.quantum.model.Networks;
+import com.woorea.openstack.quantum.model.Port;
import com.woorea.openstack.quantum.model.Segment;
@Component
public class MsoNeutronUtils extends MsoCommonUtils
{
- // Cache Neutron Clients statically. Since there is just one MSO user, there is no
- // benefit to re-authentication on every request (or across different flows). The
- // token will be used until it expires.
- //
- // The cache key is "tenantId:cloudId"
- private static Map<String,NeutronCacheEntry> neutronClientCache = new HashMap<>();
// Fetch cloud configuration each time (may be cached in CloudConfig class)
@Autowired
@Autowired
private KeystoneV3Authentication keystoneV3Authentication;
-
- private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA, MsoNeutronUtils.class);
-
- public enum NetworkType {
+
+ private static Logger logger = LoggerFactory.getLogger(MsoNeutronUtils.class);
+
+ public enum NetworkType {
BASIC, PROVIDER, MULTI_PROVIDER
};
if (network != null) {
// Network already exists. Throw an exception
- LOGGER.error(MessageEnum.RA_NETWORK_ALREADY_EXIST, networkName, cloudSiteId, tenantId, "Openstack", "", MsoLogger.ErrorCode.DataError, "Network already exists");
- throw new MsoNetworkAlreadyExists (networkName, tenantId, cloudSiteId);
+ logger.error("{} Network {} on Cloud site {} for tenant {} already exists {}",
+ MessageEnum.RA_NETWORK_ALREADY_EXIST, networkName, cloudSiteId, tenantId,
+ ErrorCode.DataError.getValue());
+ throw new MsoNetworkAlreadyExists (networkName, tenantId, cloudSiteId);
}
// Does not exist, create a new one
*/
public NetworkInfo queryNetwork(String networkNameOrId, String tenantId, String cloudSiteId) throws MsoException
{
- LOGGER.debug("In queryNetwork");
+ logger.debug("In queryNetwork");
// Obtain the cloud site information
CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
try {
Network network = findNetworkByNameOrId (neutronClient, networkNameOrId);
if (network == null) {
- LOGGER.debug ("Query Network: " + networkNameOrId + " not found in tenant " + tenantId);
- return null;
+ logger.debug("Query Network: {} not found in tenant {}", networkNameOrId, tenantId);
+ return null;
}
return new NetworkInfoMapper(network).map();
}
throw me;
}
}
+
+ public Optional<Port> getNeutronPort(String neutronPortId, String tenantId, String cloudSiteId)
+ {
+ try {
+ logger.debug("Finding Neutron port:" + neutronPortId);
+ CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
+ () -> new MsoCloudSiteNotFound(cloudSiteId));
+ Quantum neutronClient = getNeutronClient (cloudSite, tenantId);
+ Port port = findPortById (neutronClient, neutronPortId);
+ if (port == null) {
+ return Optional.empty();
+ }
+ return Optional.of(port);
+ }
+ catch (RuntimeException | MsoException e) {
+ logger.error("Error retrieving neutron port", e);
+ return Optional.empty();
+ }
+ }
/**
* Delete the specified Network (by ID) in the given cloud.
// Check that the network exists.
Network network = findNetworkById (neutronClient, networkId);
if (network == null) {
- LOGGER.info(MessageEnum.RA_DELETE_NETWORK_EXC, networkId, cloudSiteId, tenantId, "Openstack", "");
- return false;
+ logger.info("{} Network not found! Network id: {} Cloud site: {} Tenant: {} ",
+ MessageEnum.RA_DELETE_NETWORK_EXC, networkId, cloudSiteId, tenantId);
+ return false;
}
OpenStackRequest<Void> request = neutronClient.networks().delete(network.getId());
executeAndRecordOpenstackRequest(request);
- LOGGER.debug ("Deleted Network " + network.getId() + " (" + network.getName() + ")");
- }
+ logger.debug("Deleted Network {} ({})", network.getId(), network.getName());
+ }
catch (OpenStackBaseException e) {
// Convert Neutron exception to an MsoOpenstackException
MsoException me = neutronExceptionToMsoException (e, "Delete Network");
if (network == null) {
// Network not found. Throw an exception
- LOGGER.error(MessageEnum.RA_NETWORK_NOT_FOUND, networkId, cloudSiteId, tenantId, "Openstack", "", MsoLogger.ErrorCode.DataError, "Network not found");
+ logger.error("{} Network {} on Cloud site {} for Tenant {} not found {}", MessageEnum.RA_NETWORK_NOT_FOUND,
+ networkId, cloudSiteId, tenantId, ErrorCode.DataError.getValue());
throw new MsoNetworkNotFound (networkId, tenantId, cloudSiteId);
}
private Quantum getNeutronClient(CloudSite cloudSite, String tenantId) throws MsoException
{
String cloudId = cloudSite.getId();
- String region = cloudSite.getRegionId();
-
- // Check first in the cache of previously authorized clients
- String cacheKey = cloudId + ":" + tenantId;
- if (neutronClientCache.containsKey(cacheKey)) {
- if (! neutronClientCache.get(cacheKey).isExpired()) {
- LOGGER.debug ("Using Cached HEAT Client for " + cacheKey);
- NeutronCacheEntry cacheEntry = neutronClientCache.get(cacheKey);
- Quantum neutronClient = new Quantum(cacheEntry.getNeutronUrl());
- neutronClient.token(cacheEntry.getToken());
- return neutronClient;
- }
- else {
- // Token is expired. Remove it from cache.
- neutronClientCache.remove(cacheKey);
- LOGGER.debug ("Expired Cached Neutron Client for " + cacheKey);
- }
- }
+ String region = cloudSite.getRegionId();
+
// Obtain an MSO token for the tenant from the identity service
CloudIdentity cloudIdentity = cloudSite.getIdentityService();
} catch (RuntimeException e) {
// This comes back for not found (probably an incorrect region ID)
String error = "Network service not found: region=" + region + ",cloud=" + cloudIdentity.getId();
- alarmLogger.sendAlarm("MsoConfigurationError", MsoAlarmLogger.CRITICAL, error);
throw new MsoAdapterException (error, e);
}
tokenId = access.getToken().getId();
} catch (ServiceEndpointNotFoundException e) {
// This comes back for not found (probably an incorrect region ID)
String error = "Network service not found: region=" + region + ",cloud=" + cloudIdentity.getId();
- alarmLogger.sendAlarm("MsoConfigurationError", MsoAlarmLogger.CRITICAL, error);
throw new MsoAdapterException (error, e);
}
}
if (e.getStatus() == 401) {
// Authentication error.
String error = "Authentication Failure: tenant=" + tenantId + ",cloud=" + cloudIdentity.getId();
- alarmLogger .sendAlarm("MsoAuthenticationError", MsoAlarmLogger.CRITICAL, error);
+
throw new MsoAdapterException(error);
}
else {
MsoException me = runtimeExceptionToMsoException(e, "TokenAuth");
throw me;
}
+
Quantum neutronClient = new Quantum(neutronUrl);
neutronClient.token(tokenId);
-
- neutronClientCache.put(cacheKey, new NeutronCacheEntry(neutronUrl, tokenId, expiration));
- LOGGER.debug ("Caching Neutron Client for " + cacheKey);
-
return neutronClient;
}
- /**
- * Forcibly expire a Neutron client from the cache. This call is for use by
- * the KeystoneClient in case where a tenant is deleted. In that case,
- * all cached credentials must be purged so that fresh authentication is
- * done on subsequent calls.
- * <p>
- * @param tenantName
- * @param cloudId
- */
- public void expireNeutronClient (String tenantId, String cloudId) {
- String cacheKey = cloudId + ":" + tenantId;
- if (neutronClientCache.containsKey(cacheKey)) {
- neutronClientCache.remove(cacheKey);
- LOGGER.debug ("Deleted Cached Neutron Client for " + cacheKey);
- }
- }
-
-
/*
* Find a tenant (or query its existence) by its Name or Id. Check first against the
* ID. If that fails, then try by name.
if (e.getStatus() == 404) {
return null;
} else {
- LOGGER.error (MessageEnum.RA_CONNECTION_EXCEPTION, "OpenStack", "Openstack Error, GET Network By ID (" + networkId + "): " + e, "Openstack", "", MsoLogger.ErrorCode.DataError, "Exception in Openstack");
+ logger.error("{} {} Openstack Error, GET Network By ID ({}): ", MessageEnum.RA_CONNECTION_EXCEPTION,
+ ErrorCode.DataError.getValue(), networkId, e);
+ throw e;
+ }
+ }
+ }
+
+
+ private Port findPortById (Quantum neutronClient, String neutronPortId)
+ {
+ if (neutronPortId == null) {
+ return null;
+ }
+
+ try {
+ OpenStackRequest<Port> request = neutronClient.ports().show(neutronPortId);
+ Port port = executeAndRecordOpenstackRequest(request);
+ return port;
+ }
+ catch (OpenStackResponseException e) {
+ if (e.getStatus() == 404) {
+ logger.warn("Neutron port not found: " + neutronPortId,"Neutron port not found: " + neutronPortId);
+ return null;
+ } else {
+ logger.error("{} {} Openstack Error, GET Neutron Port By ID ({}): ", MessageEnum.RA_CONNECTION_EXCEPTION,
+ ErrorCode.DataError.getValue(), neutronPortId, e);
throw e;
}
}
Networks networks = executeAndRecordOpenstackRequest(request);
for (Network network : networks.getList()) {
if (network.getName().equals(networkName)) {
- LOGGER.debug ("Found match on network name: " + networkName);
- return network;
+ logger.debug("Found match on network name: {}", networkName);
+ return network;
}
}
- LOGGER.debug ("findNetworkByName - no match found for " + networkName);
- return null;
+ logger.debug("findNetworkByName - no match found for {}", networkName);
+ return null;
}
catch (OpenStackResponseException e) {
if (e.getStatus() == 404) {
return null;
} else {
- LOGGER.error (MessageEnum.RA_CONNECTION_EXCEPTION, "OpenStack", "Openstack Error, GET Network By Name (" + networkName + "): " + e, "OpenStack", "", MsoLogger.ErrorCode.DataError, "Exception in OpenStack");
- throw e;
+ logger.error("{} {} Openstack Error, GET Network By Name ({}): ", MessageEnum.RA_CONNECTION_EXCEPTION,
+ ErrorCode.DataError.getValue(), networkName, e);
+ throw e;
}
}
}