DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_be / src / main / java / org / onap / sdc / dcae / composition / controller / VfcmtController.java
1 package org.onap.sdc.dcae.composition.controller;
2
3 import org.onap.sdc.common.onaplog.Enums.LogLevel;
4 import org.onap.sdc.dcae.composition.impl.ReferenceBusinessLogic;
5 import org.onap.sdc.dcae.composition.impl.VfcmtBusinessLogic;
6 import org.onap.sdc.dcae.composition.restmodels.CreateVFCMTRequest;
7 import org.onap.sdc.dcae.composition.restmodels.ImportVFCMTRequest;
8 import org.onap.sdc.dcae.composition.restmodels.sdc.ExternalReferencesMap;
9 import org.onap.sdc.dcae.composition.restmodels.sdc.Resource;
10 import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed;
11 import org.onap.sdc.dcae.errormng.ErrConfMgr.ApiType;
12 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
14 import org.springframework.http.HttpStatus;
15 import org.springframework.http.ResponseEntity;
16 import org.springframework.web.bind.annotation.*;
17
18 import java.util.List;
19
20 @RestController
21 @EnableAutoConfiguration
22 @CrossOrigin
23 public class VfcmtController extends BaseController{
24
25
26         @Autowired
27         private VfcmtBusinessLogic vfcmtBusinessLogic;
28     @Autowired
29     private ReferenceBusinessLogic referenceBusinessLogic;
30
31         private static final String VFCMT = "VFCMT";
32         private static final String TEMPLATE = "Template";
33     private static final String BASE_MONITORING_TEMPLATE = "Base Monitoring Template";
34
35
36
37     /***
38      * Get one resource information
39      * @param theResourceId retrieved resource id
40      * @return ResponseEntity
41      */
42     @RequestMapping(value = { "/resource/{theResourceId}" }, method = { RequestMethod.GET }, produces = {"application/json" })
43     public ResponseEntity resource(@PathVariable String theResourceId, @ModelAttribute("requestId") String requestId) {
44         try {
45             ResourceDetailed resource = baseBusinessLogic.getSdcRestClient().getResource(theResourceId, requestId);
46             return new ResponseEntity<>(resource, HttpStatus.OK);
47         }catch (Exception e) {
48             return handleException(e, ApiType.GET_VFCMT);
49         }
50     }
51
52     /***
53      * Get All resources
54      * @return ResponseEntity
55      */
56     @RequestMapping(value = { "/getResourcesByCategory" }, method = { RequestMethod.GET }, produces = {"application/json" })
57     public ResponseEntity getResourcesByCategory(@ModelAttribute("requestId") String requestId) {
58         try {
59             List<Resource> resources = baseBusinessLogic.getSdcRestClient().getResources(VFCMT, null, null, requestId);
60             return new ResponseEntity<>(resources, HttpStatus.OK);
61         } catch (Exception e) {
62             return handleException(e, ApiType.GET_ALL_VFCMTS);
63         }
64     }
65
66     /***
67      * Get All resources by Service
68      * @return ResponseEntity
69      */
70
71     @RequestMapping(value = { "/{contextType}/{uuid}/{version}/getVfcmtsForMigration" }, method = { RequestMethod.GET }, produces = {"application/json" })
72     public ResponseEntity getVfcmtsForMigration(@RequestHeader("USER_ID") String userId,
73                                                        @PathVariable String contextType,
74                                                        @PathVariable String uuid,
75                                                        @PathVariable String version,
76                                                        @ModelAttribute("requestId") String requestId){
77
78         return vfcmtBusinessLogic.getVfcmtsForMigration(userId, contextType, uuid, version, requestId);
79     }
80
81     /***
82      * Get All resources by Monitoring Template Category
83      * @return ResponseEntity
84      */
85     @RequestMapping(value = { "/getResourcesByMonitoringTemplateCategory" }, method = { RequestMethod.GET }, produces = {"application/json" })
86     public ResponseEntity getResourcesByMonitoringTemplateCategory(@ModelAttribute("requestId") String requestId) {
87         try {
88             List<Resource> resources = baseBusinessLogic.getSdcRestClient().getResources(VFCMT, TEMPLATE, BASE_MONITORING_TEMPLATE, requestId);
89             return new ResponseEntity<>(resources, HttpStatus.OK);
90         } catch (Exception e) {
91             return handleException(e, ApiType.GET_ALL_VFCMTS);
92         }
93     }
94
95     /***
96      * Create new Vfcmt
97      * @param userId retrieved user ID
98      * @param request retrieved request
99      * @return ResponseEntity
100      */
101     @RequestMapping(value = "/createVFCMT", method = RequestMethod.POST, produces = {"application/json" })
102     public ResponseEntity createVFCMT(@RequestHeader("USER_ID") String userId, @RequestBody CreateVFCMTRequest request, @ModelAttribute("requestId") String requestId) {
103                 vfcmtBusinessLogic.addSdcMandatoryFields(request, userId);
104         try {
105             ResourceDetailed response = baseBusinessLogic.getSdcRestClient().createResource(userId, request, requestId);
106             debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "createVFCMT after post: {}", response);
107             return new ResponseEntity<>(response, HttpStatus.OK);
108         } catch (Exception e) {
109             return handleException(e, ApiType.CREATE_NEW_VFCMT);
110         }
111     }
112
113     /***
114      * Create new Vfcmt from general screen
115      * @param userId retrieved user ID
116      * @param request retrieved request
117      * @return ResponseEntity
118      */
119     @RequestMapping(value = "/createMC", method = RequestMethod.POST, produces = {"application/json" })
120     public ResponseEntity createMC(@RequestHeader("USER_ID") String userId, @RequestBody CreateVFCMTRequest request, @ModelAttribute("requestId") String requestId) {
121         return vfcmtBusinessLogic.createMcFromTemplate(userId, request, requestId);
122     }
123
124
125         /***
126          * Clone or import existing VFCMT and attach to selected service/resource
127          * @param userId
128          * @param request
129          * @return ResponseEntity
130          */
131         @RequestMapping(value = "/importMC", method = RequestMethod.POST, produces = {"application/json" })
132         public ResponseEntity importMC(@RequestHeader("USER_ID") String userId, @RequestBody ImportVFCMTRequest request, @ModelAttribute("requestId") String requestId) {
133                 return vfcmtBusinessLogic.importMC(userId, request, requestId);
134         }
135
136     /***
137      * GET a list of Monitoring Components of a service by uuid and version
138      * @param context the context type of this request
139      * @param uuid the uuid of the type requested
140      * @param version the version of the entity requested
141      * @return ResponseEntity
142      */
143     @RequestMapping(value = { "/{context}/{uuid}/{version}/monitoringComponents" }, method = { RequestMethod.GET }, produces = {"application/json" })
144     public ResponseEntity getMonitoringComponents(@PathVariable String context, @PathVariable String uuid, @PathVariable String version, @ModelAttribute("requestId") String requestId) {
145         try {
146             ExternalReferencesMap mcRefs = baseBusinessLogic.getSdcRestClient().getMonitoringReferences(context, uuid, version, requestId);
147             debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Got monitoring references map from SDC: {}", mcRefs.values());
148             return new ResponseEntity<>(referenceBusinessLogic.fetchMonitoringComponents(mcRefs, requestId), HttpStatus.OK);
149         } catch (Exception e) {
150             return handleException(e, ApiType.GET_SERVICE);
151         }
152     }
153
154     @RequestMapping(value = { "/{context}/{serviceUuid}/{vfiName}/{vfcmtUuid}/deleteVfcmtReference" }, method = { RequestMethod.DELETE }, produces = {"application/json" })
155     public ResponseEntity deleteVfcmtReference(@RequestHeader("USER_ID")  String userId, @PathVariable String context, @PathVariable String serviceUuid, @PathVariable String vfiName, @PathVariable String vfcmtUuid, @ModelAttribute String requestId) {
156         try {
157             referenceBusinessLogic.deleteVfcmtReference(userId, context, serviceUuid, vfiName, vfcmtUuid, requestId);
158             return new ResponseEntity<>(HttpStatus.OK);
159         } catch (Exception e) {
160             return handleException(e, ApiType.DELETE_VFCMT_REFERENCE);
161         }
162     }
163
164     @RequestMapping(value = { "/{context}/{monitoringComponentName}/{serviceUuid}/{vfiName}/{vfcmtUuid}/deleteVfcmtReference" }, method = { RequestMethod.DELETE }, produces = {"application/json" })
165     public ResponseEntity deleteVfcmtReferenceWithBlueprint(@RequestHeader("USER_ID")  String userId, @PathVariable String context, @PathVariable String monitoringComponentName, @PathVariable String serviceUuid, @PathVariable String vfiName, @PathVariable String vfcmtUuid, @ModelAttribute String requestId) {
166         try {
167             referenceBusinessLogic.deleteVfcmtReference(userId, context, serviceUuid, vfiName, vfcmtUuid, requestId);
168         } catch (Exception e) {
169             return handleException(e, ApiType.DELETE_VFCMT_REFERENCE);
170         }
171         return referenceBusinessLogic.deleteVfcmtReferenceBlueprint(userId, context, monitoringComponentName, serviceUuid, vfiName, vfcmtUuid, requestId);
172     }
173
174         @RequestMapping(value = { "/getVfcmtReferenceData/{vfcmtUuid}" }, method = { RequestMethod.GET }, produces = {"application/json" })
175         public ResponseEntity getVfcmtReferenceData(@PathVariable String vfcmtUuid, @ModelAttribute String requestId) {
176                 try {
177                         return vfcmtBusinessLogic.getVfcmtReferenceData(vfcmtUuid, requestId);
178                 } catch (Exception e) {
179                         return handleException(e, ApiType.GET_VFCMT);
180                 }
181         }
182
183 }