lowered code smells
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / onap / appc / provider / AppcProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 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;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.common.util.concurrent.Futures;
29 import com.google.common.util.concurrent.ListenableFuture;
30 import org.opendaylight.mdsal.binding.api.DataBroker;
31 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
32 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
33 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
34 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.AppcProviderService;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.EvacuateInput;
36 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.EvacuateOutput;
37 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.MigrateInput;
38 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.MigrateOutput;
39 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.ModifyConfigInput;
40 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.ModifyConfigOutput;
41 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.RebuildInput;
42 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.RebuildOutput;
43 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.RestartInput;
44 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.RestartOutput;
45 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.SnapshotInput;
46 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.SnapshotOutput;
47 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.VmstatuscheckInput;
48 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.VmstatuscheckOutput;
49 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.common.request.header.CommonRequestHeader;
50 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.config.payload.ConfigPayload;
51 import org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.vnf.resource.VnfResource;
52 import org.opendaylight.yangtools.yang.common.RpcResult;
53 import org.onap.appc.Constants;
54 import org.onap.appc.configuration.Configuration;
55 import org.onap.appc.configuration.ConfigurationFactory;
56 import org.onap.appc.i18n.Msg;
57 import org.onap.appc.provider.topology.TopologyService;
58
59 import java.util.concurrent.ExecutorService;
60 import java.util.concurrent.Executors;
61
62 /* ADDED FOR FUSION SERVICE CODE */
63
64 @SuppressWarnings("JavaDoc")
65 /**
66  * Defines the APPC service provider.
67  * <p>
68  * The rpc definition in the YANG model is shown below. This model is used to generate code to manage the inputs and
69  * outputs of the RPC service. For example, the input is defined by a class named {@link ConfigurationOperationInput},
70  * which is generated from the name of the RPC and the "input" definition of the RPC. This class encapsulates the
71  * various objects that are passed to the RPC and is used to obtain values from the input parameters.
72  * </p>
73  * <p>
74  * Likewise, the outputs are defined by a class named {@link ConfigurationOperationOutput}. This class encapsulates the
75  * defined outputs. To make construction of the outputs easier, there are also generated builder classes that are named
76  * for the various elements of the output they "build", such as {@link ConfigurationResponseBuilder}.
77  * </p>
78  *
79  * <pre>
80  *   rpc configuration-operation {
81  *      description "An operation to view, change, or audit the configuration of a VM";
82  *      input {
83  *          uses configuration-request-header;
84  *          uses configuration-request;
85  *      }
86  *      output {
87  *          uses common-response-header;
88  *          uses configuration-response;
89  *      }
90  *  }
91  * </pre>
92  *
93  */
94 public class AppcProvider implements AutoCloseable, AppcProviderService {
95
96     private final EELFLogger logger = EELFManager.getInstance().getLogger(AppcProviderClient.class);
97
98     private final ExecutorService executor;
99
100     /**
101      * The ODL data store broker. Provides access to a conceptual data tree store and also provides the ability to
102      * subscribe for changes to data under a given branch of the tree.
103      */
104     protected DataBroker dataBroker;
105
106     /**
107      * ODL Notification Service that provides publish/subscribe capabilities for YANG modeled notifications.
108      */
109     protected NotificationPublishService notificationService;
110
111     /**
112      * Provides a registry for Remote Procedure Call (RPC) service implementations. The RPCs are defined in YANG models.
113      */
114     protected RpcProviderRegistry rpcRegistry;
115
116     /**
117      * Represents our RPC implementation registration
118      */
119     protected BindingAwareBroker.RpcRegistration<AppcProviderService> rpcRegistration;
120
121     /**
122      * The configuration
123      */
124     private Configuration configuration = ConfigurationFactory.getConfiguration();
125     
126     private AppcProviderClient appcProviderClient;
127
128     /**
129      * @param dataBroker2
130      * @param notificationProviderService
131      * @param rpcProviderRegistry
132      */
133     @SuppressWarnings({
134         "javadoc", "nls"
135     })
136     public AppcProvider(DataBroker dataBroker2, NotificationPublishService notificationProviderService,
137                         RpcProviderRegistry rpcProviderRegistry, AppcProviderClient appcProviderClient) {
138
139         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
140         logger.info(Msg.COMPONENT_INITIALIZING, appName, "provider");
141
142         executor = Executors.newFixedThreadPool(1);
143         dataBroker = dataBroker2;
144         notificationService = notificationProviderService;
145         rpcRegistry = rpcProviderRegistry;
146         this.appcProviderClient = appcProviderClient;
147
148         logger.info(Msg.COMPONENT_INITIALIZED, appName, "provider");
149     }
150
151     /**
152      * Implements the close of the service
153      *
154      * @see java.lang.AutoCloseable#close()
155      */
156     @SuppressWarnings("nls")
157     @Override
158     public void close() throws Exception {
159         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
160         logger.info(Msg.COMPONENT_TERMINATING, appName, "provider");
161         executor.shutdown();
162         if (rpcRegistration != null) {
163             rpcRegistration.close();
164         }
165         logger.info(Msg.COMPONENT_TERMINATED, appName, "provider");
166     }
167
168     public ListenableFuture<RpcResult<ModifyConfigOutput>> modifyConfig(ModifyConfigInput input) {
169         CommonRequestHeader hdr = input.getCommonRequestHeader();
170         ConfigPayload data = input.getConfigPayload();
171         RpcResult<ModifyConfigOutput> result = getTopologyService().modifyConfig(hdr, data);
172         return Futures.immediateFuture(result);
173     }
174
175     /**
176      * Rebuilds a specific VNF
177      *
178      * @see org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.AppcProviderService#rebuild(org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.RebuildInput)
179      */
180     @Override
181     public ListenableFuture<RpcResult<RebuildOutput>> rebuild(RebuildInput input) {
182
183         CommonRequestHeader hdr = input.getCommonRequestHeader();
184         VnfResource vnf = input.getVnfResource();
185
186         RpcResult<RebuildOutput> result = getTopologyService().rebuild(hdr, vnf);
187         return Futures.immediateFuture(result);
188     }
189
190     /**
191      * Restarts a specific VNF
192      *
193      * @see org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.AppcProviderService#restart(org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.RestartInput)
194      */
195     @Override
196     public ListenableFuture<RpcResult<RestartOutput>> restart(RestartInput input) {
197         CommonRequestHeader hdr = input.getCommonRequestHeader();
198         VnfResource vnf = input.getVnfResource();
199
200         RpcResult<RestartOutput> result = getTopologyService().restart(hdr, vnf);
201         return Futures.immediateFuture(result);
202     }
203
204     /**
205      * Migrates a specific VNF
206      *
207      * @see org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.AppcProviderService#migrate(org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.MigrateInput)
208      */
209     @Override
210     public ListenableFuture<RpcResult<MigrateOutput>> migrate(MigrateInput input) {
211         CommonRequestHeader hdr = input.getCommonRequestHeader();
212         VnfResource vnf = input.getVnfResource();
213
214         RpcResult<MigrateOutput> result = getTopologyService().migrate(hdr, vnf);
215         return Futures.immediateFuture(result);
216     }
217
218     /**
219      * Evacuates a specific VNF
220      *
221      * @see org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.AppcProviderService#evacuate(org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.EvacuateInput)
222      */
223     @Override
224     public ListenableFuture<RpcResult<EvacuateOutput>> evacuate(EvacuateInput input) {
225
226         return null;
227     }
228
229     /**
230      * Evacuates a specific VNF
231      *
232      * @see org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.AppcProviderService#evacuate(org.opendaylight.yang.gen.v1.org.onap.appc.provider.rev160104.EvacuateInput)
233      */
234     @Override
235     public ListenableFuture<RpcResult<SnapshotOutput>> snapshot(SnapshotInput input) {
236         CommonRequestHeader hdr = input.getCommonRequestHeader();
237         VnfResource vnf = input.getVnfResource();
238
239         RpcResult<SnapshotOutput> result = getTopologyService().snapshot(hdr, vnf);
240         return Futures.immediateFuture(result);
241     }
242
243     /**
244      * Checks status of a VM
245      */
246     @Override
247     public ListenableFuture<RpcResult<VmstatuscheckOutput>> vmstatuscheck(VmstatuscheckInput input) {
248         CommonRequestHeader hdr = input.getCommonRequestHeader();
249         VnfResource vnf = input.getVnfResource();
250
251         TopologyService topology = getTopologyService();
252         RpcResult<VmstatuscheckOutput> result = getTopologyService().vmstatuscheck(hdr, vnf);
253         return Futures.immediateFuture(result);
254     }
255
256     TopologyService getTopologyService() {
257         return new TopologyService(this);
258     }
259     
260     public AppcProviderClient getClient() {
261         return appcProviderClient;
262     }
263 }