eb1594bfa054af264b86c8435b9ccc15c521f7b7
[appc.git] / appc-inbound / appc-design-services / provider / src / main / java / org / onap / appc / design / services / impl / DesignServicesImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
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  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.design.services.impl;
26
27 import com.google.common.util.concurrent.Futures;
28 import java.util.concurrent.Future;
29 import org.onap.appc.design.dbervices.DbResponseProcessor;
30 import org.onap.appc.design.dbervices.DesignDBService;
31 import org.onap.appc.design.services.util.DesignServiceConstants;
32 import org.onap.appc.design.validator.ValidatorService;
33 import org.onap.appc.design.xinterface.XInterfaceService;
34 import org.onap.appc.design.xinterface.XResponseProcessor;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.DbserviceInput;
36 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.DbserviceOutput;
37 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.DbserviceOutputBuilder;
38 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.DesignServicesService;
39 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.ValidatorInput;
40 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.ValidatorOutput;
41 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.ValidatorOutputBuilder;
42 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.XinterfaceserviceInput;
43 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.XinterfaceserviceOutput;
44 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.XinterfaceserviceOutputBuilder;
45 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.data.DataBuilder;
46 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.status.StatusBuilder;
47 import org.opendaylight.yangtools.yang.common.RpcResult;
48 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 public class DesignServicesImpl implements DesignServicesService {
53
54     private static final Logger log = LoggerFactory.getLogger(DesignServicesImpl.class);
55     private static final String RECEIVED_REQUEST_STR = "Received Request: ";
56     private static final String WITH_PAYLOAD_STR = " with Payload :";
57     private static final String ACTION_STR = " Action : ";
58
59     @Override
60     public Future<RpcResult<DbserviceOutput>> dbservice(DbserviceInput input) {
61
62         log.info(RECEIVED_REQUEST_STR + input.getDesignRequest().getRequestId() + ACTION_STR +
63             input.getDesignRequest().getAction() + WITH_PAYLOAD_STR + input.getDesignRequest().getPayload());
64
65         DbserviceOutputBuilder outputBuilder = new DbserviceOutputBuilder();
66         DataBuilder databuilder = new DataBuilder();
67         StatusBuilder statusBuilder = new StatusBuilder();
68
69         try {
70             DesignDBService dbservices = DesignDBService.initialise();
71             DbResponseProcessor responseProcessor = new DbResponseProcessor();
72             String response = responseProcessor.parseResponse(dbservices
73                 .execute(input.getDesignRequest().getAction(), input.getDesignRequest().getPayload(),
74                     input.getDesignRequest().getRequestId()), input.getDesignRequest().getAction());
75             log.info("Response in for Design Service : " + response);
76             databuilder.setBlock(response);
77             databuilder.setRequestId(input.getDesignRequest().getRequestId());
78             statusBuilder.setCode("400");
79             statusBuilder.setMessage("success");
80         } catch (Exception e) {
81             log.error("Error", e);
82             statusBuilder.setCode("401");
83             statusBuilder.setMessage(e.getMessage());
84         }
85
86         outputBuilder.setData(databuilder.build());
87         outputBuilder.setStatus(statusBuilder.build());
88
89         RpcResult<DbserviceOutput> result = RpcResultBuilder.<DbserviceOutput>status(true)
90             .withResult(outputBuilder.build()).build();
91         return Futures.immediateFuture(result);
92     }
93
94     @Override
95     public Future<RpcResult<XinterfaceserviceOutput>> xinterfaceservice(XinterfaceserviceInput input) {
96         log.info(RECEIVED_REQUEST_STR + input.getDesignRequest().getRequestId() + ACTION_STR +
97             input.getDesignRequest().getAction() + WITH_PAYLOAD_STR + input.getDesignRequest().getPayload());
98         XinterfaceserviceOutputBuilder outputBuilder = new XinterfaceserviceOutputBuilder();
99         DataBuilder databuilder = new DataBuilder();
100         StatusBuilder statusBuilder = new StatusBuilder();
101         try {
102
103             XInterfaceService xInterfaceService = new XInterfaceService();
104             XResponseProcessor responseProcessor = new XResponseProcessor();
105             String response = responseProcessor.parseResponse(
106                 xInterfaceService.execute(input.getDesignRequest().getAction(), input.getDesignRequest().getPayload()),
107                 input.getDesignRequest().getAction());
108             databuilder.setBlock(response);
109             databuilder.setRequestId(input.getDesignRequest().getRequestId());
110             statusBuilder.setCode("400");
111             statusBuilder.setMessage("success");
112         } catch (Exception e) {
113             log.error("An error occurred in xInterfaceService", e);
114             statusBuilder.setCode("401");
115             statusBuilder.setMessage(e.getMessage());
116         }
117         outputBuilder.setData(databuilder.build());
118         outputBuilder.setStatus(statusBuilder.build());
119
120         RpcResult<XinterfaceserviceOutput> result = RpcResultBuilder.<XinterfaceserviceOutput>status(true)
121             .withResult(outputBuilder.build()).build();
122         return Futures.immediateFuture(result);
123     }
124
125     @Override
126     public Future<RpcResult<ValidatorOutput>> validator(ValidatorInput input) {
127         log.info(RECEIVED_REQUEST_STR + input.getDesignRequest().getRequestId() + ACTION_STR +
128             input.getDesignRequest().getAction() + WITH_PAYLOAD_STR + input.getDesignRequest().getPayload()
129             + " and Data Type = " + input.getDesignRequest().getDataType());
130         ValidatorOutputBuilder outputBuilder = new ValidatorOutputBuilder();
131         StatusBuilder statusBuilder = new StatusBuilder();
132
133         build(input, statusBuilder);
134
135         outputBuilder.setStatus(statusBuilder.build());
136
137         RpcResult<ValidatorOutput> result = RpcResultBuilder.<ValidatorOutput>status(true)
138             .withResult(outputBuilder.build()).build();
139         return Futures.immediateFuture(result);
140     }
141
142     private void build(ValidatorInput input, StatusBuilder statusBuilder) {
143         try {
144             if (input.getDesignRequest().getDataType() == null || input.getDesignRequest().getDataType().isEmpty()) {
145                 throw new RequestValidationException("Data Type required for validate Serivce");
146             }
147             if (input.getDesignRequest().getAction() == null || input.getDesignRequest().getAction().isEmpty()) {
148                 throw new RequestValidationException("Action required for validate Serivce");
149             }
150
151             if (validateInput(input)) {
152                 throw new RequestValidationException("Request Data format " + input.getDesignRequest().getDataType()
153                     + " is not supported by validate Service : Supported data types are : XML, YAML, VELOCITY, JSON ");
154             }
155
156             ValidatorService validatorService = new ValidatorService();
157             String response = validatorService
158                 .execute(input.getDesignRequest().getAction(), input.getDesignRequest().getPayload(),
159                     input.getDesignRequest().getDataType());
160             statusBuilder.setCode("400");
161             statusBuilder.setMessage(response);
162         } catch (Exception e) {
163             log.error("An error occurred in validator", e);
164             statusBuilder.setCode("401");
165             statusBuilder.setMessage(e.getMessage());
166         }
167     }
168
169     private boolean validateInput(ValidatorInput input) {
170         return !input.getDesignRequest().getDataType().equals(DesignServiceConstants.DATA_TYPE_JSON) &&
171             !input.getDesignRequest().getDataType().equals(DesignServiceConstants.DATA_TYPE_YAML) &&
172             !input.getDesignRequest().getDataType().equals(DesignServiceConstants.DATA_TYPE_XML) &&
173             !input.getDesignRequest().getDataType().equals(DesignServiceConstants.DATA_TYPE_VELOCITY);
174     }
175 }