2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
18 package org.onap.dcae.runtime.web.controllers;
20 import io.swagger.annotations.Api;
21 import io.swagger.annotations.ApiOperation;
22 import org.onap.dcae.runtime.core.Edge;
23 import org.onap.dcae.runtime.core.FlowGraph;
24 import org.onap.dcae.runtime.core.FlowGraphParser.BlueprintVessel;
25 import org.onap.dcae.runtime.core.Node;
26 import org.onap.dcae.runtime.web.exception.MainGraphNotFoundException;
27 import org.onap.dcae.runtime.web.models.DistributeGraphRequest;
28 import org.onap.dcae.runtime.web.models.GraphRequest;
29 import org.onap.dcae.runtime.web.service.GraphService;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.http.HttpStatus;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.web.bind.annotation.DeleteMapping;
36 import org.springframework.web.bind.annotation.ExceptionHandler;
37 import org.springframework.web.bind.annotation.PathVariable;
38 import org.springframework.web.bind.annotation.PostMapping;
39 import org.springframework.web.bind.annotation.RequestBody;
40 import org.springframework.web.bind.annotation.RequestMapping;
41 import org.springframework.web.bind.annotation.RequestMethod;
42 import org.springframework.web.bind.annotation.ResponseStatus;
43 import org.springframework.web.bind.annotation.RestController;
45 import javax.validation.Valid;
46 import java.util.HashMap;
47 import java.util.List;
51 @RequestMapping(value = "/api/graph")
52 @Api(tags = "Graph", description = "API to manage Graph")
53 public class GraphController {
56 private GraphService graphService;
58 Logger logger = LoggerFactory.getLogger(GraphController.class);
60 @RequestMapping(method = RequestMethod.GET,value = "/main")
61 @ApiOperation("Get main graph")
62 public FlowGraph<Node, Edge> getMainGraph(){
63 return graphService.getMainGraph();
66 @PostMapping(value = "/main")
67 @ApiOperation("Initialize a main graph")
68 public ResponseEntity initializeGraph(@RequestBody @Valid GraphRequest graphRequest){
69 logger.info(graphRequest.toString());
70 graphService.initializeMainGraph(graphRequest);
71 return new ResponseEntity(HttpStatus.CREATED);
74 @PostMapping(value = "/{id}/distribute")
75 @ApiOperation("Submit actions on a given graph and distribute blueprints to the inventory")
76 public ResponseEntity<Map<String,String>> distributeGraph(@PathVariable(value = "id") String id,
77 @RequestBody @Valid DistributeGraphRequest distributeGraphRequest){
78 logger.info(distributeGraphRequest.getActions().toString());
79 List<BlueprintVessel> blueprints = graphService.distribute(distributeGraphRequest);
81 Map<String, String> response = new HashMap<>();
82 for (BlueprintVessel bpv : blueprints) {
83 response.put(String.format("%s_%d", bpv.name, bpv.version), bpv.blueprint);
85 return new ResponseEntity<Map<String,String>>(response, HttpStatus.OK);
88 @DeleteMapping(value = "/main")
89 @ApiOperation("Remove the main graph")
90 public String deleteMainGraph(){
91 graphService.deleteMainGraph();
92 return "Main Graph has been deleted.";
96 @ResponseStatus(HttpStatus.NOT_FOUND)
97 private void graphNotFoundHandler(MainGraphNotFoundException ex){}