Skip to content

Commit f04f04c

Browse files
Jake DsouzaGerrit Code Review
Jake Dsouza
authored and
Gerrit Code Review
committed
Photon-Model: add NetworkInterfaceService service
TODO- Add unit tests Change-Id: I276de9c34a42411bb80514056c369e9441836634
1 parent 7472f25 commit f04f04c

File tree

4 files changed

+177
-2
lines changed

4 files changed

+177
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ If you're looking to play with the code, keep reading.
2323
The product is arranged in a single repository, with subdirectories arranged by language. See the individual READMEs for instructions on building the code.
2424

2525
* [Devbox](devbox-photon/README.md): Devbox uses [Vagrant](http://vagrantup.com) to create a small standalone deployment of Photon Controller for test purposes.
26-
* [Java](java/README.md): Most of the Photon Controller management plane is written in Java, with many individual services implemented on top of the [Xenon framework](http://vmware.github.io/xenon) -- coming soon.
26+
* [Java](java/README.md): Most of the Photon Controller management plane is written in Java, with many individual services implemented on top of the [Xenon framework](http://vmware.github.io/xenon).
2727
* [Python](python/README.md): The ESX agent and its test and analysis collateral are implemented in Python.
2828
* [Ruby](ruby/README.md): The Photon Controller CLI is implemented in Ruby, as are the integration tests for the product.
2929
* **Note**: The Ruby CLI will soon be replaced with a Golang version.

java/photon-model/src/main/java/com/vmware/photon/controller/model/UriPaths.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
* specific language governing permissions and limitations under the License.
1212
*/
1313

14-
package com.vmware.photon.controller.model;
14+
package com.vmware.photon.controller.model;
1515

1616
/**
1717
* Service paths used in the provisioning model.
1818
*/
1919
public class UriPaths {
2020
public static final String RESOURCES = "/resources";
21+
public static final String RESOURCES_NETWORKS = RESOURCES + "/networks";
2122
public static final String PROVISIONING = "/provisioning";
2223
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2015 VMware, Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software distributed
9+
* under the License is distributed on an "AS IS" BASIS, without warranties or
10+
* conditions of any kind, EITHER EXPRESS OR IMPLIED. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package com.vmware.photon.controller.model.resources;
15+
16+
import com.vmware.dcp.common.FactoryService;
17+
import com.vmware.dcp.common.Operation;
18+
import com.vmware.dcp.common.Service;
19+
import com.vmware.photon.controller.model.UriPaths;
20+
21+
import java.util.UUID;
22+
23+
/**
24+
* Creates a NetworkInterfaceService instance.
25+
*/
26+
public class NetworkInterfaceFactoryService extends FactoryService {
27+
public static final String SELF_LINK = UriPaths.RESOURCES_NETWORKS + "/interfaces";
28+
29+
public NetworkInterfaceFactoryService() {
30+
super(NetworkInterfaceService.NetworkInterfaceState.class);
31+
}
32+
33+
@Override
34+
public void handlePost(Operation post) {
35+
if (!post.hasBody()) {
36+
post.fail(new IllegalArgumentException("body is required"));
37+
return;
38+
}
39+
40+
NetworkInterfaceService.NetworkInterfaceState initState = post.getBody(NetworkInterfaceService
41+
.NetworkInterfaceState.class);
42+
43+
if (initState.id == null) {
44+
initState.id = UUID.randomUUID().toString();
45+
}
46+
initState.documentSelfLink = initState.id;
47+
post.setBody(initState).complete();
48+
49+
}
50+
51+
@Override
52+
public Service createServiceInstance() {
53+
return new NetworkInterfaceService();
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright 2015 VMware, Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software distributed
9+
* under the License is distributed on an "AS IS" BASIS, without warranties or
10+
* conditions of any kind, EITHER EXPRESS OR IMPLIED. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package com.vmware.photon.controller.model.resources;
15+
16+
import com.vmware.dcp.common.Operation;
17+
import com.vmware.dcp.common.ServiceDocument;
18+
import com.vmware.dcp.common.ServiceDocumentDescription;
19+
import com.vmware.dcp.common.StatefulService;
20+
21+
import org.apache.commons.validator.routines.InetAddressValidator;
22+
23+
import java.util.List;
24+
25+
/**
26+
* Represents a network interface.
27+
*/
28+
public class NetworkInterfaceService extends StatefulService {
29+
30+
/**
31+
* Represents the state of a network interface.
32+
*/
33+
public static class NetworkInterfaceState extends ServiceDocument {
34+
/**
35+
* The name or id of the interface on the compute.
36+
*/
37+
public String id;
38+
39+
/**
40+
* The description for the interface. If this is an interface which uses DHCP
41+
* to resolve its address, then this is a link to the subnet document.
42+
*/
43+
public String networkDescriptionLink;
44+
45+
/**
46+
* The IP information of the interface. Optional.
47+
*/
48+
public String leaseLink;
49+
50+
/**
51+
* The static IP of the interface. Optional. If networkDescriptionLink / leaseLink
52+
* are defined, this cannot be and vice versa.
53+
*/
54+
public String address;
55+
56+
/**
57+
* The bridge this interface will be instantiated on. Optional.
58+
*/
59+
public String networkBridgeLink;
60+
61+
/**
62+
* A list of tenant links which can access this network interface.
63+
*/
64+
public List<String> tenantLinks;
65+
}
66+
67+
public NetworkInterfaceService() {
68+
super(NetworkInterfaceState.class);
69+
super.toggleOption(ServiceOption.PERSISTENCE, true);
70+
super.toggleOption(ServiceOption.REPLICATION, true);
71+
super.toggleOption(ServiceOption.OWNER_SELECTION, true);
72+
}
73+
74+
@Override
75+
public void handlePost(Operation post) {
76+
NetworkInterfaceState body = post.getBody(NetworkInterfaceState.class);
77+
78+
try {
79+
if (body.address != null) {
80+
if (body.networkDescriptionLink != null) {
81+
throw new IllegalArgumentException(
82+
"both networkDescriptionLink and IP cannot be set");
83+
}
84+
InetAddressValidator.getInstance().isValidInet4Address(body.address);
85+
} else if (body.networkDescriptionLink == null) {
86+
throw new IllegalArgumentException(
87+
"either IP or networkDescriptionLink must be set");
88+
}
89+
} catch (Exception e) {
90+
post.fail(e);
91+
return;
92+
}
93+
94+
post.complete();
95+
}
96+
97+
@Override
98+
public void handlePatch(Operation patch) {
99+
NetworkInterfaceState currentState = getState(patch);
100+
NetworkInterfaceState patchBody = patch.getBody(NetworkInterfaceState.class);
101+
102+
if (patchBody.id != null) {
103+
currentState.id = patchBody.id;
104+
}
105+
106+
if (patchBody.leaseLink != null) {
107+
currentState.leaseLink = patchBody.leaseLink;
108+
}
109+
110+
patch.setBody(currentState).complete();
111+
}
112+
113+
@Override
114+
public ServiceDocument getDocumentTemplate() {
115+
ServiceDocument td = super.getDocumentTemplate();
116+
ServiceDocumentDescription.expandTenantLinks(td.documentDescription);
117+
return td;
118+
}
119+
}

0 commit comments

Comments
 (0)