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