95afea213b4f8303389e2b22579c9829dcee5154
[ccsdk/apps.git] / ms / neng / src / main / java / org / onap / ccsdk / apps / ms / neng / core / service / rs / RestServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK.apps
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.ccsdk.apps.ms.neng.core.service.rs;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.logging.Logger;
28 import javax.validation.Valid;
29 import javax.ws.rs.core.Response;
30 import org.onap.ccsdk.apps.ms.neng.core.exceptions.NengException;
31 import org.onap.ccsdk.apps.ms.neng.core.resource.model.HelloWorld;
32 import org.onap.ccsdk.apps.ms.neng.core.resource.model.NameGenRequest;
33 import org.onap.ccsdk.apps.ms.neng.core.resource.model.NameGenResponse;
34 import org.onap.ccsdk.apps.ms.neng.core.service.SpringService;
35 import org.onap.ccsdk.apps.ms.neng.persistence.entity.PolicyDetails;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
38 import org.springframework.web.bind.annotation.RequestBody;
39
40 /**
41  * Implementation of the REST-style interface/API to this micro-service.
42  */
43 @Component
44 public class RestServiceImpl implements RestService {
45     private static Logger log = Logger.getLogger(RestServiceImpl.class.getName());
46     private static final String INTERNAL_ERROR_MSG = "Internal error occured while processing the request.";
47     private static final String ERROR="error";
48     private static final String ERROR_500="err-0500";
49     
50     @Autowired SpringService service;
51
52     /**
53      * Heart-beat/ping API.
54      */
55     @Override
56     public Response getQuickHello(String name) {
57         log.info(name);
58         HelloWorld hw = service.getQuickHello(name);
59         return Response.ok().entity(hw).build();
60     }
61
62     /**
63      * Name generation API.
64      */
65     @Override
66     public Response generateNetworkElementName(@RequestBody @Valid NameGenRequest request) {
67         log.info("Received request: " + request.toString());
68         Map<String, Object> response = new HashMap<>();
69         try {
70             NameGenResponse resp = service.generateOrUpdateName(request);
71             return buildResponse(resp);
72         } catch (NengException e) {
73             log.warning(e.getMessage());
74             response.put(ERROR, buildErrorResponse("NELGEN-0003", e.getMessage()));
75             return buildErrorResponse(response);
76         } catch (Exception e) {
77             log.warning(e.getMessage());
78             response.put(ERROR, buildErrorResponse(ERROR_500, INTERNAL_ERROR_MSG));
79             return buildErrorResponse(response);
80         }
81     }
82
83     /**
84      * Name removal API.
85      */
86     @Override
87     public Response releaseNetworkElementName(NameGenRequest request) {
88         NameGenResponse resp;
89         Map<String, Object> response = new HashMap<>();
90         try {
91             resp = service.releaseNetworkElementName(request);
92             return buildResponse(resp);
93         } catch (NengException e) {
94             log.warning(e.getMessage());
95             response.put(ERROR, buildErrorResponse("NELGEN-0002", e.getMessage()));
96             return buildErrorResponse(response);
97         } catch (Exception e) {
98             log.warning(e.getMessage());
99             response.put(ERROR, buildErrorResponse(ERROR_500, INTERNAL_ERROR_MSG));
100             return buildErrorResponse(response);
101         }
102     }
103
104     /**
105      * API to return naming policy cached in this micro-service.
106      */
107     @Override
108     public Response getPolicyResponse(String policyName) throws Exception {
109         log.info("get-policy: " + policyName);
110         
111         PolicyDetails policyDetails = service.getPolicyDetails(policyName);
112         return Response.ok().entity(policyDetails.getPolicyResponse()).build();
113     }
114
115     /**
116      * API to add a naming policy to the database cache in this micro-service.
117      */
118     @Override
119     public Response addPolicyToDb(Object request) throws Exception {
120         Map<String, Object> response = new HashMap<>();
121         try {
122             service.addPolicy(request);
123             response.put("status", "Policy added successfully");
124             return buildResponse(response);
125         } catch (Exception e) {
126             log.warning(e.getMessage());
127             response.put(ERROR, buildErrorResponse(ERROR_500, e.getMessage()));
128             return buildErrorResponse(response);
129         }
130     }
131
132     Response buildResponse(Object response) {
133         return Response.ok().entity(response).build();
134     }
135
136     Response buildErrorResponse(Map<String, Object> response) {
137         return Response.status(500).entity(response).build();
138     }
139
140     Map<String,Object> buildErrorResponse(String errorCode, String message) {
141         Map<String,Object> error = new HashMap<>();
142         error.put("errorId", errorCode);
143         error.put("message", message);
144         return error;
145     }
146 }