From 4371989e7a50d015b6eefa074d4988033223551b Mon Sep 17 00:00:00 2001 From: "Desai, Dhrumin (dd303q)" Date: Fri, 27 Aug 2021 17:53:06 -0400 Subject: integrate helm-chart-generator to runtime api Issue-ID: DCAEGEN2-2694 Issue-ID: DCAEGEN2-2805 Change-Id: I1b7d1bd7d2254280e0faa561e49223357c615090 Signed-off-by: Dhrumin Desai --- .../runtime/web/controllers/GraphController.java | 25 +++-- .../runtime/web/service/BlueprintInventory.java | 20 ++-- .../runtime/web/service/GraphActionsParser.java | 92 ++++++++++++++++ .../runtime/web/service/GraphServiceHelmProxy.java | 120 +++++++++++++++++++++ .../dcae/runtime/web/service/GraphServiceImpl.java | 66 ++---------- 5 files changed, 249 insertions(+), 74 deletions(-) create mode 100644 mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/service/GraphActionsParser.java create mode 100644 mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/service/GraphServiceHelmProxy.java (limited to 'mod/runtimeapi/runtime-web/src/main/java/org') diff --git a/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/controllers/GraphController.java b/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/controllers/GraphController.java index fc222f2..484a426 100644 --- a/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/controllers/GraphController.java +++ b/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/controllers/GraphController.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,28 +17,35 @@ */ package org.onap.dcae.runtime.web.controllers; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; import org.onap.dcae.runtime.core.Edge; import org.onap.dcae.runtime.core.FlowGraph; -import org.onap.dcae.runtime.core.Node; import org.onap.dcae.runtime.core.FlowGraphParser.BlueprintVessel; +import org.onap.dcae.runtime.core.Node; import org.onap.dcae.runtime.web.exception.MainGraphNotFoundException; -import org.onap.dcae.runtime.web.models.*; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; import org.onap.dcae.runtime.web.models.DistributeGraphRequest; import org.onap.dcae.runtime.web.models.GraphRequest; +import org.onap.dcae.runtime.web.service.GraphService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; -import org.onap.dcae.runtime.web.service.GraphServiceImpl; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; import javax.validation.Valid; -import java.util.Map; import java.util.HashMap; import java.util.List; +import java.util.Map; @RestController @RequestMapping(value = "/api/graph") @@ -46,7 +53,7 @@ import java.util.List; public class GraphController { @Autowired - private GraphServiceImpl graphService; + private GraphService graphService; Logger logger = LoggerFactory.getLogger(GraphController.class); diff --git a/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/service/BlueprintInventory.java b/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/service/BlueprintInventory.java index 789c9d1..d63deeb 100644 --- a/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/service/BlueprintInventory.java +++ b/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/service/BlueprintInventory.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ */ package org.onap.dcae.runtime.web.service; +import org.json.JSONException; import org.onap.dcae.runtime.core.FlowGraphParser.BlueprintVessel; import org.onap.dcae.runtime.web.models.DashboardConfig; import org.json.JSONObject; @@ -46,7 +47,6 @@ public class BlueprintInventory { JSONObject body = prepareBlueprintJsonObject(bpv.name, bpv.version, bpv.blueprint); postToDashboard(body); logger.info(String.format("Distributed: %s", bpv.toString())); - //System.out.println(bpv.blueprint); } } @@ -74,12 +74,16 @@ public class BlueprintInventory { private JSONObject prepareBlueprintJsonObject(String blueprintName, int version, String blueprintContent) { JSONObject blueprintJsonObject = new JSONObject(); - blueprintJsonObject.put("owner","dcae_mod"); - blueprintJsonObject.put("typeName",blueprintName); - blueprintJsonObject.put("typeVersion",version); - blueprintJsonObject.put("blueprintTemplate",blueprintContent); - blueprintJsonObject.put("application","DCAE"); - blueprintJsonObject.put("component","dcae"); + try { + blueprintJsonObject.put("owner","dcae_mod"); + blueprintJsonObject.put("typeName",blueprintName); + blueprintJsonObject.put("typeVersion",version); + blueprintJsonObject.put("blueprintTemplate",blueprintContent); + blueprintJsonObject.put("application","DCAE"); + blueprintJsonObject.put("component","dcae"); + } catch (JSONException e) { + e.printStackTrace(); + } return blueprintJsonObject; } diff --git a/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/service/GraphActionsParser.java b/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/service/GraphActionsParser.java new file mode 100644 index 0000000..5d290a7 --- /dev/null +++ b/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/service/GraphActionsParser.java @@ -0,0 +1,92 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.dcae.runtime.web.service; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.onap.dcae.runtime.core.Edge; +import org.onap.dcae.runtime.core.EdgeLocation; +import org.onap.dcae.runtime.core.FlowGraph; +import org.onap.dcae.runtime.core.FlowGraphParser; +import org.onap.dcae.runtime.core.Node; +import org.onap.dcae.runtime.web.exception.ActionsNotDefinedException; +import org.onap.dcae.runtime.web.models.Action; +import org.onap.dcae.runtime.web.models.DistributeGraphRequest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * A helper class that parses the actions from the request and apply them to the main graph + */ +@Component +public class GraphActionsParser { + + @Autowired + private FlowGraphParser flowGraphParser; + + void applyActionsToGraph(FlowGraph mainFlowGraph, DistributeGraphRequest distributeGraphRequest) { + if(distributeGraphRequest.getActions() == null){ + throw new ActionsNotDefinedException("Action(s) must be defined in the request"); + } + for(Action action : distributeGraphRequest.getActions()){ + if(action.getCommand().equals("addnode")){ + Node node = prepareNodeFromAddNAddNodeAction(action); + mainFlowGraph.addNode(node); + } + else if(action.getCommand().equals("addedge")) { + Edge edge = prepareEdgeFromAddEdgeAction(action); + Node srcNode = flowGraphParser.getNodeFromId(edge.getSrc().getNode()); + Node tgtNode = flowGraphParser.getNodeFromId(edge.getTgt().getNode()); + srcNode = fillPlaceholderIfNodeIsEmpty(srcNode); + tgtNode =fillPlaceholderIfNodeIsEmpty(tgtNode); + mainFlowGraph.addEdge(srcNode,tgtNode,edge); + } + } + } + + private Node fillPlaceholderIfNodeIsEmpty(Node node) { + if (node == null) { + node = flowGraphParser.getNodeFromId("dummy_id"); + } + return node; + } + + + private Edge prepareEdgeFromAddEdgeAction(Action action) { + ObjectMapper objectMapper = new ObjectMapper(); + Edge edge = objectMapper.convertValue(action.getPayload(),Edge.class); + return edge; + } + + private void fillPlaceholderIfLocaionsAreEmpty(Edge edge) { + if(edge.getSrc().getNode() == null && edge.getSrc().getPort() == null){ + EdgeLocation src = new EdgeLocation("node-id-placeholder", "node-port-placeholder"); + edge.setSrc(src); + } + if(edge.getTgt().getNode() == null && edge.getTgt().getPort() == null){ + EdgeLocation tgt = new EdgeLocation("node-id-placeholder", "node-port-placeholder"); + edge.setTgt(tgt); + } + } + + private Node prepareNodeFromAddNAddNodeAction(Action action) { + String componentId = (String) action.getPayload().get("component_id"); + String componentName = (String) action.getPayload().get("name"); + String componentSpec = (String) action.getPayload().get("component_spec"); + return new Node(componentId,componentName,componentSpec); + } +} diff --git a/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/service/GraphServiceHelmProxy.java b/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/service/GraphServiceHelmProxy.java new file mode 100644 index 0000000..89c9d47 --- /dev/null +++ b/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/service/GraphServiceHelmProxy.java @@ -0,0 +1,120 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcae.runtime.web.service; + +import org.onap.dcae.runtime.core.BlueprintData; +import org.onap.dcae.runtime.core.Edge; +import org.onap.dcae.runtime.core.FlowGraph; +import org.onap.dcae.runtime.core.FlowGraphParser; +import org.onap.dcae.runtime.core.Node; +import org.onap.dcae.runtime.core.helm.HelmChartGeneratorClient; +import org.onap.dcae.runtime.web.models.DistributeGraphRequest; +import org.onap.dcae.runtime.web.models.GraphRequest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * A proxy class which will be enable if artifact.type is set to HELM + */ +@Service +@Primary +@ConditionalOnProperty( + value="artifact.type", + havingValue = "HELM", + matchIfMissing = true) +@ComponentScan(basePackages = "org.onap.dcae.runtime.core") +public class GraphServiceHelmProxy implements GraphService { + + @Autowired + private GraphService defaultGraphService; + + @Autowired + private HelmChartGeneratorClient helmChartGeneratorClient; + + @Autowired + private GraphActionsParser actionsParser; + + /** + * returns a main graph + * @return main graph + */ + @Override + public FlowGraph getMainGraph() { + return defaultGraphService.getMainGraph(); + } + + /** + * initialize a main graph + * @param mainGraph a main graph request + */ + @Override + public boolean initializeMainGraph(GraphRequest mainGraph) { + return defaultGraphService.initializeMainGraph(mainGraph); + } + + /** + * apply actions on the main graph and creates, distributes helm charts + * @param distributeGraphRequest distribute request + * @return list of distributed charts + */ + @Override + public List distribute(DistributeGraphRequest distributeGraphRequest) { + actionsParser.applyActionsToGraph(getMainGraph(), distributeGraphRequest); + return createHelmCharts(); + } + + public List createHelmCharts() { + final FlowGraph flowGraph = getMainGraph(); + List blueprints = new ArrayList<>(); + for(Node node : flowGraph.getNodes()){ + if(node.getComponentId().equals("dummy_id")){ + continue; + } + final File helmChart = helmChartGeneratorClient.generateHelmChart(node.getComponentSpec()); + helmChartGeneratorClient.distribute(helmChart); + BlueprintData blueprintData = new BlueprintData("1", helmChart.getName()); + node.setBlueprintData(blueprintData); + blueprints.add(createBlueprintVessel(helmChart)); + } + return blueprints; + } + + private FlowGraphParser.BlueprintVessel createBlueprintVessel(File helmChart) { + FlowGraphParser.BlueprintVessel bpv = new FlowGraphParser.BlueprintVessel(); + bpv.blueprint = helmChart.getName(); + bpv.version = 1; + bpv.name = helmChart.getName(); + return bpv; + } + + /** + * deletes main graph + */ + @Override + public void deleteMainGraph() { + defaultGraphService.deleteMainGraph(); + } +} diff --git a/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/service/GraphServiceImpl.java b/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/service/GraphServiceImpl.java index f1329c1..94d4b40 100644 --- a/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/service/GraphServiceImpl.java +++ b/mod/runtimeapi/runtime-web/src/main/java/org/onap/dcae/runtime/web/service/GraphServiceImpl.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,15 +17,15 @@ */ package org.onap.dcae.runtime.web.service; -import org.onap.dcae.runtime.core.*; +import org.onap.dcae.runtime.core.Edge; +import org.onap.dcae.runtime.core.FlowGraph; +import org.onap.dcae.runtime.core.FlowGraphParser; import org.onap.dcae.runtime.core.FlowGraphParser.BlueprintVessel; -import org.onap.dcae.runtime.web.exception.ActionsNotDefinedException; +import org.onap.dcae.runtime.core.Node; import org.onap.dcae.runtime.web.exception.MainGraphAlreadyExistException; import org.onap.dcae.runtime.web.exception.MainGraphNotFoundException; -import org.onap.dcae.runtime.web.models.Action; import org.onap.dcae.runtime.web.models.DistributeGraphRequest; import org.onap.dcae.runtime.web.models.GraphRequest; -import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -46,6 +46,9 @@ public class GraphServiceImpl implements GraphService{ @Autowired private FlowGraphParser flowGraphParser; + @Autowired + private GraphActionsParser actionsParser; + @Override public FlowGraph getMainGraph() { if(mainFlowGraph == null){ @@ -74,7 +77,7 @@ public class GraphServiceImpl implements GraphService{ public List distribute(DistributeGraphRequest distributeGraphRequest) { //1.Iterate through list of actions logger.info("applying actions to graph"); - applyActionsToGraph(distributeGraphRequest); + actionsParser.applyActionsToGraph(mainFlowGraph, distributeGraphRequest); //2. generate blueprint from compspec of the node logger.info("generating blueprints for the affected nodes"); @@ -99,56 +102,5 @@ public class GraphServiceImpl implements GraphService{ return flowGraphParser.createAndProcessBlueprints(); } - private void applyActionsToGraph(DistributeGraphRequest distributeGraphRequest) { - if(distributeGraphRequest.getActions() == null){ - throw new ActionsNotDefinedException("Action(s) must be defined in the request"); - } - for(Action action : distributeGraphRequest.getActions()){ - if(action.getCommand().equals("addnode")){ - Node node = prepareNodeFromAddNAddNodeAction(action); - mainFlowGraph.addNode(node); - } - else if(action.getCommand().equals("addedge")) { - Edge edge = prepareEdgeFromAddEdgeAction(action); - Node srcNode = flowGraphParser.getNodeFromId(edge.getSrc().getNode()); - Node tgtNode = flowGraphParser.getNodeFromId(edge.getTgt().getNode()); - srcNode = fillPlaceholderIfNodeIsEmpty(srcNode); - tgtNode =fillPlaceholderIfNodeIsEmpty(tgtNode); - mainFlowGraph.addEdge(srcNode,tgtNode,edge); - } - } - } - - private Node fillPlaceholderIfNodeIsEmpty(Node node) { - if (node == null) { - node = flowGraphParser.getNodeFromId("dummy_id"); - } - return node; - } - - - private Edge prepareEdgeFromAddEdgeAction(Action action) { - ObjectMapper objectMapper = new ObjectMapper(); - Edge edge = objectMapper.convertValue(action.getPayload(),Edge.class); - return edge; - } - - private void fillPlaceholderIfLocaionsAreEmpty(Edge edge) { - if(edge.getSrc().getNode() == null && edge.getSrc().getPort() == null){ - EdgeLocation src = new EdgeLocation("node-id-placeholder", "node-port-placeholder"); - edge.setSrc(src); - } - if(edge.getTgt().getNode() == null && edge.getTgt().getPort() == null){ - EdgeLocation tgt = new EdgeLocation("node-id-placeholder", "node-port-placeholder"); - edge.setTgt(tgt); - } - } - - private Node prepareNodeFromAddNAddNodeAction(Action action) { - String componentId = (String) action.getPayload().get("component_id"); - String componentName = (String) action.getPayload().get("name"); - String componentSpec = (String) action.getPayload().get("component_spec"); - return new Node(componentId,componentName,componentSpec); - } } -- cgit 1.2.3-korg