Moving all files to root directory
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / openecomp / appc / provider / lcm / util / RequestInputBuilder.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.lcm.util;
23
24 import java.text.ParseException;
25 import java.text.SimpleDateFormat;
26
27 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160108.Payload;
28 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160108.action.identifiers.ActionIdentifiers;
29 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160108.common.header.CommonHeader;
30 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160108.common.header.common.header.Flags;
31 import org.openecomp.appc.domainmodel.lcm.RequestContext;
32 import org.openecomp.appc.domainmodel.lcm.VNFOperation;
33 import org.openecomp.appc.requesthandler.objects.RequestHandlerInput;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36
37
38
39 public class RequestInputBuilder {
40     private static EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
41
42     private static final String FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
43
44     private RequestContext requestContext;
45     private String rpcName;
46
47     public RequestInputBuilder() {
48     }
49
50
51     public RequestInputBuilder requestContext() {
52         this.requestContext = new RequestContext();
53         return this;
54     }
55
56     public RequestInputBuilder action(String action) {
57         this.requestContext.setAction(VNFOperation.findByString(action));
58         return this;
59     }
60
61
62     public RequestInputBuilder additionalContext(String key, String value) {
63         this.requestContext.addKeyValueToAdditionalContext(key, value);
64         return this;
65     }
66
67     public RequestInputBuilder payload(Payload payload) {
68         if (payload != null) {
69             this.requestContext.setPayload(payload.getValue());
70         }
71         return this;
72     }
73
74     public RequestHandlerInput  build (){
75         RequestHandlerInput request = new RequestHandlerInput();
76         request.setRequestContext(this.requestContext);
77         request.setRpcName(rpcName);
78         return  request;
79     }
80
81     public RequestInputBuilder rpcName(String rpcName) {
82         this.rpcName = rpcName;
83         return this;
84     }
85
86     public RequestInputBuilder commonHeader(CommonHeader commonHeader) throws ParseException {
87         org.openecomp.appc.domainmodel.lcm.CommonHeader header = new org.openecomp.appc.domainmodel.lcm.CommonHeader();
88         this.requestContext.setCommonHeader(header);
89
90         try {
91             if(null != commonHeader.getTimestamp()) {
92                 SimpleDateFormat format = new SimpleDateFormat(FORMAT);
93                 format.setLenient(false);
94                 header.setTimestamp(format.parse(commonHeader.getTimestamp().getValue()));
95             }else{
96                 throw new ParseException("Missing mandatory parameter : timestamp " , 0);
97             }
98         } catch (ParseException e) {
99             logger.error(String.format("DATE format is incorrect: %s", e.getMessage()));
100             throw e;
101         }
102         header.setApiVer(commonHeader.getApiVer());
103         header.setRequestId(commonHeader.getRequestId());
104         header.setOriginatorId(commonHeader.getOriginatorId());
105         header.setSubRequestId(commonHeader.getSubRequestId());
106
107         Flags inFlags = commonHeader.getFlags();
108         org.openecomp.appc.domainmodel.lcm.Flags flags = new org.openecomp.appc.domainmodel.lcm.Flags();
109         if (inFlags != null) {
110
111             if(null != inFlags.getForce()) {
112                 flags.setForce(Boolean.parseBoolean(inFlags.getForce().toString().toLowerCase()));
113             }
114             if(null!=inFlags.getMode()) {
115                 flags.setMode(inFlags.getMode().name());
116             }
117             if(null!=  inFlags.getTtl()) {
118                 flags.setTtl(inFlags.getTtl());
119             }
120
121         }
122         this.requestContext.getCommonHeader().setFlags(flags);
123         return this;
124     }
125
126     public RequestInputBuilder actionIdentifiers(ActionIdentifiers actionIdentifiers)  throws ParseException  {
127         if(null!= actionIdentifiers) {
128             org.openecomp.appc.domainmodel.lcm.ActionIdentifiers actionIds = new org.openecomp.appc.domainmodel.lcm.ActionIdentifiers();
129             actionIds.setServiceInstanceId(actionIdentifiers.getServiceInstanceId());
130             actionIds.setVnfcName(actionIdentifiers.getVnfcName());
131             actionIds.setvServerId(actionIdentifiers.getVserverId());
132             actionIds.setVnfId(actionIdentifiers.getVnfId());
133             this.requestContext.setActionIdentifiers(actionIds);
134             return this;
135         }else{
136             throw new ParseException("Missing action identifier" , 0);
137         }
138     }
139
140
141 }