xuzhang
Committed by Gerrit Code Review

[ONOS-2158]The implementation of TenantNetwork.

Change-Id: I13715bcc7687bffe878eb6f4c5f7ec1b2489e944
......@@ -55,6 +55,7 @@
<module>flowanalyzer</module>
<module>vtnrsc</module>
<module>vtn</module>
<module>vtnweb</module>
</modules>
<properties>
......
/*
* 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.Objects;
/**
* Default implementation of TenantNetwork interface.
*/
public final class DefaultTenantNetwork implements TenantNetwork {
private final TenantNetworkId id;
private final String name;
private final boolean adminStateUp;
private final State state;
private final boolean shared;
private final Type type;
private final TenantId tenantId;
private final boolean routerExternal;
private final PhysicalNetwork physicalNetwork;
private final SegmentationId segmentationId;
/**
* Creates a neutronNetwork element attributed to the specified provider.
*
* @param id network identifier
* @param name the network name
* @param adminStateUp administrative state of the network
* @param state the network state
* @param shared indicates whether this network is shared across all
* tenants, By default, only administrative user can change this
* value
* @param tenantId tenant identifier
* @param routerExternal network routerExternal
* @param type the network type
* @param physicalNetwork physicalNetwork identifier
* @param segmentationId segmentation identifier
*/
public DefaultTenantNetwork(TenantNetworkId id, String name,
boolean adminStateUp, State state,
boolean shared, TenantId tenantId,
boolean routerExternal, Type type,
PhysicalNetwork physicalNetwork,
SegmentationId segmentationId) {
this.id = id;
this.name = name;
this.adminStateUp = adminStateUp;
this.state = state;
this.shared = shared;
this.type = type;
this.tenantId = tenantId;
this.routerExternal = routerExternal;
this.physicalNetwork = physicalNetwork;
this.segmentationId = segmentationId;
}
@Override
public TenantNetworkId id() {
return id;
}
@Override
public String name() {
return name;
}
@Override
public boolean adminStateUp() {
return adminStateUp;
}
@Override
public State state() {
return state;
}
@Override
public boolean shared() {
return shared;
}
@Override
public TenantId tenantId() {
return tenantId;
}
@Override
public boolean routerExternal() {
return routerExternal;
}
@Override
public Type type() {
return type;
}
@Override
public PhysicalNetwork physicalNetwork() {
return physicalNetwork;
}
@Override
public SegmentationId segmentationId() {
return segmentationId;
}
@Override
public int hashCode() {
return Objects.hash(id, name, adminStateUp, state, shared, tenantId,
routerExternal, type, physicalNetwork,
segmentationId);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof DefaultTenantNetwork) {
final DefaultTenantNetwork that = (DefaultTenantNetwork) obj;
return Objects.equals(this.id, that.id)
&& Objects.equals(this.name, that.name)
&& Objects.equals(this.adminStateUp, that.adminStateUp)
&& Objects.equals(this.state, that.state)
&& Objects.equals(this.shared, that.shared)
&& Objects.equals(this.tenantId, that.tenantId)
&& Objects.equals(this.routerExternal, that.routerExternal)
&& Objects.equals(this.type, that.type)
&& Objects.equals(this.physicalNetwork,
that.physicalNetwork)
&& Objects.equals(this.segmentationId, that.segmentationId);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("id", id).add("name", name)
.add("adminStateUp", adminStateUp).add("state", state)
.add("shared", shared).add("tenantId", tenantId)
.add("routeExternal", routerExternal).add("type", type)
.add("physicalNetwork", physicalNetwork)
.add("segmentationId", segmentationId).toString();
}
}
......@@ -20,39 +20,40 @@ import java.util.Objects;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Immutable representation of a Segmentation identity.
* Immutable representation of a Segmentation identifier.
*/
public final class SegmentationId {
private final String segmentationid;
private final String segmentationId;
// Public construction is prohibited
private SegmentationId(String segmentationid) {
checkNotNull(segmentationid, "Segmentationid cannot be null");
this.segmentationid = segmentationid;
private SegmentationId(String segmentationId) {
checkNotNull(segmentationId, "SegmentationId cannot be null");
this.segmentationId = segmentationId;
}
/**
* Creates a network id using the segmentationid.
* Creates a SegmentationId object.
*
* @param segmentationid network String
* @param segmentationId segmentation identifier
* @return SegmentationId
*/
public static SegmentationId segmentationID(String segmentationid) {
return new SegmentationId(segmentationid);
public static SegmentationId segmentationId(String segmentationId) {
return new SegmentationId(segmentationId);
}
/**
* Returns the segmentation identifier.
*
* @return segmentationid
* @return segmentationId
*/
public String segmentationid() {
return segmentationid;
public String segmentationId() {
return segmentationId;
}
@Override
public int hashCode() {
return Objects.hash(segmentationid);
return Objects.hash(segmentationId);
}
@Override
......@@ -63,14 +64,14 @@ public final class SegmentationId {
if (obj instanceof SegmentationId) {
final SegmentationId that = (SegmentationId) obj;
return this.getClass() == that.getClass()
&& Objects.equals(this.segmentationid, that.segmentationid);
&& Objects.equals(this.segmentationId, that.segmentationId);
}
return false;
}
@Override
public String toString() {
return segmentationid;
return segmentationId;
}
}
......
......@@ -71,14 +71,14 @@ public interface TenantNetwork {
* Returns the administrative state of the tenantNetwork,which is up(true)
* or down(false).
*
* @return network admin state up
* @return true or false
*/
boolean adminStateUp();
/**
* Returns the tenantNetwork state.
*
* @return tenantNetwork state
* @return tenant network state
*/
State state();
......@@ -86,7 +86,7 @@ public interface TenantNetwork {
* Indicates whether this tenantNetwork is shared across all tenants. By
* default,only administrative user can change this value.
*
* @return tenantNetwork shared
* @return true or false
*/
boolean shared();
......@@ -103,7 +103,7 @@ public interface TenantNetwork {
* Returns the routerExternal.Indicates whether this network is externally
* accessible.
*
* @return true if tenantNetwork router external
* @return true or false
*/
boolean routerExternal();
......
......@@ -23,35 +23,36 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/
public final class TenantNetworkId {
private final String networkid;
private final String networkId;
// Public construction is prohibited
private TenantNetworkId(String networkid) {
this.networkid = networkid;
private TenantNetworkId(String networkId) {
this.networkId = networkId;
}
/**
* Creates a tenantNetwork id using the networkid.
* Creates a TenantNetwork identifier.
*
* @param networkid tenantnetwork String
* @return NetworkId
* @param networkId tenantNetwork identify string
* @return the attached tenantNetwork identifier
*/
public static TenantNetworkId networkId(String networkid) {
checkNotNull(networkid, "Networkid cannot be null");
return new TenantNetworkId(networkid);
public static TenantNetworkId networkId(String networkId) {
checkNotNull(networkId, "Networkid cannot be null");
return new TenantNetworkId(networkId);
}
/**
* Returns tenantNetwork identifier.
*
* @return tenantNetworkid
* @return the tenantNetwork identifier
*/
public String networkid() {
return networkid;
public String networkId() {
return networkId;
}
@Override
public int hashCode() {
return Objects.hash(networkid);
return Objects.hash(networkId);
}
@Override
......@@ -62,14 +63,14 @@ public final class TenantNetworkId {
if (obj instanceof TenantNetworkId) {
final TenantNetworkId that = (TenantNetworkId) obj;
return this.getClass() == that.getClass()
&& Objects.equals(this.networkid, that.networkid);
&& Objects.equals(this.networkId, that.networkId);
}
return false;
}
@Override
public String toString() {
return networkid;
return networkId;
}
}
......
/*
* 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.tenantnetwork.impl;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.Collections;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
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.app.vtnrsc.TenantNetwork;
import org.onosproject.app.vtnrsc.TenantNetworkId;
import org.onosproject.app.vtnrsc.tenantnetwork.TenantNetworkService;
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;
/**
* Provides implementation of the tenantNetworkService.
*/
@Component(immediate = true)
@Service
public class TenantNetworkManager implements TenantNetworkService {
private static final String NETWORK_ID_NULL = "Network ID cannot be null";
private static final String NETWORK_NOT_NULL = "Network ID cannot be null";
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected StorageService storageService;
private EventuallyConsistentMap<TenantNetworkId, TenantNetwork> networkIdAsKeyStore;
private final Logger log = getLogger(getClass());
@Activate
public void activate() {
KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
.register(MultiValuedTimestamp.class);
networkIdAsKeyStore = storageService
.<TenantNetworkId, TenantNetwork>eventuallyConsistentMapBuilder()
.withName("all_network").withSerializer(serializer)
.withTimestampProvider((k, v) -> new WallClockTimestamp())
.build();
log.info("Started");
}
@Deactivate
public void deactivate() {
networkIdAsKeyStore.destroy();
log.info("Stopped");
}
@Override
public boolean exists(TenantNetworkId networkId) {
checkNotNull(networkId, NETWORK_ID_NULL);
return networkIdAsKeyStore.containsKey(networkId);
}
@Override
public int getNetworkCount() {
return networkIdAsKeyStore.size();
}
@Override
public Iterable<TenantNetwork> getNetworks() {
return Collections.unmodifiableCollection(networkIdAsKeyStore.values());
}
@Override
public TenantNetwork getNetwork(TenantNetworkId networkId) {
checkNotNull(networkId, NETWORK_ID_NULL);
return networkIdAsKeyStore.get(networkId);
}
@Override
public boolean createNetworks(Iterable<TenantNetwork> networks) {
checkNotNull(networks, NETWORK_NOT_NULL);
for (TenantNetwork network : networks) {
networkIdAsKeyStore.put(network.id(), network);
if (!networkIdAsKeyStore.containsKey(network.id())) {
log.debug("the network created failed which identifier was {}", network.id()
.toString());
return false;
}
}
return true;
}
@Override
public boolean updateNetworks(Iterable<TenantNetwork> networks) {
checkNotNull(networks, NETWORK_NOT_NULL);
for (TenantNetwork network : networks) {
if (!networkIdAsKeyStore.containsKey(network.id())) {
log.debug("the tenantNetwork did not exist whose identifier was {} ",
network.id().toString());
return false;
}
networkIdAsKeyStore.put(network.id(), network);
if (network.equals(networkIdAsKeyStore.get(network.id()))) {
log.debug("the network updated failed whose identifier was {} ",
network.id().toString());
return false;
}
}
return true;
}
@Override
public boolean removeNetworks(Iterable<TenantNetworkId> networkIds) {
checkNotNull(networkIds, NETWORK_NOT_NULL);
for (TenantNetworkId networkId : networkIds) {
networkIdAsKeyStore.remove(networkId);
if (networkIdAsKeyStore.containsKey(networkId)) {
log.debug("the network removed failed whose identifier was {}",
networkId.toString());
return false;
}
}
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.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.app.vtnrsc.TenantNetwork;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* TenantNetwork JSON codec.
*/
public final class TenantNetworkCodec extends JsonCodec<TenantNetwork> {
@Override
public ObjectNode encode(TenantNetwork network, CodecContext context) {
checkNotNull(network, "Network cannot be null");
ObjectNode result = context.mapper().createObjectNode()
.put("id", network.id().toString())
.put("name", network.name().toString())
.put("admin_state_up", network.adminStateUp())
.put("status", "" + network.state())
.put("shared", network.shared())
.put("tenant_id", network.tenantId().toString())
.put("router:external", network.routerExternal())
.put("provider:network_type", "" + network.type())
.put("provider:physical_network", network.physicalNetwork().toString())
.put("provider:segmentation_id", network.segmentationId().toString());
return result;
}
}
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.onosproject</groupId>
<artifactId>onos-apps</artifactId>
<version>1.3.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>onos-app-vtnweb</artifactId>
<packaging>bundle</packaging>
<properties>
<onos.app.name>org.onosproject.vtnweb</onos.app.name>
<web.context>/onos/vtn</web.context>
</properties>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-api</artifactId>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-app-vtnrsc</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<_wab>src/main/webapp/</_wab>
<Bundle-SymbolicName>
${project.groupId}.${project.artifactId}
</Bundle-SymbolicName>
<Import-Package>
org.slf4j,
org.osgi.framework,
javax.ws.rs,
javax.ws.rs.core,
com.sun.jersey.api.core,
com.sun.jersey.spi.container.servlet,
com.sun.jersey.server.impl.container.servlet,
com.fasterxml.jackson.databind,
com.fasterxml.jackson.databind.node,
com.fasterxml.jackson.core,
org.apache.karaf.shell.commands,
org.apache.commons.lang.math.*,
com.google.common.*,
org.onlab.packet.*,
org.onlab.rest.*,
org.onosproject.*,
org.onlab.util.*,
org.jboss.netty.util.*
</Import-Package>
<Web-ContextPath>${web.context}</Web-ContextPath>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="ONOS" version="2.5">
<display-name>VTNRSC REST API v1.0</display-name>
<servlet>
<servlet-name>JAX-RS Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
<param-value>com.sun.jersey.api.core.ClassNamesResourceConfig</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.classnames</param-name>
<param-value>
org.onosproject.vtnweb.resources.TenantNetworkWebResource
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>