b1c38c883a5f87387565e292296b582d1f8998a8
[integration.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.pnfsimulator.simulator.keywords;
21
22 import io.vavr.Tuple;
23 import io.vavr.Tuple1;
24 import io.vavr.match.annotation.Patterns;
25 import io.vavr.match.annotation.Unapply;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.regex.Pattern;
29 import lombok.Getter;
30 import lombok.Setter;
31 import lombok.val;
32
33 @Patterns
34 @Getter
35 @Setter
36 public class SingleParameterKeyword extends Keyword {
37
38     public static final int KEYWORD_NAME_GROUP = 2;
39     public static final int ADDITIONAL_PARAMETER_GROUP = 3;
40
41     private static final String KEYWORD_REGEX = new StringBuilder()
42         .append(OPTIONAL.apply(NONLETTERS_REGEX))
43         .append("#")
44         .append(LETTERS_REGEX)
45         .append("\\((\\d+)\\)")
46         .append(OPTIONAL.apply(NONLETTERS_REGEX))
47         .toString();
48     public static final int SKIPPED_GROUP_NUMBER = 3;
49
50     private Integer additionalParameter;
51
52     private SingleParameterKeyword(String name, List<String> meaningfulParts,
53         Integer additionalParameter) {
54         super(name, meaningfulParts);
55         this.additionalParameter = additionalParameter;
56     }
57
58     @Unapply
59     static Tuple1<SingleParameterKeyword> singleParameterKeyword(String keyword) {
60         val matcher = Pattern.compile(KEYWORD_REGEX).matcher(keyword);
61         SingleParameterKeyword spk = null;
62         if (matcher.find()) {
63             spk = new SingleParameterKeyword(
64                 matcher.group(KEYWORD_NAME_GROUP),
65                 extractPartsFrom(matcher, Collections.singletonList(SKIPPED_GROUP_NUMBER)),
66                 Integer.parseInt(matcher.group(ADDITIONAL_PARAMETER_GROUP))
67             );
68         }
69         return Tuple.of(spk);
70     }
71 }
72
73