77b28701749d63ab5d30e7d0b7852399b4f1a286
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / onap / appc / provider / lcm / service / RebootService.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.provider.lcm.service;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
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.RebootInput;
30 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.RebootOutputBuilder;
31 import org.onap.appc.executor.objects.LCMCommandStatus;
32 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
33 import org.onap.appc.util.JsonUtil;
34
35 import java.io.IOException;
36 import java.util.Arrays;
37 import java.util.List;
38 import java.util.Map;
39
40 /**
41  * Provide LCM command service for rebooting virtual machine (VM)
42  */
43 public class RebootService extends AbstractBaseService {
44     private final EELFLogger myLogger = EELFManager.getInstance().getLogger(RebootService.class);
45     private static final String REBOOT_TYPE_PARAMETER = "reboot-type";
46     private final List<String> rebootTypeList = Arrays.asList("hard", "soft");
47
48     /**
49      * Constructor
50      */
51     public RebootService() {
52         super(Action.Reboot);
53     }
54
55     public RebootOutputBuilder process(RebootInput input) {
56         validate(input);
57         if (status == null) {
58             proceedAction(input);
59         }
60
61         final RebootOutputBuilder outputBuilder = new RebootOutputBuilder();
62         outputBuilder.setCommonHeader(input.getCommonHeader());
63         outputBuilder.setStatus(status);
64         return outputBuilder;
65     }
66
67     private void proceedAction(RebootInput input) {
68         RequestHandlerInput requestHandlerInput = getRequestHandlerInput(
69             input.getCommonHeader(), input.getActionIdentifiers(), input.getPayload(), this.getClass().getName());
70         if (requestHandlerInput != null) {
71             executeAction(requestHandlerInput);
72         }
73     }
74
75     private String getRebootType(RebootInput input) {
76         String rebootType = null;
77         if (input.getPayload() != null) {
78             Map<String, String> payloadMap;
79             try {
80                 payloadMap = JsonUtil.convertJsonStringToFlatMap(input.getPayload().getValue());
81                 rebootType = payloadMap.get(REBOOT_TYPE_PARAMETER);
82             } catch (IOException e) {
83                 myLogger.error("Error in converting payload of RebootInput", e.getMessage());
84             }
85         }
86
87         return rebootType;
88     }
89
90     /**
91      * Validate the input.
92      *
93      * @param input of RebootInput from the REST API input
94      */
95     private void validate(RebootInput input) {
96         status = validateVserverId(input.getCommonHeader(), input.getAction(), input.getActionIdentifiers());
97         if (status != null) {
98             return;
99         }
100
101         //reboot-type validation
102         final String rebootType = getRebootType(input);
103         if (null == rebootType) {
104             status = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, REBOOT_TYPE_PARAMETER);
105         } else if (!rebootTypeList.contains(rebootType)) {
106             status = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, REBOOT_TYPE_PARAMETER);
107         }
108     }
109 }