From 7576237fd14304e987eb3c907d481f503dab8232 Mon Sep 17 00:00:00 2001 From: daniel Date: Fri, 11 Aug 2017 11:36:09 -0500 Subject: Add APPC LCM Interface The LCM model code and service provider code is implemented. This model code is now used for the vCPE use case scenario. The vFW use case is still using the legacy APPC API as request by APPC for Policy to use in R1. The APPC service provider now generates a demo legacy APPC request with pg-streams. An A&AI named query is implemented for the vFW use case. The code for obtaining the generic-vnf.vnf-id is not in use until we set up the A&AI simulator to work without having cyclical depenncies inside the POMs. JUnits for the vFW and vCPE use cases are now supported. These JUnits test the complete flow of each scenario. A&AI testing is not supported in the JUnit test yet. These JUnits use a target type of VM as VNF is not supported yet. This will be changed when the Target Lock supports VNF. Amended to fix Sonar blockers. Issue-ID: POLICY-104 Change-Id: I46869c5fd5094919da55ad563d608fe6d4b6fea7 Signed-off-by: Daniel Cruz --- .../org/onap/policy/appclcm/LCMCommonHeader.java | 233 ++++++++++++++++++++ .../java/org/onap/policy/appclcm/LCMRequest.java | 161 ++++++++++++++ .../org/onap/policy/appclcm/LCMRequestWrapper.java | 90 ++++++++ .../java/org/onap/policy/appclcm/LCMResponse.java | 151 +++++++++++++ .../org/onap/policy/appclcm/LCMResponseCode.java | 80 +++++++ .../org/onap/policy/appclcm/LCMResponseStatus.java | 107 +++++++++ .../onap/policy/appclcm/LCMResponseWrapper.java | 85 +++++++ .../java/org/onap/policy/appclcm/LCMWrapper.java | 187 ++++++++++++++++ .../onap/policy/appclcm/util/Serialization.java | 101 +++++++++ .../appclcm/src/main/resources/definitions.yaml | 118 ++++++++++ .../java/org/onap/policy/appclcm/AppcLcmTest.java | 244 +++++++++++++++++++++ 11 files changed, 1557 insertions(+) create mode 100644 controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMCommonHeader.java create mode 100644 controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMRequest.java create mode 100644 controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMRequestWrapper.java create mode 100644 controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMResponse.java create mode 100644 controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMResponseCode.java create mode 100644 controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMResponseStatus.java create mode 100644 controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMResponseWrapper.java create mode 100644 controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMWrapper.java create mode 100644 controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/util/Serialization.java create mode 100644 controlloop/common/model-impl/appclcm/src/main/resources/definitions.yaml create mode 100644 controlloop/common/model-impl/appclcm/src/test/java/org/onap/policy/appclcm/AppcLcmTest.java (limited to 'controlloop/common/model-impl/appclcm/src') diff --git a/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMCommonHeader.java b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMCommonHeader.java new file mode 100644 index 000000000..e2f520ad8 --- /dev/null +++ b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMCommonHeader.java @@ -0,0 +1,233 @@ +/*- + * ============LICENSE_START======================================================= + * appclcm + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.appclcm; + +import java.io.Serializable; +import java.time.Instant; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import com.google.gson.annotations.SerializedName; + +public class LCMCommonHeader implements Serializable { + + private static final long serialVersionUID = 6581963539127062114L; + + @SerializedName(value="timestamp") + private Instant timeStamp = Instant.now(); + + @SerializedName(value="api-ver") + private String apiVer="5.00"; + + @SerializedName(value="originator-id") + private String originatorId; + + @SerializedName(value="request-id") + private UUID requestId; + + @SerializedName(value="sub-request-id") + private String subRequestId; + + @SerializedName(value="flags") + private Map flags = new HashMap<>(); + + public LCMCommonHeader() { + + } + + /** + * Used to copy a common header. + * + * @param commonHeader + * a header that is defined by the lcm api guide that contains + * information about the request (requestId, flags, etc.) + */ + public LCMCommonHeader(LCMCommonHeader commonHeader) { + this.originatorId = commonHeader.originatorId; + this.requestId = commonHeader.requestId; + this.subRequestId = commonHeader.subRequestId; + if (commonHeader.flags != null) { + this.flags.putAll(commonHeader.flags); + } + } + + /** + * @return the timeStamp + */ + public Instant getTimeStamp() { + return timeStamp; + } + + /** + * @param timeStamp the timeStamp to set + */ + public void setTimeStamp(Instant timeStamp) { + this.timeStamp = timeStamp; + } + + /** + * @return the apiVer + */ + public String getApiVer() { + return apiVer; + } + + /** + * @param apiVer the apiVer to set + */ + public void setApiVer(String apiVer) { + this.apiVer = apiVer; + } + + /** + * @return the originatorId + */ + public String getOriginatorId() { + return originatorId; + } + + /** + * @param originatorId the originatorId to set + */ + public void setOriginatorId(String originatorId) { + this.originatorId = originatorId; + } + + /** + * @return the requestId + */ + public UUID getRequestId() { + return requestId; + } + + /** + * @param requestId the requestId to set + */ + public void setRequestId(UUID requestId) { + this.requestId = requestId; + } + + /** + * @return the subRequestId + */ + public String getSubRequestId() { + return subRequestId; + } + + /** + * @param subRequestId the subRequestId to set + */ + public void setSubRequestId(String subRequestId) { + this.subRequestId = subRequestId; + } + + /** + * @return the flags + */ + public Map getFlags() { + return flags; + } + + /** + * @param flags the flags to set + */ + public void setFlags(Map flags) { + this.flags = flags; + } + + @Override + public String toString() { + return "CommonHeader [timeStamp=" + timeStamp + ", apiVer=" + apiVer + ", originatorId=" + originatorId + + ", requestId=" + requestId + ", subRequestId=" + subRequestId + ", flags=" + flags + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((apiVer == null) ? 0 : apiVer.hashCode()); + result = prime * result + ((flags == null) ? 0 : flags.hashCode()); + result = prime * result + ((originatorId == null) ? 0 : originatorId.hashCode()); + result = prime * result + ((requestId == null) ? 0 : requestId.hashCode()); + result = prime * result + ((subRequestId == null) ? 0 : subRequestId.hashCode()); + result = prime * result + ((timeStamp == null) ? 0 : timeStamp.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + LCMCommonHeader other = (LCMCommonHeader) obj; + if (apiVer == null) { + if (other.apiVer != null) { + return false; + } + } else if (!apiVer.equals(other.apiVer)) { + return false; + } + if (flags == null) { + if (other.flags != null) { + return false; + } + } else if (!flags.equals(other.flags)) { + return false; + } + if (originatorId == null) { + if (other.originatorId != null) { + return false; + } + } else if (!originatorId.equals(other.originatorId)) { + return false; + } + if (requestId == null) { + if (other.requestId != null) { + return false; + } + } else if (!requestId.equals(other.requestId)) { + return false; + } + if (subRequestId == null) { + if (other.subRequestId != null) { + return false; + } + } else if (!subRequestId.equals(other.subRequestId)) { + return false; + } + if (timeStamp == null) { + if (other.timeStamp != null) { + return false; + } + } else if (!timeStamp.equals(other.timeStamp)) { + return false; + } + return true; + } + +} diff --git a/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMRequest.java b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMRequest.java new file mode 100644 index 000000000..51db7fef0 --- /dev/null +++ b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMRequest.java @@ -0,0 +1,161 @@ +/*- + * ============LICENSE_START======================================================= + * appclcm + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.appclcm; + +import java.io.Serializable; +import java.util.Map; + +import com.google.gson.annotations.SerializedName; + +public class LCMRequest implements Serializable { + + private static final long serialVersionUID = 219375564922846624L; + + @SerializedName(value="common-header") + private LCMCommonHeader commonHeader; + + @SerializedName(value="action") + private String action; + + @SerializedName(value="action-identifiers") + private Map actionIdentifiers; + + @SerializedName(value="payload") + private String payload; + + public LCMRequest() { + + } + + public LCMCommonHeader getCommonHeader() { + return commonHeader; + } + + /** + * @return the action + */ + public String getAction() { + return action; + } + + /** + * @param action the action to set + */ + public void setAction(String action) { + this.action = action; + } + + /** + * @return the actionIdentifiers + */ + public Map getActionIdentifiers() { + return actionIdentifiers; + } + + /** + * @param actionIdentifiers the actionIdentifiers to set + */ + public void setActionIdentifiers(Map actionIdentifiers) { + this.actionIdentifiers = actionIdentifiers; + } + + /** + * @return the payload + */ + public String getPayload() { + return payload; + } + + /** + * @param payload the payload to set + */ + public void setPayload(String payload) { + this.payload = payload; + } + + /** + * @param commonHeader the commonHeader to set + */ + public void setCommonHeader(LCMCommonHeader commonHeader) { + this.commonHeader = commonHeader; + } + + @Override + public String toString() { + return "Request [commonHeader=" + commonHeader + ", action=" + action + ", actionIdentifiers=" + + actionIdentifiers + ", payload=" + payload + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((commonHeader == null) ? 0 : commonHeader.hashCode()); + result = prime * result + ((action == null) ? 0 : action.hashCode()); + result = prime * result + ((actionIdentifiers == null) ? 0 : actionIdentifiers.hashCode()); + result = prime * result + ((payload == null) ? 0 : payload.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + LCMRequest other = (LCMRequest) obj; + if (commonHeader == null) { + if (other.commonHeader != null) { + return false; + } + } else if (!commonHeader.equals(other.commonHeader)) { + return false; + } + if (action == null) { + if (other.action != null) { + return false; + } + } else if (!action.equals(other.action)) { + return false; + } + if (actionIdentifiers == null) { + if (other.actionIdentifiers != null) { + return false; + } + } else if (!actionIdentifiers.equals(other.actionIdentifiers)) { + return false; + } + if (payload == null) { + if (other.payload != null) { + return false; + } + } else if (!payload.equals(other.payload)) { + return false; + } + return true; + } + +} diff --git a/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMRequestWrapper.java b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMRequestWrapper.java new file mode 100644 index 000000000..ea7e6762f --- /dev/null +++ b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMRequestWrapper.java @@ -0,0 +1,90 @@ +/*- + * ============LICENSE_START======================================================= + * appclcm + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.appclcm; + +import com.google.gson.annotations.SerializedName; +import java.io.Serializable; + +public class LCMRequestWrapper extends LCMWrapper implements Serializable { + + private static final long serialVersionUID = 424866914715980798L; + + @SerializedName(value="body") + private LCMRequest body; + + public LCMRequestWrapper() { + super(); + } + + public LCMRequestWrapper(LCMRequest request) { + body = request; + } + + /** + * @return the body + */ + public LCMRequest getBody() { + return body; + } + + /** + * @param body the body to set + */ + public void setBody(LCMRequest body) { + this.body = body; + } + + @Override + public String toString() { + return "RequestWrapper [body=" + body + ", toString()=" + super.toString() + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((body == null) ? 0 : body.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + LCMRequestWrapper other = (LCMRequestWrapper) obj; + if (body == null) { + if (other.body != null) { + return false; + } + } else if (!body.equals(other.body)) { + return false; + } + return true; + } + +} diff --git a/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMResponse.java b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMResponse.java new file mode 100644 index 000000000..003dc573b --- /dev/null +++ b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMResponse.java @@ -0,0 +1,151 @@ +/*- + * ============LICENSE_START======================================================= + * appclcm + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.appclcm; + +import com.google.gson.annotations.SerializedName; +import java.io.Serializable; + +public class LCMResponse implements Serializable { + + private static final long serialVersionUID = 6332508597287669750L; + + @SerializedName(value = "common-header") + private LCMCommonHeader commonHeader; + + @SerializedName(value = "status") + private LCMResponseStatus status = new LCMResponseStatus(); + + @SerializedName(value = "payload") + private String payload; + + public LCMResponse() { + + } + + /** + * Constructs a response using the common header of the request since they + * will be the same. + * + * @param request + * an appc lcm request object specified by the lcm api guide + */ + public LCMResponse(LCMRequest request) { + this.commonHeader = new LCMCommonHeader(request.getCommonHeader()); + String requestPayload = request.getPayload(); + if (requestPayload != null) { + // this.payload.putAll(request.payload); + this.payload = requestPayload; + } + } + + /** + * @return the commonHeader + */ + public LCMCommonHeader getCommonHeader() { + return commonHeader; + } + + /** + * @param commonHeader the commonHeader to set + */ + public void setCommonHeader(LCMCommonHeader commonHeader) { + this.commonHeader = commonHeader; + } + + /** + * @return the status + */ + public LCMResponseStatus getStatus() { + return status; + } + + /** + * @param status the status to set + */ + public void setStatus(LCMResponseStatus status) { + this.status = status; + } + + /** + * @return the payload + */ + public String getPayload() { + return payload; + } + + /** + * @param payload the payload to set + */ + public void setPayload(String payload) { + this.payload = payload; + } + + @Override + public String toString() { + return "Response [commonHeader=" + commonHeader + ", status=" + status + ", payload=" + payload + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((commonHeader == null) ? 0 : commonHeader.hashCode()); + result = prime * result + ((payload == null) ? 0 : payload.hashCode()); + result = prime * result + ((status == null) ? 0 : status.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + LCMResponse other = (LCMResponse) obj; + if (commonHeader == null) { + if (other.commonHeader != null) { + return false; + } + } else if (!commonHeader.equals(other.commonHeader)) { + return false; + } + if (payload == null) { + if (other.payload != null) { + return false; + } + } else if (!payload.equals(other.payload)) { + return false; + } + if (status == null) { + if (other.status != null) { + return false; + } + } else if (!status.equals(other.status)) { + return false; + } + return true; + } +} diff --git a/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMResponseCode.java b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMResponseCode.java new file mode 100644 index 000000000..484e793e2 --- /dev/null +++ b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMResponseCode.java @@ -0,0 +1,80 @@ +/*- + * ============LICENSE_START======================================================= + * appclcm + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.appclcm; + +public class LCMResponseCode { + + /* These fields define the key to the response code value. */ + public static final String ACCEPTED = "ACCEPTED"; + public static final String ERROR = "ERROR"; + public static final String REJECT = "REJECT"; + public static final String SUCCESS = "SUCCESS"; + public static final String FAILURE = "FAILURE"; + public static final String PARTIAL_SUCCESS = "PARTIAL SUCCESS"; + public static final String PARTIAL_FAILURE = "PARTIAL FAILURE"; + + private Integer code; + + private LCMResponseCode(int code) { + this.code = code; + } + + public int getCode() { + return this.code; + } + + public String toString() { + return Integer.toString(this.code); + } + + /** + * Translates the code to a string value that represents the meaning of the code. + * + * @param code + * the numeric value that is returned by APPC based on success, + * failure, etc. of the action requested + * @return the string value equivalent of the APPC response code + */ + public static String toResponseValue(int code) { + if (code == 100) { + return ACCEPTED; + } + else if (code == 200) { + return ERROR; + } + else if (code >= 300 && code <= 313) { + return REJECT; + } + else if (code == 400) { + return SUCCESS; + } + else if (code == 450 || (code >= 401 && code <= 406)) { + return FAILURE; + } + else if (code == 500) { + return PARTIAL_SUCCESS; + } + else if (code >= 501 && code <= 599) { + return PARTIAL_FAILURE; + } + return null; + } +} diff --git a/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMResponseStatus.java b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMResponseStatus.java new file mode 100644 index 000000000..131e331bf --- /dev/null +++ b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMResponseStatus.java @@ -0,0 +1,107 @@ +/*- + * ============LICENSE_START======================================================= + * appclcm + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.appclcm; + +import com.google.gson.annotations.SerializedName; +import java.io.Serializable; + +public class LCMResponseStatus implements Serializable { + + private static final long serialVersionUID = 974891505135467199L; + + @SerializedName(value="code") + private int code; + + @SerializedName(value="message") + private String message; + + public LCMResponseStatus() { + + } + + /** + * @return the code + */ + public int getCode() { + return code; + } + + /** + * @param code the code to set + */ + public void setCode(int code) { + this.code = code; + } + + /** + * @return the message + */ + public String getMessage() { + return message; + } + + /** + * @param message the message to set + */ + public void setMessage(String message) { + this.message = message; + } + + @Override + public String toString() { + return "ResponseStatus [code=" + code + ", message=" + message + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + code; + result = prime * result + ((message == null) ? 0 : message.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + LCMResponseStatus other = (LCMResponseStatus) obj; + if (code != other.code) { + return false; + } + if (message == null) { + if (other.message != null) { + return false; + } + } else if (!message.equals(other.message)) { + return false; + } + return true; + } + +} diff --git a/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMResponseWrapper.java b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMResponseWrapper.java new file mode 100644 index 000000000..7d9062979 --- /dev/null +++ b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMResponseWrapper.java @@ -0,0 +1,85 @@ +/*- + * ============LICENSE_START======================================================= + * appclcm + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.appclcm; + +import com.google.gson.annotations.SerializedName; +import java.io.Serializable; + +public class LCMResponseWrapper extends LCMWrapper implements Serializable { + + private static final long serialVersionUID = 463937813781086802L; + + @SerializedName(value="body") + private LCMResponse body; + + public LCMResponseWrapper() { + super(); + } + + /** + * @return the body + */ + public LCMResponse getBody() { + return body; + } + + /** + * @param body the body to set + */ + public void setBody(LCMResponse body) { + this.body = body; + } + + @Override + public String toString() { + return "ResponseWrapper [body=" + body + ", toString()=" + super.toString() + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((body == null) ? 0 : body.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + LCMResponseWrapper other = (LCMResponseWrapper) obj; + if (body == null) { + if (other.body != null) { + return false; + } + } else if (!body.equals(other.body)) { + return false; + } + return true; + } +} diff --git a/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMWrapper.java b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMWrapper.java new file mode 100644 index 000000000..c9f72c2e2 --- /dev/null +++ b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LCMWrapper.java @@ -0,0 +1,187 @@ +/*- + * ============LICENSE_START======================================================= + * appclcm + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.appclcm; + +import com.google.gson.annotations.SerializedName; +import java.io.Serializable; + +public class LCMWrapper implements Serializable { + + private static final long serialVersionUID = 753005805432396532L; + + @SerializedName(value="version") + private String version; + + @SerializedName(value="cambria-partition") + private String cambriaPartition; + + @SerializedName(value="rpc-name") + private String rpcName; + + @SerializedName(value="correlation-id") + private String correlationId; + + @SerializedName(value="type") + private String type; + + public LCMWrapper() { + + } + + /** + * @return the version + */ + public String getVersion() { + return version; + } + + /** + * @param version the version to set + */ + public void setVersion(String version) { + this.version = version; + } + + /** + * @return the cambriaPartition + */ + public String getCambriaPartition() { + return cambriaPartition; + } + + /** + * @param cambriaPartition the cambriaPartition to set + */ + public void setCambriaPartition(String cambriaPartition) { + this.cambriaPartition = cambriaPartition; + } + + /** + * @return the rpcName + */ + public String getRpcName() { + return rpcName; + } + + /** + * @param rpcName the rpcName to set + */ + public void setRpcName(String rpcName) { + this.rpcName = rpcName; + } + + /** + * @return the correlationId + */ + public String getCorrelationId() { + return correlationId; + } + + /** + * @param correlationId the correlationId to set + */ + public void setCorrelationId(String correlationId) { + this.correlationId = correlationId; + } + + /** + * @return the type + */ + public String getType() { + return type; + } + + /** + * @param type the type to set + */ + public void setType(String type) { + this.type = type; + } + + @Override + public String toString() { + return "Wrapper [version=" + version + ", cambriaPartition=" + cambriaPartition + ", rpcName=" + rpcName + + ", correlationId=" + correlationId + ", type=" + type + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((cambriaPartition == null) ? 0 : cambriaPartition.hashCode()); + result = prime * result + ((correlationId == null) ? 0 : correlationId.hashCode()); + result = prime * result + ((rpcName == null) ? 0 : rpcName.hashCode()); + result = prime * result + ((type == null) ? 0 : type.hashCode()); + result = prime * result + ((version == null) ? 0 : version.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + LCMWrapper other = (LCMWrapper) obj; + if (cambriaPartition == null) { + if (other.cambriaPartition != null) { + return false; + } + } else if (!cambriaPartition.equals(other.cambriaPartition)) { + return false; + } + if (correlationId == null) { + if (other.correlationId != null) { + return false; + } + } else if (!correlationId.equals(other.correlationId)) { + return false; + } + if (rpcName == null) { + if (other.rpcName != null) { + return false; + } + } else if (!rpcName.equals(other.rpcName)) { + return false; + } + if (type == null) { + if (other.type != null) { + return false; + } + } else if (!type.equals(other.type)) { + return false; + } + if (version == null) { + if (other.version != null) { + return false; + } + } else if (!version.equals(other.version)) { + return false; + } + return true; + } + +} diff --git a/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/util/Serialization.java b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/util/Serialization.java new file mode 100644 index 000000000..7187ab1e7 --- /dev/null +++ b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/util/Serialization.java @@ -0,0 +1,101 @@ +/*- + * ============LICENSE_START======================================================= + * appc + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.appclcm.util; + +import java.lang.reflect.Type; +import java.time.Instant; + +import org.onap.policy.appclcm.LCMRequest; +import org.onap.policy.appclcm.LCMResponse; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +public final class Serialization { + + public static class RequestAdapter implements JsonSerializer, JsonDeserializer { + + @Override + public JsonElement serialize(LCMRequest src, Type typeOfSrc, JsonSerializationContext context) { + JsonElement requestJson = gsonPretty.toJsonTree(src, LCMRequest.class); + JsonObject input = new JsonObject(); + input.add("input", requestJson); + + return input; + } + + @Override + public LCMRequest deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + LCMRequest request = gsonPretty.fromJson(json.getAsJsonObject().get("input"), LCMRequest.class); + return request; + } + } + + public static class ResponseAdapter implements JsonSerializer, JsonDeserializer { + + @Override + public JsonElement serialize(LCMResponse src, Type typeOfSrc, JsonSerializationContext context) { + JsonElement responseJson = gsonPretty.toJsonTree(src, LCMResponse.class); + JsonObject output = new JsonObject(); + output.add("output", responseJson); + return output; + } + + @Override + public LCMResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + LCMResponse response = gsonPretty.fromJson(json.getAsJsonObject().get("output"), LCMResponse.class); + return response; + } + } + + public static class InstantAdapter implements JsonSerializer, JsonDeserializer { + + @Override + public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + return Instant.parse(json.getAsString()); + } + + @Override + public JsonElement serialize(Instant src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(src.toString()); + } + + } + + public static final Gson gsonPretty = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting() + .registerTypeAdapter(Instant.class, new InstantAdapter()).create(); + + public static final Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting() + .registerTypeAdapter(LCMRequest.class, new RequestAdapter()) + .registerTypeAdapter(LCMResponse.class, new ResponseAdapter()).create(); + +} diff --git a/controlloop/common/model-impl/appclcm/src/main/resources/definitions.yaml b/controlloop/common/model-impl/appclcm/src/main/resources/definitions.yaml new file mode 100644 index 000000000..680a9929c --- /dev/null +++ b/controlloop/common/model-impl/appclcm/src/main/resources/definitions.yaml @@ -0,0 +1,118 @@ +### +# ============LICENSE_START======================================================= +# appc +# ================================================================================ +# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +### + +Request: + type: object + properties: + CommonHeader: + type: object + properties: + TimeStamp: + type: string + APIver: + type: string + value: '1.01' + OriginatorID: + type: string + RequestID: + type: string + pattern: "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$" + SubRequestID: + type: string + Flags: + type: object + required: + - TimeStamp + - APIver + - OriginatorID + - RequestID + Action: + type: string + enum: + - Audit + - ActionStatus + - BlockAudits + - Configure + - HealthCheck + - Install + - LiveUpgrade + - Migrate + - ModifyConfig + - Query + - Rebuild + - Reconfigure + - Restart + - Rollback + - Scale + - Start + - Stop + - Sync + - Terminate + - Test + - Upgrade + TargetID: + type: string + ObjectID: + type: string + Payload: + type: object + required: + - CommonHeader + - Action + - TargetID +Response: + type: object + properties: + CommonHeader: + type: object + properties: + TimeStamp: + type: string + APIver: + type: string + OriginatorID: + type: string + RequestID: + type: string + SubRequestID: + type: string + Flags: + type: object + required: + - TimeStamp + - APIver + - OriginatorID + - RequestID + Status: + type: object + properties: + Code: + type: integer + Value: + type: string + required: + - Code + - Value + Payload: + type: object + required: + - CommonHeader + - Status + diff --git a/controlloop/common/model-impl/appclcm/src/test/java/org/onap/policy/appclcm/AppcLcmTest.java b/controlloop/common/model-impl/appclcm/src/test/java/org/onap/policy/appclcm/AppcLcmTest.java new file mode 100644 index 000000000..41bafe498 --- /dev/null +++ b/controlloop/common/model-impl/appclcm/src/test/java/org/onap/policy/appclcm/AppcLcmTest.java @@ -0,0 +1,244 @@ +/*- + * ============LICENSE_START======================================================= + * appclcm + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.appclcm; + +import static org.junit.Assert.*; + +import java.util.HashMap; +import java.util.UUID; + +import org.junit.Test; +import org.onap.policy.appclcm.util.Serialization; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AppcLcmTest { + + private static final Logger logger = LoggerFactory.getLogger(AppcLcmTest.class); + + private static LCMRequestWrapper dmaapRequest; + private static LCMResponseWrapper dmaapResponse; + + static { + /* + * Construct an APPCLCM Request to be Serialized + */ + dmaapRequest = new LCMRequestWrapper(); + dmaapRequest.setCorrelationId("664be3d2-6c12-4f4b-a3e7-c349acced200" + "-" + "1"); + dmaapRequest.setRpcName("restart"); + dmaapRequest.setType("request"); + + dmaapResponse = new LCMResponseWrapper(); + dmaapResponse.setCorrelationId("664be3d2-6c12-4f4b-a3e7-c349acced200" + "-" + "1"); + dmaapResponse.setRpcName("restart"); + dmaapResponse.setType("response"); + + LCMRequest appcRequest = new LCMRequest(); + + appcRequest.setAction("restart"); + + HashMap actionIdentifiers = new HashMap<>(); + actionIdentifiers.put("vnf-id", "trial-vnf-003"); + actionIdentifiers.put("vserver-id", "08f6c1f9-99e7-49f3-a662-c62b9f200d79"); + + appcRequest.setActionIdentifiers(actionIdentifiers); + + LCMCommonHeader commonHeader = new LCMCommonHeader(); + commonHeader.setRequestId(UUID.fromString("664be3d2-6c12-4f4b-a3e7-c349acced200")); + commonHeader.setSubRequestId("1"); + commonHeader.setOriginatorId("664be3d2-6c12-4f4b-a3e7-c349acced200"); + + appcRequest.setCommonHeader(commonHeader); + + appcRequest.setPayload(null); + + dmaapRequest.setBody(appcRequest); + + /* + * Construct an APPCLCM Response to be Serialized + */ + LCMResponse appcResponse = new LCMResponse(appcRequest); + appcResponse.getStatus().setCode(400); + appcResponse.getStatus().setMessage("Restart Successful"); + appcResponse.setPayload(null); + + dmaapResponse.setBody(appcResponse); + } + + @Test + public void testRequestSerialization() { + + /* + * Use the gson serializer to obtain json + */ + String jsonRequest = Serialization.gson.toJson(dmaapRequest, LCMRequestWrapper.class); + assertNotNull(jsonRequest); + + /* + * The serializer should have added an extra + * sub-tag called "input" that wraps the request + */ + assertTrue(jsonRequest.contains("input")); + + /* + * The common-header, request-id, and + * sub-request-id should exist + */ + assertTrue(jsonRequest.contains("common-header")); + assertTrue(jsonRequest.contains("request-id")); + assertTrue(jsonRequest.contains("sub-request-id")); + + /* + * action-identifiers should exist and contain + * a vnf-id + */ + assertTrue(jsonRequest.contains("action-identifiers")); + assertTrue(jsonRequest.contains("vnf-id")); + + /* + * The action sub-tag should exist + */ + assertTrue(jsonRequest.contains("action")); + + logger.debug("Request as JSON: " + jsonRequest + "\n\n"); + } + + @Test + public void testRequestDeserialization() { + + /* + * Convert the LCM request object into json + * so we have a string of json to use for testing + */ + String jsonRequest = Serialization.gson.toJson(dmaapRequest, LCMRequestWrapper.class); + + /* + * Use the serializer to convert the json string + * into a java object + */ + LCMRequestWrapper dmaapRequest = Serialization.gson.fromJson(jsonRequest, LCMRequestWrapper.class); + assertNotNull(dmaapRequest); + + /* + * The type of the DMAAP wrapper should be request + */ + assertEquals(dmaapRequest.getType(), "request"); + + /* + * The DMAAP wrapper must have a body as that is + * the true APPC request + */ + assertNotNull(dmaapRequest.getBody()); + LCMRequest appcRequest = dmaapRequest.getBody(); + assertNotNull(appcRequest); + + /* + * The common header should not be null + */ + assertNotNull(appcRequest.getCommonHeader()); + + /* + * The action should not be null and should be + * set to restart + */ + assertNotNull(appcRequest.getAction()); + assertEquals(appcRequest.getAction(), "restart"); + + /* + * The action-identifiers should not be null + * and should contain a vnf-id + */ + assertNotNull(appcRequest.getActionIdentifiers()); + assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id")); + + logger.debug("Request as a Java Object: \n" + appcRequest.toString() + "\n\n"); + } + + @Test + public void testResponseSerialization() { + + /* + * Use the serializer to convert the object into json + */ + String jsonResponse = Serialization.gson.toJson(dmaapResponse, LCMResponseWrapper.class); + assertNotNull(jsonResponse); + + /* + * The serializer should have added an extra + * sub-tag called "input" that wraps the request + */ + assertTrue(jsonResponse.contains("output")); + + /* + * The response should contain a common-header, + * request-id, sub-request-id, and status + */ + assertTrue(jsonResponse.contains("common-header")); + assertTrue(jsonResponse.contains("request-id")); + assertTrue(jsonResponse.contains("sub-request-id")); + assertTrue(jsonResponse.contains("status")); + + logger.debug("Response as JSON: " + jsonResponse + "\n\n"); + } + + @Test + public void testResponseDeserialization() { + /* + * Convert the LCM response object into json + * so we have a string of json to use for testing + */ + String jsonResponse = Serialization.gson.toJson(dmaapResponse, LCMResponseWrapper.class); + + /* + * Use the serializer to convert the json string + * into a java object + */ + LCMResponseWrapper dmaapResponse = Serialization.gson.fromJson(jsonResponse, LCMResponseWrapper.class); + assertNotNull(dmaapResponse); + + /* + * The type of the DMAAP wrapper should be response + */ + assertEquals(dmaapResponse.getType(), "response"); + + /* + * The DMAAP wrapper must have a body as that is + * the true APPC response + */ + assertNotNull(dmaapResponse.getBody()); + LCMResponse appcResponse = dmaapResponse.getBody(); + assertNotNull(appcResponse); + + /* + * The common header should not be null + */ + assertNotNull(appcResponse.getCommonHeader()); + + /* + * The status should not be null and the + * status code should be 400 + */ + assertNotNull(appcResponse.getStatus()); + assertEquals(appcResponse.getStatus().getCode(), 400); + + logger.debug("Response as a Java Object: \n" + appcResponse.toString() + "\n\n"); + } +} -- cgit 1.2.3-korg