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