Moti Asayag has uploaded a new change for review.

Change subject: restapi: Support network labels
......................................................................

restapi: Support network labels

Adding support for labels on network level.
Currently the engine supports a single label
per network.

The support actions for labels are add and delete.

See: http://www.ovirt.org/Features/NetworkLabels#REST

Change-Id: Id8f6f18c6195a2822d87ad626f034cefe43847b4
Bug-Url: https://bugzilla.redhat.com/1040586
Signed-off-by: Moti Asayag <masa...@redhat.com>
---
A 
backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/LabelResource.java
A 
backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/LabelsResource.java
M 
backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/NetworkResource.java
M 
backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
M 
backend/manager/modules/restapi/interface/definition/src/main/resources/rsdl_metadata.yaml
A 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworkLabelResource.java
A 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworkLabelsResource.java
M 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworkResource.java
M 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworksResource.java
A 
backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/LabelMapper.java
10 files changed, 344 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/04/22804/1

diff --git 
a/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/LabelResource.java
 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/LabelResource.java
new file mode 100644
index 0000000..33a202a
--- /dev/null
+++ 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/LabelResource.java
@@ -0,0 +1,31 @@
+/*
+* Copyright (c) 2010 Red Hat, Inc.
+*
+* 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.ovirt.engine.api.resource;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Produces;
+
+import org.jboss.resteasy.annotations.providers.jaxb.Formatted;
+import org.ovirt.engine.api.model.Label;
+
+@Produces({ApiMediaType.APPLICATION_XML, ApiMediaType.APPLICATION_JSON, 
ApiMediaType.APPLICATION_X_YAML})
+public interface LabelResource {
+
+    @GET
+    @Formatted
+    public Label get();
+}
diff --git 
a/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/LabelsResource.java
 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/LabelsResource.java
new file mode 100644
index 0000000..72792fc
--- /dev/null
+++ 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/LabelsResource.java
@@ -0,0 +1,51 @@
+/*
+* Copyright (c) 2010 Red Hat, Inc.
+*
+* 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.ovirt.engine.api.resource;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Response;
+
+import org.jboss.resteasy.annotations.providers.jaxb.Formatted;
+import org.ovirt.engine.api.model.Label;
+import org.ovirt.engine.api.model.Labels;
+
+
+@Produces({ApiMediaType.APPLICATION_XML, ApiMediaType.APPLICATION_JSON, 
ApiMediaType.APPLICATION_X_YAML})
+public interface LabelsResource {
+
+    @GET
+    @Formatted
+    public Labels list();
+
+    @POST
+    @Formatted
+    @Consumes({ ApiMediaType.APPLICATION_XML, ApiMediaType.APPLICATION_JSON, 
ApiMediaType.APPLICATION_X_YAML })
+    public Response add(Label label);
+
+    @DELETE
+    @Path("{id}")
+    public Response remove(@PathParam("id") String id);
+
+    @Path("{id}")
+    public LabelResource getLabelSubResource(@PathParam("id") String id);
+}
diff --git 
a/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/NetworkResource.java
 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/NetworkResource.java
index 9a7178b..775488b 100644
--- 
a/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/NetworkResource.java
+++ 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/NetworkResource.java
@@ -30,4 +30,7 @@
 
     @Path("vnicprofiles")
     public AssignedVnicProfilesResource getVnicProfilesResource();
+
+    @Path("labels")
+    public LabelsResource getLabelsResource();
 }
diff --git 
a/backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
 
b/backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
index a88507b..beb19ed 100644
--- 
a/backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
+++ 
b/backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
@@ -1983,6 +1983,31 @@
     </xs:complexContent>
   </xs:complexType>
 
+  <xs:element name="label" type="Label"/>
+
+  <xs:element name="labels" type="Labels"/>
+
+  <xs:complexType name="Label">
+       <xs:complexContent>
+         <xs:extension base="BaseResource"/>
+       </xs:complexContent>
+  </xs:complexType>
+
+  <xs:complexType name="Labels">
+    <xs:complexContent>
+      <xs:extension base="BaseResources">
+        <xs:sequence>
+          <xs:annotation>
+            <xs:appinfo>
+                <jaxb:property name="Labels"/>
+            </xs:appinfo>
+          </xs:annotation>
+          <xs:element ref="label" minOccurs="0" maxOccurs="unbounded"/>
+        </xs:sequence>
+      </xs:extension>
+    </xs:complexContent>
+  </xs:complexType>
+
   <!-- Vnic Profiles -->
   <xs:element name="vnic_profile" type="VnicProfile"/>
 
diff --git 
a/backend/manager/modules/restapi/interface/definition/src/main/resources/rsdl_metadata.yaml
 
b/backend/manager/modules/restapi/interface/definition/src/main/resources/rsdl_metadata.yaml
index a9abe8f..cabf0be 100644
--- 
a/backend/manager/modules/restapi/interface/definition/src/main/resources/rsdl_metadata.yaml
+++ 
b/backend/manager/modules/restapi/interface/definition/src/main/resources/rsdl_metadata.yaml
@@ -1130,6 +1130,45 @@
       Content-Type: {value: application/xml|json, required: true}
       Expect: {value: 201-created, required: false}
       Correlation-Id: {value: 'any string', required: false}
+- name: /datacenters/{datacenter:id}/networks/{network:id}/labels|rel=get
+  description: get the labels of a network
+  request:
+    body:
+      parameterType: null
+      signatures: []
+    urlparams: {}
+    headers: {}
+- name: /datacenters/{datacenter:id}/networks/{network:id}/labels/|rel=add
+  description: add a new label to the network
+  request:
+    body:
+      parameterType: Label
+      signatures:
+      - mandatoryArguments: {label.id: 'xs:string'}
+        description: add a new label to the network
+    urlparams: {}
+    headers:
+      Content-Type: {value: application/xml|json, required: true}
+      Expect: {value: 201-created, required: false}
+      Correlation-Id: {value: 'any string', required: false}
+- name: 
/datacenters/{datacenter:id}/networks/{network:id}/labels/{label:id}|rel=get
+  description: get a specific label of a network
+  request:
+    body:
+      parameterType: null
+      signatures: []
+    urlparams: {}
+    headers: {}
+- name: 
/datacenters/{datacenter:id}/networks/{network:id}/labels/{label:id}|rel=delete
+  description: delete the specified label of a network
+  request:
+    body:
+      parameterType: null
+      signatures: []
+    urlparams:
+      async: {context: matrix, type: 'xs:boolean', value: true|false, 
required: false}
+    headers:
+      Correlation-Id: {value: 'any string', required: false}
 - name: /storagedomains|rel=get
   description: get storage domains in the system
   request:
@@ -2857,6 +2896,45 @@
       Content-Type: {value: application/xml|json, required: true}
       Expect: {value: 201-created, required: false}
       Correlation-Id: {value: 'any string', required: false}
+- name: /networks/{network:id}/labels|rel=get
+  description: get the labels of a network
+  request:
+    body:
+      parameterType: null
+      signatures: []
+    urlparams: {}
+    headers: {}
+- name: /networks/{network:id}/labels/|rel=add
+  description: add a new label to the network
+  request:
+    body:
+      parameterType: Label
+      signatures:
+      - mandatoryArguments: {label.id: 'xs:string'}
+        description: add a new label to the network
+    urlparams: {}
+    headers:
+      Content-Type: {value: application/xml|json, required: true}
+      Expect: {value: 201-created, required: false}
+      Correlation-Id: {value: 'any string', required: false}
+- name: /networks/{network:id}/labels/{label:id}|rel=get
+  description: get a specific label of a network
+  request:
+    body:
+      parameterType: null
+      signatures: []
+    urlparams: {}
+    headers: {}
+- name: /networks/{network:id}/labels/{label:id}|rel=delete
+  description: delete the specified label of a network
+  request:
+    body:
+      parameterType: null
+      signatures: []
+    urlparams:
+      async: {context: matrix, type: 'xs:boolean', value: true|false, 
required: false}
+    headers:
+      Correlation-Id: {value: 'any string', required: false}
 - name: /vnicprofiles|rel=get
   description: get the list of all virtual network interface card profiles in 
the system
   request:
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworkLabelResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworkLabelResource.java
new file mode 100644
index 0000000..ff54622
--- /dev/null
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworkLabelResource.java
@@ -0,0 +1,24 @@
+package org.ovirt.engine.api.restapi.resource;
+
+import org.ovirt.engine.api.model.Label;
+import org.ovirt.engine.api.resource.LabelResource;
+import org.ovirt.engine.core.common.businessentities.network.NetworkLabel;
+import org.ovirt.engine.core.common.queries.IdQueryParameters;
+import org.ovirt.engine.core.common.queries.VdcQueryType;
+
+public class BackendNetworkLabelResource extends 
AbstractBackendSubResource<Label, NetworkLabel> implements LabelResource {
+
+    protected BackendNetworkLabelResource(String id) {
+        super(id, Label.class, NetworkLabel.class);
+    }
+
+    @Override
+    public Label get() {
+        return performGet(VdcQueryType.GetNetworkLabelsByNetworkId, new 
IdQueryParameters(guid));
+    }
+
+    @Override
+    protected Label doPopulate(Label model, NetworkLabel entity) {
+        return model;
+    }
+}
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworkLabelsResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworkLabelsResource.java
new file mode 100644
index 0000000..c8befe8
--- /dev/null
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworkLabelsResource.java
@@ -0,0 +1,93 @@
+package org.ovirt.engine.api.restapi.resource;
+
+import java.util.List;
+
+import javax.ws.rs.core.Response;
+
+import org.ovirt.engine.api.model.Label;
+import org.ovirt.engine.api.model.Labels;
+import org.ovirt.engine.api.resource.LabelResource;
+import org.ovirt.engine.api.resource.LabelsResource;
+import org.ovirt.engine.core.common.action.LabelNetworkParameters;
+import org.ovirt.engine.core.common.action.UnlabelNetworkParameters;
+import org.ovirt.engine.core.common.action.VdcActionType;
+import org.ovirt.engine.core.common.businessentities.network.NetworkLabel;
+import org.ovirt.engine.core.common.queries.IdQueryParameters;
+import org.ovirt.engine.core.common.queries.VdcQueryType;
+import org.ovirt.engine.core.compat.Guid;
+
+public class BackendNetworkLabelsResource extends 
AbstractBackendCollectionResource<Label, NetworkLabel> implements 
LabelsResource {
+
+    private String networkId;
+
+    protected BackendNetworkLabelsResource(String networkId) {
+        super(Label.class, NetworkLabel.class);
+        this.networkId = networkId;
+    }
+
+    @Override
+    public Labels list() {
+        return mapCollection(getNetworkLabels(networkId));
+    }
+
+    private List<NetworkLabel> getNetworkLabels(String networkId) {
+        return getBackendCollection(VdcQueryType.GetNetworkLabelsByNetworkId, 
new IdQueryParameters(asGuid(networkId)));
+    }
+
+    private Labels mapCollection(List<NetworkLabel> networkLabels) {
+        Labels labels = new Labels();
+        for (NetworkLabel networkLabel : networkLabels) {
+            Label label = new Label();
+            label.setId(networkLabel.getId());
+            labels.getLabels().add(label);
+        }
+
+        return labels;
+    }
+
+    @Override
+    public Response add(Label label) {
+        validateParameters(label, "id");
+        return performCreate(VdcActionType.LabelNetwork,
+                new LabelNetworkParameters(asGuid(networkId), label.getId()),
+                new NetworkLabelIdResolver(networkId));
+    }
+
+    @SingleEntityResource
+    @Override
+    public LabelResource getLabelSubResource(String id) {
+        return inject(new BackendNetworkLabelResource(id));
+    }
+
+    @Override
+    protected Response performRemove(String id) {
+        return performAction(VdcActionType.UnlabelNetwork, new 
UnlabelNetworkParameters(asGuid(networkId)));
+    }
+
+    @Override
+    protected Label doPopulate(Label model, NetworkLabel entity) {
+        return model;
+    }
+
+    protected class NetworkLabelIdResolver extends EntityIdResolver<Guid> {
+
+        protected String networkId;
+
+        NetworkLabelIdResolver() {
+        }
+
+        NetworkLabelIdResolver(String networkId) {
+            this.networkId = networkId;
+        }
+
+        @Override
+        public NetworkLabel lookupEntity(Guid id) throws 
BackendFailureException {
+            List<NetworkLabel> labels = getNetworkLabels(networkId);
+            if (!labels.isEmpty()) {
+                return labels.get(0);
+            }
+
+            return null;
+        }
+    }
+}
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworkResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworkResource.java
index 1aedb44..f26495f 100644
--- 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworkResource.java
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworkResource.java
@@ -4,6 +4,7 @@
 import org.ovirt.engine.api.model.Network;
 import org.ovirt.engine.api.resource.AssignedPermissionsResource;
 import org.ovirt.engine.api.resource.AssignedVnicProfilesResource;
+import org.ovirt.engine.api.resource.LabelsResource;
 import org.ovirt.engine.api.resource.NetworkResource;
 import org.ovirt.engine.core.common.VdcObjectType;
 import org.ovirt.engine.core.common.action.AddNetworkStoragePoolParameters;
@@ -20,13 +21,18 @@
 
     @Override
     public Network get() {
-        org.ovirt.engine.core.common.businessentities.network.Network entity = 
parent.lookupNetwork(guid);
+        org.ovirt.engine.core.common.businessentities.network.Network entity = 
getNetwork();
         if (entity == null) {
             return notFound();
         }
         Network network = map(entity);
         network.setDisplay(null);
         return addLinks(network);
+    }
+
+    public org.ovirt.engine.core.common.businessentities.network.Network 
getNetwork() {
+        org.ovirt.engine.core.common.businessentities.network.Network entity = 
parent.lookupNetwork(guid);
+        return entity;
     }
 
     @Override
@@ -59,4 +65,9 @@
     public AssignedVnicProfilesResource getVnicProfilesResource() {
         return inject(new BackendAssignedVnicProfilesResource(id));
     }
+
+    @Override
+    public LabelsResource getLabelsResource() {
+        return inject(new BackendNetworkLabelsResource(id));
+    }
 }
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworksResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworksResource.java
index 2d16d28..3575ce8 100644
--- 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworksResource.java
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendNetworksResource.java
@@ -18,7 +18,7 @@
 
 public class BackendNetworksResource extends AbstractBackendNetworksResource 
implements NetworksResource {
 
-    static final String[] SUB_COLLECTIONS = { "permissions", "vnicprofiles" };
+    static final String[] SUB_COLLECTIONS = { "permissions", "vnicprofiles", 
"labels" };
 
     public BackendNetworksResource() {
         this(VdcQueryType.GetAllNetworks);
diff --git 
a/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/LabelMapper.java
 
b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/LabelMapper.java
new file mode 100644
index 0000000..a82e1f3
--- /dev/null
+++ 
b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/LabelMapper.java
@@ -0,0 +1,26 @@
+package org.ovirt.engine.api.restapi.types;
+
+import org.ovirt.engine.api.model.Label;
+import org.ovirt.engine.core.common.businessentities.network.NetworkLabel;
+
+public class LabelMapper {
+    @Mapping(from = Label.class, to = NetworkLabel.class)
+    public static NetworkLabel map(Label model, NetworkLabel template) {
+        NetworkLabel entity = template != null ? template : new NetworkLabel();
+        if (model.isSetId()) {
+            entity.setId(model.getId());
+        }
+
+        return entity;
+    }
+
+    @Mapping(from = NetworkLabel.class, to = Label.class)
+    public static Label map(NetworkLabel entity, Label template) {
+        Label model = template != null ? template : new Label();
+        if (entity.getId() != null) {
+            model.setId(entity.getId());
+        }
+
+        return model;
+    }
+}


-- 
To view, visit http://gerrit.ovirt.org/22804
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id8f6f18c6195a2822d87ad626f034cefe43847b4
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Moti Asayag <masa...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to