diff options
Diffstat (limited to 'controlloop/src/main/java')
12 files changed, 799 insertions, 0 deletions
diff --git a/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopEvent.java b/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopEvent.java new file mode 100644 index 000000000..d7fa04884 --- /dev/null +++ b/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopEvent.java @@ -0,0 +1,69 @@ +/*- + * ============LICENSE_START======================================================= + * controlloop + * ================================================================================ + * 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.openecomp.policy.controlloop; + +import java.io.Serializable; +import java.util.UUID; + +public abstract class ControlLoopEvent implements Serializable { + + private static final long serialVersionUID = 2391252138583119195L; + + public String closedLoopControlName; + public String version = "1.0.2"; + public UUID requestID; + public String closedLoopEventClient; + public ControlLoopTargetType target_type; + public String target; + public String from; + public String policyScope; + public String policyName; + public String policyVersion; + public ControlLoopEventStatus closedLoopEventStatus; + + public ControlLoopEvent() { + + } + + public ControlLoopEvent(ControlLoopEvent event) { + if (event == null) { + return; + } + this.closedLoopControlName = event.closedLoopControlName; + this.requestID = event.requestID; + this.closedLoopEventClient = event.closedLoopEventClient; + this.target_type = event.target_type; + this.target = event.target; + this.from = event.from; + this.policyScope = event.policyScope; + this.policyName = event.policyName; + this.policyVersion = event.policyVersion; + this.closedLoopEventStatus = event.closedLoopEventStatus; + } + + public boolean isEventStatusValid() { + if (this.closedLoopEventStatus == null) { + return false; + } + return true; + } + +} diff --git a/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopEventStatus.java b/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopEventStatus.java new file mode 100644 index 000000000..063a611b9 --- /dev/null +++ b/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopEventStatus.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * controlloop + * ================================================================================ + * 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.openecomp.policy.controlloop; + +public enum ControlLoopEventStatus { + ONSET("ONSET"), + ABATED("ABATED") + ; + + private String status; + + private ControlLoopEventStatus(String status) { + this.status = status; + } + + public String toString() { + return this.status; + } + + public static ControlLoopEventStatus toStatus(String status) { + if (ONSET.status.equalsIgnoreCase(status)) { + return ONSET; + } + if (ABATED.status.equalsIgnoreCase(status)) { + return ABATED; + } + // + // In case DCAE uses the old abatement + // + if (status.equalsIgnoreCase("abatement")) { + return ABATED; + } + return null; + } + +} diff --git a/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopNotification.java b/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopNotification.java new file mode 100644 index 000000000..15c61a80a --- /dev/null +++ b/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopNotification.java @@ -0,0 +1,62 @@ +/*- + * ============LICENSE_START======================================================= + * controlloop + * ================================================================================ + * 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.openecomp.policy.controlloop; + +import java.io.Serializable; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.util.LinkedList; +import java.util.List; +import java.util.UUID; + +public abstract class ControlLoopNotification implements Serializable { + + private static final long serialVersionUID = 7538596984567127915L; + + public String closedLoopControlName; + public String version = "1.0.2"; + public UUID requestID; + public String closedLoopEventClient; + public ControlLoopTargetType target_type; + public String target; + public String from; + public String policyScope; + public String policyName; + public String policyVersion; + public ControlLoopNotificationType notification; + public String message; + public ZonedDateTime notificationTime = ZonedDateTime.now(ZoneOffset.UTC);; + public Integer OPS_CL_timer; + public List<ControlLoopOperation> history = new LinkedList<ControlLoopOperation>(); + + public ControlLoopNotification() { + + } + + public ControlLoopNotification(ControlLoopEvent event) { + this.closedLoopControlName = event.closedLoopControlName; + this.requestID = event.requestID; + this.closedLoopEventClient = event.closedLoopEventClient; + this.target_type = event.target_type; + this.target = event.target; + } + +} diff --git a/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopNotificationType.java b/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopNotificationType.java new file mode 100644 index 000000000..a5618df02 --- /dev/null +++ b/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopNotificationType.java @@ -0,0 +1,72 @@ +/*- + * ============LICENSE_START======================================================= + * controlloop + * ================================================================================ + * 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.openecomp.policy.controlloop; + +public enum ControlLoopNotificationType { + ACTIVE("ACTIVE"), + REJECTED("REJECTED"), + OPERATION("OPERATION"), + OPERATION_SUCCESS("OPERATION: SUCCESS"), + OPERATION_FAILURE("OPERATION: FAILURE"), + FINAL_FAILURE("FINAL: FAILURE"), + FINAL_SUCCESS("FINAL: SUCCESS"), + FINAL_OPENLOOP("FINAL: OPENLOOP") + ; + + private String type; + + private ControlLoopNotificationType(String type) { + this.type = type; + } + + public String toString() { + return this.type; + } + + public static ControlLoopNotificationType toType(String type) { + if (ACTIVE.toString().equals(type)) { + return ACTIVE; + } + if (REJECTED.toString().equals(type)) { + return REJECTED; + } + if (OPERATION.toString().equals(type)) { + return OPERATION; + } + if (OPERATION_SUCCESS.toString().equals(type)) { + return OPERATION_SUCCESS; + } + if (OPERATION_FAILURE.toString().equals(type)) { + return OPERATION_FAILURE; + } + if (FINAL_FAILURE.toString().equals(type)) { + return FINAL_FAILURE; + } + if (FINAL_SUCCESS.toString().equals(type)) { + return FINAL_SUCCESS; + } + if (FINAL_OPENLOOP.toString().equals(type)) { + return FINAL_OPENLOOP; + } + return null; + } + +} diff --git a/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopOperation.java b/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopOperation.java new file mode 100644 index 000000000..8925ee287 --- /dev/null +++ b/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopOperation.java @@ -0,0 +1,139 @@ +/*- + * ============LICENSE_START======================================================= + * controlloop + * ================================================================================ + * 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.openecomp.policy.controlloop; + +import java.io.Serializable; +import java.time.Instant; + +public class ControlLoopOperation implements Serializable { + + /** + * + */ + private static final long serialVersionUID = 8662706581293017099L; + + public String actor; + public String operation; + public String target; + public Instant start = Instant.now(); + public Instant end; + public String subRequestId; + public String outcome; + public String message; + + public ControlLoopOperation() { + + } + + public ControlLoopOperation(ControlLoopOperation op) { + this.actor = op.actor; + this.operation = op.operation; + this.target = op.target; + this.start = op.start; + this.end = op.end; + this.subRequestId = op.subRequestId; + this.outcome = op.outcome; + this.message = op.message; + } + + public String toMessage() { + return "actor="+actor+",operation="+operation+",target="+target+",subRequestId="+subRequestId; + } + + public String toHistory() { + return "actor="+actor+",operation="+operation+",target="+target+",start="+start+",end="+end+",subRequestId="+subRequestId+",outcome="+outcome+",message="+message; + } + + @Override + public String toString() { + return "ControlLoopOperation [actor=" + actor + ", operation=" + operation + ", target=" + target + ", start=" + + start + ", end=" + end + ", subRequestId=" + subRequestId + ", outcome=" + outcome + ", message=" + + message + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((actor == null) ? 0 : actor.hashCode()); + result = prime * result + ((end == null) ? 0 : end.hashCode()); + result = prime * result + ((message == null) ? 0 : message.hashCode()); + result = prime * result + ((operation == null) ? 0 : operation.hashCode()); + result = prime * result + ((outcome == null) ? 0 : outcome.hashCode()); + result = prime * result + ((start == null) ? 0 : start.hashCode()); + result = prime * result + ((subRequestId == null) ? 0 : subRequestId.hashCode()); + result = prime * result + ((target == null) ? 0 : target.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; + ControlLoopOperation other = (ControlLoopOperation) obj; + if (actor == null) { + if (other.actor != null) + return false; + } else if (!actor.equals(other.actor)) + return false; + if (end == null) { + if (other.end != null) + return false; + } else if (!end.equals(other.end)) + return false; + if (message == null) { + if (other.message != null) + return false; + } else if (!message.equals(other.message)) + return false; + if (operation == null) { + if (other.operation != null) + return false; + } else if (!operation.equals(other.operation)) + return false; + if (outcome == null) { + if (other.outcome != null) + return false; + } else if (!outcome.equals(other.outcome)) + return false; + if (start == null) { + if (other.start != null) + return false; + } else if (!start.equals(other.start)) + return false; + if (subRequestId == null) { + if (other.subRequestId != null) + return false; + } else if (!subRequestId.equals(other.subRequestId)) + return false; + if (target == null) { + if (other.target != null) + return false; + } else if (!target.equals(other.target)) + return false; + return true; + } + +} diff --git a/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopOperationWrapper.java b/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopOperationWrapper.java new file mode 100644 index 000000000..417a59d8e --- /dev/null +++ b/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopOperationWrapper.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * controlloop + * ================================================================================ + * 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.openecomp.policy.controlloop; + +import java.util.UUID; + +public class ControlLoopOperationWrapper { + + public UUID requestID; + public ControlLoopOperation operation; + + public ControlLoopOperationWrapper() { + + } + + public ControlLoopOperationWrapper(UUID requestID, ControlLoopOperation operation) { + this.requestID = requestID; + this.operation = operation; + } +} diff --git a/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopTargetType.java b/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopTargetType.java new file mode 100644 index 000000000..c1ed0e1d8 --- /dev/null +++ b/controlloop/src/main/java/org/openecomp/policy/controlloop/ControlLoopTargetType.java @@ -0,0 +1,55 @@ +/*- + * ============LICENSE_START======================================================= + * controlloop + * ================================================================================ + * 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.openecomp.policy.controlloop; + +public enum ControlLoopTargetType { + VM("VM"), + VF("VF"), + VFC("VFC"), + ENODEB("eNodeB") + ; + + private String type; + + private ControlLoopTargetType(String type) { + this.type = type; + } + + public String toString() { + return this.type; + } + + public static ControlLoopTargetType toType(String type) { + if (VM.toString().equals(type)) { + return VM; + } + if (VF.toString().equals(type)) { + return VF; + } + if (VFC.toString().equals(type)) { + return VFC; + } + if (ENODEB.toString().equals(type)) { + return ENODEB; + } + return null; + } +} diff --git a/controlloop/src/main/java/org/openecomp/policy/controlloop/PhysicalControlLoopEvent.java b/controlloop/src/main/java/org/openecomp/policy/controlloop/PhysicalControlLoopEvent.java new file mode 100644 index 000000000..615ee48bc --- /dev/null +++ b/controlloop/src/main/java/org/openecomp/policy/controlloop/PhysicalControlLoopEvent.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * controlloop + * ================================================================================ + * 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.openecomp.policy.controlloop; + +public class PhysicalControlLoopEvent extends ControlLoopEvent { + + /** + * + */ + private static final long serialVersionUID = -7282930271094849487L; + + public PhysicalControlLoopEvent() { + } + + public PhysicalControlLoopEvent(PhysicalControlLoopEvent event) { + super(event); + if (event == null) { + return; + } + } + + +} diff --git a/controlloop/src/main/java/org/openecomp/policy/controlloop/PhysicalControlLoopNotification.java b/controlloop/src/main/java/org/openecomp/policy/controlloop/PhysicalControlLoopNotification.java new file mode 100644 index 000000000..ee093cb67 --- /dev/null +++ b/controlloop/src/main/java/org/openecomp/policy/controlloop/PhysicalControlLoopNotification.java @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * controlloop + * ================================================================================ + * 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.openecomp.policy.controlloop; + +public class PhysicalControlLoopNotification extends ControlLoopNotification { + + /** + * + */ + private static final long serialVersionUID = 8105197217140032892L; + + public PhysicalControlLoopNotification() { + } + + public PhysicalControlLoopNotification(PhysicalControlLoopEvent event) { + super(event); + if (event == null) { + return; + } + } + +} diff --git a/controlloop/src/main/java/org/openecomp/policy/controlloop/VirtualControlLoopEvent.java b/controlloop/src/main/java/org/openecomp/policy/controlloop/VirtualControlLoopEvent.java new file mode 100644 index 000000000..6ca280e4a --- /dev/null +++ b/controlloop/src/main/java/org/openecomp/policy/controlloop/VirtualControlLoopEvent.java @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * controlloop + * ================================================================================ + * 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.openecomp.policy.controlloop; + +import java.time.Instant; +import java.util.HashMap; +import java.util.Map; + +public class VirtualControlLoopEvent extends ControlLoopEvent { + + /** + * + */ + private static final long serialVersionUID = -5752405682246066226L; + public Instant closedLoopAlarmStart; + public Instant closedLoopAlarmEnd; + public Map<String, String> AAI = new HashMap<String, String>(); + + public VirtualControlLoopEvent() { + } + + public VirtualControlLoopEvent(VirtualControlLoopEvent event) { + super(event); + if (event == null) { + return; + } + if (event.AAI != null) { + this.AAI = new HashMap<String, String>(event.AAI); + } + this.closedLoopAlarmStart = event.closedLoopAlarmStart; + this.closedLoopAlarmEnd = event.closedLoopAlarmEnd; + } + +} diff --git a/controlloop/src/main/java/org/openecomp/policy/controlloop/VirtualControlLoopNotification.java b/controlloop/src/main/java/org/openecomp/policy/controlloop/VirtualControlLoopNotification.java new file mode 100644 index 000000000..239a3ef21 --- /dev/null +++ b/controlloop/src/main/java/org/openecomp/policy/controlloop/VirtualControlLoopNotification.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * controlloop + * ================================================================================ + * 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.openecomp.policy.controlloop; + +import java.time.Instant; +import java.util.HashMap; +import java.util.Map; + +public class VirtualControlLoopNotification extends ControlLoopNotification { + + /** + * + */ + private static final long serialVersionUID = 5354756047932144017L; + + public Map<String, String> AAI = new HashMap<String, String>(); + public Instant closedLoopAlarmStart; + public Instant closedLoopAlarmEnd; + + public VirtualControlLoopNotification() { + } + + public VirtualControlLoopNotification(VirtualControlLoopEvent event) { + super(event); + if (event == null) { + return; + } + if (event.AAI != null) { + this.AAI = new HashMap<String, String>(event.AAI); + } + this.closedLoopAlarmStart = event.closedLoopAlarmStart; + this.closedLoopAlarmEnd = event.closedLoopAlarmEnd; + } + +} diff --git a/controlloop/src/main/java/org/openecomp/policy/controlloop/util/Serialization.java b/controlloop/src/main/java/org/openecomp/policy/controlloop/util/Serialization.java new file mode 100644 index 000000000..2625da712 --- /dev/null +++ b/controlloop/src/main/java/org/openecomp/policy/controlloop/util/Serialization.java @@ -0,0 +1,124 @@ +/*- + * ============LICENSE_START======================================================= + * controlloop + * ================================================================================ + * 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.openecomp.policy.controlloop.util; + +import java.lang.reflect.Type; +import java.time.Instant; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; + +import org.openecomp.policy.controlloop.ControlLoopNotificationType; +import org.openecomp.policy.controlloop.ControlLoopTargetType; + +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.JsonParseException; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +public final class Serialization { + + public static class notificationTypeAdapter implements JsonSerializer<ControlLoopNotificationType>, JsonDeserializer<ControlLoopNotificationType> { + + @Override + public JsonElement serialize(ControlLoopNotificationType src, Type typeOfSrc, + JsonSerializationContext context) { + return new JsonPrimitive(src.toString()); + } + + @Override + public ControlLoopNotificationType deserialize(JsonElement json, Type typeOfT, + JsonDeserializationContext context) throws JsonParseException { + return ControlLoopNotificationType.toType(json.getAsString()); + } + + } + + public static class targetTypeAdapter implements JsonSerializer<ControlLoopTargetType>, JsonDeserializer<ControlLoopTargetType> { + + @Override + public JsonElement serialize(ControlLoopTargetType src, Type typeOfSrc, + JsonSerializationContext context) { + return new JsonPrimitive(src.toString()); + } + + @Override + public ControlLoopTargetType deserialize(JsonElement json, Type typeOfT, + JsonDeserializationContext context) throws JsonParseException { + return ControlLoopTargetType.toType(json.getAsString()); + } + + } + + public static class gsonUTCAdapter implements JsonSerializer<ZonedDateTime>, JsonDeserializer<ZonedDateTime> { + public static DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSxxx"); + + public ZonedDateTime deserialize(JsonElement element, Type type, JsonDeserializationContext context) + throws JsonParseException { + try { + return ZonedDateTime.parse(element.getAsString(), format); + } catch (Exception e) { + System.err.println(e); + } + return null; + } + + public JsonElement serialize(ZonedDateTime datetime, Type type, JsonSerializationContext context) { + return new JsonPrimitive(datetime.format(format)); + } + } + + public static class gsonInstantAdapter implements JsonSerializer<Instant>, JsonDeserializer<Instant> { + + @Override + public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + return Instant.ofEpochMilli(json.getAsLong()); + } + + @Override + public JsonElement serialize(Instant src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(src.toEpochMilli()); + } + + } + + final static public Gson gson = new GsonBuilder().disableHtmlEscaping() + .registerTypeAdapter(ZonedDateTime.class, new gsonUTCAdapter()) + .registerTypeAdapter(Instant.class, new gsonInstantAdapter()) + .registerTypeAdapter(ControlLoopNotificationType.class, new notificationTypeAdapter()) + .registerTypeAdapter(ControlLoopTargetType.class, new targetTypeAdapter()) + .create(); + + + final static public Gson gsonPretty = new GsonBuilder().disableHtmlEscaping() + .setPrettyPrinting() + .registerTypeAdapter(ZonedDateTime.class, new gsonUTCAdapter()) + .registerTypeAdapter(Instant.class, new gsonInstantAdapter()) + .registerTypeAdapter(ControlLoopNotificationType.class, new notificationTypeAdapter()) + .registerTypeAdapter(ControlLoopTargetType.class, new targetTypeAdapter()) + .create(); + +} |