use of net.juniper.contrail.api.types.VirtualNetwork in project cloudstack by apache.
the class VirtualNetworkModelTest method setUp.
@Override
@Before
public void setUp() throws IOException {
//Network UUIDs
String uuid = UUID.randomUUID().toString();
String uuid1 = UUID.randomUUID().toString();
String uuid2 = UUID.randomUUID().toString();
String uuid3 = UUID.randomUUID().toString();
//ContrailManager
ContrailManagerImpl contrailMgr = mock(ContrailManagerImpl.class);
controller = mock(ModelController.class);
VlanDao vlanDao = mock(VlanDao.class);
ApiConnector api = mock(ApiConnectorMock.class);
//Mock classes/methods
when(controller.getManager()).thenReturn(contrailMgr);
when(controller.getApiAccessor()).thenReturn(api);
when(controller.getVlanDao()).thenReturn(vlanDao);
//Policy References used by vnModel1
List<ObjectReference<VirtualNetworkPolicyType>> policyRefs1 = new ArrayList<ObjectReference<VirtualNetworkPolicyType>>();
ObjectReference<VirtualNetworkPolicyType> objectReference1 = new ObjectReference<VirtualNetworkPolicyType>();
policyRefs1.add(objectReference1);
//Policy References used by vnModel2
List<ObjectReference<VirtualNetworkPolicyType>> policyRefs2 = new ArrayList<ObjectReference<VirtualNetworkPolicyType>>();
ObjectReference<VirtualNetworkPolicyType> objectReference2 = new ObjectReference<VirtualNetworkPolicyType>();
policyRefs2.add(objectReference2);
//Policy References used by vnModel3
List<ObjectReference<VirtualNetworkPolicyType>> policyRefs3 = new ArrayList<ObjectReference<VirtualNetworkPolicyType>>();
ObjectReference<VirtualNetworkPolicyType> objectReference3 = new ObjectReference<VirtualNetworkPolicyType>();
objectReference3.setReference(Arrays.asList(""), null, null, UUID.randomUUID().toString());
policyRefs3.add(objectReference3);
//Network to be compared with
VirtualNetwork vn = mock(VirtualNetwork.class);
when(api.findById(VirtualNetwork.class, uuid)).thenReturn(vn);
//Network to be compared with
VirtualNetwork vn1 = mock(VirtualNetwork.class);
when(api.findById(VirtualNetwork.class, uuid1)).thenReturn(vn1);
when(vn1.getNetworkPolicy()).thenReturn(policyRefs1);
//Network to be compared to
VirtualNetwork vn2 = mock(VirtualNetwork.class);
when(api.findById(VirtualNetwork.class, uuid2)).thenReturn(vn2);
when(vn2.getNetworkPolicy()).thenReturn(policyRefs2);
//Network to be compared to
VirtualNetwork vn3 = mock(VirtualNetwork.class);
when(api.findById(VirtualNetwork.class, uuid3)).thenReturn(vn3);
when(vn3.getNetworkPolicy()).thenReturn(policyRefs3);
//Virtual-Network 1
NetworkVO network1 = mock(NetworkVO.class);
when(network1.getName()).thenReturn("testnetwork");
when(network1.getState()).thenReturn(State.Allocated);
when(network1.getGateway()).thenReturn("10.1.1.1");
when(network1.getCidr()).thenReturn("10.1.1.0/24");
when(network1.getPhysicalNetworkId()).thenReturn(42L);
when(network1.getDomainId()).thenReturn(10L);
when(network1.getAccountId()).thenReturn(42L);
//Virtual-Network 2
NetworkVO network2 = mock(NetworkVO.class);
when(network2.getName()).thenReturn("Testnetwork");
when(network2.getState()).thenReturn(State.Allocated);
when(network2.getGateway()).thenReturn("10.1.1.1");
when(network2.getCidr()).thenReturn("10.1.1.0/24");
when(network2.getPhysicalNetworkId()).thenReturn(42L);
when(network2.getDomainId()).thenReturn(10L);
when(network2.getAccountId()).thenReturn(42L);
//Virtual-Network 3
NetworkVO network3 = mock(NetworkVO.class);
when(network3.getName()).thenReturn("Testnetwork");
when(network3.getState()).thenReturn(State.Allocated);
when(network3.getGateway()).thenReturn("10.1.1.1");
when(network3.getCidr()).thenReturn("10.1.1.0/24");
when(network3.getPhysicalNetworkId()).thenReturn(42L);
when(network3.getDomainId()).thenReturn(10L);
when(network3.getAccountId()).thenReturn(42L);
when(contrailMgr.getCanonicalName(network1)).thenReturn("testnetwork");
when(contrailMgr.getProjectId(network1.getDomainId(), network1.getAccountId())).thenReturn("testProjectId");
vnModel = new VirtualNetworkModel(network1, uuid, "testnetwork", TrafficType.Guest);
vnModel1 = new VirtualNetworkModel(network1, uuid1, "testnetwork", TrafficType.Guest);
vnModel2 = new VirtualNetworkModel(network2, uuid2, "testnetwork", TrafficType.Guest);
vnModel3 = new VirtualNetworkModel(network3, uuid3, "testnetwork", TrafficType.Guest);
}
use of net.juniper.contrail.api.types.VirtualNetwork in project cloudstack by apache.
the class NetworkProviderTest method dbSyncTest.
@Test
public void dbSyncTest() {
Network network = lookupTestNetwork("test-db-only-net");
if (network == null) {
network = createTestNetwork("test-db-only-net");
}
UserVm vm = _server.createVM("test-db-only-vm", network);
try {
createFloatingIp(network, vm);
} catch (Exception e) {
fail("unable to create floating ip");
}
/* reset ApiServer objects to default config only, so above created objects
* exists only in cludstack db but not in api server
*/
((ApiConnectorMock) _api).initConfig();
/* reset model cached objects */
_contrailMgr.getDatabase().initDb();
/* Create one object of each type directly in api-server - these objects does not exist in cloudstack */
net.juniper.contrail.api.types.Domain domain = new net.juniper.contrail.api.types.Domain();
domain.setName("test-vnc-only-domain--1");
domain.setUuid(UUID.randomUUID().toString());
try {
assertTrue(_api.create(domain));
} catch (IOException ex) {
fail(ex.getMessage());
}
Project project = new Project();
project.setName("test-vnc-only-project-1");
project.setUuid(UUID.randomUUID().toString());
project.setParent(domain);
try {
assertTrue(_api.create(project));
} catch (IOException ex) {
fail(ex.getMessage());
}
VirtualNetwork net = new VirtualNetwork();
net.setName("test-vnc-only-net-1");
net.setUuid(UUID.randomUUID().toString());
net.setParent(project);
NetworkIpam ipam = null;
try {
// Find default-network-ipam
String ipam_id = _api.findByName(NetworkIpam.class, null, "default-network-ipam");
assertNotNull(ipam_id);
ipam = (NetworkIpam) _api.findById(NetworkIpam.class, ipam_id);
assertNotNull(ipam);
} catch (IOException ex) {
fail(ex.getMessage());
}
VnSubnetsType subnet = new VnSubnetsType();
subnet.addIpamSubnets(new SubnetType("10.0.2.0", 24), "10.0.2.254");
net.addNetworkIpam(ipam, subnet);
VirtualMachine vncVm = new VirtualMachine();
vncVm.setName("test-vnc-only-vm-1");
try {
assertTrue(_api.create(vncVm));
} catch (IOException ex) {
fail(ex.getMessage());
}
VirtualMachineInterface vmi = new VirtualMachineInterface();
vmi.setParent(vncVm);
vmi.setName("test-vnc-only-vmi-1");
try {
assertTrue(_api.create(vmi));
assertTrue(_api.create(net));
} catch (IOException ex) {
fail(ex.getMessage());
}
InstanceIp ip_obj = new InstanceIp();
ip_obj.setName(net.getName() + ":0");
ip_obj.setVirtualNetwork(net);
ip_obj.setVirtualMachineInterface(vmi);
try {
assertTrue(_api.create(ip_obj));
// Must perform a GET in order to update the object contents.
assertTrue(_api.read(ip_obj));
assertNotNull(ip_obj.getAddress());
} catch (IOException ex) {
fail(ex.getMessage());
}
//now db sync
if (_dbSync.syncAll(DBSyncGeneric.SYNC_MODE_UPDATE) == ServerDBSync.SYNC_STATE_OUT_OF_SYNC) {
s_logger.info("# Cloudstack DB & VNC are out of sync - resync done");
}
if (_dbSync.syncAll(DBSyncGeneric.SYNC_MODE_CHECK) == ServerDBSync.SYNC_STATE_OUT_OF_SYNC) {
s_logger.info("# Cloudstack DB & VNC are still out of sync");
fail("DB Sync failed");
}
}
use of net.juniper.contrail.api.types.VirtualNetwork in project cloudstack by apache.
the class VirtualNetworkModel method update.
@Override
public void update(ModelController controller) throws InternalErrorException, IOException {
assert _initialized;
ApiConnector api = controller.getApiAccessor();
VlanDao vlanDao = controller.getVlanDao();
VirtualNetwork vn = _vn;
if (!isDynamicNetwork()) {
_vn = (VirtualNetwork) controller.getApiAccessor().findById(VirtualNetwork.class, _uuid);
return;
}
assert _uuid != null : "uuid is not set";
if (_vn == null) {
vn = _vn = (VirtualNetwork) controller.getApiAccessor().findById(VirtualNetwork.class, _uuid);
if (vn == null) {
vn = new VirtualNetwork();
if (_projectId != null) {
Project project;
try {
project = (Project) api.findById(Project.class, _projectId);
} catch (IOException ex) {
s_logger.debug("project read", ex);
throw new CloudRuntimeException("Failed to read project", ex);
}
vn.setParent(project);
}
vn.setName(_name);
vn.setUuid(_uuid);
}
}
if (_policyModel == null) {
vn.clearNetworkPolicy();
} else if (!_policyModel.hasPolicyRules()) {
vn.clearNetworkPolicy();
_policyModel.removeSuccessor(this);
} else {
vn.setNetworkPolicy(_policyModel.getPolicy(), new VirtualNetworkPolicyType(new VirtualNetworkPolicyType.SequenceType(1, 0), null));
}
if (_ipam == null) {
NetworkIpam ipam = null;
try {
String ipam_id = api.findByName(NetworkIpam.class, null, "default-network-ipam");
if (ipam_id == null) {
s_logger.debug("could not find default-network-ipam");
return;
}
ipam = (NetworkIpam) api.findById(NetworkIpam.class, ipam_id);
if (ipam == null) {
s_logger.debug("could not find NetworkIpam with ipam_id: " + ipam_id);
return;
}
} catch (IOException ex) {
s_logger.error(ex);
return;
}
_ipam = ipam;
}
if (_prefix != null) {
VnSubnetsType subnet = new VnSubnetsType();
String[] addr_pair = _prefix.split("\\/");
subnet.addIpamSubnets(new SubnetType(addr_pair[0], Integer.parseInt(addr_pair[1])), _gateway);
vn.setNetworkIpam(_ipam, subnet);
} else if (_trafficType == TrafficType.Public) {
vn.clearNetworkIpam();
/* Subnet information for Public is stored in the vlan table */
List<VlanVO> vlan_list = vlanDao.listVlansByNetworkId(_id);
for (VlanVO vlan : vlan_list) {
String cidr = NetUtils.ipAndNetMaskToCidr(vlan.getVlanGateway(), vlan.getVlanNetmask());
int slash = cidr.indexOf('/');
String ip_addr = cidr.substring(0, slash);
int plen = Integer.parseInt(cidr.substring(slash + 1));
VnSubnetsType subnet = new VnSubnetsType();
subnet.addIpamSubnets(new SubnetType(ip_addr, plen), vlan.getVlanGateway());
vn.addNetworkIpam(_ipam, subnet);
}
}
if (_vn == null) {
try {
api.create(vn);
} catch (Exception ex) {
s_logger.debug("virtual-network create", ex);
throw new CloudRuntimeException("Failed to create virtual-network", ex);
}
_vn = vn;
} else {
try {
api.update(vn);
} catch (IOException ex) {
s_logger.warn("virtual-network update", ex);
throw new CloudRuntimeException("Unable to update virtual-network object", ex);
}
}
for (ModelObject successor : successors()) {
successor.update(controller);
}
}
use of net.juniper.contrail.api.types.VirtualNetwork in project cloudstack by apache.
the class ServerDBSyncImpl method syncVirtualNetwork.
/*
* Virtual Network Synchronization methods
*/
@SuppressWarnings({ "unchecked" })
public boolean syncVirtualNetwork() throws Exception {
final ApiConnector api = _manager.getApiConnector();
try {
List<TrafficType> types = new ArrayList<TrafficType>();
types.add(TrafficType.Public);
types.add(TrafficType.Guest);
List<NetworkVO> dbNets = _manager.findManagedNetworks(types);
List<VirtualNetwork> vList = (List<VirtualNetwork>) api.list(VirtualNetwork.class, null);
List<VirtualNetwork> vncList = new ArrayList<VirtualNetwork>();
for (VirtualNetwork vn : vList) {
if (!_manager.isSystemDefaultNetwork(vn)) {
vncList.add(vn);
}
}
s_logger.debug("sync VN - DB size: " + dbNets.size() + " VNC Size: " + vncList.size());
return _dbSync.syncGeneric(VirtualNetwork.class, dbNets, vncList);
} catch (Exception ex) {
s_logger.warn("sync virtual-networks", ex);
throw ex;
}
}
use of net.juniper.contrail.api.types.VirtualNetwork in project cloudstack by apache.
the class InstanceIpModel method update.
@Override
public void update(ModelController controller) throws InternalErrorException, IOException {
assert _vmiModel != null;
ApiConnector api = controller.getApiAccessor();
VirtualNetworkModel vnModel = _vmiModel.getVirtualNetworkModel();
assert vnModel != null;
VirtualMachineInterface vmi = _vmiModel.getVMInterface();
VirtualNetwork vnet = vnModel.getVirtualNetwork();
if (vnet == null) {
vnet = (VirtualNetwork) api.findById(VirtualNetwork.class, _vmiModel.getNetworkUuid());
}
String ipid = api.findByName(InstanceIp.class, null, _name);
if (ipid == null) {
InstanceIp ip_obj = new InstanceIp();
ip_obj.setName(_name);
ip_obj.setVirtualNetwork(vnet);
if (_ipAddress != null) {
ip_obj.setAddress(_ipAddress);
}
ip_obj.setVirtualMachineInterface(vmi);
if (!api.create(ip_obj)) {
throw new InternalErrorException("Unable to create instance-ip " + _name);
}
api.read(ip_obj);
_uuid = ip_obj.getUuid();
if (_ipAddress == null) {
if (!api.read(ip_obj)) {
throw new InternalErrorException("Unable to read instance-ip " + _name);
}
}
_ipAddress = ip_obj.getAddress();
} else {
// Ensure that the instance-ip has the correct value and is pointing at the VMI.
InstanceIp ip_obj = (InstanceIp) api.findById(InstanceIp.class, ipid);
if (ip_obj == null) {
throw new InternalErrorException("Unable to read instance-ip " + _name);
}
boolean update = false;
String ipnet_id = ObjectReference.getReferenceListUuid(ip_obj.getVirtualNetwork());
if (ipnet_id == null || !ipnet_id.equals(_vmiModel.getNetworkUuid())) {
ip_obj.setVirtualNetwork(vnet);
update = true;
}
if (_ipAddress != null && !ip_obj.getAddress().equals(_ipAddress)) {
ip_obj.setAddress(_ipAddress);
update = true;
}
String vmi_id = ObjectReference.getReferenceListUuid(ip_obj.getVirtualMachineInterface());
if (vmi_id == null || !vmi_id.equals(_vmiModel.getUuid())) {
if (vmi != null) {
ip_obj.setVirtualMachineInterface(vmi);
update = true;
}
}
if (update && !api.update(ip_obj)) {
throw new InternalErrorException("Unable to update instance-ip: " + ip_obj.getName());
}
api.read(ip_obj);
_uuid = ip_obj.getUuid();
_ipAddress = ip_obj.getAddress();
}
}
Aggregations