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