Fix Reference Key columns persistence issue in db
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfKey.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
24 import lombok.NonNull;
25
26 /**
27  * The key uniquely identifies every entity in the system. This class is an abstract class to give a common parent for
28  * all key types in the system.
29  */
30 public abstract class PfKey extends PfConcept {
31     private static final long serialVersionUID = 6281159885962014041L;
32
33     /** Regular expression to specify the structure of key names. */
34     public static final String NAME_REGEXP = "^[A-Za-z0-9\\-_\\.]+$";
35
36     /** Regular expression to specify the structure of key versions. */
37     public static final String VERSION_REGEXP = "^(\\d+.){2}\\d+$";
38
39     /** Regular expression to specify the structure of key IDs. */
40     public static final String KEY_ID_REGEXP = "^[A-Za-z0-9\\-_\\.]+:(\\d+.){2}\\d+$";
41
42     /** Specifies the value for names in NULL keys. */
43     public static final String NULL_KEY_NAME = "NULL";
44
45     /** Specifies the value for versions in NULL keys. */
46     public static final String NULL_KEY_VERSION = "0.0.0";
47
48     /**
49      * This enumeration is returned on key compatibility checks.
50      */
51     public enum Compatibility {
52         /** The keys have different names. */
53         DIFFERENT,
54         /**
55          * The name of the key matches but the Major version number of the keys is different (x in x.y.z do not match).
56          */
57         MAJOR,
58         /**
59          * The name of the key matches but the Minor version number of the keys is different (y in x.y.z do not match).
60          */
61         MINOR,
62         /**
63          * The name of the key matches but the Patch version number of the keys is different (z in x.y.z do not match).
64          */
65         PATCH,
66         /** The keys match completely. */
67         IDENTICAL
68     }
69
70     /**
71      * Default constructor.
72      */
73     protected PfKey() {
74         super();
75     }
76
77     /**
78      * Copy constructor.
79      *
80      * @param copyConcept the concept to copy from
81      */
82     protected PfKey(final PfKey copyConcept) {
83         super(copyConcept);
84     }
85
86     @Override
87     public abstract String getId();
88
89     /**
90      * Return the result of a compatibility check of two keys.
91      *
92      * @param otherKey the key to check compatibility against
93      * @return the compatibility result of the check
94      */
95     public abstract Compatibility getCompatibility(@NonNull PfKey otherKey);
96
97     /**
98      * Check if two keys are compatible, that is the keys are IDENTICAL or have only MINOR, PATCH differences.
99      *
100      * @param otherKey the key to check compatibility against
101      * @return true, if the keys are compatible
102      */
103     public abstract boolean isCompatible(@NonNull PfKey otherKey);
104
105     /**
106      * Check if this key is a newer version than the other key.
107      *
108      * @param otherKey the key to check against
109      * @return true, if this key is newer than the other key
110      */
111     public abstract boolean isNewerThan(@NonNull PfKey otherKey);
112
113     /**
114      * Check if a key equals its null key.
115      *
116      * @return true, if the key is a null key
117      */
118     public abstract boolean isNullKey();
119
120     /**
121      * Get the major version of a key.
122      *
123      * @return the major version of a key
124      */
125     public abstract int getMajorVersion();
126
127     /**
128      * Get the minor version of a key.
129      *
130      * @return the minor version of a key
131      */
132     public abstract int getMinorVersion();
133
134     /**
135      * Get the patch version of a key.
136      *
137      * @return the patch version of a key
138      */
139     public abstract int getPatchVersion();
140 }