xuzhang
Committed by Gerrit Code Review

[ONOS-2248]The implementation of port resource service.

Change-Id: I4ad8422f9bc199e2e56166c041adece8de707e5d
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--
~ Copyright 2015 Open Networking Laboratory
~
~ 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
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" name="${project.artifactId}-${project.version}">
<repository>mvn:${project.groupId}/${project.artifactId}/${project.version}/xml/features</repository>
<feature name="onos-app-vtnrsc" version="@FEATURE-VERSION"
description="ONOS app vtnrsc components">
<feature>onos-api</feature>
<bundle>mvn:org.onosproject/onos-app-vtnrsc/@ONOS-VERSION
</bundle>
</feature>
</features>
/*
* Copyright 2015 Open Networking Laboratory
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.app.vtnrsc;
import static com.google.common.base.MoreObjects.toStringHelper;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import org.onlab.packet.MacAddress;
import org.onosproject.net.DeviceId;
import org.onosproject.net.HostId;
/**
* Default implementation of VirtualPort interface .
*/
public final class DefaultVirtualPort implements VirtualPort {
private final VirtualPortId id;
private final TenantNetworkId networkId;
private final Boolean adminStateUp;
private final String name;
private final State state;
private final MacAddress macAddress;
private final TenantId tenantId;
private final String deviceOwner;
private final DeviceId deviceId;
private final FixedIp fixedIp;
private final HostId bindingHostId;
private final String bindingVnicType;
private final String bindingVifType;
private final String bindingVifDetails;
private final Collection<AllowedAddressPair> allowedAddressPairs;
private final Collection<SecurityGroup> securityGroups;
/**
* Creates a VirtualPort object.
*
* @param id the virtual port identifier
* @param networkId the network identifier
* @param adminStateUp adminStateup true or false
* @param strMap the map of properties of virtual port
* @param state virtual port state
* @param macAddress the MAC address
* @param tenantId the tenant identifier
* @param deviceId the device identifier
* @param fixedIp the fixed IP
* @param bindingHostId the binding host identifier
* @param allowedAddressPairs the collection of allowdeAddressPairs
* @param securityGroups the collection of securityGroups
*/
public DefaultVirtualPort(VirtualPortId id,
TenantNetworkId networkId,
Boolean adminStateUp,
Map<String, String> strMap,
State state,
MacAddress macAddress,
TenantId tenantId,
DeviceId deviceId,
FixedIp fixedIp,
HostId bindingHostId,
Collection<AllowedAddressPair> allowedAddressPairs,
Collection<SecurityGroup> securityGroups) {
this.id = id;
this.networkId = networkId;
this.adminStateUp = adminStateUp;
this.name = strMap.get("name");
this.state = state;
this.macAddress = macAddress;
this.tenantId = tenantId;
this.deviceOwner = strMap.get("deviceOwner");
this.deviceId = deviceId;
this.fixedIp = fixedIp;
this.bindingHostId = bindingHostId;
this.bindingVnicType = strMap.get("bindingVnicType");
this.bindingVifType = strMap.get("bindingVifType");
this.bindingVifDetails = strMap.get("bindingVifDetails");
this.allowedAddressPairs = allowedAddressPairs;
this.securityGroups = securityGroups;
}
@Override
public VirtualPortId portId() {
return id;
}
@Override
public TenantNetworkId networkId() {
return networkId;
}
@Override
public String name() {
return name;
}
@Override
public boolean adminStateUp() {
return adminStateUp;
}
@Override
public State state() {
return state;
}
@Override
public MacAddress macAddress() {
return macAddress;
}
@Override
public TenantId tenantId() {
return tenantId;
}
@Override
public DeviceId deviceId() {
return deviceId;
}
@Override
public String deviceOwner() {
return deviceOwner;
}
@Override
public Collection<AllowedAddressPair> allowedAddressPairs() {
return allowedAddressPairs;
}
@Override
public FixedIp fixedIps() {
return fixedIp;
}
@Override
public HostId bindingHostId() {
return bindingHostId;
}
@Override
public String bindingVnicType() {
return bindingVifType;
}
@Override
public String bindingVifType() {
return bindingVifType;
}
@Override
public String bindingVifDetails() {
return bindingVifDetails;
}
@Override
public Collection<SecurityGroup> securityGroups() {
return securityGroups;
}
@Override
public int hashCode() {
return Objects.hash(id, networkId, adminStateUp, name, state,
macAddress, tenantId, deviceId, deviceOwner,
allowedAddressPairs, fixedIp, bindingHostId,
bindingVnicType, bindingVifType, bindingVifDetails,
securityGroups);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof DefaultVirtualPort) {
final DefaultVirtualPort that = (DefaultVirtualPort) obj;
return Objects.equals(this.id, that.id)
&& Objects.equals(this.networkId, that.networkId)
&& Objects.equals(this.adminStateUp, that.adminStateUp)
&& Objects.equals(this.state, that.state)
&& Objects.equals(this.name, that.name)
&& Objects.equals(this.tenantId, that.tenantId)
&& Objects.equals(this.macAddress, that.macAddress)
&& Objects.equals(this.deviceId, that.deviceId)
&& Objects.equals(this.deviceOwner, that.deviceOwner)
&& Objects.equals(this.allowedAddressPairs,
that.allowedAddressPairs)
&& Objects.equals(this.fixedIp, that.fixedIp)
&& Objects.equals(this.bindingHostId, that.bindingHostId)
&& Objects.equals(this.bindingVifDetails,
that.bindingVifDetails)
&& Objects.equals(this.bindingVifType, that.bindingVifType)
&& Objects.equals(this.bindingVnicType,
that.bindingVnicType)
&& Objects.equals(this.securityGroups, that.securityGroups);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("id", id).add("network_id", networkId)
.add("adminStateUp", adminStateUp).add("state", state)
.add("name", name).add("state", state)
.add("macAddress", macAddress).add("tenantId", tenantId)
.add("deviced", deviceId).add("deviceOwner", deviceOwner)
.add("allowedAddressPairs", allowedAddressPairs)
.add("fixedIp", fixedIp).add("bindingHostId", bindingHostId)
.add("bindingVnicType", bindingVnicType)
.add("bindingVifDetails", bindingVifDetails)
.add("bindingVifType", bindingVifType)
.add("securityGroups", securityGroups).toString();
}
}
......@@ -22,7 +22,7 @@ import org.onosproject.net.DeviceId;
import org.onosproject.net.HostId;
/**
* Representation of a virtual port.
* Representation of the VirtualPort.
*/
public interface VirtualPort {
/**
......@@ -50,7 +50,7 @@ public interface VirtualPort {
/**
* Returns the network identifier.
*
* @return tenantNetwork ID
* @return tenantNetwork identifier
*/
TenantNetworkId networkId();
......@@ -65,9 +65,9 @@ public interface VirtualPort {
* Returns the administrative status of the port,which is up(true) or
* down(false).
*
* @return true or false
* @return true if the administrative status of the port is up
*/
Boolean adminStateUp();
boolean adminStateUp();
/**
* Returns the state.
......@@ -145,7 +145,7 @@ public interface VirtualPort {
*
* @return virtualPort bindingvifDetail
*/
String bindingvifDetails();
String bindingVifDetails();
/**
* Returns the security groups.
......
......@@ -45,32 +45,30 @@ public interface VirtualPortService {
/**
* Returns the collection of the currently known virtualPort.
*
* @return virtualPort.
*/
Collection<VirtualPort> getPorts();
/**
* Returns the collection of the virtualPorts associated with the networkId.
*
* @param networkId network identifier
* @return collection of virtualPort
* @param networkId the network identifer
* @return collection of virtualPort.
*/
Collection<VirtualPort> getPorts(TenantNetworkId networkId);
/**
* Returns the collection of the virtualPorts associated with the tenantId.
*
* @param tenantId tenant identifier
* @return collection of virtualPort
* @param tenantId the tenant identifier
* @return collection of virtualPorts.
*/
Collection<VirtualPort> getPorts(TenantId tenantId);
/**
* Returns the collection of the virtualPorts associated with the deviceId.
*
* @param deviceId device identifier
* @return collection of virtualPort
* @param deviceId the device identifier
* @return collection of virtualPort.
*/
Collection<VirtualPort> getPorts(DeviceId deviceId);
......@@ -86,7 +84,7 @@ public interface VirtualPortService {
* Updates virtualPorts by virtualPorts.
*
* @param virtualPorts the iterable collection of virtualPorts
* @return true if all given identifiers updated successfully
* @return true if all given identifiers updated successfully.
*/
boolean updatePorts(Iterable<VirtualPort> virtualPorts);
......@@ -95,7 +93,7 @@ public interface VirtualPortService {
*
* @param virtualPortIds the iterable collection of virtualPort identifiers
* @return true or false if one with the given identifier to delete is
* successfully
* successfully.
*/
boolean removePorts(Iterable<VirtualPortId> virtualPortIds);
}
......
......@@ -15,6 +15,8 @@
*/
package org.onosproject.app.vtnrsc.virtualport.impl;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Collection;
import java.util.Collections;
......@@ -25,17 +27,17 @@ import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onlab.util.KryoNamespace;
import org.onosproject.net.DeviceId;
import org.onosproject.store.service.EventuallyConsistentMap;
import org.onosproject.store.service.MultiValuedTimestamp;
import org.onosproject.store.service.StorageService;
import org.onosproject.store.service.WallClockTimestamp;
import org.onosproject.app.vtnrsc.TenantId;
import org.onosproject.app.vtnrsc.TenantNetworkId;
import org.onosproject.app.vtnrsc.VirtualPort;
import org.onosproject.app.vtnrsc.VirtualPortId;
import org.onosproject.app.vtnrsc.tenantnetwork.TenantNetworkService;
import org.onosproject.app.vtnrsc.virtualport.VirtualPortService;
import org.onosproject.net.DeviceId;
import org.onosproject.store.service.EventuallyConsistentMap;
import org.onosproject.store.service.MultiValuedTimestamp;
import org.onosproject.store.service.StorageService;
import org.onosproject.store.service.WallClockTimestamp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -45,8 +47,15 @@ import org.slf4j.LoggerFactory;
@Component(immediate = true)
@Service
public class VirtualPortManager implements VirtualPortService {
private final Logger log = LoggerFactory.getLogger(getClass());
private static final String VIRTUALPORT_ID_NULL = "VirtualPort ID cannot be null";
private static final String VIRTUALPORT_NOT_NULL = "VirtualPort cannot be null";
private static final String TENANTID_NOT_NULL = "TenantId cannot be null";
private static final String NETWORKID_NOT_NULL = "NetworkId cannot be null";
private static final String DEVICEID_NOT_NULL = "DeviceId cannot be null";
private EventuallyConsistentMap<VirtualPortId, VirtualPort> vPortStore;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
......@@ -74,14 +83,13 @@ public class VirtualPortManager implements VirtualPortService {
@Override
public boolean exists(VirtualPortId vPortId) {
checkNotNull(vPortId, VIRTUALPORT_ID_NULL);
return vPortStore.containsKey(vPortId);
}
@Override
public VirtualPort getPort(VirtualPortId vPortId) {
if (!exists(vPortId)) {
return null;
}
checkNotNull(vPortId, VIRTUALPORT_ID_NULL);
return vPortStore.get(vPortId);
}
......@@ -92,81 +100,93 @@ public class VirtualPortManager implements VirtualPortService {
@Override
public Collection<VirtualPort> getPorts(TenantNetworkId networkId) {
Collection<VirtualPort> vPortWithNetworkId =
Collections.unmodifiableCollection(vPortStore.values());
if (networkId == null || !networkService.exists(networkId)) {
return null;
}
for (VirtualPort vPort : vPortWithNetworkId) {
checkNotNull(networkId, NETWORKID_NOT_NULL);
Collection<VirtualPort> vPortWithNetworkIds = vPortStore.values();
for (VirtualPort vPort : vPortWithNetworkIds) {
if (!vPort.networkId().equals(networkId)) {
vPortWithNetworkId.remove(vPort);
vPortWithNetworkIds.remove(vPort);
}
}
return vPortWithNetworkId;
return vPortWithNetworkIds;
}
@Override
public Collection<VirtualPort> getPorts(TenantId tenantId) {
Collection<VirtualPort> vPortWithTenantId =
Collections.unmodifiableCollection(vPortStore.values());
if (tenantId == null) {
return null;
}
for (VirtualPort vPort : vPortWithTenantId) {
checkNotNull(tenantId, TENANTID_NOT_NULL);
Collection<VirtualPort> vPortWithTenantIds = vPortStore.values();
for (VirtualPort vPort : vPortWithTenantIds) {
if (!vPort.tenantId().equals(tenantId)) {
vPortWithTenantId.remove(vPort);
vPortWithTenantIds.remove(vPort);
}
}
return vPortWithTenantId;
return vPortWithTenantIds;
}
@Override
public Collection<VirtualPort> getPorts(DeviceId deviceId) {
Collection<VirtualPort> vPortWithDeviceId =
Collections.unmodifiableCollection(vPortStore.values());
if (deviceId == null) {
return null;
}
for (VirtualPort vPort : vPortWithDeviceId) {
checkNotNull(deviceId, DEVICEID_NOT_NULL);
Collection<VirtualPort> vPortWithDeviceIds = vPortStore.values();
for (VirtualPort vPort : vPortWithDeviceIds) {
if (!vPort.deviceId().equals(deviceId)) {
vPortWithDeviceId.remove(vPort);
vPortWithDeviceIds.remove(vPort);
}
}
return vPortWithDeviceId;
return vPortWithDeviceIds;
}
@Override
public boolean createPorts(Iterable<VirtualPort> vPorts) {
for (VirtualPort vPort:vPorts) {
log.info("vPortId is {} ", vPort.portId().toString());
checkNotNull(vPorts, VIRTUALPORT_NOT_NULL);
for (VirtualPort vPort : vPorts) {
log.debug("vPortId is {} ", vPort.portId().toString());
vPortStore.put(vPort.portId(), vPort);
if (!vPortStore.containsKey(vPort.portId())) {
log.debug("the virtualPort created failed whose identifier was {} ",
vPort.portId().toString());
return false;
}
}
return true;
}
@Override
public boolean updatePorts(Iterable<VirtualPort> vPorts) {
Boolean flag = false;
checkNotNull(vPorts, VIRTUALPORT_NOT_NULL);
if (vPorts != null) {
for (VirtualPort vPort:vPorts) {
for (VirtualPort vPort : vPorts) {
vPortStore.put(vPort.portId(), vPort);
flag = true;
if (!vPortStore.containsKey(vPort.portId())) {
log.debug("the virtualPort did not exist whose identifier was {}",
vPort.portId().toString());
return false;
}
vPortStore.put(vPort.portId(), vPort);
if (!vPort.equals(vPortStore.get(vPort.portId()))) {
log.debug("the virtualPort updated failed whose identifier was {}",
vPort.portId().toString());
return false;
}
}
}
return flag;
return true;
}
@Override
public boolean removePorts(Iterable<VirtualPortId> vPortIds) {
Boolean flag = false;
checkNotNull(vPortIds, VIRTUALPORT_ID_NULL);
if (vPortIds != null) {
for (VirtualPortId vPortId:vPortIds) {
for (VirtualPortId vPortId : vPortIds) {
vPortStore.remove(vPortId);
flag = true;
log.info("The result of removing vPortId is {}", flag.toString());
if (vPortStore.containsKey(vPortId)) {
log.debug("the virtualPort removed failed whose identifier was {}",
vPortId.toString());
return false;
}
}
}
return flag;
return true;
}
}
......
/*
* Copyright 2015 Open Networking Laboratory
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.app.vtnrsc.web;
import static com.google.common.base.Preconditions.checkNotNull;
import org.onosproject.app.vtnrsc.AllowedAddressPair;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* VirtualPort AllowedAddressPair codec.
*/
public final class AllowedAddressPairCodec extends JsonCodec<AllowedAddressPair> {
@Override
public ObjectNode encode(AllowedAddressPair alocAddPair, CodecContext context) {
checkNotNull(alocAddPair, "AllowedAddressPair cannot be null");
ObjectNode result = context.mapper().createObjectNode()
.put("ip_address", alocAddPair.ip().toString())
.put("mac_address", alocAddPair.mac().toString());
return result;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.app.vtnrsc.web;
import static com.google.common.base.Preconditions.checkNotNull;
import org.onosproject.app.vtnrsc.FixedIp;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* VirtualPort FixedIp codec.
*/
public final class FixedIpCodec extends JsonCodec<FixedIp> {
@Override
public ObjectNode encode(FixedIp fixIp, CodecContext context) {
checkNotNull(fixIp, "FixedIp cannot be null");
ObjectNode result = context.mapper().createObjectNode()
.put("subnet_id", fixIp.subnetId().toString())
.put("ip_address", fixIp.ip().toString());
return result;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.app.vtnrsc.web;
import static com.google.common.base.Preconditions.checkNotNull;
import org.onosproject.app.vtnrsc.SecurityGroup;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* Virtualport SecurityGroup codec.
*/
public final class SecurityGroupCodec extends JsonCodec<SecurityGroup> {
@Override
public ObjectNode encode(SecurityGroup securGroup, CodecContext context) {
checkNotNull(securGroup, "SecurityGroup cannot be null");
ObjectNode result = context.mapper().createObjectNode()
.put("security_group", securGroup.securityGroup());
return result;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.app.vtnrsc.web;
import static com.google.common.base.Preconditions.checkNotNull;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.app.vtnrsc.VirtualPort;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* VirtualPort JSON codec.
*/
public final class VirtualPortCodec extends JsonCodec<VirtualPort> {
@Override
public ObjectNode encode(VirtualPort vPort, CodecContext context) {
checkNotNull(vPort, "VPort cannot be null");
ObjectNode result = context
.mapper()
.createObjectNode()
.put("id", vPort.portId().toString())
.put("network_id", vPort.networkId().toString())
.put("admin_state_up", vPort.adminStateUp())
.put("name", vPort.name().toString())
.put("status", vPort.state().toString())
.put("mac_address", vPort.macAddress().toString())
.put("tenant_id", vPort.tenantId().toString())
.put("device_id", vPort.deviceId().toString())
.put("device_owner", vPort.deviceOwner().toString())
.put("binding:vnic_type", vPort.bindingVnicType().toString())
.put("binding:Vif_type", vPort.bindingVifType().toString())
.put("binding:host_id", vPort.bindingHostId().mac().toString())
.put("binding:vif_details", vPort.bindingVifDetails().toString());
result.set("allowed_address_pairs", new AllowedAddressPairCodec().encode(
vPort.allowedAddressPairs(), context));
result.set("fixed_ips", new FixedIpCodec().encode(
vPort.fixedIps(), context));
result.set("security_groups", new SecurityGroupCodec().encode(
vPort.securityGroups(), context));
return result;
}
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--
~ Copyright 2015 Open Networking Laboratory
~
~ 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
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" name="${project.artifactId}-${project.version}">
<repository>mvn:${project.groupId}/${project.artifactId}/${project.version}/xml/features</repository>
<feature name="onos-app-vtnweb" version="@FEATURE-VERSION"
description="ONOS app vtnweb components">
<feature>onos-app-vtnrsc</feature>
<bundle>mvn:org.onosproject/vtnweb/@ONOS-VERSION
</bundle>
</feature>
</features>
/*
* Copyright 2015 Open Networking Laboratory
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.vtnweb.resources;
import static com.google.common.base.Preconditions.checkNotNull;
import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
import static javax.ws.rs.core.Response.Status.OK;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.onlab.packet.IpAddress;
import org.onlab.packet.MacAddress;
import org.onlab.util.ItemNotFoundException;
import org.onosproject.net.DeviceId;
import org.onosproject.net.HostId;
import org.onosproject.rest.AbstractWebResource;
import org.onosproject.app.vtnrsc.AllowedAddressPair;
import org.onosproject.app.vtnrsc.DefaultVirtualPort;
import org.onosproject.app.vtnrsc.FixedIp;
import org.onosproject.app.vtnrsc.SecurityGroup;
import org.onosproject.app.vtnrsc.SubnetId;
import org.onosproject.app.vtnrsc.TenantId;
import org.onosproject.app.vtnrsc.TenantNetworkId;
import org.onosproject.app.vtnrsc.VirtualPort;
import org.onosproject.app.vtnrsc.VirtualPortId;
import org.onosproject.app.vtnrsc.VirtualPort.State;
import org.onosproject.app.vtnrsc.virtualport.VirtualPortService;
import org.onosproject.app.vtnrsc.web.VirtualPortCodec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Maps;
/**
* REST resource for interacting with the inventory of infrastructure
* virtualPort.
*/
@Path("ports")
public class VirtualPortWebResource extends AbstractWebResource {
public static final String VPORT_NOT_FOUND = "VirtualPort is not found";
public static final String VPORT_ID_EXIST = "VirtualPort id is exist";
public static final String VPORT_ID_NOT_EXIST = "VirtualPort id is not exist";
public static final String JSON_NOT_NULL = "JsonNode can not be null";
protected static final Logger log = LoggerFactory
.getLogger(VirtualPortService.class);
@GET
@Produces({ MediaType.APPLICATION_JSON })
public Response getPorts() {
Iterable<VirtualPort> virtualPorts = get(VirtualPortService.class)
.getPorts();
ObjectNode result = new ObjectMapper().createObjectNode();
result.set("ports", new VirtualPortCodec().encode(virtualPorts, this));
return ok(result.toString()).build();
}
@GET
@Path("{id}")
@Produces({ MediaType.APPLICATION_JSON })
public Response getportsById(@PathParam("id") String id) {
if (!get(VirtualPortService.class).exists(VirtualPortId.portId(id))) {
return ok("the virtualPort does not exists").build();
}
VirtualPort virtualPort = nullIsNotFound(get(VirtualPortService.class)
.getPort(VirtualPortId.portId(id)), VPORT_NOT_FOUND);
ObjectNode result = new ObjectMapper().createObjectNode();
result.set("ports", new VirtualPortCodec().encode(virtualPort, this));
return ok(result.toString()).build();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createPorts(InputStream input) {
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode cfg = mapper.readTree(input);
Iterable<VirtualPort> vPorts = createOrUpdateByInputStream(cfg);
Boolean issuccess = nullIsNotFound(get(VirtualPortService.class)
.createPorts(vPorts), VPORT_NOT_FOUND);
if (!issuccess) {
return Response.status(INTERNAL_SERVER_ERROR)
.entity(VPORT_ID_NOT_EXIST).build();
}
return Response.status(OK).entity(issuccess.toString()).build();
} catch (Exception e) {
log.error("Creates VirtualPort failed because of exception {}",
e.toString());
return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString())
.build();
}
}
@Path("{portUUID}")
@DELETE
public Response deletePorts(@PathParam("portUUID") String id) {
Set<VirtualPortId> vPortIds = new HashSet<VirtualPortId>();
try {
if (id != null) {
vPortIds.add(VirtualPortId.portId(id));
}
Boolean issuccess = nullIsNotFound(get(VirtualPortService.class)
.removePorts(vPortIds), VPORT_NOT_FOUND);
if (!issuccess) {
return Response.status(INTERNAL_SERVER_ERROR)
.entity(VPORT_ID_NOT_EXIST).build();
}
return Response.status(OK).entity(issuccess.toString()).build();
} catch (Exception e) {
log.error("Deletes VirtualPort failed because of exception {}",
e.toString());
return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString())
.build();
}
}
@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updatePorts(@PathParam("id") String id, InputStream input) {
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode cfg = mapper.readTree(input);
Iterable<VirtualPort> vPorts = createOrUpdateByInputStream(cfg);
Boolean issuccess = nullIsNotFound(get(VirtualPortService.class)
.updatePorts(vPorts), VPORT_NOT_FOUND);
if (!issuccess) {
return Response.status(INTERNAL_SERVER_ERROR)
.entity(VPORT_ID_NOT_EXIST).build();
}
return Response.status(OK).entity(issuccess.toString()).build();
} catch (Exception e) {
log.error("Updates failed because of exception {}", e.toString());
return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString())
.build();
}
}
/**
* Returns a Object of the currently known infrastructure virtualPort.
*
* @param vPortNode the virtualPort json node
* @return a collection of virtualPorts
*/
public Iterable<VirtualPort> createOrUpdateByInputStream(JsonNode vPortNode) {
checkNotNull(vPortNode, JSON_NOT_NULL);
JsonNode vPortNodes = vPortNode.get("ports");
if (vPortNodes == null) {
vPortNodes = vPortNode.get("port");
}
if (vPortNodes.isArray()) {
return changeJsonToPorts(vPortNodes);
} else {
return changeJsonToPort(vPortNodes);
}
}
/**
* Returns the iterable collection of virtualports from subnetNodes.
*
* @param vPortNodes the virtualPort json node
* @return virtualPorts a collection of virtualPorts
*/
public Iterable<VirtualPort> changeJsonToPorts(JsonNode vPortNodes) {
checkNotNull(vPortNodes, JSON_NOT_NULL);
Map<VirtualPortId, VirtualPort> portMap = new HashMap<VirtualPortId, VirtualPort>();
Map<String, String> strMap = new HashMap<String, String>();
for (JsonNode vPortnode : vPortNodes) {
VirtualPortId id = VirtualPortId.portId(vPortnode.get("id")
.asText());
String name = vPortnode.get("name").asText();
TenantId tenantId = TenantId.tenantId(vPortnode.get("tenant_id")
.asText());
TenantNetworkId networkId = TenantNetworkId.networkId(vPortnode
.get("network_id").asText());
Boolean adminStateUp = vPortnode.get("admin_state_up").asBoolean();
String state = vPortnode.get("status").asText();
MacAddress macAddress = MacAddress.valueOf(vPortnode
.get("mac_address").asText());
DeviceId deviceId = DeviceId.deviceId(vPortnode.get("device_id")
.asText());
String deviceOwner = vPortnode.get("device_owner").asText();
JsonNode fixedIpNode = vPortnode.get("fixed_ips");
FixedIp fixedIp = jsonNodeToFixedIps(fixedIpNode);
HostId bindingHostId = HostId.hostId(MacAddress.valueOf(vPortnode
.get("binding:host_id").asText()));
String bindingVnicType = vPortnode.get("binding:vnic_type")
.asText();
String bindingVifType = vPortnode.get("binding:vif_type").asText();
String bindingVifDetails = vPortnode.get("binding:vif_details")
.asText();
JsonNode allowedAddressPairJsonNode = vPortnode
.get("allowed_address_pairs");
Collection<AllowedAddressPair> allowedAddressPairs =
jsonNodeToAllowedAddressPair(allowedAddressPairJsonNode);
JsonNode securityGroupNode = vPortnode.get("security_groups");
Collection<SecurityGroup> securityGroups =
jsonNodeToSecurityGroup(securityGroupNode);
strMap.putIfAbsent("name", name);
strMap.putIfAbsent("deviceOwner", deviceOwner);
strMap.putIfAbsent("bindingVnicType", bindingVnicType);
strMap.putIfAbsent("bindingVifType", bindingVifType);
strMap.putIfAbsent("bindingVifDetails", bindingVifDetails);
VirtualPort vPort = new DefaultVirtualPort(id, networkId,
adminStateUp, strMap,
isState(state),
macAddress, tenantId,
deviceId, fixedIp,
bindingHostId,
allowedAddressPairs,
securityGroups);
portMap.put(id, vPort);
}
return Collections.unmodifiableCollection(portMap.values());
}
/**
* Returns a collection of virtualPorts from subnetNodes.
*
* @param vPortNodes the virtualPort json node
* @return virtualPorts a collection of virtualPorts
*/
public Iterable<VirtualPort> changeJsonToPort(JsonNode vPortNodes) {
checkNotNull(vPortNodes, JSON_NOT_NULL);
Map<VirtualPortId, VirtualPort> vportMap = new HashMap<VirtualPortId, VirtualPort>();
Map<String, String> strMap = new HashMap<String, String>();
VirtualPortId id = VirtualPortId.portId(vPortNodes.get("id").asText());
String name = vPortNodes.get("name").asText();
TenantId tenantId = TenantId.tenantId(vPortNodes.get("tenant_id")
.asText());
TenantNetworkId networkId = TenantNetworkId.networkId(vPortNodes
.get("network_id").asText());
Boolean adminStateUp = vPortNodes.get("admin_state_up").asBoolean();
String state = vPortNodes.get("status").asText();
MacAddress macAddress = MacAddress.valueOf(vPortNodes
.get("mac_address").asText());
DeviceId deviceId = DeviceId.deviceId(vPortNodes.get("device_id")
.asText());
String deviceOwner = vPortNodes.get("device_owner").asText();
JsonNode fixedIpNode = vPortNodes.get("fixed_ips");
FixedIp fixedIp = jsonNodeToFixedIps(fixedIpNode);
HostId bindingHostId = HostId.hostId(MacAddress.valueOf(vPortNodes
.get("binding:host_id").asText()));
String bindingVnicType = vPortNodes.get("binding:vnic_type").asText();
String bindingVifType = vPortNodes.get("binding:vif_type").asText();
String bindingVifDetails = vPortNodes.get("binding:vif_details")
.asText();
JsonNode allowedAddressPairJsonNode = vPortNodes
.get("allowed_address_pairs");
Collection<AllowedAddressPair> allowedAddressPairs =
jsonNodeToAllowedAddressPair(allowedAddressPairJsonNode);
JsonNode securityGroupNode = vPortNodes.get("security_groups");
Collection<SecurityGroup> securityGroups =
jsonNodeToSecurityGroup(securityGroupNode);
strMap.putIfAbsent("name", name);
strMap.putIfAbsent("deviceOwner", deviceOwner);
strMap.putIfAbsent("bindingVnicType", bindingVnicType);
strMap.putIfAbsent("bindingVifType", bindingVifType);
strMap.putIfAbsent("bindingVifDetails", bindingVifDetails);
VirtualPort vPort = new DefaultVirtualPort(id, networkId, adminStateUp,
strMap, isState(state),
macAddress, tenantId,
deviceId, fixedIp,
bindingHostId,
allowedAddressPairs,
securityGroups);
vportMap.put(id, vPort);
return Collections.unmodifiableCollection(vportMap.values());
}
/**
* Returns a Object of the currently known infrastructure virtualPort.
*
* @param allowedAddressPairs the allowedAddressPairs json node
* @return a collection of allowedAddressPair
*/
public Collection<AllowedAddressPair> jsonNodeToAllowedAddressPair(JsonNode allowedAddressPairs) {
checkNotNull(allowedAddressPairs, JSON_NOT_NULL);
ConcurrentMap<Integer, AllowedAddressPair> allowMaps = Maps
.newConcurrentMap();
int i = 0;
for (JsonNode node : allowedAddressPairs) {
IpAddress ip = IpAddress.valueOf(node.get("ip_address").asText());
MacAddress mac = MacAddress.valueOf(node.get("mac_address")
.asText());
AllowedAddressPair allows = AllowedAddressPair
.allowedAddressPair(ip, mac);
allowMaps.put(i, allows);
i++;
}
log.debug("The jsonNode of allowedAddressPairallow is {}"
+ allowedAddressPairs.toString());
return Collections.unmodifiableCollection(allowMaps.values());
}
/**
* Returns a collection of virtualPorts.
*
* @param securityGroups the virtualPort jsonnode
* @return a collection of securityGroups
*/
public Collection<SecurityGroup> jsonNodeToSecurityGroup(JsonNode securityGroups) {
checkNotNull(securityGroups, JSON_NOT_NULL);
ConcurrentMap<Integer, SecurityGroup> securMaps = Maps
.newConcurrentMap();
int i = 0;
for (JsonNode node : securityGroups) {
SecurityGroup securityGroup = SecurityGroup.securityGroup(node
.get("security_group").asText());
securMaps.put(i, securityGroup);
i++;
}
return Collections.unmodifiableCollection(securMaps.values());
}
/**
* Returns a collection of fixedIps.
*
* @param fixedIpNode the fixedIp jsonnode
* @return a collection of SecurityGroup
*/
public FixedIp jsonNodeToFixedIps(JsonNode fixedIpNode) {
SubnetId subnetId = SubnetId.subnetId(fixedIpNode.get("subnet_id")
.asText());
IpAddress ipAddress = IpAddress.valueOf(fixedIpNode.get("ip_address")
.asText());
FixedIp fixedIps = FixedIp.fixedIp(subnetId, ipAddress);
return fixedIps;
}
/**
* Returns VirtualPort State.
*
* @param state the virtualport state
* @return the virtualPort state
*/
private State isState(String state) {
if (state.equals("ACTIVE")) {
return VirtualPort.State.ACTIVE;
} else {
return VirtualPort.State.DOWN;
}
}
/**
* Returns the specified item if that items is null; otherwise throws not
* found exception.
*
* @param item item to check
* @param <T> item type
* @param message not found message
* @return item if not null
* @throws org.onlab.util.ItemNotFoundException if item is null
*/
protected <T> T nullIsNotFound(T item, String message) {
if (item == null) {
throw new ItemNotFoundException(message);
}
return item;
}
}