2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.ccsdk.apps.ms.neng.core.service.rs;
25 import java.util.HashMap;
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;
41 * Implementation of the REST-style interface/API to this micro-service.
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";
50 @Autowired SpringService service;
53 * Heart-beat/ping API.
56 public Response getQuickHello(String name) {
58 HelloWorld hw = service.getQuickHello(name);
59 return Response.ok().entity(hw).build();
63 * Name generation API.
66 public Response generateNetworkElementName(@RequestBody @Valid NameGenRequest request) {
67 log.info("Received request: " + request.toString());
68 Map<String, Object> response = new HashMap<>();
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);
87 public Response releaseNetworkElementName(NameGenRequest request) {
89 Map<String, Object> response = new HashMap<>();
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);
105 * API to return naming policy cached in this micro-service.
108 public Response getPolicyResponse(String policyName) throws Exception {
109 log.info("get-policy: " + policyName);
111 PolicyDetails policyDetails = service.getPolicyDetails(policyName);
112 return Response.ok().entity(policyDetails.getPolicyResponse()).build();
116 * API to add a naming policy to the database cache in this micro-service.
119 public Response addPolicyToDb(Object request) throws Exception {
120 Map<String, Object> response = new HashMap<>();
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);
132 Response buildResponse(Object response) {
133 return Response.ok().entity(response).build();
136 Response buildErrorResponse(Map<String, Object> response) {
137 return Response.status(500).entity(response).build();
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);