Watchdog-process that changes CM Handles state
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / yangmodels / YangModelCmHandle.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21
22 package org.onap.cps.ncmp.api.impl.yangmodels;
23
24 import com.fasterxml.jackson.annotation.JsonInclude;
25 import com.fasterxml.jackson.annotation.JsonInclude.Include;
26 import com.fasterxml.jackson.annotation.JsonProperty;
27 import com.google.common.base.Strings;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31 import lombok.AllArgsConstructor;
32 import lombok.Data;
33 import lombok.Getter;
34 import lombok.NoArgsConstructor;
35 import lombok.Setter;
36 import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService;
37 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
38 import org.onap.cps.utils.CpsValidator;
39
40 /**
41  * Cm Handle which follows the Yang resource dmi registry model when persisting data to DMI or the DB.
42  * Yang model CmHandle
43  */
44 @Getter
45 @Setter
46 @NoArgsConstructor
47 @JsonInclude(Include.NON_NULL)
48 public class YangModelCmHandle {
49
50     private String id;
51
52     @JsonProperty("dmi-service-name")
53     private String dmiServiceName;
54
55     @JsonProperty("dmi-data-service-name")
56     private String dmiDataServiceName;
57
58     @JsonProperty("state")
59     private String cmHandleState;
60
61     @JsonProperty("dmi-model-service-name")
62     private String dmiModelServiceName;
63
64     @JsonProperty("additional-properties")
65     private List<Property> dmiProperties;
66
67     @JsonProperty("public-properties")
68     private List<Property> publicProperties;
69
70     /**
71      * Create a yangModelCmHandle.
72      * @param dmiServiceName dmi service name
73      * @param dmiDataServiceName dmi data service name
74      * @param dmiModelServiceName dmi model service name
75      * @param ncmpServiceCmHandle the cm handle
76      * @return instance of yangModelCmHandle
77      */
78     public static YangModelCmHandle toYangModelCmHandle(final String dmiServiceName,
79                                                         final String dmiDataServiceName,
80                                                         final String dmiModelServiceName,
81                                                         final NcmpServiceCmHandle ncmpServiceCmHandle) {
82         CpsValidator.validateNameCharacters(ncmpServiceCmHandle.getCmHandleId());
83         final YangModelCmHandle yangModelCmHandle = new YangModelCmHandle();
84         yangModelCmHandle.setId(ncmpServiceCmHandle.getCmHandleId());
85         yangModelCmHandle.setDmiServiceName(dmiServiceName);
86         yangModelCmHandle.setDmiDataServiceName(dmiDataServiceName);
87         yangModelCmHandle.setDmiModelServiceName(dmiModelServiceName);
88         yangModelCmHandle.setDmiProperties(asYangModelCmHandleProperties(ncmpServiceCmHandle.getDmiProperties()));
89         yangModelCmHandle.setPublicProperties(asYangModelCmHandleProperties(
90             ncmpServiceCmHandle.getPublicProperties()));
91         return yangModelCmHandle;
92     }
93
94     /**
95      * Resolve a dmi service name.
96      * @param requiredService indicates what typo of service is required
97      * @return dmi service name
98      */
99     public String resolveDmiServiceName(final RequiredDmiService requiredService) {
100         if (isNullEmptyOrBlank(dmiServiceName)) {
101             if (RequiredDmiService.DATA.equals(requiredService)) {
102                 return dmiDataServiceName;
103             }
104             return dmiModelServiceName;
105         }
106         return dmiServiceName;
107     }
108
109     private static List<Property> asYangModelCmHandleProperties(final Map<String, String> propertiesAsMap) {
110         final List<Property> yangModelCmHandleProperties = new ArrayList<>(propertiesAsMap.size());
111         for (final Map.Entry<String, String> entry : propertiesAsMap.entrySet()) {
112             yangModelCmHandleProperties.add(new YangModelCmHandle.Property(entry.getKey(), entry.getValue()));
113         }
114         return yangModelCmHandleProperties;
115     }
116
117     private static boolean isNullEmptyOrBlank(final String serviceName) {
118         return Strings.isNullOrEmpty(serviceName) || serviceName.isBlank();
119     }
120
121     @AllArgsConstructor
122     @Data
123     public static class Property {
124
125         @JsonProperty()
126         private final String name;
127
128         @JsonProperty()
129         private final String value;
130     }
131
132 }