|
| 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