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>