Merge "PoC Contract Stubs NCMP Rest Endpoints"
[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.inventory.CmHandleState;
38 import org.onap.cps.ncmp.api.inventory.CompositeState;
39 import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder;
40 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
41 import org.onap.cps.utils.CpsValidator;
42
43 /**
44  * Cm Handle which follows the Yang resource dmi registry model when persisting data to DMI or the DB.
45  * Yang model CmHandle
46  */
47 @Getter
48 @Setter
49 @NoArgsConstructor
50 @JsonInclude(Include.NON_NULL)
51 public class YangModelCmHandle {
52
53     private String id;
54
55     @JsonProperty("dmi-service-name")
56     private String dmiServiceName;
57
58     @JsonProperty("dmi-data-service-name")
59     private String dmiDataServiceName;
60
61     @JsonProperty("state")
62     private CompositeState compositeState;
63
64     @JsonProperty("dmi-model-service-name")
65     private String dmiModelServiceName;
66
67     @JsonProperty("additional-properties")
68     private List<Property> dmiProperties;
69
70     @JsonProperty("public-properties")
71     private List<Property> publicProperties;
72
73     private static final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder();
74
75     /**
76      * Create a yangModelCmHandle.
77      *
78      * @param dmiServiceName      dmi service name
79      * @param dmiDataServiceName  dmi data service name
80      * @param dmiModelServiceName dmi model service name
81      * @param ncmpServiceCmHandle the cm handle
82      * @return instance of yangModelCmHandle
83      */
84     public static YangModelCmHandle toYangModelCmHandle(final String dmiServiceName,
85                                                         final String dmiDataServiceName,
86                                                         final String dmiModelServiceName,
87                                                         final CmHandleState cmHandleState,
88                                                         final NcmpServiceCmHandle ncmpServiceCmHandle) {
89         CpsValidator.validateNameCharacters(ncmpServiceCmHandle.getCmHandleId());
90         final YangModelCmHandle yangModelCmHandle = new YangModelCmHandle();
91         yangModelCmHandle.setId(ncmpServiceCmHandle.getCmHandleId());
92         yangModelCmHandle.setDmiServiceName(dmiServiceName);
93         yangModelCmHandle.setDmiDataServiceName(dmiDataServiceName);
94         yangModelCmHandle.setDmiModelServiceName(dmiModelServiceName);
95         yangModelCmHandle.setDmiProperties(asYangModelCmHandleProperties(ncmpServiceCmHandle.getDmiProperties()));
96         yangModelCmHandle.setPublicProperties(asYangModelCmHandleProperties(
97                 ncmpServiceCmHandle.getPublicProperties()));
98         compositeStateBuilder.withCmHandleState(cmHandleState);
99         yangModelCmHandle.setCompositeState(compositeStateBuilder.build());
100         return yangModelCmHandle;
101     }
102
103     /**
104      * Resolve a dmi service name.
105      *
106      * @param requiredService indicates what typo of service is required
107      * @return dmi service name
108      */
109     public String resolveDmiServiceName(final RequiredDmiService requiredService) {
110         if (isNullEmptyOrBlank(dmiServiceName)) {
111             if (RequiredDmiService.DATA.equals(requiredService)) {
112                 return dmiDataServiceName;
113             }
114             return dmiModelServiceName;
115         }
116         return dmiServiceName;
117     }
118
119     private static List<Property> asYangModelCmHandleProperties(final Map<String, String> propertiesAsMap) {
120         final List<Property> yangModelCmHandleProperties = new ArrayList<>(propertiesAsMap.size());
121         for (final Map.Entry<String, String> entry : propertiesAsMap.entrySet()) {
122             yangModelCmHandleProperties.add(new YangModelCmHandle.Property(entry.getKey(), entry.getValue()));
123         }
124         return yangModelCmHandleProperties;
125     }
126
127     private static boolean isNullEmptyOrBlank(final String serviceName) {
128         return Strings.isNullOrEmpty(serviceName) || serviceName.isBlank();
129     }
130
131     @AllArgsConstructor
132     @Data
133     public static class Property {
134
135         @JsonProperty()
136         private final String name;
137
138         @JsonProperty()
139         private final String value;
140     }
141
142 }