96b2ff0817df43e242a51cb92e6dd7275179c910
[ccsdk/cds.git] / ms / sdclistener / application / src / main / java / org / onap / ccsdk / cds / sdclistener / handler / BluePrintProcesssorHandler.java
1 /*
2  * Copyright © 2019 Bell Canada
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.ccsdk.cds.sdclistener.handler;
17
18 import io.grpc.ManagedChannel;
19 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status;
20 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementOutput;
21 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementServiceGrpc;
22 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementServiceGrpc.BluePrintManagementServiceBlockingStub;
23 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintUploadInput;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.boot.context.properties.ConfigurationProperties;
27 import org.springframework.stereotype.Component;
28
29 @ConfigurationProperties("listenerservice")
30 @Component
31 public class BluePrintProcesssorHandler implements AutoCloseable {
32
33     private static final Logger LOGGER = LoggerFactory.getLogger(BluePrintProcesssorHandler.class);
34
35     private ManagedChannel channel;
36
37     /**
38      * Sending CBA archive to CDS backend to store into its Database.
39      *
40      * @param request BluePrintManagementInput object holds CBA archive, its version and blueprints.
41      * @param managedChannel - ManagedChannel object helps to access the server or application end point.
42      * @return A response object
43      */
44     public Status sendRequest(BluePrintUploadInput request, ManagedChannel managedChannel) {
45         LOGGER.info("Sending request to blueprint processor");
46
47         this.channel = managedChannel;
48
49         final BluePrintManagementServiceBlockingStub syncStub = BluePrintManagementServiceGrpc.newBlockingStub(channel);
50
51         // Send the request to CDS backend.
52         final BluePrintManagementOutput response = syncStub.uploadBlueprint(request);
53
54         return response.getStatus();
55     }
56
57     @Override
58     public void close() {
59         if (channel != null) {
60             channel.shutdown();
61         }
62         LOGGER.info("Stopping GRPC connection to CDS backend");
63     }
64 }