use of org.apache.cloudstack.network.contrail.model.VirtualNetworkModel in project cloudstack by apache.
the class ServiceManagerImpl method createServiceInstance.
@Override
public ServiceVirtualMachine createServiceInstance(DataCenter zone, Account owner, VirtualMachineTemplate template, ServiceOffering serviceOffering, String name, Network left, Network right) {
s_logger.debug("createServiceInstance by " + owner.getAccountName());
// service instances need to be able to access the public network.
if (left.getTrafficType() == TrafficType.Guest) {
_networkModel.checkNetworkPermissions(owner, left);
}
if (right.getTrafficType() == TrafficType.Guest) {
_networkModel.checkNetworkPermissions(owner, right);
}
final ApiConnector api = _manager.getApiConnector();
VirtualNetworkModel leftModel = _manager.getDatabase().lookupVirtualNetwork(left.getUuid(), _manager.getCanonicalName(left), left.getTrafficType());
if (leftModel == null) {
throw new CloudRuntimeException("Unable to read virtual-network object");
}
VirtualNetworkModel rightModel = _manager.getDatabase().lookupVirtualNetwork(right.getUuid(), _manager.getCanonicalName(right), right.getTrafficType());
if (rightModel == null) {
throw new CloudRuntimeException("Unable to read virtual-network object");
}
net.juniper.contrail.api.types.Project project;
try {
project = _manager.getVncProject(owner.getDomainId(), owner.getAccountId());
} catch (IOException ex) {
s_logger.warn("read project", ex);
throw new CloudRuntimeException(ex);
}
try {
final String srvid = api.findByName(ServiceInstance.class, project, name);
if (srvid != null) {
throw new InvalidParameterValueException("service-instance " + name + " already exists uuid=" + srvid);
}
} catch (IOException ex) {
s_logger.warn("service-instance lookup", ex);
throw new CloudRuntimeException(ex);
}
// 1. Create service-instance.
ServiceInstanceModel serviceModel = new ServiceInstanceModel(project, name, template, serviceOffering, leftModel, rightModel);
try {
serviceModel.update(_manager.getModelController());
} catch (Exception ex) {
s_logger.warn("service-instance update", ex);
throw new CloudRuntimeException(ex);
}
s_logger.debug("service-instance object created");
ServiceInstance siObj;
try {
_manager.getDatabase().getServiceInstances().add(serviceModel);
siObj = serviceModel.getServiceInstance();
} catch (Exception ex) {
s_logger.warn("DB add", ex);
throw new CloudRuntimeException(ex);
}
// 2. Create one virtual-machine.
String svmName = name.replace(" ", "_") + "-1";
ServiceVirtualMachine svm = createServiceVM(zone, owner, template, serviceOffering, svmName, siObj, left, right);
s_logger.debug("created VMInstance " + svm.getUuid());
// 3. Create the virtual-machine model and push the update.
VirtualMachineModel instanceModel = new VirtualMachineModel(svm, svm.getUuid());
_manager.getDatabase().getVirtualMachines().add(instanceModel);
try {
instanceModel.setServiceInstance(_manager.getModelController(), svm, serviceModel);
instanceModel.update(_manager.getModelController());
} catch (Exception ex) {
s_logger.warn("service virtual-machine update", ex);
throw new CloudRuntimeException(ex);
}
return svm;
}
use of org.apache.cloudstack.network.contrail.model.VirtualNetworkModel in project cloudstack by apache.
the class ServerDBSyncImpl method equalFloatingIp.
public Boolean equalFloatingIp(IPAddressVO db, FloatingIp vnc, StringBuffer syncLogMsg) throws IOException {
syncLogMsg.append("fip# DB: " + db.getAddress().addr() + "; VNC: " + vnc.getAddress() + "; action: equal" + "\n");
VirtualNetworkModel vnModel = _manager.lookupPublicNetworkModel();
assert vnModel != null : "public network vn model is null";
FloatingIpPoolModel fipPoolModel = vnModel.getFipPoolModel();
if (fipPoolModel == null) {
fipPoolModel = new FloatingIpPoolModel();
fipPoolModel.addToVirtualNetwork(vnModel);
fipPoolModel.build(_manager.getModelController());
try {
fipPoolModel.update(_manager.getModelController());
vnModel.setFipPoolModel(fipPoolModel);
} catch (Exception ex) {
s_logger.warn("floating-ip-pool create: ", ex);
return false;
}
}
FloatingIpModel current = fipPoolModel.getFloatingIpModel(db.getUuid());
if (current == null) {
s_logger.debug("add model " + db.getAddress().addr());
FloatingIpModel fipModel = new FloatingIpModel(db.getUuid());
fipModel.addToFloatingIpPool(fipPoolModel);
fipModel.build(_manager.getModelController(), PublicIp.createFromAddrAndVlan(db, _vlanDao.findById(db.getVlanId())));
try {
fipModel.update(_manager.getModelController());
} catch (Exception ex) {
s_logger.warn("floating-ip create: ", ex);
return false;
}
}
return true;
}
use of org.apache.cloudstack.network.contrail.model.VirtualNetworkModel in project cloudstack by apache.
the class ServerDBSyncImpl method createVirtualNetwork.
public void createVirtualNetwork(NetworkVO dbNet, StringBuffer syncLogMesg) throws IOException {
syncLogMesg.append("VN# DB: " + _manager.getCanonicalName(dbNet) + "(" + dbNet.getUuid() + "); VNC: none; action: create\n");
if (_manager.getDatabase().lookupVirtualNetwork(dbNet.getUuid(), _manager.getCanonicalName(dbNet), dbNet.getTrafficType()) != null) {
s_logger.warn("VN model object is already present in DB: " + dbNet.getUuid() + ", name: " + dbNet.getName());
}
VirtualNetworkModel vnModel = new VirtualNetworkModel(dbNet, dbNet.getUuid(), _manager.getCanonicalName(dbNet), dbNet.getTrafficType());
if (dbNet.getTrafficType() == TrafficType.Guest && dbNet.getNetworkACLId() != null) {
NetworkACLVO acl = _networkACLDao.findById(dbNet.getNetworkACLId());
NetworkPolicyModel policyModel = _manager.getDatabase().lookupNetworkPolicy(acl.getUuid());
if (policyModel == null) {
s_logger.error("Network(" + dbNet.getName() + ") has ACL but policy model not created: " + acl.getUuid() + ", name: " + acl.getName());
} else {
vnModel.addToNetworkPolicy(policyModel);
}
}
vnModel.build(_manager.getModelController(), dbNet);
if (_rwMode) {
try {
if (!vnModel.verify(_manager.getModelController())) {
vnModel.update(_manager.getModelController());
}
} catch (InternalErrorException ex) {
s_logger.warn("create virtual-network", ex);
syncLogMesg.append("Error: VN# VNC : Unable to create network " + dbNet.getName() + "\n");
return;
}
s_logger.debug("add model " + vnModel.getName());
_manager.getDatabase().getVirtualNetworks().add(vnModel);
syncLogMesg.append("VN# VNC: " + dbNet.getUuid() + ", " + vnModel.getName() + " created\n");
} else {
syncLogMesg.append("VN# VNC: " + vnModel.getName() + " created \n");
}
}
use of org.apache.cloudstack.network.contrail.model.VirtualNetworkModel in project cloudstack by apache.
the class ContrailManagerImpl method createFloatingIp.
@Override
public boolean createFloatingIp(PublicIpAddress ip) {
VirtualNetworkModel vnModel = lookupPublicNetworkModel();
assert vnModel != null : "public network vn model is null";
FloatingIpPoolModel fipPoolModel = vnModel.getFipPoolModel();
/* create only, no updates */
if (fipPoolModel == null) {
fipPoolModel = new FloatingIpPoolModel();
fipPoolModel.addToVirtualNetwork(vnModel);
fipPoolModel.build(getModelController());
try {
fipPoolModel.update(getModelController());
vnModel.setFipPoolModel(fipPoolModel);
} catch (Exception ex) {
s_logger.warn("floating-ip-pool create: ", ex);
return false;
}
}
FloatingIpModel fipModel = fipPoolModel.getFloatingIpModel(ip.getUuid());
/* create only, no updates*/
if (fipModel == null) {
fipModel = new FloatingIpModel(ip.getUuid());
fipModel.addToFloatingIpPool(fipPoolModel);
fipModel.build(getModelController(), ip);
try {
fipModel.update(getModelController());
} catch (Exception ex) {
s_logger.warn("floating-ip create: ", ex);
return false;
}
}
return true;
}
use of org.apache.cloudstack.network.contrail.model.VirtualNetworkModel in project cloudstack by apache.
the class ContrailVpcElementImpl method applyNetworkACLs.
@Override
public boolean applyNetworkACLs(Network net, List<? extends NetworkACLItem> rules) throws ResourceUnavailableException {
s_logger.debug("NetworkElement applyNetworkACLs");
if (rules == null || rules.isEmpty()) {
s_logger.debug("no rules to apply");
return true;
}
Long aclId = rules.get(0).getAclId();
NetworkACLVO acl = _networkACLDao.findById(aclId);
NetworkPolicyModel policyModel = _manager.getDatabase().lookupNetworkPolicy(acl.getUuid());
if (policyModel == null) {
/*
* For the first time, when a CS ACL applied to a network, create a network-policy in VNC
* and when there are no networks associated to CS ACL, delete it from VNC.
*/
policyModel = new NetworkPolicyModel(acl.getUuid(), acl.getName());
net.juniper.contrail.api.types.Project project;
try {
project = _manager.getVncProject(net.getDomainId(), net.getAccountId());
if (project == null) {
project = _manager.getDefaultVncProject();
}
} catch (IOException ex) {
s_logger.warn("read project", ex);
return false;
}
policyModel.setProject(project);
}
VirtualNetworkModel vnModel = _manager.getDatabase().lookupVirtualNetwork(net.getUuid(), _manager.getCanonicalName(net), net.getTrafficType());
NetworkPolicyModel oldPolicyModel = null;
/* this method is called when network is destroyed too, hence vn model might have been deleted already */
if (vnModel != null) {
oldPolicyModel = vnModel.getNetworkPolicyModel();
vnModel.addToNetworkPolicy(policyModel);
}
try {
policyModel.build(_manager.getModelController(), rules);
} catch (Exception e) {
s_logger.error(e);
e.printStackTrace();
return false;
}
try {
if (!policyModel.verify(_manager.getModelController())) {
policyModel.update(_manager.getModelController());
}
_manager.getDatabase().getNetworkPolicys().add(policyModel);
} catch (Exception ex) {
s_logger.error("network-policy update: ", ex);
ex.printStackTrace();
return false;
}
if (!policyModel.hasPolicyRules()) {
try {
policyModel.delete(_manager.getModelController());
_manager.getDatabase().getNetworkPolicys().remove(policyModel);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/*
* if no other VNs are associated with the old policy,
* we could delete it from the Contrail VNC
*/
if (policyModel != oldPolicyModel && oldPolicyModel != null && !oldPolicyModel.hasDescendents()) {
try {
oldPolicyModel.delete(_manager.getModelController());
_manager.getDatabase().getNetworkPolicys().remove(oldPolicyModel);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
Aggregations