56690a8c384ad67e1ffd51948adf5f414c6de69d
[so.git] / asdc-controller / src / main / java / org / onap / so / asdc / client / test / rest / ASDCRestInterface.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
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  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.asdc.client.test.rest;
24
25
26 import java.io.IOException;
27 import javax.transaction.Transactional;
28 import javax.ws.rs.HeaderParam;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response;
34 import org.onap.so.logger.LoggingAnchor;
35 import org.onap.so.asdc.client.ASDCController;
36 import org.onap.so.asdc.client.exceptions.ASDCControllerException;
37 import org.onap.so.asdc.client.exceptions.ASDCParametersException;
38 import org.onap.so.asdc.client.test.emulators.DistributionClientEmulator;
39 import org.onap.so.asdc.client.test.emulators.NotificationDataImpl;
40 import org.onap.so.asdc.client.test.emulators.JsonStatusData;
41 import org.onap.so.asdc.installer.heat.ToscaResourceInstaller;
42 import org.onap.so.logger.ErrorCode;
43 import org.onap.so.logger.MessageEnum;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.context.annotation.Profile;
48 import org.springframework.stereotype.Component;
49
50 /**
51  * This is a TEST only rest interface. It is not used in production, it is used to aid in testing the ASDC service on
52  * jboss without the need to be connected to the ASDC service broker. It starts the test at the treatNotification step
53  * and simulates both the notification step as well as the artifact download step.
54  * <p>
55  * i.e. http://localhost:8080/asdc/treatNotification/v1
56  * <p>
57  * i.e. http://localhost:8080/asdc/statusData/v1
58  *
59  * @author jm5423
60  */
61
62 @Path("/")
63 @Component
64 @Profile("test")
65 public class ASDCRestInterface {
66
67     private static final Logger logger = LoggerFactory.getLogger(ASDCRestInterface.class);
68
69     @Autowired
70     private ASDCController asdcController;
71
72     @Autowired
73     private ToscaResourceInstaller toscaInstaller;
74
75     @POST
76     @Path("/treatNotification/v1")
77     @Produces(MediaType.APPLICATION_JSON)
78     @Transactional
79     public Response invokeASDCService(NotificationDataImpl request,
80             @HeaderParam("resource-location") String resourceLocation)
81             throws ASDCControllerException, ASDCParametersException, IOException {
82         DistributionClientEmulator distributionClientEmulator = new DistributionClientEmulator(resourceLocation);
83
84         asdcController.setControllerName("asdc-controller1");
85         asdcController.setDistributionClient(distributionClientEmulator);
86         asdcController.initASDC();
87         asdcController.treatNotification(request);
88         asdcController.closeASDC();
89         return Response.status(200).build();
90     }
91
92     @POST
93     @Path("/statusData/v1")
94     @Produces(MediaType.APPLICATION_JSON)
95     @Transactional
96     public Response invokeASDCStatusData(String request) {
97
98         try {
99             DistributionClientEmulator distributionClientEmulator =
100                     new DistributionClientEmulator("resource-examples/");
101             JsonStatusData statusData = JsonStatusData.instantiateNotifFromJsonFile("resource-examples/");
102
103             ASDCController controller = new ASDCController("asdc-controller1", distributionClientEmulator);
104             controller.initASDC();
105             toscaInstaller.installTheComponentStatus(statusData);
106             controller.closeASDC();
107
108             logger.info(LoggingAnchor.FOUR, MessageEnum.ASDC_ARTIFACT_DEPLOY_SUC.toString(),
109                     statusData.getDistributionID(), "ASDC", "ASDC Updates Are Complete");
110         } catch (Exception e) {
111             logger.info("Error caught " + e.getMessage());
112             logger.error(LoggingAnchor.SIX, MessageEnum.ASDC_GENERAL_EXCEPTION.toString(),
113                     "Exception caught during ASDCRestInterface", "ASDC", "invokeASDCService",
114                     ErrorCode.BusinessProcesssError.getValue(), "Exception in invokeASDCService", e);
115         }
116
117         return null;
118     }
119 }