Update license header in misc appc files
[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-2018 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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
31 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
32 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
33 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.AppcProviderService;
34 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.EvacuateInput;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.EvacuateOutput;
36 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.MigrateInput;
37 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.MigrateOutput;
38 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.ModifyConfigInput;
39 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.ModifyConfigOutput;
40 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RebuildInput;
41 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RebuildOutput;
42 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RestartInput;
43 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RestartOutput;
44 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.SnapshotInput;
45 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.SnapshotOutput;
46 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.VmstatuscheckInput;
47 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.VmstatuscheckOutput;
48 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.common.request.header.CommonRequestHeader;
49 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.config.payload.ConfigPayload;
50 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.vnf.resource.VnfResource;
51 import org.opendaylight.yangtools.yang.common.RpcResult;
52 import org.onap.appc.Constants;
53 import org.onap.appc.configuration.Configuration;
54 import org.onap.appc.configuration.ConfigurationFactory;
55 import org.onap.appc.i18n.Msg;
56 import org.onap.appc.provider.topology.TopologyService;
57
58 import java.util.concurrent.ExecutorService;
59 import java.util.concurrent.Executors;
60 import java.util.concurrent.Future;
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 NotificationProviderService 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     /**
127      * @param dataBroker2
128      * @param notificationProviderService
129      * @param rpcProviderRegistry
130      */
131     @SuppressWarnings({
132         "javadoc", "nls"
133     })
134     public AppcProvider(DataBroker dataBroker2, NotificationProviderService notificationProviderService,
135                         RpcProviderRegistry rpcProviderRegistry) {
136
137         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
138         logger.info(Msg.COMPONENT_INITIALIZING, appName, "provider");
139
140         executor = Executors.newFixedThreadPool(1);
141         dataBroker = dataBroker2;
142         notificationService = notificationProviderService;
143         rpcRegistry = rpcProviderRegistry;
144
145         if (rpcRegistry != null) {
146             rpcRegistration = rpcRegistry.addRpcImplementation(AppcProviderService.class, this);
147         }
148
149         logger.info(Msg.COMPONENT_INITIALIZED, appName, "provider");
150     }
151
152     /**
153      * Implements the close of the service
154      *
155      * @see java.lang.AutoCloseable#close()
156      */
157     @SuppressWarnings("nls")
158     @Override
159     public void close() throws Exception {
160         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
161         logger.info(Msg.COMPONENT_TERMINATING, appName, "provider");
162         executor.shutdown();
163         if (rpcRegistration != null) {
164             rpcRegistration.close();
165         }
166         logger.info(Msg.COMPONENT_TERMINATED, appName, "provider");
167     }
168
169     public Future<RpcResult<ModifyConfigOutput>> modifyConfig(ModifyConfigInput input) {
170         CommonRequestHeader hdr = input.getCommonRequestHeader();
171         ConfigPayload data = input.getConfigPayload();
172         RpcResult<ModifyConfigOutput> result = getTopologyService().modifyConfig(hdr, data);
173         return Futures.immediateFuture(result);
174     }
175
176     /**
177      * Rebuilds a specific VNF
178      *
179      * @see org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.AppcProviderService#rebuild(org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RebuildInput)
180      */
181     @Override
182     public Future<RpcResult<RebuildOutput>> rebuild(RebuildInput input) {
183
184         CommonRequestHeader hdr = input.getCommonRequestHeader();
185         VnfResource vnf = input.getVnfResource();
186
187         RpcResult<RebuildOutput> result = getTopologyService().rebuild(hdr, vnf);
188         return Futures.immediateFuture(result);
189     }
190
191     /**
192      * Restarts a specific VNF
193      *
194      * @see org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.AppcProviderService#restart(org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RestartInput)
195      */
196     @Override
197     public Future<RpcResult<RestartOutput>> restart(RestartInput input) {
198         CommonRequestHeader hdr = input.getCommonRequestHeader();
199         VnfResource vnf = input.getVnfResource();
200
201         RpcResult<RestartOutput> result = getTopologyService().restart(hdr, vnf);
202         return Futures.immediateFuture(result);
203     }
204
205     /**
206      * Migrates a specific VNF
207      *
208      * @see org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.AppcProviderService#migrate(org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.MigrateInput)
209      */
210     @Override
211     public Future<RpcResult<MigrateOutput>> migrate(MigrateInput input) {
212         CommonRequestHeader hdr = input.getCommonRequestHeader();
213         VnfResource vnf = input.getVnfResource();
214
215         RpcResult<MigrateOutput> result = getTopologyService().migrate(hdr, vnf);
216         return Futures.immediateFuture(result);
217     }
218
219     /**
220      * Evacuates a specific VNF
221      *
222      * @see org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.AppcProviderService#evacuate(org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.EvacuateInput)
223      */
224     @Override
225     public Future<RpcResult<EvacuateOutput>> evacuate(EvacuateInput input) {
226
227         return null;
228     }
229
230     /**
231      * Evacuates a specific VNF
232      *
233      * @see org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.AppcProviderService#evacuate(org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.EvacuateInput)
234      */
235     @Override
236     public Future<RpcResult<SnapshotOutput>> snapshot(SnapshotInput input) {
237         CommonRequestHeader hdr = input.getCommonRequestHeader();
238         VnfResource vnf = input.getVnfResource();
239
240         RpcResult<SnapshotOutput> result = getTopologyService().snapshot(hdr, vnf);
241         return Futures.immediateFuture(result);
242     }
243
244     /**
245      * Checks status of a VM
246      */
247     @Override
248     public Future<RpcResult<VmstatuscheckOutput>> vmstatuscheck(VmstatuscheckInput input) {
249         CommonRequestHeader hdr = input.getCommonRequestHeader();
250         VnfResource vnf = input.getVnfResource();
251
252         TopologyService topology = getTopologyService();
253         RpcResult<VmstatuscheckOutput> result = getTopologyService().vmstatuscheck(hdr, vnf);
254         return Futures.immediateFuture(result);
255     }
256
257     TopologyService getTopologyService() {
258         return new TopologyService(this);
259     }
260 }