890f40d3d9b008b47b0f02ddfb8afb271277b653
[appc.git] /
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  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.design.services.impl;
27
28 import com.google.common.util.concurrent.Futures;
29 import java.util.concurrent.Future;
30 import org.onap.appc.design.dbervices.DbResponseProcessor;
31 import org.onap.appc.design.dbervices.DesignDBService;
32 import org.onap.appc.design.services.util.DesignServiceConstants;
33 import org.onap.appc.design.validator.ValidatorService;
34 import org.onap.appc.design.xinterface.XInterfaceService;
35 import org.onap.appc.design.xinterface.XResponseProcessor;
36 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.DbserviceInput;
37 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.DbserviceOutput;
38 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.DbserviceOutputBuilder;
39 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.DesignServicesService;
40 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.ValidatorInput;
41 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.ValidatorOutput;
42 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.ValidatorOutputBuilder;
43 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.XinterfaceserviceInput;
44 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.XinterfaceserviceOutput;
45 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.XinterfaceserviceOutputBuilder;
46 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.data.DataBuilder;
47 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.status.StatusBuilder;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 public class DesignServicesImpl implements DesignServicesService {
54
55     private static final Logger log = LoggerFactory.getLogger(DesignServicesImpl.class);
56     private static final String RECEIVED_REQUEST_STR = "Received Request: ";
57     private static final String WITH_PAYLOAD_STR = " with Payload :";
58     private static final String ACTION_STR = " Action : ";
59
60     @Override
61     public Future<RpcResult<DbserviceOutput>> dbservice(DbserviceInput input) {
62
63         log.info(RECEIVED_REQUEST_STR + input.getDesignRequest().getRequestId() + ACTION_STR +
64             input.getDesignRequest().getAction() + WITH_PAYLOAD_STR + input.getDesignRequest().getPayload());
65
66         DbserviceOutputBuilder outputBuilder = new DbserviceOutputBuilder();
67         DataBuilder databuilder = new DataBuilder();
68         StatusBuilder statusBuilder = new StatusBuilder();
69
70         try {
71             DesignDBService dbservices = DesignDBService.initialise();
72             DbResponseProcessor responseProcessor = new DbResponseProcessor();
73             String response = responseProcessor.parseResponse(dbservices
74                 .execute(input.getDesignRequest().getAction(), input.getDesignRequest().getPayload(),
75                     input.getDesignRequest().getRequestId()), input.getDesignRequest().getAction());
76             log.info("Response in for Design Service : " + response);
77             databuilder.setBlock(response);
78             databuilder.setRequestId(input.getDesignRequest().getRequestId());
79             statusBuilder.setCode("400");
80             statusBuilder.setMessage("success");
81         } catch (Exception e) {
82             log.error("Error", e);
83             statusBuilder.setCode("401");
84             statusBuilder.setMessage(e.getMessage());
85         }
86
87         outputBuilder.setData(databuilder.build());
88         outputBuilder.setStatus(statusBuilder.build());
89
90         RpcResult<DbserviceOutput> result = RpcResultBuilder.<DbserviceOutput>status(true)
91             .withResult(outputBuilder.build()).build();
92         return Futures.immediateFuture(result);
93     }
94
95     @Override
96     public Future<RpcResult<XinterfaceserviceOutput>> xinterfaceservice(XinterfaceserviceInput input) {
97         log.info(RECEIVED_REQUEST_STR + input.getDesignRequest().getRequestId() + ACTION_STR +
98             input.getDesignRequest().getAction() + WITH_PAYLOAD_STR + input.getDesignRequest().getPayload());
99         XinterfaceserviceOutputBuilder outputBuilder = new XinterfaceserviceOutputBuilder();
100         DataBuilder databuilder = new DataBuilder();
101         StatusBuilder statusBuilder = new StatusBuilder();
102         try {
103
104             XInterfaceService xInterfaceService = XInterfaceService.getInstance();
105             XResponseProcessor responseProcessor = XResponseProcessor.getInstance();
106             String response = responseProcessor.parseResponse(
107                 xInterfaceService.execute(input.getDesignRequest().getAction(), input.getDesignRequest().getPayload()),
108                 input.getDesignRequest().getAction());
109             databuilder.setBlock(response);
110             databuilder.setRequestId(input.getDesignRequest().getRequestId());
111             statusBuilder.setCode("400");
112             statusBuilder.setMessage("success");
113         } catch (Exception e) {
114             log.error("An error occurred in xInterfaceService", e);
115             statusBuilder.setCode("401");
116             statusBuilder.setMessage(e.getMessage());
117         }
118         outputBuilder.setData(databuilder.build());
119         outputBuilder.setStatus(statusBuilder.build());
120
121         RpcResult<XinterfaceserviceOutput> result = RpcResultBuilder.<XinterfaceserviceOutput>status(true)
122             .withResult(outputBuilder.build()).build();
123         return Futures.immediateFuture(result);
124     }
125
126     @Override
127     public Future<RpcResult<ValidatorOutput>> validator(ValidatorInput input) {
128         log.info(RECEIVED_REQUEST_STR + input.getDesignRequest().getRequestId() + ACTION_STR +
129             input.getDesignRequest().getAction() + WITH_PAYLOAD_STR + input.getDesignRequest().getPayload()
130             + " and Data Type = " + input.getDesignRequest().getDataType());
131         ValidatorOutputBuilder outputBuilder = new ValidatorOutputBuilder();
132         StatusBuilder statusBuilder = new StatusBuilder();
133
134         build(input, statusBuilder);
135
136         outputBuilder.setStatus(statusBuilder.build());
137
138         RpcResult<ValidatorOutput> result = RpcResultBuilder.<ValidatorOutput>status(true)
139             .withResult(outputBuilder.build()).build();
140         return Futures.immediateFuture(result);
141     }
142
143     private void build(ValidatorInput input, StatusBuilder statusBuilder) {
144         try {
145             if (input.getDesignRequest().getDataType() == null || input.getDesignRequest().getDataType().isEmpty()) {
146                 throw new RequestValidationException("Data Type required for validate Serivce");
147             }
148             if (input.getDesignRequest().getAction() == null || input.getDesignRequest().getAction().isEmpty()) {
149                 throw new RequestValidationException("Action required for validate Serivce");
150             }
151
152             if (validateInput(input)) {
153                 throw new RequestValidationException("Request Data format " + input.getDesignRequest().getDataType()
154                     + " is not supported by validate Service : Supported data types are : XML, YAML, VELOCITY, JSON ");
155             }
156
157             ValidatorService validatorService = new ValidatorService();
158             String response = validatorService
159                 .execute(input.getDesignRequest().getAction(), input.getDesignRequest().getPayload(),
160                     input.getDesignRequest().getDataType());
161             statusBuilder.setCode("400");
162             statusBuilder.setMessage(response);
163         } catch (Exception e) {
164             log.error("An error occurred in validator", e);
165             statusBuilder.setCode("401");
166             statusBuilder.setMessage(e.getMessage());
167         }
168     }
169
170     private boolean validateInput(ValidatorInput input) {
171         return !input.getDesignRequest().getDataType().equals(DesignServiceConstants.DATA_TYPE_JSON) &&
172             !input.getDesignRequest().getDataType().equals(DesignServiceConstants.DATA_TYPE_YAML) &&
173             !input.getDesignRequest().getDataType().equals(DesignServiceConstants.DATA_TYPE_XML) &&
174             !input.getDesignRequest().getDataType().equals(DesignServiceConstants.DATA_TYPE_VELOCITY);
175     }
176 }