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