Repository: ambari Updated Branches: refs/heads/trunk e90009310 -> 47a988245
http://git-wip-us.apache.org/repos/asf/ambari/blob/47a98824/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/AssetResource.java ---------------------------------------------------------------------- diff --git a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/AssetResource.java b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/AssetResource.java new file mode 100644 index 0000000..0622971 --- /dev/null +++ b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/AssetResource.java @@ -0,0 +1,197 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.apache.oozie.ambari.view.assets; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import org.apache.ambari.view.ViewContext; +import org.apache.oozie.ambari.view.*; +import org.apache.oozie.ambari.view.assets.model.ActionAsset; +import org.apache.oozie.ambari.view.assets.model.ActionAssetDefinition; +import org.apache.oozie.ambari.view.assets.model.AssetDefintion; +import org.apache.oozie.ambari.view.model.APIResult; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.ws.rs.*; +import javax.ws.rs.core.*; +import javax.ws.rs.core.Response.Status; +import java.io.IOException; +import java.util.*; + +import static org.apache.oozie.ambari.view.Constants.*; + +public class AssetResource { + + private final static Logger LOGGER = LoggerFactory + .getLogger(AssetResource.class); + private final AssetService assetService; + private final ViewContext viewContext; + private final HDFSFileUtils hdfsFileUtils; + private OozieUtils oozieUtils = new OozieUtils(); + private final OozieDelegate oozieDelegate; + + + public AssetResource(ViewContext viewContext) { + this.viewContext = viewContext; + this.assetService = new AssetService(viewContext); + hdfsFileUtils = new HDFSFileUtils(viewContext); + oozieDelegate = new OozieDelegate(viewContext); + } + + @GET + public Response getAssets() { + try { + Collection<ActionAsset> assets = assetService.getAssets(); + APIResult result = new APIResult(); + result.setStatus(APIResult.Status.SUCCESS); + result.getPaging().setTotal(assets != null ? assets.size() : 0L); + result.setData(assets); + return Response.ok(result).build(); + } catch (Exception e) { + throw new ServiceFormattedException(e); + } + } + + @GET + @Path("/mine") + public Response getMyAssets() { + try { + Collection<ActionAsset> assets = assetService.getMyAssets(); + APIResult result = new APIResult(); + result.setStatus(APIResult.Status.SUCCESS); + result.getPaging().setTotal(assets != null ? assets.size() : 0L); + result.setData(assets); + return Response.ok(result).build(); + } catch (Exception e) { + throw new ServiceFormattedException(e); + } + } + @POST + public Response saveAsset(@Context HttpHeaders headers, + @QueryParam("id") String id, @Context UriInfo ui, String body) { + try { + Gson gson = new Gson(); + AssetDefintion assetDefinition = gson.fromJson(body, + AssetDefintion.class); + Map<String, String> validateAsset = validateAsset(headers, + assetDefinition.getDefinition(), ui.getQueryParameters()); + if (!STATUS_OK.equals(validateAsset.get(STATUS_KEY))) { + return Response.status(Status.BAD_REQUEST).build(); + } + assetService.saveAsset(id, viewContext.getUsername(), assetDefinition); + APIResult result = new APIResult(); + result.setStatus(APIResult.Status.SUCCESS); + return Response.ok(result).build(); + } catch (Exception e) { + throw new ServiceFormattedException(e); + } + } + + private List<String> getAsList(String string) { + ArrayList<String> li = new ArrayList<>(1); + li.add(string); + return li; + } + + public Map<String, String> validateAsset(HttpHeaders headers, + String postBody, MultivaluedMap<String, String> queryParams) { + String workflowXml = oozieUtils.generateWorkflowXml(postBody); + try { + Map<String, String> result = new HashMap<>(); + String tempWfPath = "/tmp" + "/tmpooziewfs/tempwf.xml"; + hdfsFileUtils.writeToFile(tempWfPath, workflowXml, true); + queryParams.put("oozieparam.action", getAsList("dryrun")); + queryParams.put("oozieconfig.rerunOnFailure", getAsList("false")); + queryParams.put("oozieconfig.useSystemLibPath", getAsList("true")); + queryParams.put("resourceManager", getAsList("useDefault")); + String dryRunResp = oozieDelegate.submitWorkflowJobToOozie(headers, + tempWfPath, queryParams, JobType.WORKFLOW); + LOGGER.info(String.format("resp from validating asset=[%s]", + dryRunResp)); + if (dryRunResp != null && dryRunResp.trim().startsWith("{")) { + JsonElement jsonElement = new JsonParser().parse(dryRunResp); + JsonElement idElem = jsonElement.getAsJsonObject().get("id"); + if (idElem != null) { + result.put(STATUS_KEY, STATUS_OK); + } else { + result.put(STATUS_KEY, STATUS_FAILED); + result.put(MESSAGE_KEY, dryRunResp); + } + } else { + result.put(STATUS_KEY, STATUS_FAILED); + result.put(MESSAGE_KEY, dryRunResp); + } + return result; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @GET + @Path("/{id}") + public Response getAssetDetail(@PathParam("id") String id) { + try { + AssetDefintion assetDefinition = assetService.getAssetDetail(id); + APIResult result = new APIResult(); + result.setStatus(APIResult.Status.SUCCESS); + result.setData(assetDefinition); + return Response.ok(result).build(); + } catch (Exception e) { + throw new ServiceFormattedException(e); + } + } + + @GET + @Path("/definition/id}") + public Response getAssetDefinition(@PathParam("defnitionId") String id) { + try { + ActionAssetDefinition assetDefinition = assetService + .getAssetDefinition(id); + APIResult result = new APIResult(); + result.setStatus(APIResult.Status.SUCCESS); + result.setData(assetDefinition); + return Response.ok(result).build(); + } catch (Exception e) { + throw new ServiceFormattedException(e); + } + } + + @DELETE + @Path("/{id}") + public Response delete(@PathParam("id") String id) { + try { + ActionAsset asset = assetService.getAsset(id); + if (asset == null) { + throw new RuntimeException("Asset doesnt exist"); + } + if (!viewContext.getUsername().equals(asset.getOwner())){ + throw new RuntimeException( + "Dont have permission to delete this asset"); + } + assetService.deleteAsset(id); + APIResult result = new APIResult(); + result.setStatus(APIResult.Status.SUCCESS); + return Response.ok(result).build(); + } catch (Exception e) { + throw new ServiceFormattedException(e); + } + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/47a98824/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/AssetService.java ---------------------------------------------------------------------- diff --git a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/AssetService.java b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/AssetService.java index 648a89f..9fe2f9c 100644 --- a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/AssetService.java +++ b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/AssetService.java @@ -6,9 +6,9 @@ * to you 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 - * + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> * 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. @@ -21,31 +21,95 @@ import java.util.Collection; import org.apache.ambari.view.ViewContext; import org.apache.oozie.ambari.view.assets.model.ActionAsset; +import org.apache.oozie.ambari.view.assets.model.ActionAssetDefinition; +import org.apache.oozie.ambari.view.assets.model.AssetDefintion; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class AssetService { - private AssetRepo assetRepo; + private final static Logger LOGGER = LoggerFactory + .getLogger(AssetService.class); + private final AssetRepo assetRepo; + private final AssetDefinitionRepo assetDefinitionRepo; - public AssetService(ViewContext viewContext) { - super(); - assetRepo = new AssetRepo(viewContext.getDataStore()); - } + private final ViewContext viewContext; - public Collection<ActionAsset> getAssets() { - return null; - } + public AssetService(ViewContext viewContext) { + super(); + this.viewContext = viewContext; - public Collection<ActionAsset> getPrioritizedAssets() { - Collection<ActionAsset> assets = getAssets(); - // reorder - return assets; - } + this.assetDefinitionRepo = new AssetDefinitionRepo( + viewContext.getDataStore()); + this.assetRepo = new AssetRepo(viewContext.getDataStore()); - public void addAsset() { + } - } + public Collection<ActionAsset> getAssets() { + return assetRepo.findAll(); + } - public void deleteAsset() { + public Collection<ActionAsset> getPrioritizedAssets() { + Collection<ActionAsset> assets = getAssets(); + return assets; + } + public void saveAsset(String assetId, String userName, + AssetDefintion assetDefinition) { + if (assetId != null) { + ActionAsset actionAsset = assetRepo.findById(assetId); + if (actionAsset == null) { + throw new RuntimeException("could not find asset with id :" + + assetId); + } + actionAsset.setDescription(assetDefinition.getDescription()); + actionAsset.setName(assetDefinition.getName()); + actionAsset.setType(assetDefinition.getType()); + ActionAssetDefinition assetDefinintion = assetDefinitionRepo + .findById(actionAsset.getDefinitionRef()); + assetDefinintion.setData(assetDefinintion.getData()); + assetDefinitionRepo.update(assetDefinintion); + assetRepo.update(actionAsset); + } else { + ActionAsset actionAsset = new ActionAsset(); + actionAsset.setOwner(userName); + ActionAssetDefinition definition = new ActionAssetDefinition(); + definition.setData(assetDefinition.getDefinition()); + ActionAssetDefinition createdDefinition = assetDefinitionRepo + .create(definition); + actionAsset.setDefinitionRef(createdDefinition.getId()); + actionAsset.setDescription(assetDefinition.getDescription()); + actionAsset.setName(assetDefinition.getName()); + actionAsset.setType(assetDefinition.getType()); + assetRepo.create(actionAsset); } + } + + + public void deleteAsset(String id) { + assetRepo.deleteById(id); + } + + public AssetDefintion getAssetDetail(String assetId) { + AssetDefintion ad = new AssetDefintion(); + ActionAsset actionAsset = assetRepo.findById(assetId); + ActionAssetDefinition actionDefinition = assetDefinitionRepo + .findById(actionAsset.getDefinitionRef()); + ad.setDefinition(actionDefinition.getData()); + ad.setDescription(actionAsset.getDescription()); + ad.setName(actionAsset.getName()); + return ad; + } + + public ActionAssetDefinition getAssetDefinition(String assetDefintionId) { + return assetDefinitionRepo.findById(assetDefintionId); + } + + public ActionAsset getAsset(String id) { + return assetRepo.findById(id); + } + + public Collection<ActionAsset> getMyAssets() { + return assetRepo.getMyAsets(viewContext.getUsername()); + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/47a98824/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/model/ActionAsset.java ---------------------------------------------------------------------- diff --git a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/model/ActionAsset.java b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/model/ActionAsset.java index 8eb6081..200a4aa 100644 --- a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/model/ActionAsset.java +++ b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/model/ActionAsset.java @@ -6,9 +6,9 @@ * to you 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 - * + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> * 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. @@ -17,44 +17,74 @@ */ package org.apache.oozie.ambari.view.assets.model; +import org.apache.oozie.ambari.view.AssetDefinitionRefType; +import org.apache.oozie.ambari.view.EntityStatus; import org.apache.oozie.ambari.view.model.BaseModel; +import org.apache.oozie.ambari.view.model.Indexed; -public class ActionAsset extends BaseModel { - private String id; - private String name; - private String description; - private String assetLocation; - private String type; - - public String getId() { - return id; - } - public void setId(String id) { - this.id = id; - } - public String getName() { - return name; - } - public void setName(String name) { - this.name = name; - } - public String getDescription() { - return description; - } - public void setDescription(String description) { - this.description = description; - } - - public String getType() { - return type; - } - public void setType(String type) { - this.type = type; - } - public String getAssetLocation() { - return assetLocation; - } - public void setAssetLocation(String assetLocation) { - this.assetLocation = assetLocation; - } +public class ActionAsset extends BaseModel implements Indexed { + private static final long serialVersionUID = 1L; + private String id; + private String name; + private String description; + private String type; + private String definitionRefType = AssetDefinitionRefType.DB.name();//can be db or fs + private String definitionRef;//point to dbid or filesystem + private String status = EntityStatus.DRAFT.name(); + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getDefinitionRef() { + return definitionRef; + } + + public void setDefinitionRef(String definitionRef) { + this.definitionRef = definitionRef; + } + + public String getDefinitionRefType() { + return definitionRefType; + } + + public void setDefinitionRefType(String definitionRefType) { + this.definitionRefType = definitionRefType; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/47a98824/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/model/ActionAssetDefinition.java ---------------------------------------------------------------------- diff --git a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/model/ActionAssetDefinition.java b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/model/ActionAssetDefinition.java new file mode 100644 index 0000000..0c6e630 --- /dev/null +++ b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/model/ActionAssetDefinition.java @@ -0,0 +1,42 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.apache.oozie.ambari.view.assets.model; + +import org.apache.oozie.ambari.view.model.Indexed; + +public class ActionAssetDefinition implements Indexed { + private String id; + private String data; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/47a98824/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/model/AssetDefintion.java ---------------------------------------------------------------------- diff --git a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/model/AssetDefintion.java b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/model/AssetDefintion.java new file mode 100644 index 0000000..e80be27 --- /dev/null +++ b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/model/AssetDefintion.java @@ -0,0 +1,69 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.apache.oozie.ambari.view.assets.model; + +import org.apache.oozie.ambari.view.AssetDefinitionRefType; + +public class AssetDefintion { + private String definition = AssetDefinitionRefType.DB.name(); + private String type; + private String name; + private String description; + private String status; + + public String getDefinition() { + return definition; + } + + public void setDefinition(String definition) { + this.definition = definition; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/47a98824/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/APIResult.java ---------------------------------------------------------------------- diff --git a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/APIResult.java b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/APIResult.java new file mode 100644 index 0000000..2a8075a --- /dev/null +++ b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/APIResult.java @@ -0,0 +1,63 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.apache.oozie.ambari.view.model; + +public class APIResult { + + private Status status; + private Object data; + private Paging paging = new Paging(); + ; + + public Status getStatus() { + return status; + } + + public void setStatus(Status status) { + this.status = status; + } + + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public Paging getPaging() { + return paging; + + } + + public void setPaging(Paging paging) { + this.paging = paging; + } + + + public static enum Status { + SUCCESS, + ERROR + } + + public static void main(String[] args) { + System.out.println("hello"); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/47a98824/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/BaseModel.java ---------------------------------------------------------------------- diff --git a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/BaseModel.java b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/BaseModel.java index 553ce24..76d4d84 100644 --- a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/BaseModel.java +++ b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/BaseModel.java @@ -6,9 +6,9 @@ * to you 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 - * + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> * 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. @@ -20,29 +20,34 @@ package org.apache.oozie.ambari.view.model; import java.io.Serializable; -public class BaseModel implements Serializable{ - private static final long serialVersionUID = 1L; - private String createdAt; - private String updatedAt; - private String owner; - - public String getCreatedAt() { - return createdAt; - } - public void setCreatedAt(String createdAt) { - this.createdAt = createdAt; - } - public String getUpdatedAt() { - return updatedAt; - } - public void setUpdatedAt(String updatedAt) { - this.updatedAt = updatedAt; - } - public String getOwner() { - return owner; - } - public void setOwner(String owner) { - this.owner = owner; - } - +public class BaseModel implements When, Serializable { + private static final long serialVersionUID = 1L; + private String createdAt; + private String updatedAt; + private String owner; + + public String getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(String createdAt) { + this.createdAt = createdAt; + } + + public String getUpdatedAt() { + return updatedAt; + } + + public void setUpdatedAt(String updatedAt) { + this.updatedAt = updatedAt; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + } http://git-wip-us.apache.org/repos/asf/ambari/blob/47a98824/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/Indexed.java ---------------------------------------------------------------------- diff --git a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/Indexed.java b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/Indexed.java new file mode 100644 index 0000000..b391cbe --- /dev/null +++ b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/Indexed.java @@ -0,0 +1,24 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.apache.oozie.ambari.view.model; + +public interface Indexed { + String getId(); + + void setId(String id); +} http://git-wip-us.apache.org/repos/asf/ambari/blob/47a98824/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/Paging.java ---------------------------------------------------------------------- diff --git a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/Paging.java b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/Paging.java new file mode 100644 index 0000000..f6f1468 --- /dev/null +++ b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/Paging.java @@ -0,0 +1,30 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.apache.oozie.ambari.view.model; + +public class Paging { + private Long total; + + public Long getTotal() { + return total; + } + + public void setTotal(Long total) { + this.total = total; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/47a98824/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/When.java ---------------------------------------------------------------------- diff --git a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/When.java b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/When.java new file mode 100644 index 0000000..aa39c84 --- /dev/null +++ b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/model/When.java @@ -0,0 +1,28 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.apache.oozie.ambari.view.model; + +public interface When { + String getCreatedAt(); + + void setCreatedAt(String createdAt); + + String getUpdatedAt(); + + void setUpdatedAt(String updatedAt); +} http://git-wip-us.apache.org/repos/asf/ambari/blob/47a98824/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/repo/BaseRepo.java ---------------------------------------------------------------------- diff --git a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/repo/BaseRepo.java b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/repo/BaseRepo.java new file mode 100644 index 0000000..c05f475 --- /dev/null +++ b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/repo/BaseRepo.java @@ -0,0 +1,113 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.apache.oozie.ambari.view.repo; + +import java.util.Collection; +import java.util.Date; + +import org.apache.ambari.view.DataStore; +import org.apache.ambari.view.PersistenceException; +import org.apache.oozie.ambari.view.model.Indexed; +import org.apache.oozie.ambari.view.model.When; + +public class BaseRepo<T> { + + protected final DataStore dataStore; + private final Class type; + + public BaseRepo(Class type, DataStore dataStore) { + this.type = type; + this.dataStore = dataStore; + } + + public String generateId() { + return java.util.UUID.randomUUID().toString(); + } + + public Collection<T> findAll() { + try { + return dataStore.findAll(type, null); + } catch (PersistenceException e) { + throw new RuntimeException(e); + } + } + + public T findById(String id) { + try { + return (T) dataStore.find(type, id); + } catch (PersistenceException e) { + throw new RuntimeException(e); + } + } + + public T create(T obj) { + try { + if (obj instanceof Indexed) { + Indexed idxObj = (Indexed) obj; + if (idxObj.getId() == null) { + idxObj.setId(this.generateId()); + } else { + T findById = findById(idxObj.getId()); + if (findById != null) { + throw new RuntimeException("Object already exist in db"); + } + } + } + if (obj instanceof When) { + Date now = new Date(); + When when = (When) obj; + when.setCreatedAt(String.valueOf(now.getTime())); + when.setUpdatedAt(String.valueOf(now.getTime())); + } + this.dataStore.store(obj); + return obj; + } catch (PersistenceException e) { + throw new RuntimeException(e); + } + } + + public void update(T obj) { + try { + if (obj instanceof When) { + Date now = new Date(); + When when = (When) obj; + when.setUpdatedAt(String.valueOf(now.getTime())); + } + this.dataStore.store(obj); + } catch (PersistenceException e) { + throw new RuntimeException(e); + } + } + + public void delete(T obj) { + try { + this.dataStore.remove(obj); + } catch (PersistenceException e) { + throw new RuntimeException(e); + } + } + + public void deleteById(String id) { + try { + T findById = this.findById(id); + this.dataStore.remove(findById); + } catch (PersistenceException e) { + throw new RuntimeException(e); + } + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/47a98824/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/WorkflowManagerService.java ---------------------------------------------------------------------- diff --git a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/WorkflowManagerService.java b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/WorkflowManagerService.java index 4c88454..afdee9e 100644 --- a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/WorkflowManagerService.java +++ b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/WorkflowManagerService.java @@ -6,9 +6,9 @@ * to you 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 - * + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> * 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. @@ -25,52 +25,64 @@ import org.apache.ambari.view.ViewContext; import org.apache.oozie.ambari.view.HDFSFileUtils; import org.apache.oozie.ambari.view.JobType; import org.apache.oozie.ambari.view.workflowmanager.model.Workflow; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class WorkflowManagerService { + private final static Logger LOGGER = LoggerFactory + .getLogger(WorkflowManagerService.class); + private final WorkflowsRepo workflowsRepository; + private final HDFSFileUtils hdfsFileUtils; - private WorkflowsRepo workflowsRepository; - private HDFSFileUtils hdfsFileUtils; + public WorkflowManagerService(ViewContext viewContext) { + workflowsRepository = new WorkflowsRepo(viewContext.getDataStore()); + hdfsFileUtils = new HDFSFileUtils(viewContext); + } - public WorkflowManagerService(ViewContext viewContext) { - workflowsRepository = new WorkflowsRepo(viewContext.getDataStore()); - hdfsFileUtils = new HDFSFileUtils(viewContext); + public void saveWorkflow(String projectId, String path, JobType jobType, + String descripton, String userName, String name) { + LOGGER.debug("save workflow called"); + if (projectId != null) { + Workflow workflowById = workflowsRepository.findById(projectId); + if (workflowById == null) { + throw new RuntimeException("could not find project with id :" + + projectId); + } + setWorkflowAttributes(jobType, userName, name, workflowById); + workflowsRepository.update(workflowById); + } else { + Workflow wf = new Workflow(); + wf.setId(workflowsRepository.generateId()); + setWorkflowAttributes(jobType, userName, name, wf); + wf.setWorkflowDefinitionPath(path); + workflowsRepository.create(wf); } + } - public void saveWorkflow(String path, JobType jobType, String descripton, - String userName) { - // workflowsRepository.getWorkflow(path); - Workflow workflowByPath = getWorkflowByPath(path); - if (workflowByPath == null) { - Workflow wf = new Workflow(); - wf.setOwner(userName); - wf.setType(jobType.name()); - wf.setWorkflowDefinitionPath(path); - Date now = new Date(); - wf.setUpdatedAt(String.valueOf(now.getTime())); - workflowsRepository.updateWorkflow(wf); - } else { - Date now = new Date(); - workflowByPath.setUpdatedAt(String.valueOf(now.getTime())); - workflowsRepository.updateWorkflow(workflowByPath); - } - } + private void setWorkflowAttributes(JobType jobType, String userName, + String name, Workflow wf) { + wf.setOwner(userName); + wf.setName(name); + wf.setType(jobType.name()); + } - public Collection<Workflow> getAllWorkflows() { - return workflowsRepository.getAllWorkflows(); - } + public Collection<Workflow> getAllWorkflows() { + return workflowsRepository.findAll(); + } - public Workflow getWorkflowByPath(String path) { - return workflowsRepository.getWorkflow(path); - } + public Workflow getWorkflowByPath(String path) { + return workflowsRepository.getWorkflowByPath(path); + } - public void deleteWorkflow(String path, Boolean deleteDefinition) { - if (deleteDefinition) { - try { - hdfsFileUtils.deleteFile(path); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - workflowsRepository.deleteWorkflow(path); + public void deleteWorkflow(String projectId, Boolean deleteDefinition) { + Workflow workflow = workflowsRepository.findById(projectId); + if (deleteDefinition) { + try { + hdfsFileUtils.deleteFile(workflow.getWorkflowDefinitionPath()); + } catch (IOException e) { + throw new RuntimeException(e); + } } + workflowsRepository.delete(workflow); + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/47a98824/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/WorkflowsManagerResource.java ---------------------------------------------------------------------- diff --git a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/WorkflowsManagerResource.java b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/WorkflowsManagerResource.java index 17a3296..64e2060a 100644 --- a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/WorkflowsManagerResource.java +++ b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/WorkflowsManagerResource.java @@ -23,6 +23,8 @@ import java.util.Map; import javax.ws.rs.DELETE; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import org.apache.ambari.view.ViewContext; @@ -44,8 +46,10 @@ public class WorkflowsManagerResource { @DELETE - public void deleteWorkflow( @QueryParam("worfkflowPath") String path, + @Path("/projectId") + public void deleteWorkflow( @PathParam("projectId") String id, @DefaultValue("false") @QueryParam("deleteDefinition") Boolean deleteDefinition){ - workflowManagerService.deleteWorkflow(path,deleteDefinition); + workflowManagerService.deleteWorkflow(id,deleteDefinition); } + } http://git-wip-us.apache.org/repos/asf/ambari/blob/47a98824/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/WorkflowsRepo.java ---------------------------------------------------------------------- diff --git a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/WorkflowsRepo.java b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/WorkflowsRepo.java index 978c059..7787bda 100644 --- a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/WorkflowsRepo.java +++ b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/WorkflowsRepo.java @@ -6,9 +6,9 @@ * to you 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 - * + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> * 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. @@ -17,53 +17,24 @@ */ package org.apache.oozie.ambari.view.workflowmanager; -import java.util.Collection; - import org.apache.ambari.view.DataStore; import org.apache.ambari.view.PersistenceException; +import org.apache.oozie.ambari.view.repo.BaseRepo; import org.apache.oozie.ambari.view.workflowmanager.model.Workflow; -public class WorkflowsRepo { - private final DataStore dataStore; +public class WorkflowsRepo extends BaseRepo<Workflow> { - public WorkflowsRepo(DataStore dataStore) { - super(); - this.dataStore=dataStore; - } - public Collection<Workflow> getAllWorkflows(){ - try { - return dataStore.findAll(Workflow.class,null); - } catch (PersistenceException e) { - throw new RuntimeException(e); - } - } - public void deleteWorkflow(String workflowPath){ - try { - Workflow workflow = this.getWorkflow(workflowPath); - this.dataStore.remove(workflow); - } catch (PersistenceException e) { - throw new RuntimeException(e); - } - } - public void createWorkflow(Workflow wf){ - try { - this.dataStore.store(wf); - } catch (PersistenceException e) { - throw new RuntimeException(e); - } - } - public void updateWorkflow(Workflow wf){ - try { - this.dataStore.store(wf); - } catch (PersistenceException e) { - throw new RuntimeException(e); - } - } - public Workflow getWorkflow(String path) { - try { - return this.dataStore.find(Workflow.class, "workflowDefinitionPath='"+path+"'"); - } catch (PersistenceException e) { - throw new RuntimeException(e); - } + public WorkflowsRepo(DataStore dataStore) { + super(Workflow.class, dataStore); + + } + + public Workflow getWorkflowByPath(String path) { + try { + return this.dataStore.find(Workflow.class, + "workflowDefinitionPath='" + path + "'"); + } catch (PersistenceException e) { + throw new RuntimeException(e); } + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/47a98824/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/model/Workflow.java ---------------------------------------------------------------------- diff --git a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/model/Workflow.java b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/model/Workflow.java index ad5f48b..cc19c80 100644 --- a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/model/Workflow.java +++ b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/workflowmanager/model/Workflow.java @@ -18,71 +18,72 @@ package org.apache.oozie.ambari.view.workflowmanager.model; import org.apache.oozie.ambari.view.model.BaseModel; +import org.apache.oozie.ambari.view.model.Indexed; + +public class Workflow extends BaseModel implements Indexed { + private static final long serialVersionUID = 1L; + private String id = null; + private String name; + private String desciption; + private String workflowDefinitionPath; + private String type; + private String isDraft; + private String definitionMissing;//true or not if path is fine. + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getWorkflowDefinitionPath() { + return workflowDefinitionPath; + } + + public void setWorkflowDefinitionPath(String workflowDefinitionPath) { + this.workflowDefinitionPath = workflowDefinitionPath; + } + + public String getIsDraft() { + return isDraft; + } + + public void setIsDraft(String isDraft) { + this.isDraft = isDraft; + } + + public String getDesciption() { + return desciption; + } + + public void setDesciption(String desciption) { + this.desciption = desciption; + } + + public String getDefinitionMissing() { + return definitionMissing; + } + + public void setDefinitionMissing(String definitionMissing) { + this.definitionMissing = definitionMissing; + } -public class Workflow extends BaseModel{ - private static final long serialVersionUID = 1L; - private String id = null; - private String name; - private String desciption; - private String workflowDefinitionPath; - private String type; - private String isDraft; - private String definitionMissing;//true or not if path is fine. - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getWorkflowDefinitionPath() { - return workflowDefinitionPath; - } - - public void setWorkflowDefinitionPath(String workflowDefinitionPath) { - this.workflowDefinitionPath = workflowDefinitionPath; - } - - public String getIsDraft() { - return isDraft; - } - - public void setIsDraft(String isDraft) { - this.isDraft = isDraft; - } - - public String getDesciption() { - return desciption; - } - - public void setDesciption(String desciption) { - this.desciption = desciption; - } - - public String getDefinitionMissing() { - return definitionMissing; - } - - public void setDefinitionMissing(String definitionMissing) { - this.definitionMissing = definitionMissing; - } - } http://git-wip-us.apache.org/repos/asf/ambari/blob/47a98824/contrib/views/wfmanager/src/main/resources/view.xml ---------------------------------------------------------------------- diff --git a/contrib/views/wfmanager/src/main/resources/view.xml b/contrib/views/wfmanager/src/main/resources/view.xml index bfa1253..b8e2fcc 100644 --- a/contrib/views/wfmanager/src/main/resources/view.xml +++ b/contrib/views/wfmanager/src/main/resources/view.xml @@ -120,6 +120,23 @@ <required>false</required> </parameter> + <persistence> + <!-- + <entity> + <class>org.apache.oozie.ambari.view.workflowmanager.model.Workflow</class> + <id-property>id</id-property> + </entity> --> + <entity> + <class>org.apache.oozie.ambari.view.assets.model.ActionAssetDefinition</class> + <id-property>id</id-property> + </entity> + <entity> + <class>org.apache.oozie.ambari.view.assets.model.ActionAsset</class> + <id-property>id</id-property> + </entity> + + </persistence> + <!--<auto-instance> <name>AUTO_OOZIE_VIEW</name> <label>Oozie UI View</label>