xuzhang
Committed by Gerrit Code Review

[ONOS-2158]The implementation of TenantNetwork.

Change-Id: I13715bcc7687bffe878eb6f4c5f7ec1b2489e944
...@@ -55,6 +55,7 @@ ...@@ -55,6 +55,7 @@
55 <module>flowanalyzer</module> 55 <module>flowanalyzer</module>
56 <module>vtnrsc</module> 56 <module>vtnrsc</module>
57 <module>vtn</module> 57 <module>vtn</module>
58 + <module>vtnweb</module>
58 </modules> 59 </modules>
59 60
60 <properties> 61 <properties>
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.app.vtnrsc;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +
20 +import java.util.Objects;
21 +
22 +/**
23 + * Default implementation of TenantNetwork interface.
24 + */
25 +public final class DefaultTenantNetwork implements TenantNetwork {
26 + private final TenantNetworkId id;
27 + private final String name;
28 + private final boolean adminStateUp;
29 + private final State state;
30 + private final boolean shared;
31 + private final Type type;
32 + private final TenantId tenantId;
33 + private final boolean routerExternal;
34 + private final PhysicalNetwork physicalNetwork;
35 + private final SegmentationId segmentationId;
36 +
37 + /**
38 + * Creates a neutronNetwork element attributed to the specified provider.
39 + *
40 + * @param id network identifier
41 + * @param name the network name
42 + * @param adminStateUp administrative state of the network
43 + * @param state the network state
44 + * @param shared indicates whether this network is shared across all
45 + * tenants, By default, only administrative user can change this
46 + * value
47 + * @param tenantId tenant identifier
48 + * @param routerExternal network routerExternal
49 + * @param type the network type
50 + * @param physicalNetwork physicalNetwork identifier
51 + * @param segmentationId segmentation identifier
52 + */
53 + public DefaultTenantNetwork(TenantNetworkId id, String name,
54 + boolean adminStateUp, State state,
55 + boolean shared, TenantId tenantId,
56 + boolean routerExternal, Type type,
57 + PhysicalNetwork physicalNetwork,
58 + SegmentationId segmentationId) {
59 + this.id = id;
60 + this.name = name;
61 + this.adminStateUp = adminStateUp;
62 + this.state = state;
63 + this.shared = shared;
64 + this.type = type;
65 + this.tenantId = tenantId;
66 + this.routerExternal = routerExternal;
67 + this.physicalNetwork = physicalNetwork;
68 + this.segmentationId = segmentationId;
69 + }
70 +
71 + @Override
72 + public TenantNetworkId id() {
73 + return id;
74 + }
75 +
76 + @Override
77 + public String name() {
78 + return name;
79 + }
80 +
81 + @Override
82 + public boolean adminStateUp() {
83 + return adminStateUp;
84 + }
85 +
86 + @Override
87 + public State state() {
88 + return state;
89 + }
90 +
91 + @Override
92 + public boolean shared() {
93 + return shared;
94 + }
95 +
96 + @Override
97 + public TenantId tenantId() {
98 + return tenantId;
99 + }
100 +
101 + @Override
102 + public boolean routerExternal() {
103 + return routerExternal;
104 + }
105 +
106 + @Override
107 + public Type type() {
108 + return type;
109 + }
110 +
111 + @Override
112 + public PhysicalNetwork physicalNetwork() {
113 + return physicalNetwork;
114 + }
115 +
116 + @Override
117 + public SegmentationId segmentationId() {
118 + return segmentationId;
119 + }
120 +
121 + @Override
122 + public int hashCode() {
123 + return Objects.hash(id, name, adminStateUp, state, shared, tenantId,
124 + routerExternal, type, physicalNetwork,
125 + segmentationId);
126 + }
127 +
128 + @Override
129 + public boolean equals(Object obj) {
130 + if (this == obj) {
131 + return true;
132 + }
133 + if (obj instanceof DefaultTenantNetwork) {
134 + final DefaultTenantNetwork that = (DefaultTenantNetwork) obj;
135 + return Objects.equals(this.id, that.id)
136 + && Objects.equals(this.name, that.name)
137 + && Objects.equals(this.adminStateUp, that.adminStateUp)
138 + && Objects.equals(this.state, that.state)
139 + && Objects.equals(this.shared, that.shared)
140 + && Objects.equals(this.tenantId, that.tenantId)
141 + && Objects.equals(this.routerExternal, that.routerExternal)
142 + && Objects.equals(this.type, that.type)
143 + && Objects.equals(this.physicalNetwork,
144 + that.physicalNetwork)
145 + && Objects.equals(this.segmentationId, that.segmentationId);
146 + }
147 + return false;
148 + }
149 +
150 + @Override
151 + public String toString() {
152 + return toStringHelper(this).add("id", id).add("name", name)
153 + .add("adminStateUp", adminStateUp).add("state", state)
154 + .add("shared", shared).add("tenantId", tenantId)
155 + .add("routeExternal", routerExternal).add("type", type)
156 + .add("physicalNetwork", physicalNetwork)
157 + .add("segmentationId", segmentationId).toString();
158 + }
159 +
160 +}
...@@ -20,39 +20,40 @@ import java.util.Objects; ...@@ -20,39 +20,40 @@ import java.util.Objects;
20 import static com.google.common.base.Preconditions.checkNotNull; 20 import static com.google.common.base.Preconditions.checkNotNull;
21 21
22 /** 22 /**
23 - * Immutable representation of a Segmentation identity. 23 + * Immutable representation of a Segmentation identifier.
24 */ 24 */
25 public final class SegmentationId { 25 public final class SegmentationId {
26 26
27 - private final String segmentationid; 27 + private final String segmentationId;
28 28
29 // Public construction is prohibited 29 // Public construction is prohibited
30 - private SegmentationId(String segmentationid) { 30 + private SegmentationId(String segmentationId) {
31 - checkNotNull(segmentationid, "Segmentationid cannot be null"); 31 + checkNotNull(segmentationId, "SegmentationId cannot be null");
32 - this.segmentationid = segmentationid; 32 + this.segmentationId = segmentationId;
33 } 33 }
34 34
35 /** 35 /**
36 - * Creates a network id using the segmentationid. 36 + * Creates a SegmentationId object.
37 * 37 *
38 - * @param segmentationid network String 38 + * @param segmentationId segmentation identifier
39 * @return SegmentationId 39 * @return SegmentationId
40 */ 40 */
41 - public static SegmentationId segmentationID(String segmentationid) { 41 + public static SegmentationId segmentationId(String segmentationId) {
42 - return new SegmentationId(segmentationid); 42 + return new SegmentationId(segmentationId);
43 } 43 }
44 44
45 /** 45 /**
46 + * Returns the segmentation identifier.
46 * 47 *
47 - * @return segmentationid 48 + * @return segmentationId
48 */ 49 */
49 - public String segmentationid() { 50 + public String segmentationId() {
50 - return segmentationid; 51 + return segmentationId;
51 } 52 }
52 53
53 @Override 54 @Override
54 public int hashCode() { 55 public int hashCode() {
55 - return Objects.hash(segmentationid); 56 + return Objects.hash(segmentationId);
56 } 57 }
57 58
58 @Override 59 @Override
...@@ -63,14 +64,14 @@ public final class SegmentationId { ...@@ -63,14 +64,14 @@ public final class SegmentationId {
63 if (obj instanceof SegmentationId) { 64 if (obj instanceof SegmentationId) {
64 final SegmentationId that = (SegmentationId) obj; 65 final SegmentationId that = (SegmentationId) obj;
65 return this.getClass() == that.getClass() 66 return this.getClass() == that.getClass()
66 - && Objects.equals(this.segmentationid, that.segmentationid); 67 + && Objects.equals(this.segmentationId, that.segmentationId);
67 } 68 }
68 return false; 69 return false;
69 } 70 }
70 71
71 @Override 72 @Override
72 public String toString() { 73 public String toString() {
73 - return segmentationid; 74 + return segmentationId;
74 } 75 }
75 76
76 } 77 }
......
...@@ -71,14 +71,14 @@ public interface TenantNetwork { ...@@ -71,14 +71,14 @@ public interface TenantNetwork {
71 * Returns the administrative state of the tenantNetwork,which is up(true) 71 * Returns the administrative state of the tenantNetwork,which is up(true)
72 * or down(false). 72 * or down(false).
73 * 73 *
74 - * @return network admin state up 74 + * @return true or false
75 */ 75 */
76 boolean adminStateUp(); 76 boolean adminStateUp();
77 77
78 /** 78 /**
79 * Returns the tenantNetwork state. 79 * Returns the tenantNetwork state.
80 * 80 *
81 - * @return tenantNetwork state 81 + * @return tenant network state
82 */ 82 */
83 State state(); 83 State state();
84 84
...@@ -86,7 +86,7 @@ public interface TenantNetwork { ...@@ -86,7 +86,7 @@ public interface TenantNetwork {
86 * Indicates whether this tenantNetwork is shared across all tenants. By 86 * Indicates whether this tenantNetwork is shared across all tenants. By
87 * default,only administrative user can change this value. 87 * default,only administrative user can change this value.
88 * 88 *
89 - * @return tenantNetwork shared 89 + * @return true or false
90 */ 90 */
91 boolean shared(); 91 boolean shared();
92 92
...@@ -103,7 +103,7 @@ public interface TenantNetwork { ...@@ -103,7 +103,7 @@ public interface TenantNetwork {
103 * Returns the routerExternal.Indicates whether this network is externally 103 * Returns the routerExternal.Indicates whether this network is externally
104 * accessible. 104 * accessible.
105 * 105 *
106 - * @return true if tenantNetwork router external 106 + * @return true or false
107 */ 107 */
108 boolean routerExternal(); 108 boolean routerExternal();
109 109
......
...@@ -23,35 +23,36 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -23,35 +23,36 @@ import static com.google.common.base.Preconditions.checkNotNull;
23 */ 23 */
24 public final class TenantNetworkId { 24 public final class TenantNetworkId {
25 25
26 - private final String networkid; 26 + private final String networkId;
27 27
28 // Public construction is prohibited 28 // Public construction is prohibited
29 - private TenantNetworkId(String networkid) { 29 + private TenantNetworkId(String networkId) {
30 - this.networkid = networkid; 30 + this.networkId = networkId;
31 } 31 }
32 32
33 /** 33 /**
34 - * Creates a tenantNetwork id using the networkid. 34 + * Creates a TenantNetwork identifier.
35 * 35 *
36 - * @param networkid tenantnetwork String 36 + * @param networkId tenantNetwork identify string
37 - * @return NetworkId 37 + * @return the attached tenantNetwork identifier
38 */ 38 */
39 - public static TenantNetworkId networkId(String networkid) { 39 + public static TenantNetworkId networkId(String networkId) {
40 - checkNotNull(networkid, "Networkid cannot be null"); 40 + checkNotNull(networkId, "Networkid cannot be null");
41 - return new TenantNetworkId(networkid); 41 + return new TenantNetworkId(networkId);
42 } 42 }
43 43
44 /** 44 /**
45 + * Returns tenantNetwork identifier.
45 * 46 *
46 - * @return tenantNetworkid 47 + * @return the tenantNetwork identifier
47 */ 48 */
48 - public String networkid() { 49 + public String networkId() {
49 - return networkid; 50 + return networkId;
50 } 51 }
51 52
52 @Override 53 @Override
53 public int hashCode() { 54 public int hashCode() {
54 - return Objects.hash(networkid); 55 + return Objects.hash(networkId);
55 } 56 }
56 57
57 @Override 58 @Override
...@@ -62,14 +63,14 @@ public final class TenantNetworkId { ...@@ -62,14 +63,14 @@ public final class TenantNetworkId {
62 if (obj instanceof TenantNetworkId) { 63 if (obj instanceof TenantNetworkId) {
63 final TenantNetworkId that = (TenantNetworkId) obj; 64 final TenantNetworkId that = (TenantNetworkId) obj;
64 return this.getClass() == that.getClass() 65 return this.getClass() == that.getClass()
65 - && Objects.equals(this.networkid, that.networkid); 66 + && Objects.equals(this.networkId, that.networkId);
66 } 67 }
67 return false; 68 return false;
68 } 69 }
69 70
70 @Override 71 @Override
71 public String toString() { 72 public String toString() {
72 - return networkid; 73 + return networkId;
73 } 74 }
74 75
75 } 76 }
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.app.vtnrsc.tenantnetwork.impl;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +import static org.slf4j.LoggerFactory.getLogger;
20 +
21 +import java.util.Collections;
22 +
23 +import org.apache.felix.scr.annotations.Activate;
24 +import org.apache.felix.scr.annotations.Component;
25 +import org.apache.felix.scr.annotations.Deactivate;
26 +import org.apache.felix.scr.annotations.Reference;
27 +import org.apache.felix.scr.annotations.ReferenceCardinality;
28 +import org.apache.felix.scr.annotations.Service;
29 +import org.onlab.util.KryoNamespace;
30 +import org.onosproject.app.vtnrsc.TenantNetwork;
31 +import org.onosproject.app.vtnrsc.TenantNetworkId;
32 +import org.onosproject.app.vtnrsc.tenantnetwork.TenantNetworkService;
33 +import org.onosproject.store.service.EventuallyConsistentMap;
34 +import org.onosproject.store.service.MultiValuedTimestamp;
35 +import org.onosproject.store.service.StorageService;
36 +import org.onosproject.store.service.WallClockTimestamp;
37 +import org.slf4j.Logger;
38 +
39 +/**
40 + * Provides implementation of the tenantNetworkService.
41 + */
42 +@Component(immediate = true)
43 +@Service
44 +public class TenantNetworkManager implements TenantNetworkService {
45 +
46 + private static final String NETWORK_ID_NULL = "Network ID cannot be null";
47 + private static final String NETWORK_NOT_NULL = "Network ID cannot be null";
48 +
49 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 + protected StorageService storageService;
51 + private EventuallyConsistentMap<TenantNetworkId, TenantNetwork> networkIdAsKeyStore;
52 + private final Logger log = getLogger(getClass());
53 +
54 + @Activate
55 + public void activate() {
56 + KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
57 + .register(MultiValuedTimestamp.class);
58 + networkIdAsKeyStore = storageService
59 + .<TenantNetworkId, TenantNetwork>eventuallyConsistentMapBuilder()
60 + .withName("all_network").withSerializer(serializer)
61 + .withTimestampProvider((k, v) -> new WallClockTimestamp())
62 + .build();
63 + log.info("Started");
64 + }
65 +
66 + @Deactivate
67 + public void deactivate() {
68 + networkIdAsKeyStore.destroy();
69 + log.info("Stopped");
70 + }
71 +
72 + @Override
73 + public boolean exists(TenantNetworkId networkId) {
74 + checkNotNull(networkId, NETWORK_ID_NULL);
75 + return networkIdAsKeyStore.containsKey(networkId);
76 + }
77 +
78 + @Override
79 + public int getNetworkCount() {
80 + return networkIdAsKeyStore.size();
81 + }
82 +
83 + @Override
84 + public Iterable<TenantNetwork> getNetworks() {
85 + return Collections.unmodifiableCollection(networkIdAsKeyStore.values());
86 + }
87 +
88 + @Override
89 + public TenantNetwork getNetwork(TenantNetworkId networkId) {
90 + checkNotNull(networkId, NETWORK_ID_NULL);
91 + return networkIdAsKeyStore.get(networkId);
92 + }
93 +
94 + @Override
95 + public boolean createNetworks(Iterable<TenantNetwork> networks) {
96 + checkNotNull(networks, NETWORK_NOT_NULL);
97 + for (TenantNetwork network : networks) {
98 + networkIdAsKeyStore.put(network.id(), network);
99 + if (!networkIdAsKeyStore.containsKey(network.id())) {
100 + log.debug("the network created failed which identifier was {}", network.id()
101 + .toString());
102 + return false;
103 + }
104 + }
105 + return true;
106 + }
107 +
108 + @Override
109 + public boolean updateNetworks(Iterable<TenantNetwork> networks) {
110 + checkNotNull(networks, NETWORK_NOT_NULL);
111 + for (TenantNetwork network : networks) {
112 + if (!networkIdAsKeyStore.containsKey(network.id())) {
113 + log.debug("the tenantNetwork did not exist whose identifier was {} ",
114 + network.id().toString());
115 + return false;
116 + }
117 +
118 + networkIdAsKeyStore.put(network.id(), network);
119 +
120 + if (network.equals(networkIdAsKeyStore.get(network.id()))) {
121 + log.debug("the network updated failed whose identifier was {} ",
122 + network.id().toString());
123 + return false;
124 + }
125 +
126 + }
127 + return true;
128 + }
129 +
130 + @Override
131 + public boolean removeNetworks(Iterable<TenantNetworkId> networkIds) {
132 + checkNotNull(networkIds, NETWORK_NOT_NULL);
133 + for (TenantNetworkId networkId : networkIds) {
134 + networkIdAsKeyStore.remove(networkId);
135 + if (networkIdAsKeyStore.containsKey(networkId)) {
136 + log.debug("the network removed failed whose identifier was {}",
137 + networkId.toString());
138 + return false;
139 + }
140 + }
141 + return true;
142 + }
143 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.app.vtnrsc.web;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +
20 +import org.onosproject.codec.CodecContext;
21 +import org.onosproject.codec.JsonCodec;
22 +import org.onosproject.app.vtnrsc.TenantNetwork;
23 +
24 +import com.fasterxml.jackson.databind.node.ObjectNode;
25 +
26 +/**
27 + * TenantNetwork JSON codec.
28 + */
29 +public final class TenantNetworkCodec extends JsonCodec<TenantNetwork> {
30 +
31 + @Override
32 + public ObjectNode encode(TenantNetwork network, CodecContext context) {
33 + checkNotNull(network, "Network cannot be null");
34 + ObjectNode result = context.mapper().createObjectNode()
35 + .put("id", network.id().toString())
36 + .put("name", network.name().toString())
37 + .put("admin_state_up", network.adminStateUp())
38 + .put("status", "" + network.state())
39 + .put("shared", network.shared())
40 + .put("tenant_id", network.tenantId().toString())
41 + .put("router:external", network.routerExternal())
42 + .put("provider:network_type", "" + network.type())
43 + .put("provider:physical_network", network.physicalNetwork().toString())
44 + .put("provider:segmentation_id", network.segmentationId().toString());
45 + return result;
46 + }
47 +}
1 +<?xml version="1.0"?>
2 +<project
3 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4 + xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5 + <modelVersion>4.0.0</modelVersion>
6 + <parent>
7 + <groupId>org.onosproject</groupId>
8 + <artifactId>onos-apps</artifactId>
9 + <version>1.3.0-SNAPSHOT</version>
10 + <relativePath>../pom.xml</relativePath>
11 + </parent>
12 +
13 +
14 + <artifactId>onos-app-vtnweb</artifactId>
15 + <packaging>bundle</packaging>
16 + <properties>
17 + <onos.app.name>org.onosproject.vtnweb</onos.app.name>
18 + <web.context>/onos/vtn</web.context>
19 + </properties>
20 +
21 + <dependencies>
22 + <dependency>
23 + <groupId>javax.ws.rs</groupId>
24 + <artifactId>jsr311-api</artifactId>
25 + <version>1.1.1</version>
26 + </dependency>
27 + <dependency>
28 + <groupId>org.onosproject</groupId>
29 + <artifactId>onos-api</artifactId>
30 + </dependency>
31 + <dependency>
32 + <groupId>org.onosproject</groupId>
33 + <artifactId>onos-app-vtnrsc</artifactId>
34 + <version>${project.version}</version>
35 + </dependency>
36 + </dependencies>
37 + <build>
38 + <plugins>
39 + <plugin>
40 + <groupId>org.apache.felix</groupId>
41 + <artifactId>maven-bundle-plugin</artifactId>
42 + <extensions>true</extensions>
43 + <configuration>
44 + <instructions>
45 + <_wab>src/main/webapp/</_wab>
46 + <Bundle-SymbolicName>
47 + ${project.groupId}.${project.artifactId}
48 + </Bundle-SymbolicName>
49 + <Import-Package>
50 + org.slf4j,
51 + org.osgi.framework,
52 + javax.ws.rs,
53 + javax.ws.rs.core,
54 + com.sun.jersey.api.core,
55 + com.sun.jersey.spi.container.servlet,
56 + com.sun.jersey.server.impl.container.servlet,
57 + com.fasterxml.jackson.databind,
58 + com.fasterxml.jackson.databind.node,
59 + com.fasterxml.jackson.core,
60 + org.apache.karaf.shell.commands,
61 + org.apache.commons.lang.math.*,
62 + com.google.common.*,
63 + org.onlab.packet.*,
64 + org.onlab.rest.*,
65 + org.onosproject.*,
66 + org.onlab.util.*,
67 + org.jboss.netty.util.*
68 + </Import-Package>
69 + <Web-ContextPath>${web.context}</Web-ContextPath>
70 + </instructions>
71 + </configuration>
72 + </plugin>
73 + </plugins>
74 + </build>
75 +
76 +</project>
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.vtnweb.resources;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
20 +import static javax.ws.rs.core.Response.Status.OK;
21 +
22 +import java.io.InputStream;
23 +import java.util.Collections;
24 +import java.util.HashSet;
25 +import java.util.Iterator;
26 +import java.util.Set;
27 +import java.util.concurrent.ConcurrentMap;
28 +
29 +import javax.ws.rs.Consumes;
30 +import javax.ws.rs.DELETE;
31 +import javax.ws.rs.GET;
32 +import javax.ws.rs.POST;
33 +import javax.ws.rs.PUT;
34 +import javax.ws.rs.Path;
35 +import javax.ws.rs.PathParam;
36 +import javax.ws.rs.Produces;
37 +import javax.ws.rs.QueryParam;
38 +import javax.ws.rs.core.MediaType;
39 +import javax.ws.rs.core.Response;
40 +
41 +import org.onlab.util.ItemNotFoundException;
42 +import org.onosproject.app.vtnrsc.DefaultTenantNetwork;
43 +import org.onosproject.app.vtnrsc.PhysicalNetwork;
44 +import org.onosproject.app.vtnrsc.SegmentationId;
45 +import org.onosproject.app.vtnrsc.TenantId;
46 +import org.onosproject.app.vtnrsc.TenantNetwork;
47 +import org.onosproject.app.vtnrsc.TenantNetwork.State;
48 +import org.onosproject.app.vtnrsc.TenantNetwork.Type;
49 +import org.onosproject.app.vtnrsc.TenantNetworkId;
50 +import org.onosproject.app.vtnrsc.tenantnetwork.TenantNetworkService;
51 +import org.onosproject.app.vtnrsc.web.TenantNetworkCodec;
52 +import org.onosproject.rest.AbstractWebResource;
53 +import org.slf4j.Logger;
54 +import org.slf4j.LoggerFactory;
55 +
56 +import com.fasterxml.jackson.databind.JsonNode;
57 +import com.fasterxml.jackson.databind.ObjectMapper;
58 +import com.fasterxml.jackson.databind.node.ObjectNode;
59 +import com.google.common.collect.Maps;
60 +
61 +/**
62 + * REST resource for interacting with the inventory of networks.
63 + */
64 +@Path("networks")
65 +public class TenantNetworkWebResource extends AbstractWebResource {
66 + public static final String NETWORK_NOT_FOUND = "Network is not found";
67 + public static final String NETWORK_ID_EXIST = "Network id is existed";
68 + public static final String NETWORK_ID_NOT_EXIST = "Network id is not existed";
69 + public static final String CREATE_NETWORK = "create network";
70 + public static final String UPDATE_NETWORK = "update network";
71 + public static final String DELETE_NETWORK = "delete network";
72 + public static final String JSON_NOT_NULL = "JsonNode can not be null";
73 +
74 + protected static final Logger log = LoggerFactory
75 + .getLogger(TenantNetworkWebResource.class);
76 + private final ConcurrentMap<TenantNetworkId, TenantNetwork> networksMap = Maps
77 + .newConcurrentMap();
78 +
79 + @GET
80 + @Produces({ MediaType.APPLICATION_JSON })
81 + public Response getNetworks(@QueryParam("id") String queryId,
82 + @QueryParam("name") String queryName,
83 + @QueryParam("admin_state_up") String queryadminStateUp,
84 + @QueryParam("status") String querystate,
85 + @QueryParam("shared") String queryshared,
86 + @QueryParam("tenant_id") String querytenantId,
87 + @QueryParam("router:external") String routerExternal,
88 + @QueryParam("provider:network_type") String type,
89 + @QueryParam("provider:physical_network") String physicalNetwork,
90 + @QueryParam("provider:segmentation_id") String segmentationId) {
91 + Iterable<TenantNetwork> networks = get(TenantNetworkService.class)
92 + .getNetworks();
93 + Iterator<TenantNetwork> networkors = networks.iterator();
94 + while (networkors.hasNext()) {
95 + TenantNetwork network = networkors.next();
96 + if ((queryId == null || queryId.equals(network.id().toString()))
97 + && (queryName == null || queryName.equals(network.name()
98 + .toString()))
99 + && (queryadminStateUp == null || queryadminStateUp
100 + .equals(network.adminStateUp()))
101 + && (querystate == null || querystate.equals(network.state()
102 + .toString()))
103 + && (queryshared == null || queryshared.equals(network
104 + .shared()))
105 + && (querytenantId == null || querytenantId.equals(network
106 + .tenantId().toString()))
107 + && (routerExternal == null || routerExternal.equals(network
108 + .routerExternal()))
109 + && (type == null || type.equals(network.type()))
110 + && (physicalNetwork == null || physicalNetwork
111 + .equals(network.physicalNetwork()))
112 + && (segmentationId == null || segmentationId.equals(network
113 + .segmentationId()))) {
114 + networksMap.putIfAbsent(network.id(), network);
115 + }
116 + }
117 + networks = Collections.unmodifiableCollection(networksMap.values());
118 + ObjectNode result = new ObjectMapper().createObjectNode();
119 + result.set("networks", new TenantNetworkCodec().encode(networks, this));
120 +
121 + return ok(result.toString()).build();
122 + }
123 +
124 + private State isState(String state) {
125 + if (state.equals("ACTIVE")) {
126 + return TenantNetwork.State.ACTIVE;
127 + } else if (state.equals("BUILD")) {
128 + return TenantNetwork.State.BUILD;
129 + } else if (state.equals("DOWN")) {
130 + return TenantNetwork.State.DOWN;
131 + } else if (state.equals("ERROR")) {
132 + return TenantNetwork.State.ERROR;
133 + } else {
134 + return null;
135 + }
136 + }
137 +
138 + private Type isType(String type) {
139 + if (type.equals("LOCAL")) {
140 + return TenantNetwork.Type.LOCAL;
141 + } else {
142 + return null;
143 + }
144 + }
145 +
146 + @GET
147 + @Path("{id}")
148 + @Produces({ MediaType.APPLICATION_JSON })
149 + public Response getNetwork(@PathParam("id") String id) {
150 +
151 + if (!get(TenantNetworkService.class).exists(TenantNetworkId
152 + .networkId(id))) {
153 + return ok("The tenantNetwork does not exists").build();
154 + }
155 + TenantNetwork network = nullIsNotFound(get(TenantNetworkService.class)
156 + .getNetwork(TenantNetworkId.networkId(id)), NETWORK_NOT_FOUND);
157 + ObjectNode result = new ObjectMapper().createObjectNode();
158 + result.set("network", new TenantNetworkCodec().encode(network, this));
159 +
160 + return ok(result.toString()).build();
161 +
162 + }
163 +
164 + @POST
165 + @Produces(MediaType.APPLICATION_JSON)
166 + @Consumes(MediaType.APPLICATION_JSON)
167 + public Response createNetworks(InputStream input) {
168 + try {
169 + ObjectMapper mapper = new ObjectMapper();
170 + JsonNode cfg = mapper.readTree(input);
171 + JsonNode nodes = null;
172 + Iterable<TenantNetwork> networks = null;
173 + if (cfg.get("network") != null) {
174 + nodes = cfg.get("network");
175 + if (nodes.isArray()) {
176 + networks = changeJson2objs(nodes);
177 + } else {
178 + networks = changeJson2obj(CREATE_NETWORK, null, nodes);
179 + }
180 + } else if (cfg.get("networks") != null) {
181 + nodes = cfg.get("networks");
182 + networks = changeJson2objs(nodes);
183 + }
184 + Boolean issuccess = nullIsNotFound((get(TenantNetworkService.class)
185 + .createNetworks(networks)),
186 + NETWORK_NOT_FOUND);
187 +
188 + if (!issuccess) {
189 + return Response.status(INTERNAL_SERVER_ERROR)
190 + .entity(NETWORK_ID_EXIST).build();
191 + }
192 + return Response.status(OK).entity(issuccess.toString()).build();
193 + } catch (Exception e) {
194 + log.error("Creates tenantNetwork exception {}.", e.toString());
195 + return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString())
196 + .build();
197 + }
198 + }
199 +
200 + @PUT
201 + @Path("{id}")
202 + @Produces(MediaType.APPLICATION_JSON)
203 + @Consumes(MediaType.APPLICATION_JSON)
204 + public Response updateNetworks(@PathParam("id") String id, InputStream input) {
205 + try {
206 + ObjectMapper mapper = new ObjectMapper();
207 + JsonNode cfg = mapper.readTree(input);
208 + JsonNode nodes = null;
209 + Iterable<TenantNetwork> networks = null;
210 + if (cfg.get("network") != null) {
211 + nodes = cfg.get("network");
212 + if (nodes.isArray()) {
213 + networks = changeJson2objs(nodes);
214 + } else {
215 + networks = changeJson2obj(UPDATE_NETWORK,
216 + TenantNetworkId.networkId(id),
217 + nodes);
218 + }
219 + } else if (cfg.get("networks") != null) {
220 + nodes = cfg.get("networks");
221 + networks = changeJson2objs(nodes);
222 + }
223 + Boolean issuccess = nullIsNotFound((get(TenantNetworkService.class)
224 + .updateNetworks(networks)),
225 + NETWORK_NOT_FOUND);
226 + if (!issuccess) {
227 + return Response.status(INTERNAL_SERVER_ERROR)
228 + .entity(NETWORK_ID_NOT_EXIST).build();
229 + }
230 + return Response.status(OK).entity(issuccess.toString()).build();
231 + } catch (Exception e) {
232 + log.error("Updates tenantNetwork failed because of exception {}.",
233 + e.toString());
234 + return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString())
235 + .build();
236 + }
237 + }
238 +
239 + @DELETE
240 + @Path("{id}")
241 + public Response deleteNetworks(@PathParam("id") String id) {
242 + log.debug("Deletes network by identifier {}.", id);
243 + Set<TenantNetworkId> networkSet = new HashSet<TenantNetworkId>();
244 + networkSet.add(TenantNetworkId.networkId(id));
245 + Boolean issuccess = nullIsNotFound(get(TenantNetworkService.class)
246 + .removeNetworks(networkSet), NETWORK_NOT_FOUND);
247 + if (!issuccess) {
248 + log.debug("Network identifier {} is not existed", id);
249 + return Response.status(INTERNAL_SERVER_ERROR)
250 + .entity(NETWORK_ID_NOT_EXIST).build();
251 + }
252 + return Response.status(OK).entity(issuccess.toString()).build();
253 + }
254 +
255 + /**
256 + * Returns a collection of tenantNetworks.
257 + *
258 + * @param flag the flag
259 + * @param networkId network identifier
260 + * @param node the network json node
261 + * @return a collection of tenantNetworks
262 + */
263 + public Iterable<TenantNetwork> changeJson2obj(String flag,
264 + TenantNetworkId networkId,
265 + JsonNode node) {
266 + checkNotNull(node, JSON_NOT_NULL);
267 + TenantNetwork network = null;
268 + ConcurrentMap<TenantNetworkId, TenantNetwork> networksMap = Maps
269 + .newConcurrentMap();
270 + if (node != null) {
271 + String name = node.get("name").asText();
272 + boolean adminStateUp = node.get("admin_state_up").asBoolean();
273 + String state = node.get("status").asText();
274 + boolean shared = node.get("shared").asBoolean();
275 + String tenantId = node.get("tenant_id").asText();
276 + boolean routerExternal = node.get("router:external").asBoolean();
277 + String type = node.get("provider:network_type").asText();
278 + String physicalNetwork = node.get("provider:physical_network")
279 + .asText();
280 + String segmentationId = node.get("provider:segmentation_id")
281 + .asText();
282 + TenantNetworkId id = null;
283 + if (flag == CREATE_NETWORK) {
284 + id = TenantNetworkId.networkId(node.get("id").asText());
285 + } else if (flag == UPDATE_NETWORK) {
286 + id = networkId;
287 + }
288 + network = new DefaultTenantNetwork(
289 + id,
290 + name,
291 + adminStateUp,
292 + isState(state),
293 + shared,
294 + TenantId.tenantId(tenantId),
295 + routerExternal,
296 + isType(type),
297 + PhysicalNetwork
298 + .physicalNetwork(physicalNetwork),
299 + SegmentationId
300 + .segmentationId(segmentationId));
301 + networksMap.putIfAbsent(id, network);
302 + }
303 + return Collections.unmodifiableCollection(networksMap.values());
304 + }
305 +
306 + /**
307 + * Returns a collection of tenantNetworks.
308 + *
309 + * @param nodes the network jsonnodes
310 + * @return a collection of tenantNetworks
311 + */
312 + public Iterable<TenantNetwork> changeJson2objs(JsonNode nodes) {
313 + checkNotNull(nodes, JSON_NOT_NULL);
314 + TenantNetwork network = null;
315 + ConcurrentMap<TenantNetworkId, TenantNetwork> networksMap = Maps
316 + .newConcurrentMap();
317 + if (nodes != null) {
318 + for (JsonNode node : nodes) {
319 + String id = node.get("id").asText();
320 + String name = node.get("name").asText();
321 + boolean adminStateUp = node.get("admin_state_up").asBoolean();
322 + String state = node.get("status").asText();
323 + boolean shared = node.get("shared").asBoolean();
324 + String tenantId = node.get("tenant_id").asText();
325 + boolean routerExternal = node.get("router:external")
326 + .asBoolean();
327 + String type = node.get("provider:network_type").asText();
328 + String physicalNetwork = node.get("provider:physical_network")
329 + .asText();
330 + String segmentationId = node.get("provider:segmentation_id")
331 + .asText();
332 + network = new DefaultTenantNetwork(
333 + TenantNetworkId
334 + .networkId(id),
335 + name,
336 + adminStateUp,
337 + isState(state),
338 + shared,
339 + TenantId.tenantId(tenantId),
340 + routerExternal,
341 + isType(type),
342 + PhysicalNetwork
343 + .physicalNetwork(physicalNetwork),
344 + SegmentationId
345 + .segmentationId(segmentationId));
346 + networksMap.putIfAbsent(TenantNetworkId.networkId(id), network);
347 + }
348 + }
349 + return Collections.unmodifiableCollection(networksMap.values());
350 + }
351 +
352 + /**
353 + * Returns the specified item if that items is null; otherwise throws not
354 + * found exception.
355 + *
356 + * @param item item to check
357 + * @param <T> item type
358 + * @param message not found message
359 + * @return item if not null
360 + * @throws org.onlab.util.ItemNotFoundException if item is null
361 + */
362 + protected <T> T nullIsNotFound(T item, String message) {
363 + if (item == null) {
364 + throw new ItemNotFoundException(message);
365 + }
366 + return item;
367 + }
368 +}
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!--
3 + ~ Copyright 2015 Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
18 + xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
19 + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
20 + id="ONOS" version="2.5">
21 + <display-name>VTNRSC REST API v1.0</display-name>
22 +
23 + <servlet>
24 + <servlet-name>JAX-RS Service</servlet-name>
25 + <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
26 + <init-param>
27 + <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
28 + <param-value>com.sun.jersey.api.core.ClassNamesResourceConfig</param-value>
29 + </init-param>
30 + <init-param>
31 + <param-name>com.sun.jersey.config.property.classnames</param-name>
32 + <param-value>
33 + org.onosproject.vtnweb.resources.TenantNetworkWebResource
34 + </param-value>
35 + </init-param>
36 + <load-on-startup>1</load-on-startup>
37 + </servlet>
38 +
39 + <servlet-mapping>
40 + <servlet-name>JAX-RS Service</servlet-name>
41 + <url-pattern>/*</url-pattern>
42 + </servlet-mapping>
43 +</web-app>