Fixing ASDCRestInterface for CSIT to simulate SDC req
[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 javax.transaction.Transactional;
27 import javax.ws.rs.HeaderParam;
28 import javax.ws.rs.POST;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.Response;
33 import javax.ws.rs.core.Response.Status;
34 import org.onap.so.asdc.client.ASDCController;
35 import org.onap.so.asdc.client.test.emulators.DistributionClientEmulator;
36 import org.onap.so.asdc.client.test.emulators.JsonStatusData;
37 import org.onap.so.asdc.client.test.emulators.NotificationDataImpl;
38 import org.onap.so.asdc.installer.heat.ToscaResourceInstaller;
39 import org.onap.so.logger.ErrorCode;
40 import org.onap.so.logger.LoggingAnchor;
41 import org.onap.so.logger.MessageEnum;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.context.annotation.Profile;
46 import org.springframework.stereotype.Component;
47
48 /**
49  * This is a TEST only rest interface. It is not used in production, it is used to aid in testing the ASDC service
50  * without the need to be connected to the ASDC service broker. It starts the test at the treatNotification step and
51  * simulates both the notification step as well as the artifact download step.
52  * <p>
53  * i.e. http://localhost:8085/test/treatNotification/v1
54  * <p>
55  * i.e. http://localhost:8085/test/statusData/v1
56  * 
57  * This interface is also used in CSIT to simulate a distribution of a service, without using SDC
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     private final ASDCController asdcController;
70
71     private final ToscaResourceInstaller toscaInstaller;
72
73     @Autowired
74     public ASDCRestInterface(final ASDCController asdcController, final ToscaResourceInstaller toscaInstaller) {
75         this.asdcController = asdcController;
76         this.toscaInstaller = toscaInstaller;
77     }
78
79     @POST
80     @Path("/treatNotification/v1")
81     @Produces(MediaType.APPLICATION_JSON)
82     @Transactional
83     public Response invokeASDCService(final NotificationDataImpl request,
84             @HeaderParam("resource-location") final String resourceLocation) {
85
86         try {
87             logger.info("Received message : {}", request);
88             logger.info("resource-location : {}", resourceLocation);
89             final DistributionClientEmulator distributionClientEmulator =
90                     getDistributionClientEmulator(resourceLocation);
91
92             asdcController.setControllerName("asdc-controller1");
93             asdcController.setDistributionClient(distributionClientEmulator);
94
95             if (asdcController.isStopped()) {
96                 logger.info("{} not running will try to initialize it, currrent status: {}",
97                         asdcController.getClass().getName(), asdcController.getControllerStatus());
98                 asdcController.initASDC();
99             }
100
101             asdcController.treatNotification(request);
102
103             if (!asdcController.isBusy()) {
104                 asdcController.closeASDC();
105             }
106
107             return Response.status(Status.OK).build();
108         } catch (final Exception exception) {
109             logger.error("Unable to process notification request", exception);
110             return Response.status(Status.INTERNAL_SERVER_ERROR).build();
111         }
112
113     }
114
115     private DistributionClientEmulator getDistributionClientEmulator(final String resourceLocation) {
116         return new DistributionClientEmulator(resourceLocation);
117     }
118
119     @POST
120     @Path("/statusData/v1")
121     @Produces(MediaType.APPLICATION_JSON)
122     @Transactional
123     public Response invokeASDCStatusData(final String request) {
124
125         try {
126             final DistributionClientEmulator distributionClientEmulator =
127                     getDistributionClientEmulator("resource-examples/");
128             final JsonStatusData statusData = JsonStatusData.instantiateNotifFromJsonFile("resource-examples/");
129
130             asdcController.setDistributionClient(distributionClientEmulator);
131             asdcController.initASDC();
132             toscaInstaller.installTheComponentStatus(statusData);
133             asdcController.closeASDC();
134
135             logger.info(LoggingAnchor.FOUR, MessageEnum.ASDC_ARTIFACT_DEPLOY_SUC.toString(),
136                     statusData.getDistributionID(), "ASDC", "ASDC Updates Are Complete");
137         } catch (final Exception e) {
138             logger.info("Error caught " + e.getMessage());
139             logger.error(LoggingAnchor.SIX, MessageEnum.ASDC_GENERAL_EXCEPTION.toString(),
140                     "Exception caught during ASDCRestInterface", "ASDC", "invokeASDCService",
141                     ErrorCode.BusinessProcesssError.getValue(), "Exception in invokeASDCService", e);
142         }
143
144         return null;
145     }
146 }