/**
  * Maps Ericsson proto message fields to application model fields
  * @param  array $data
  * @return array
  */
 protected function _mapEricssonModelToModel(array $data)
 {
     if (isset($data['response_data'])) {
         $data = $data['response_data'];
     }
     $data = parent::_mapEricssonModelToModel($data);
     if (isset($data['universeType'])) {
         $data['universeType'] = $this->mapUniverseFromEricsson($data['universeType']);
     }
     if (isset($data['universe']['id']) && isset($data['universeType'])) {
         if (AlarmRuleModel::UNIVERSE_CUSTOMER === $data['universeType']) {
             $data['universeId'] = \Application\Model\Mapper\Organization\OrgCustomerMapper::buildOrgId($data['universe']['id']);
         } else {
             $data['universeId'] = $data['universe']['id'];
         }
         $data['universeName'] = $data['universe']['name'];
         unset($data['universe']);
     }
     if (isset($data['rules']) && is_array($data['rules'])) {
         $rules = array();
         foreach ($data['rules'] as $rule) {
             if (is_array($rule)) {
                 $rules[] = $this->_mapRuleToModel($rule);
             }
         }
         $data['rules'] = $rules;
     }
     return $data;
 }
 /**
  * Recieve the CORE async responses
  */
 public function postAction()
 {
     \App::log()->debug("CORE notification of response of a async operation.");
     $message = new Request();
     if (!$this->_request->isPost()) {
         throw new \Application\Exceptions\UnexpectedException("Post request expected");
     }
     // Parse the first level of proto buffer
     $message->parse($this->getRequest()->getRawBody());
     // Verify the transactionId
     if (!$message->hasToken()) {
         throw new \Application\Exceptions\InvalidArgumentException("Missing transactionId.");
     }
     // Verify the proto
     if (!$message->hasProto()) {
         throw new \Application\Exceptions\InvalidArgumentException("Missing proto type.");
     }
     $event = new EventModel();
     $event->namespace = 'connectivity';
     $event->entityType = 'transaction';
     $event->entityId = $message->getToken();
     $event->created = time();
     $event->pushEventData = true;
     $protoType = $message->getProto();
     $event->hiddenData = array('protoType' => $protoType);
     \App::log()->info('Start CORE notification of response of type ' . $protoType . ' from the async operation ' . $event->entityId ?: 'empty');
     // Message decode
     $internalMessage = $this->_decodeMessage($protoType, $message->getMessage());
     // Obtaining a PHP Array from proto
     $parser = new \DrSlump\Protobuf\Codec\PhpArray();
     $data = $message->serialize($parser);
     $messageData = $internalMessage->serialize($parser);
     \App::log()->debug("Async message: " . Zend_Json::encode($messageData));
     \App_Util_Array::filterKeyNames(new \App_Filter_UnderscoreToCamelCase(), $messageData);
     \App::log()->debug("Async message (filtered): " . Zend_Json::encode($messageData));
     if (isset($data['proto'])) {
         unset($data['proto']);
     }
     if (isset($data['token'])) {
         unset($data['token']);
     }
     if (isset($messageData['result'])) {
         $data['result'] = $messageData['result'];
         unset($messageData['result']);
     }
     if (!empty($messageData['failed']) || @$data['result']['code'] != 0) {
         $data['hasFailures'] = true;
     } else {
         $data['hasFailures'] = false;
     }
     \App::log()->debug("Async message (after all): " . Zend_Json::encode($messageData));
     $data['message'] = $messageData;
     $retries = 0;
     // Map data
     switch (true) {
         case $internalMessage instanceof AsyncNotification\Service\BusinessRuleResponse:
             $event->entityType = 'businessRule';
             $event->pushEventData = false;
             $arMapper = \Application\Model\Mapper\AlarmRuleMapper::getInstance();
             // Map alarm rule condition
             $value =& $data['message']['alarmRuleCondition'];
             $value = $arMapper->mapConditionFromEricsson($value);
             // Map business rule type
             $value =& $data['message']['businessRule']['businessRuleType'];
             $value = $arMapper->mapBusinessRuleTypeToModel($value);
             // Map life cycle state (optional)
             if (isset($data['message']['businessRule']['nextLifecycleState'])) {
                 $value =& $data['message']['businessRule']['nextLifecycleState'];
                 $lcMapper = \Application\Model\Mapper\LifeCycleMapper::getInstance();
                 $value = $lcMapper->mapLifeCycleStatusFromEricsson($value);
             }
             //Workaround: organization must include orgType
             if (isset($data['message']['organizationId'])) {
                 $data['organizationId'] = OrgCustomerMapper::buildOrgId($data['message']['organizationId']);
             }
             break;
         case $internalMessage instanceof \Application\Proto\SupDiagnosisAsync\DiagnosisNotification:
             $event->entityType = 'diagnosis';
             $retries = \App::config('watcher.event_retries.diagnosis', 10);
             break;
         case $internalMessage instanceof AsyncNotification\Service\ReportResponse:
             $event->entityType = 'report';
             break;
         case $internalMessage instanceof AsyncNotification\Service\TariffSwitchResponse:
             $event->entityType = 'tariffSwitch';
             $event->entityId = OrgServiceProviderMapper::buildOrgId($internalMessage->getServiceProviderId());
             break;
     }
     $event->eventData = $data;
     $compressor = new AsyncResponseCompressorEvent();
     $compressor->compress($event);
     \App::log()->debug('The content of the CORE notification of response of type ' . $protoType . ' from the async operation ' . ($event->entityId ?: 'empty') . ' is ' . \Zend_Json::encode($data));
     // Invoke the service
     $result = AsyncService::getInstance()->publish($event, $retries);
     \App::log()->info('End CORE notification of response of type' . $protoType . ' from the async operation ' . $event->entityId ?: 'empty');
     $this->_helper->output('proto');
     $this->view->setClass('Application\\Proto\\AsyncNotification\\Service\\Response');
     $this->view->code = Application\Proto\AsyncNotification\Service\Response\Code::OK;
     $this->view->reason = "Successful operation.";
 }
 protected function _mapEricssonModelToModel(array $data)
 {
     if (isset($data['serviceprovider'])) {
         $data['service_provider'] = $data['serviceprovider'];
         unset($data['serviceprovider']);
     }
     $data = parent::_mapEricssonModelToModel($data);
     $ref = array('restrictions', 'lifeCycle', 'tariffPlanServices', 'tariffPlanLifeCycle', 'biggestCommercialGroup');
     $this->_mapReference($data, $ref);
     $data = $this->_mapEricssonListsToLists($data);
     if (isset($data['customers'])) {
         $customers = array();
         foreach ($data['customers'] as $customer) {
             if (isset($customer['customer']['id'])) {
                 $customer['customerId'] = Organization\OrgCustomerMapper::buildOrgId($customer['customer']['id']);
             }
             if (isset($customer['customer']['name'])) {
                 $customer['customerName'] = $customer['customer']['name'];
             }
             if (isset($customer['customer'])) {
                 unset($customer['customer']);
             }
             $customers[] = $customer;
         }
         $data['customers'] = $customers;
     }
     if (isset($data['restricted']) && \App::getOrgUserLogged()->getType() == OrgCustomerModel::ORG_TYPE) {
         if (!isset($data['customers']) || !is_array($data['customers'])) {
             $data['customers'] = array();
         }
         $data['customers'][] = array('customerId' => \App::getOrgUserLogged()->id, 'customerName' => \App::getOrgUserLogged()->name, 'restricted' => $data['restricted']);
     }
     if (isset($data['numCustomers'])) {
         $data['customerCount'] = $data['numCustomers'];
         unset($data['numCustomers']);
     }
     $count = 0;
     if (isset($data['commercialGroup'])) {
         foreach ($data['commercialGroup'] as &$cg) {
             $this->_mapReference($cg, 'commercialGroup');
             if ($count < $cg['subscriptionCount']) {
                 $count = $cg['subscriptionCount'];
                 $data['biggestCommercialGroup'] = $cg;
             }
         }
     }
     switch ($data['published']) {
         case PublishedStatus::PUBLISHED:
             $data['published'] = ServicePackModel::STATUS_PUBLISHED;
             break;
         case PublishedStatus::PUBLISHED_PENDING:
             $data['published'] = ServicePackModel::STATUS_PUBLISHED_PENDING;
             break;
         case PublishedStatus::NOT_PUBLISHED:
             $data['published'] = ServicePackModel::STATUS_NOT_PUBLISHED;
             break;
         default:
             $data['published'] = ServicePackModel::STATUS_UNKNOWN;
     }
     return $data;
 }
 protected function _mapEricssonModelToModel(array $data)
 {
     $data = parent::_mapEricssonModelToModel($data);
     if (isset($data['entity']) && isset($data['entity']['universeType']) && ($universeType = AlarmRuleMapper::getInstance()->mapUniverseFromEricsson($data['entity']['universeType']))) {
         $data['entity']['universeType'] = $universeType;
         if (isset($data['entity']['customer']['id'])) {
             $data['entity']['customerId'] = $data['entity']['customer']['id'];
             if (isset($data['entity']['customer']['name'])) {
                 $data['entity']['customerName'] = $data['entity']['customer']['name'];
             }
             unset($data['entity']['customer']);
         }
         if (isset($data['entity']['billingAccount']['id'])) {
             $data['entity']['billingAccountId'] = $data['entity']['billingAccount']['id'];
             if (isset($data['entity']['billingAccount']['name'])) {
                 $data['entity']['billingAccountName'] = $data['entity']['billingAccount']['name'];
             }
             unset($data['entity']['billingAccount']);
         }
         if (isset($data['entity']['commercialGroup']['id'])) {
             $data['entity']['commercialGroupId'] = $data['entity']['commercialGroup']['id'];
             if (isset($data['entity']['commercialGroup']['name'])) {
                 $data['entity']['commercialGroupName'] = $data['entity']['commercialGroup']['name'];
             }
             unset($data['entity']['commercialGroup']);
         }
         if (isset($data['entity']['customerId'])) {
             $data['entity']['customerId'] = OrgCustomerMapper::buildOrgId($data['entity']['customerId']);
         }
         if (isset($data['entity']['subscriptionId'])) {
             foreach ($data['entity']['subscriptionId'] as &$simId) {
                 if (isset($simId['type'])) {
                     $idType = SimMapper::mapSubscriptionIdFromEricsson($simId['type']);
                     $simId['type'] = $idType;
                 }
             }
         }
         if (isset($data['businessRule'])) {
             foreach ($data['businessRule'] as &$businessRule) {
                 $businessRule = AlarmRuleMapper::getInstance()->mapBusinessRuleToModel($businessRule);
             }
         }
     }
     if (isset($data['state']) && ($state = $this->_mapStateToModel($data['state']))) {
         $data['state'] = $state;
     }
     if (isset($data['alarmRule']['id'])) {
         $data['alarmRuleId'] = $data['alarmRule']['id'];
         if (isset($data['alarmRule']['name'])) {
             $data['alarmRuleName'] = $data['alarmRule']['name'];
         }
         unset($data['alarmRule']);
     }
     $alarmRuleMapper = AlarmRuleMapper::getInstance();
     if (isset($data['severity']) && ($severity = $alarmRuleMapper->mapSeverityFromEricsson($data['severity']))) {
         $data['severity'] = $severity;
     }
     if (isset($data['condition']) && ($condition = $alarmRuleMapper->mapConditionFromEricsson($data['condition']))) {
         $data['condition'] = $condition;
     }
     return $data;
 }