c65cc656e835fbf610f021ca10319fb5b574f845
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / openecomp / appc / provider / AppcProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.provider;
23
24 import com.google.common.util.concurrent.Futures;
25 import org.json.JSONObject;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
28 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
29 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
30 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
31 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.*;
32 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.common.request.header.CommonRequestHeader;
33 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.responseattributes.StatusBuilder;
34 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.responseheader.ResponseHeaderBuilder;
35 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.vnf.resource.VnfResource;
36 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.config.payload.ConfigPayload;
37 import org.opendaylight.yangtools.concepts.ListenerRegistration;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
40 import org.openecomp.appc.Constants;
41 import org.openecomp.appc.configuration.Configuration;
42 import org.openecomp.appc.configuration.ConfigurationFactory;
43 import org.openecomp.appc.executor.objects.LCMCommandStatus;
44 import org.openecomp.appc.executor.objects.Params;
45 import org.openecomp.appc.i18n.Msg;
46 import org.openecomp.appc.provider.lcm.util.RequestInputBuilder;
47 import org.openecomp.appc.provider.lcm.util.ValidationService;
48 import org.openecomp.appc.provider.topology.TopologyService;
49 import org.openecomp.appc.requesthandler.RequestHandler;
50 import org.openecomp.appc.requesthandler.objects.RequestHandlerInput;
51 import org.openecomp.appc.requesthandler.objects.RequestHandlerOutput;
52 import com.att.eelf.configuration.EELFLogger;
53 import com.att.eelf.configuration.EELFManager;
54 import com.att.eelf.i18n.EELFResourceManager;
55 import org.osgi.framework.BundleContext;
56 import org.osgi.framework.FrameworkUtil;
57 import org.osgi.framework.ServiceReference;
58 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.status.Status;
59 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.Action;
60
61 import java.text.DateFormat;
62 import java.text.ParseException;
63 import java.text.SimpleDateFormat;
64 import java.util.Date;
65 import java.util.Iterator;
66 import java.util.Properties;
67 import java.util.TimeZone;
68 import java.util.concurrent.ExecutorService;
69 import java.util.concurrent.Executors;
70 import java.util.concurrent.Future;
71
72 /* ADDED FOR FUSION SERVICE CODE */
73
74 @SuppressWarnings("JavaDoc")
75 /**
76  * Defines the APPC service provider.
77  * <p>
78  * The rpc definition in the YANG model is shown below. This model is used to generate code to manage the inputs and
79  * outputs of the RPC service. For example, the input is defined by a class named {@link ConfigurationOperationInput},
80  * which is generated from the name of the RPC and the "input" definition of the RPC. This class encapsulates the
81  * various objects that are passed to the RPC and is used to obtain values from the input parameters.
82  * </p>
83  * <p>
84  * Likewise, the outputs are defined by a class named {@link ConfigurationOperationOutput}. This class encapsulates the
85  * defined outputs. To make construction of the outputs easier, there are also generated builder classes that are named
86  * for the various elements of the output they "build", such as {@link ConfigurationResponseBuilder}.
87  * </p>
88  *
89  * <pre>
90  *   rpc configuration-operation {
91  *      description "An operation to view, change, or audit the configuration of a VM";
92  *      input {
93  *          uses configuration-request-header;
94  *          uses configuration-request;
95  *      }
96  *      output {
97  *          uses common-response-header;
98  *          uses configuration-response;
99  *      }
100  *  }
101  * </pre>
102  *
103  */
104 public class AppcProvider implements AutoCloseable, AppcProviderService {
105
106     private final EELFLogger logger = EELFManager.getInstance().getLogger(AppcProviderClient.class);
107
108     private final ExecutorService executor;
109
110     private ListenerRegistration<DataChangeListener> dclServices;
111
112     /**
113      * The ODL data store broker. Provides access to a conceptual data tree store and also provides the ability to
114      * subscribe for changes to data under a given branch of the tree.
115      */
116     protected DataBroker dataBroker;
117
118     /**
119      * ODL Notification Service that provides publish/subscribe capabilities for YANG modeled notifications.
120      */
121     protected NotificationProviderService notificationService;
122
123     /**
124      * Provides a registry for Remote Procedure Call (RPC) service implementations. The RPCs are defined in YANG models.
125      */
126     protected RpcProviderRegistry rpcRegistry;
127
128     /**
129      * Represents our RPC implementation registration
130      */
131     protected BindingAwareBroker.RpcRegistration<AppcProviderService> rpcRegistration;
132
133     /**
134      * The configuration
135      */
136     private Configuration configuration = ConfigurationFactory.getConfiguration();
137
138     /**
139      * @param dataBroker2
140      * @param notificationProviderService
141      * @param rpcProviderRegistry
142      */
143     @SuppressWarnings({
144         "javadoc", "nls"
145     })
146     public AppcProvider(DataBroker dataBroker2, NotificationProviderService notificationProviderService,
147                         RpcProviderRegistry rpcProviderRegistry) {
148
149         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
150         logger.info(Msg.COMPONENT_INITIALIZING, appName, "provider");
151
152         executor = Executors.newFixedThreadPool(1);
153         dataBroker = dataBroker2;
154         notificationService = notificationProviderService;
155         rpcRegistry = rpcProviderRegistry;
156
157         if (rpcRegistry != null) {
158                 logger.info("rpcRegistry was not null");
159             rpcRegistration = rpcRegistry.addRpcImplementation(AppcProviderService.class, this);
160         } else {
161                 logger.error("rpcRegistry WAS NULL");
162         }
163
164         logger.info(Msg.COMPONENT_INITIALIZED, appName, "provider");
165     }
166
167     /**
168      * Implements the close of the service
169      *
170      * @see java.lang.AutoCloseable#close()
171      */
172     @SuppressWarnings("nls")
173     @Override
174     public void close() throws Exception {
175         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
176         logger.info(Msg.COMPONENT_TERMINATING, appName, "provider");
177         executor.shutdown();
178         if (rpcRegistration != null) {
179             rpcRegistration.close();
180         }
181         logger.info(Msg.COMPONENT_TERMINATED, appName, "provider");
182     }
183
184
185 public Future<RpcResult<ModifyConfigOutput>> modifyConfig(ModifyConfigInput input){
186         CommonRequestHeader hdr = input.getCommonRequestHeader();        
187         ConfigPayload data = input.getConfigPayload();
188     TopologyService topology = new TopologyService(this);
189     RpcResult<ModifyConfigOutput> result = topology.modifyConfig(hdr, data);
190     return Futures.immediateFuture(result);
191 }
192     /**
193      * Rebuilds a specific VNF
194      *
195      * @see org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.AppcProviderService#rebuild(org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.RebuildInput)
196      */
197     @Override
198     public Future<RpcResult<RebuildOutput>> rebuild(RebuildInput input) {
199
200         CommonRequestHeader hdr = input.getCommonRequestHeader();
201         VnfResource vnf = input.getVnfResource();
202
203         TopologyService topology = new TopologyService(this);
204         RpcResult<RebuildOutput> result = topology.rebuild(hdr, vnf);
205         return Futures.immediateFuture(result);
206     }
207
208     /**
209      * Restarts a specific VNF
210      *
211      * @see org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.AppcProviderService#restart(org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.RestartInput)
212      */
213     @Override
214     public Future<RpcResult<RestartOutput>> restart(RestartInput input) {
215         CommonRequestHeader hdr = input.getCommonRequestHeader();
216         VnfResource vnf = input.getVnfResource();
217
218         TopologyService topology = new TopologyService(this);
219         RpcResult<RestartOutput> result = topology.restart(hdr, vnf);
220         return Futures.immediateFuture(result);
221     }
222
223     /**
224      * Migrates a specific VNF
225      *
226      * @see org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.AppcProviderService#migrate(org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.MigrateInput)
227      */
228     @Override
229     public Future<RpcResult<MigrateOutput>> migrate(MigrateInput input) {
230         CommonRequestHeader hdr = input.getCommonRequestHeader();
231         VnfResource vnf = input.getVnfResource();
232
233         TopologyService topology = new TopologyService(this);
234         RpcResult<MigrateOutput> result = topology.migrate(hdr, vnf);
235         return Futures.immediateFuture(result);
236     }
237
238     /**
239      * Evacuates a specific VNF
240      *
241      * @see org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.AppcProviderService#evacuate(org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.EvacuateInput)
242      */
243     @Override
244     public Future<RpcResult<EvacuateOutput>> evacuate(EvacuateInput input) {
245         CommonRequestHeader hdr = input.getCommonRequestHeader();
246         VnfResource vnf = input.getVnfResource();
247
248         TopologyService topology = new TopologyService(this);
249         return null;
250     }
251
252     /**
253      * Evacuates a specific VNF
254      *
255      * @see org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.AppcProviderService#evacuate(org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.EvacuateInput)
256      */
257     @Override
258     public Future<RpcResult<SnapshotOutput>> snapshot(SnapshotInput input) {
259         CommonRequestHeader hdr = input.getCommonRequestHeader();
260         VnfResource vnf = input.getVnfResource();
261
262         TopologyService topology = new TopologyService(this);
263         RpcResult<SnapshotOutput> result = topology.snapshot(hdr, vnf);
264         return Futures.immediateFuture(result);
265     }
266
267 }