use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState in project photon-model by vmware.
the class BaseVSphereAdapterTest method createNic.
protected String createNic(String name, String networkLink) throws Throwable {
// Create Subnet
this.subnet = new SubnetState();
this.subnet.networkLink = networkLink;
this.subnet.endpointLink = "/some/endpoint/link";
this.subnet.lifecycleState = LifecycleState.READY;
this.subnet.id = this.subnet.name = this.networkId;
ManagedObjectReference ref = new ManagedObjectReference();
ref.setValue("network-4127");
ref.setType(VimNames.TYPE_NETWORK);
CustomProperties.of(this.subnet).put(CustomProperties.TYPE, VimNames.TYPE_NETWORK).put(CustomProperties.MOREF, ref);
this.subnet = TestUtils.doPost(this.host, this.subnet, SubnetState.class, UriUtils.buildUri(this.host, SubnetService.FACTORY_LINK));
this.nicDescription = new NetworkInterfaceDescription();
this.nicDescription.assignment = NetworkInterfaceDescriptionService.IpAssignment.DYNAMIC;
this.nicDescription = TestUtils.doPost(this.host, this.nicDescription, NetworkInterfaceDescription.class, UriUtils.buildUri(this.host, NetworkInterfaceDescriptionService.FACTORY_LINK));
this.nicDescription.subnetLink = this.subnet.documentSelfLink;
NetworkInterfaceState nic = new NetworkInterfaceState();
nic.name = name;
nic.subnetLink = this.subnet.documentSelfLink;
nic.networkInterfaceDescriptionLink = this.nicDescription.documentSelfLink;
nic = TestUtils.doPost(this.host, nic, NetworkInterfaceState.class, UriUtils.buildUri(this.host, NetworkInterfaceService.FACTORY_LINK));
return nic.documentSelfLink;
}
use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState in project photon-model by vmware.
the class AzureTestUtil method createDefaultNicStates.
/*
* NOTE: It is highly recommended to keep this method in sync with its AWS counterpart:
* TestAWSSetupUtils.createAWSNicStates
*/
public static List<NetworkInterfaceState> createDefaultNicStates(VerificationHost host, ComputeState computeHost, EndpointState endpointState, Set<String> networkRGLinks, Set<String> sgRGLinks, AzureNicSpecs azureNicSpecs, String azureVMName) throws Throwable {
// Create network state.
NetworkState networkState;
{
networkState = new NetworkState();
networkState.id = azureNicSpecs.network.name;
networkState.name = azureNicSpecs.network.name;
networkState.subnetCIDR = azureNicSpecs.network.cidr;
networkState.authCredentialsLink = endpointState.authCredentialsLink;
networkState.endpointLink = endpointState.documentSelfLink;
networkState.endpointLinks = new HashSet<>();
networkState.endpointLinks.add(endpointState.documentSelfLink);
networkState.tenantLinks = endpointState.tenantLinks;
networkState.resourcePoolLink = computeHost.resourcePoolLink;
networkState.groupLinks = networkRGLinks;
networkState.regionId = azureNicSpecs.network.zoneId;
networkState.instanceAdapterReference = UriUtils.buildUri(host, DEFAULT_INSTANCE_ADAPTER_REFERENCE);
networkState.tagLinks = createTagStateSet(host, endpointState.tenantLinks, TAG_KEY_TYPE, azure_vnet.name());
networkState.computeHostLink = endpointState.computeHostLink;
networkState = TestUtils.doPost(host, networkState, NetworkState.class, UriUtils.buildUri(host, NetworkService.FACTORY_LINK));
}
// Create NIC states.
List<NetworkInterfaceState> nics = new ArrayList<>();
for (int i = 0; i < azureNicSpecs.nicSpecs.size(); i++) {
NicSpec nicSpec = azureNicSpecs.nicSpecs.get(i);
// Create subnet state per NIC.
SubnetState subnetState;
{
subnetState = new SubnetState();
subnetState.id = azureNicSpecs.nicSpecs.get(i).subnetSpec.name;
subnetState.name = azureNicSpecs.nicSpecs.get(i).subnetSpec.name;
subnetState.subnetCIDR = azureNicSpecs.nicSpecs.get(i).subnetSpec.cidr;
subnetState.zoneId = azureNicSpecs.nicSpecs.get(i).subnetSpec.zoneId;
subnetState.networkLink = networkState.documentSelfLink;
subnetState.endpointLink = endpointState.documentSelfLink;
subnetState.endpointLinks = new HashSet<>();
subnetState.endpointLinks.add(endpointState.documentSelfLink);
subnetState.computeHostLink = endpointState.computeHostLink;
subnetState.tenantLinks = endpointState.tenantLinks;
subnetState.tagLinks = createTagStateSet(host, endpointState.tenantLinks, TAG_KEY_TYPE, azure_subnet.name());
subnetState = TestUtils.doPost(host, subnetState, SubnetState.class, UriUtils.buildUri(host, SubnetService.FACTORY_LINK));
}
// Create security group state
SecurityGroupState securityGroupState;
{
securityGroupState = new SecurityGroupState();
securityGroupState.name = AZURE_SECURITY_GROUP_NAME + "-" + azureVMName;
securityGroupState.authCredentialsLink = endpointState.authCredentialsLink;
securityGroupState.endpointLink = endpointState.documentSelfLink;
securityGroupState.endpointLinks = new HashSet<>();
securityGroupState.endpointLinks.add(endpointState.documentSelfLink);
securityGroupState.computeHostLink = endpointState.computeHostLink;
securityGroupState.tenantLinks = endpointState.tenantLinks;
securityGroupState.groupLinks = sgRGLinks;
securityGroupState.regionId = "regionId";
securityGroupState.resourcePoolLink = "/link/to/rp";
securityGroupState.instanceAdapterReference = new URI("http://instanceAdapterReference");
{
Rule ssh = new Rule();
ssh.name = "ssh-in";
ssh.protocol = "tcp";
ssh.ipRangeCidr = "0.0.0.0/0";
ssh.ports = "22";
securityGroupState.ingress = Collections.singletonList(ssh);
}
{
Rule out = new Rule();
out.name = "out";
out.protocol = "tcp";
out.ipRangeCidr = "0.0.0.0/0";
out.ports = SecurityGroupService.ALL_PORTS;
securityGroupState.egress = Collections.singletonList(out);
}
securityGroupState = TestUtils.doPost(host, securityGroupState, SecurityGroupState.class, UriUtils.buildUri(host, SecurityGroupService.FACTORY_LINK));
}
// Create NIC description.
NetworkInterfaceDescription nicDescription;
{
nicDescription = new NetworkInterfaceDescription();
nicDescription.id = "nicDesc" + i;
nicDescription.name = generateName("nicDesc" + i);
nicDescription.deviceIndex = i;
nicDescription.assignPublicIpAddress = azureNicSpecs.assignPublicIpAddress;
nicDescription.tenantLinks = endpointState.tenantLinks;
nicDescription.endpointLink = endpointState.documentSelfLink;
nicDescription.endpointLinks = new HashSet<>();
nicDescription.endpointLinks.add(endpointState.documentSelfLink);
nicDescription.computeHostLink = endpointState.computeHostLink;
nicDescription.assignment = nicSpec.getIpAssignment();
// if staticIp is null, it will be assigned automatically by DHCP.
nicDescription.address = nicSpec.ip();
nicDescription = TestUtils.doPost(host, nicDescription, NetworkInterfaceDescription.class, UriUtils.buildUri(host, NetworkInterfaceDescriptionService.FACTORY_LINK));
}
NetworkInterfaceState nicState = new NetworkInterfaceState();
nicState.id = "nic" + i;
nicState.name = generateName("nic" + i);
nicState.deviceIndex = nicDescription.deviceIndex;
nicState.networkInterfaceDescriptionLink = nicDescription.documentSelfLink;
nicState.subnetLink = subnetState.documentSelfLink;
nicState.networkLink = subnetState.networkLink;
nicState.tenantLinks = endpointState.tenantLinks;
nicState.endpointLink = endpointState.documentSelfLink;
nicState.endpointLinks = new HashSet<>();
nicState.endpointLinks.add(endpointState.documentSelfLink);
nicState.computeHostLink = endpointState.computeHostLink;
if (nicSpec.getIpAssignment() == IpAssignment.STATIC) {
// There is a rule in:
// \photon-model\photon-model\src\main\java\com\vmware\photon\controller\model\resources\NetworkInterfaceService.java::validateState()
// // which will throws java.lang.IllegalArgumentException: both networkLink and IP
// cannot be set
nicState.networkLink = null;
}
if (i == 0) {
// Attach security group only on the primary nic.
nicState.securityGroupLinks = Collections.singletonList(securityGroupState.documentSelfLink);
}
nicState.tagLinks = Collections.singleton(TagsUtil.newTagState(TAG_KEY_TYPE, azure_net_interface.name(), false, endpointState.tenantLinks).documentSelfLink);
nicState = TestUtils.doPost(host, nicState, NetworkInterfaceState.class, UriUtils.buildUri(host, NetworkInterfaceService.FACTORY_LINK));
nics.add(nicState);
}
return nics;
}
use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState in project photon-model by vmware.
the class VSphereAdapterInstanceService method updateNicsAfterProvisionSuccess.
/**
* Update the details of nics into compute state after the provisioning is successful
*/
private void updateNicsAfterProvisionSuccess(List<VirtualEthernetCard> virtualEthernetCards, ProvisionContext ctx) {
boolean changed = false;
NetworkInterfaceState nic = null;
if (ctx.nics.size() == 0) {
return;
}
for (VirtualEthernetCard virtualEthernetCard : virtualEthernetCards) {
VirtualDeviceBackingInfo deviceBackingInfo = virtualEthernetCard.getBacking();
if (deviceBackingInfo instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo) {
VirtualEthernetCardDistributedVirtualPortBackingInfo info = (VirtualEthernetCardDistributedVirtualPortBackingInfo) deviceBackingInfo;
String portGroupKey = info.getPort().getPortgroupKey();
nic = findNic(ctx, DvsProperties.PORT_GROUP_KEY, portGroupKey);
} else if (deviceBackingInfo instanceof VirtualEthernetCardOpaqueNetworkBackingInfo) {
VirtualEthernetCardOpaqueNetworkBackingInfo info = (VirtualEthernetCardOpaqueNetworkBackingInfo) deviceBackingInfo;
String opaqueNetworkId = info.getOpaqueNetworkId();
nic = findNic(ctx, NsxProperties.OPAQUE_NET_ID, opaqueNetworkId);
} else if (deviceBackingInfo instanceof VirtualEthernetCardNetworkBackingInfo) {
VirtualEthernetCardNetworkBackingInfo info = (VirtualEthernetCardNetworkBackingInfo) deviceBackingInfo;
String deviceName = info.getDeviceName();
nic = ctx.nics.stream().filter(nics -> deviceName != null && nics.network != null && nics.network.name != null && nics.network.name.equals(deviceName)).findFirst().orElse(null);
}
if (nic != null) {
if (nic.customProperties == null) {
nic.customProperties = new HashMap<>(1);
}
if (!StringUtil.isNullOrEmpty(virtualEthernetCard.getExternalId())) {
// Update nic external id
nic.customProperties.put(CustomProperties.NIC_EXTERNAL_ID, virtualEthernetCard.getExternalId());
changed = true;
}
if (!StringUtil.isNullOrEmpty(virtualEthernetCard.getMacAddress())) {
// Update nic mac address
nic.customProperties.put(CustomProperties.NIC_MAC_ADDRESS, virtualEthernetCard.getMacAddress());
changed = true;
}
if (changed) {
NetworkInterfaceState patchNic = new NetworkInterfaceState();
patchNic.customProperties = nic.customProperties;
Operation.createPatch(PhotonModelUriUtils.createInventoryUri(getHost(), nic.documentSelfLink)).setBody(patchNic).sendWith(this);
}
} else {
log(Level.WARNING, "NetworkInterfaceState not found for vsphere network adapter " + "with mac address:" + " [%s]. The custom properties NIC_EXTERNAL_ID or NIC_MAC_ADDRESS were " + "not updated for: [%s]", virtualEthernetCard.getMacAddress(), ctx.nics.stream().map(ic -> ic.documentSelfLink).collect(Collectors.toList()).toString());
}
}
}
use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState in project photon-model by vmware.
the class VSphereVirtualMachineEnumerationHelper method addNewInterfaceState.
/**
* Add new interface state to compute network interface links
* @param service
* @param enumerationProgress Enumeration progress
* @param state Compute state
* @param id The id of distributed virtual port or opaque network
* @param mode Either using distributed virtual port or opaque network
* @param docType The document class type
* @param type The type
* @param ipAddress The primary ip address of this interface
*/
static <T> Operation addNewInterfaceState(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, ComputeState state, String id, InterfaceStateMode mode, Class<? extends ServiceDocument> docType, Class<T> type, String ipAddress) {
String fieldKey;
String fieldValue;
switch(mode) {
case INTERFACE_STATE_WITH_DISTRIBUTED_VIRTUAL_PORT:
{
fieldKey = CustomProperties.MOREF;
fieldValue = TYPE_PORTGROUP + ":" + id;
break;
}
case INTERFACE_STATE_WITH_OPAQUE_NETWORK:
{
fieldKey = NsxProperties.OPAQUE_NET_ID;
fieldValue = id;
break;
}
default:
{
service.logFine(() -> String.format("invalid mode when creating compute state with " + "network interface links: [%s]", mode));
return null;
}
}
Operation operation = queryByPortGroupIdOrByOpaqueNetworkId(service, enumerationProgress, fieldKey, fieldValue, docType).setCompletion((o, e) -> {
if (e != null) {
service.logWarning(() -> String.format("Error processing queryByPortGroupIdOrByOpaqueNetworkId for id: [%s]," + "error: [%s]", fieldValue, e.toString()));
return;
}
QueryTask task = o.getBody(QueryTask.class);
if (task.results != null && !task.results.documentLinks.isEmpty()) {
T netState = VsphereEnumerationHelper.convertOnlyResultToDocument(task.results, type);
NetworkInterfaceState iface = null;
if (netState instanceof SubnetState) {
SubnetState subnetState = (SubnetState) netState;
iface = createNewInterfaceState(service, id, subnetState.networkLink, subnetState.documentSelfLink, ipAddress);
} else if (netState instanceof NetworkState) {
NetworkState networkState = (NetworkState) netState;
iface = createNewInterfaceState(service, id, null, networkState.documentSelfLink, ipAddress);
}
if (iface != null) {
state.networkInterfaceLinks.add(iface.documentSelfLink);
}
} else {
service.logFine(() -> String.format("Will not add nic with id: [%s]", fieldValue));
}
});
return operation;
}
use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState in project photon-model by vmware.
the class VSphereVirtualMachineEnumerationHelper method createNewInterfaceState.
/**
* Create new network interface state
* @param service
* @param id The id of the distributed virtual port or opaque network
* @param networkLink The network link
* @param subnetworkLink The subnetwork link
* @return The network interface state
*/
static NetworkInterfaceState createNewInterfaceState(VSphereIncrementalEnumerationService service, String id, String networkLink, String subnetworkLink, String ipAddress) {
NetworkInterfaceState iface = new NetworkInterfaceState();
iface.name = id;
iface.documentSelfLink = buildUriPath(NetworkInterfaceService.FACTORY_LINK, service.getHost().nextUUID());
iface.networkLink = networkLink;
iface.subnetLink = subnetworkLink;
iface.address = ipAddress;
Operation.createPost(PhotonModelUriUtils.createInventoryUri(service.getHost(), NetworkInterfaceService.FACTORY_LINK)).setBody(iface).sendWith(service);
return iface;
}
Aggregations