3671352653947a7e23cdf56b33d3ef685494ee55
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Orange
6  * =============================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.provider.lcm.service;
23
24
25 import org.onap.appc.executor.objects.LCMCommandStatus;
26 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
27 import org.onap.appc.util.JsonUtil;
28 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Action;
29 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.DistributeTrafficInput;
30 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.DistributeTrafficOutputBuilder;
31
32 import java.io.IOException;
33 import java.util.Map;
34
35 /**
36  * Provide LCM command service for Distribute Traffic between VNFs.
37  */
38 public class DistributeTrafficService extends AbstractBaseService {
39
40
41     private static final String CONFIG_FILE_NAME_PARAMETER = "ConfigFileName";
42     private static final String PAYLOAD = "payload";
43
44     /**
45      * Constructor
46      */
47     public DistributeTrafficService() {
48         super(Action.DistributeTraffic);
49         logger.debug("DistributeTrafficService starts");
50     }
51
52     /**
53      * Process a DistributeTraffic request
54      * @param input of DistributeTrafficInput from the REST API input
55      * @return DistributeTrafficOutputBuilder which has the process results
56      */
57     public DistributeTrafficOutputBuilder process(DistributeTrafficInput input) {
58
59         validate(input);
60         if (status == null) {
61             proceedAction(input);
62         }
63
64         DistributeTrafficOutputBuilder outputBuilder = new DistributeTrafficOutputBuilder();
65         outputBuilder.setStatus(status);
66         outputBuilder.setCommonHeader(input.getCommonHeader());
67         return outputBuilder;
68     }
69
70     /**
71      * Validate input.
72      * Set Status if any error detected.
73      *
74      * @param input of DistributeTrafficInput from the REST API input
75      */
76     void validate(DistributeTrafficInput input) {
77         status = validateVnfId(input.getCommonHeader(), input.getAction(), input.getActionIdentifiers());
78         if (status != null) {
79             return;
80         }
81
82         if (input.getPayload() == null) {
83             status = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, PAYLOAD);
84             return;
85         }
86         String payloadString = input.getPayload().getValue();
87         status = validateMustHaveParamValue(payloadString == null ? payloadString : payloadString.trim(), PAYLOAD);
88         if (status != null) {
89             return;
90         }
91
92         try {
93             Map<String, String> payloadMap = JsonUtil.convertJsonStringToFlatMap(payloadString);
94             // ConfigFileName validation
95             final String configFileName = payloadMap.get(CONFIG_FILE_NAME_PARAMETER);
96             if (configFileName == null) {
97                 status = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, CONFIG_FILE_NAME_PARAMETER);
98             }
99
100         } catch(IOException e) {
101             logger.error(String.format("DistributeTrafficService (%s) got IOException when converting payload", rpcName), e);
102             status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, e.getMessage());
103         }
104     }
105
106     void proceedAction(DistributeTrafficInput input) {
107         RequestHandlerInput requestHandlerInput = getRequestHandlerInput(
108                 input.getCommonHeader(), input.getActionIdentifiers(), input.getPayload(), this.getClass().getName());
109         if (requestHandlerInput != null) {
110             executeAction(requestHandlerInput);
111         }
112     }
113
114 }