fc222f251db8a4fe55f82bf03d4cd335fab01d95
[dcaegen2/platform.git] / mod / runtimeapi / runtime-web / src / main / java / org / onap / dcae / runtime / web / controllers / GraphController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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=========================================================
17  */
18 package org.onap.dcae.runtime.web.controllers;
19
20 import org.onap.dcae.runtime.core.Edge;
21 import org.onap.dcae.runtime.core.FlowGraph;
22 import org.onap.dcae.runtime.core.Node;
23 import org.onap.dcae.runtime.core.FlowGraphParser.BlueprintVessel;
24 import org.onap.dcae.runtime.web.exception.MainGraphNotFoundException;
25 import org.onap.dcae.runtime.web.models.*;
26 import io.swagger.annotations.Api;
27 import io.swagger.annotations.ApiOperation;
28 import org.onap.dcae.runtime.web.models.DistributeGraphRequest;
29 import org.onap.dcae.runtime.web.models.GraphRequest;
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.*;
36 import org.onap.dcae.runtime.web.service.GraphServiceImpl;
37
38 import javax.validation.Valid;
39 import java.util.Map;
40 import java.util.HashMap;
41 import java.util.List;
42
43 @RestController
44 @RequestMapping(value = "/api/graph")
45 @Api(tags = "Graph", description = "API to manage Graph")
46 public class GraphController {
47
48     @Autowired
49     private GraphServiceImpl graphService;
50
51     Logger logger = LoggerFactory.getLogger(GraphController.class);
52
53     @RequestMapping(method = RequestMethod.GET,value = "/main")
54     @ApiOperation("Get main graph")
55     public FlowGraph<Node, Edge> getMainGraph(){
56         return graphService.getMainGraph();
57     }
58
59     @PostMapping(value = "/main")
60     @ApiOperation("Initialize a main graph")
61     public ResponseEntity initializeGraph(@RequestBody @Valid GraphRequest graphRequest){
62         logger.info(graphRequest.toString());
63         graphService.initializeMainGraph(graphRequest);
64         return new ResponseEntity(HttpStatus.CREATED);
65     }
66
67     @PostMapping(value = "/{id}/distribute")
68     @ApiOperation("Submit actions on a given graph and distribute blueprints to the inventory")
69     public ResponseEntity<Map<String,String>> distributeGraph(@PathVariable(value = "id") String id,
70                                                   @RequestBody @Valid DistributeGraphRequest distributeGraphRequest){
71         logger.info(distributeGraphRequest.getActions().toString());
72         List<BlueprintVessel> blueprints = graphService.distribute(distributeGraphRequest);
73
74         Map<String, String> response = new HashMap<>();
75         for (BlueprintVessel bpv : blueprints) {
76             response.put(String.format("%s_%d", bpv.name, bpv.version), bpv.blueprint);
77         }
78         return new ResponseEntity<Map<String,String>>(response, HttpStatus.OK);
79     }
80
81     @DeleteMapping(value = "/main")
82     @ApiOperation("Remove the main graph")
83     public String deleteMainGraph(){
84         graphService.deleteMainGraph();
85         return "Main Graph has been deleted.";
86     }
87
88     @ExceptionHandler
89     @ResponseStatus(HttpStatus.NOT_FOUND)
90     private void graphNotFoundHandler(MainGraphNotFoundException ex){}
91 }